diff options
author | Cyborus <cyborus@cyborus.xyz> | 2024-01-24 22:31:34 +0100 |
---|---|---|
committer | Cyborus <cyborus@cyborus.xyz> | 2024-01-24 22:31:34 +0100 |
commit | 48eee70c8862c7b27bbf5939fdac185f78f10a5a (patch) | |
tree | 4325b7a0c08f88075caba5e35779f7eb9761cbce /generator | |
parent | Merge pull request 'autogen improvements' (#39) from Aviac/forgejo-api:aviac/... (diff) | |
download | forgejo-api-48eee70c8862c7b27bbf5939fdac185f78f10a5a.tar.xz forgejo-api-48eee70c8862c7b27bbf5939fdac185f78f10a5a.zip |
fix errors/warnings in generated code
remove import of `Write`
remove now unused output string in display impls
access query fields by reference
remove `r#` in vars accessed in format strings
replace char literals in write macros with strings
Diffstat (limited to 'generator')
-rw-r--r-- | generator/src/main.rs | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/generator/src/main.rs b/generator/src/main.rs index 97073ae..6ebac1e 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -22,7 +22,6 @@ fn main() -> eyre::Result<()> { s.push_str("use structs::*;\n"); s.push_str("pub mod structs {\n"); - s.push_str("use std::fmt::Write;\n"); if let Some(definitions) = &spec.definitions { for (name, schema) in definitions { let strukt = create_struct_for_definition(&spec, name, schema)?; @@ -900,7 +899,6 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re let mut fields = String::new(); let mut imp = String::new(); - imp.push_str("let mut s = String::new();\n"); for param in params { let param = match ¶m { MaybeRef::Value { value } => value, @@ -928,11 +926,11 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re .as_ref() .ok_or_else(|| eyre::eyre!("no type provided for query field"))?; if required { - writeln!(&mut handler, "let {field_name} = self.{field_name};")?; + writeln!(&mut handler, "let {field_name} = &self.{field_name};")?; } else { writeln!( &mut handler, - "if let Some({field_name}) = self.{field_name} {{" + "if let Some({field_name}) = &self.{field_name} {{" )?; } match ty { @@ -947,15 +945,16 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re writeln!( &mut handler, "write!(f, \"{}={{{}}}&\")?;", - param.name, field_name + param.name, field_name.strip_prefix("r#").unwrap_or(&field_name) )?; } }, ParameterType::Number | ParameterType::Integer | ParameterType::Boolean => { writeln!( &mut handler, - "write!(f, \"{}={{{field_name}}}&\")?;", - param.name + "write!(f, \"{}={{{}}}&\")?;", + param.name, + field_name.strip_prefix("r#").unwrap_or(&field_name) )?; } ParameterType::Array => { @@ -1025,7 +1024,7 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re writeln!(&mut handler, "write!(f, \"{}=\")?;", param.name)?; handler.push_str(item_pusher); handler.push('\n'); - writeln!(&mut handler, "write!(f, '&')?;")?; + writeln!(&mut handler, "write!(f, \"&\")?;")?; writeln!(&mut handler, "}}")?; writeln!(&mut handler, "}}")?; } @@ -1083,10 +1082,10 @@ if !{name}.is_empty() {{ for (item, i) in {name}.iter().enumerate() {{ {item_pusher} if i < {name}.len() - 1 {{ - write!(f, '{sep}')?; + write!(f, \"{sep}\")?; }} }} - write!(f, '&')?; + write!(f, \"&\")?; }}", param.name )?; |