1
0
Fork 0

fix warnings

This commit is contained in:
Cyborus 2024-01-29 22:12:52 -05:00
parent 787adac1a4
commit 4ccdce0395
No known key found for this signature in database
3 changed files with 24 additions and 30 deletions

View file

@ -98,7 +98,7 @@ fn schema_type_name(
}; };
Ok(name.to_owned()) Ok(name.to_owned())
} }
SchemaType::List(list) => todo!(), SchemaType::List(_) => todo!(),
} }
} else { } else {
Ok("()".into()) Ok("()".into())

View file

@ -137,8 +137,7 @@ fn fn_signature_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String
fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> { fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
let mut args = "&self".to_string(); let mut args = "&self".to_string();
let mut has_query = false; let mut has_query = false;
let mut has_headers = false; // let mut has_headers = false;
let mut has_form = false;
if let Some(params) = &op.parameters { if let Some(params) = &op.parameters {
for param in params { for param in params {
let full_param = match &param { let full_param = match &param {
@ -153,8 +152,8 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
args.push_str(": "); args.push_str(": ");
args.push_str(&type_name); args.push_str(&type_name);
} }
ParameterIn::Query { param } => has_query = true, ParameterIn::Query { param: _ } => has_query = true,
ParameterIn::Header { param }=> has_headers = true, ParameterIn::Header { param: _ }=> (), // has_headers = true,
ParameterIn::Body { schema } => { ParameterIn::Body { schema } => {
let ty = crate::schema_ref_type_name(spec, schema)?; let ty = crate::schema_ref_type_name(spec, schema)?;
args.push_str(", "); args.push_str(", ");
@ -162,7 +161,7 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> {
args.push_str(": "); args.push_str(": ");
args.push_str(&ty); args.push_str(&ty);
} }
ParameterIn::FormData { param } => { ParameterIn::FormData { param: _ } => {
args.push_str(", "); args.push_str(", ");
args.push_str(&crate::sanitize_ident(&full_param.name)); args.push_str(&crate::sanitize_ident(&full_param.name));
args.push_str(": Vec<u8>"); args.push_str(": Vec<u8>");
@ -303,7 +302,7 @@ fn create_method_body(
op: &Operation, op: &Operation,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let request = create_method_request(spec, method, path, op)?; let request = create_method_request(spec, method, path, op)?;
let response = create_method_response(spec, method, path, op)?; let response = create_method_response(spec, op)?;
Ok(format!("{request}\n {response}")) Ok(format!("{request}\n {response}"))
} }
@ -314,7 +313,7 @@ fn create_method_request(
op: &Operation, op: &Operation,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let mut has_query = false; let mut has_query = false;
let mut has_headers = false; // let mut has_headers = false;
let mut body_method = String::new(); let mut body_method = String::new();
if let Some(params) = &op.parameters { if let Some(params) = &op.parameters {
for param in params { for param in params {
@ -324,10 +323,10 @@ fn create_method_request(
}; };
let name = crate::sanitize_ident(&param.name); let name = crate::sanitize_ident(&param.name);
match &param._in { match &param._in {
ParameterIn::Path { param } => (/* do nothing */), ParameterIn::Path { param: _ } => (/* do nothing */),
ParameterIn::Query { param } => has_query = true, ParameterIn::Query { param: _ } => has_query = true,
ParameterIn::Header { param } => has_headers = true, ParameterIn::Header { param: _ } => (), // _has_headers = true,
ParameterIn::Body { schema } => { ParameterIn::Body { schema: _ } => {
if !body_method.is_empty() { if !body_method.is_empty() {
eyre::bail!("cannot have more than one body parameter"); eyre::bail!("cannot have more than one body parameter");
} }
@ -337,7 +336,7 @@ fn create_method_request(
body_method = format!(".json(&{name})"); body_method = format!(".json(&{name})");
} }
} }
ParameterIn::FormData { param } => { ParameterIn::FormData { param: _ } => {
if !body_method.is_empty() { if !body_method.is_empty() {
eyre::bail!("cannot have more than one body parameter"); eyre::bail!("cannot have more than one body parameter");
} }
@ -347,12 +346,11 @@ fn create_method_request(
} }
} }
let mut fmt_str = sanitize_path_arg(path)?; let mut fmt_str = sanitize_path_arg(path)?;
let mut fmt_args = String::new();
if has_query { if has_query {
fmt_str.push_str("?{query}"); fmt_str.push_str("?{query}");
} }
let path_arg = if fmt_str.contains("{") { let path_arg = if fmt_str.contains("{") {
format!("&format!(\"{fmt_str}\"{fmt_args})") format!("&format!(\"{fmt_str}\")")
} else { } else {
format!("\"{fmt_str}\"") format!("\"{fmt_str}\"")
}; };
@ -407,8 +405,6 @@ fn sanitize_path_arg(mut path: &str) -> eyre::Result<String> {
fn create_method_response( fn create_method_response(
spec: &OpenApiV2, spec: &OpenApiV2,
method: &str,
path: &str,
op: &Operation, op: &Operation,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let mut has_empty = false; let mut has_empty = false;
@ -523,7 +519,7 @@ impl ResponseType {
(Some(a), Some(b)) if a != b => eyre::bail!("incompatible header types in response"), (Some(a), Some(b)) if a != b => eyre::bail!("incompatible header types in response"),
(Some(a), Some("()") | None) => new.body = Some(format!("Option<{a}>")), (Some(a), Some("()") | None) => new.body = Some(format!("Option<{a}>")),
(Some("()") | None, Some(b)) => new.body = Some(format!("Option<{b}>")), (Some("()") | None, Some(b)) => new.body = Some(format!("Option<{b}>")),
(a, b) => new.body = self.body.or(other.body), (_, _) => new.body = self.body.or(other.body),
}; };
Ok(new) Ok(new)
} }

View file

@ -13,8 +13,8 @@ pub fn create_structs(spec: &OpenApiV2) -> eyre::Result<String> {
s.push_str(&strukt); s.push_str(&strukt);
} }
} }
for (path, item) in &spec.paths { for (_, item) in &spec.paths {
let strukt = create_query_structs_for_path(&spec, path, item)?; let strukt = create_query_structs_for_path(item)?;
s.push_str(&strukt); s.push_str(&strukt);
} }
s.push_str("\n}"); s.push_str("\n}");
@ -98,31 +98,29 @@ fn create_struct_docs(schema: &Schema) -> eyre::Result<String> {
} }
pub fn create_query_structs_for_path( pub fn create_query_structs_for_path(
spec: &OpenApiV2,
path: &str,
item: &PathItem, item: &PathItem,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let mut s = String::new(); let mut s = String::new();
if let Some(op) = &item.get { if let Some(op) = &item.get {
s.push_str(&create_query_struct(spec, path, op).wrap_err("GET")?); s.push_str(&create_query_struct(op).wrap_err("GET")?);
} }
if let Some(op) = &item.put { if let Some(op) = &item.put {
s.push_str(&create_query_struct(spec, path, op).wrap_err("PUT")?); s.push_str(&create_query_struct(op).wrap_err("PUT")?);
} }
if let Some(op) = &item.post { if let Some(op) = &item.post {
s.push_str(&create_query_struct(spec, path, op).wrap_err("POST")?); s.push_str(&create_query_struct(op).wrap_err("POST")?);
} }
if let Some(op) = &item.delete { if let Some(op) = &item.delete {
s.push_str(&create_query_struct(spec, path, op).wrap_err("DELETE")?); s.push_str(&create_query_struct(op).wrap_err("DELETE")?);
} }
if let Some(op) = &item.options { if let Some(op) = &item.options {
s.push_str(&create_query_struct(spec, path, op).wrap_err("OPTIONS")?); s.push_str(&create_query_struct(op).wrap_err("OPTIONS")?);
} }
if let Some(op) = &item.head { if let Some(op) = &item.head {
s.push_str(&create_query_struct(spec, path, op).wrap_err("HEAD")?); s.push_str(&create_query_struct(op).wrap_err("HEAD")?);
} }
if let Some(op) = &item.patch { if let Some(op) = &item.patch {
s.push_str(&create_query_struct(spec, path, op).wrap_err("PATCH")?); s.push_str(&create_query_struct(op).wrap_err("PATCH")?);
} }
Ok(s) Ok(s)
} }
@ -138,7 +136,7 @@ pub fn query_struct_name(op: &Operation) -> eyre::Result<String> {
Ok(ty) Ok(ty)
} }
fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Result<String> { fn create_query_struct(op: &Operation) -> eyre::Result<String> {
let params = match &op.parameters { let params = match &op.parameters {
Some(params) => params, Some(params) => params,
None => return Ok(String::new()), None => return Ok(String::new()),