summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authoraviac <aviac@mailbox.org>2024-01-19 11:26:14 +0100
committeraviac <aviac@mailbox.org>2024-01-22 09:01:09 +0100
commit1b6cd3467af2ea666e946446025d9c82cd3a8a62 (patch)
tree46254224824e48ae3baeb7bbe4f624150cf751f1 /generator
parentfix(typo?): remove weird line (diff)
downloadforgejo-api-1b6cd3467af2ea666e946446025d9c82cd3a8a62.tar.xz
forgejo-api-1b6cd3467af2ea666e946446025d9c82cd3a8a62.zip
feat(definitions): impl `Display`
Instead of adding a `to_string` method on each definition struct, this commit implements `Display`. This shouldn't result in any breaking changes since the trait also exposes a method with the same name. I also tried to format the generated text here and there to make it more readable in the generate code base as well as in the generate text. Authored-by: Aviac <aviac@mailbox.org>
Diffstat (limited to 'generator')
-rw-r--r--generator/src/main.rs67
1 files changed, 46 insertions, 21 deletions
diff --git a/generator/src/main.rs b/generator/src/main.rs
index 9e647af..0796fbb 100644
--- a/generator/src/main.rs
+++ b/generator/src/main.rs
@@ -925,20 +925,23 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
match ty {
ParameterType::String => match param.format.as_deref() {
Some("date-time" | "date") => {
- writeln!(&mut handler, "s.push_str(\"{}=\");", param.name)?;
- writeln!(&mut handler, "s.push_str(&{field_name}.format(&time::format_description::well_known::Rfc3339).unwrap());")?;
- writeln!(&mut handler, "s.push('&');")?;
+ writeln!(
+ &mut handler,
+ "write!(f, \"{}={{field_name}}&\", field_name = {field_name}.format(&time::format_description::well_known::Rfc3339).unwrap())?;",
+ param.name)?;
}
_ => {
- writeln!(&mut handler, "s.push_str(\"{}=\");", param.name)?;
- writeln!(&mut handler, "s.push_str(&{field_name});")?;
- writeln!(&mut handler, "s.push('&');")?;
+ writeln!(
+ &mut handler,
+ "write!(f, \"{}={{{}}}&\")?;",
+ param.name, field_name
+ )?;
}
},
ParameterType::Number | ParameterType::Integer | ParameterType::Boolean => {
writeln!(
&mut handler,
- "write!(&mut s, \"{}={{}}&\", {field_name}).unwrap();",
+ "write!(f, \"{}={{{field_name}}}&\")?;",
param.name
)?;
}
@@ -952,17 +955,17 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
ParameterType::String => {
match param.format.as_deref() {
Some("date-time" | "date") => {
- "s.push_str(&item.format(&time::format_description::well_known::Rfc3339).unwrap());"
+ "write!(f, \"{{date}}\", item.format(&time::format_description::well_known::Rfc3339).unwrap())?;"
},
_ => {
- "s.push_str(&item);"
+ "write!(f, \"{item}\")?;"
}
}
},
ParameterType::Number |
ParameterType::Integer |
ParameterType::Boolean => {
- "write!(&mut s, \"{item}\").unwrap();"
+ "write!(f, \"{item}\")?;"
},
ParameterType::Array => {
eyre::bail!("nested arrays not supported in query");
@@ -1003,13 +1006,13 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
)?);
}
CollectionFormat::Multi => {
- writeln!(&mut handler, "")?;
+ writeln!(&mut handler)?;
writeln!(&mut handler, "if !{field_name}.is_empty() {{")?;
writeln!(&mut handler, "for item in {field_name} {{")?;
- writeln!(&mut handler, "s.push_str(\"{}=\");", param.name)?;
+ writeln!(&mut handler, "write!(f, \"{}=\")?;", param.name)?;
handler.push_str(item_pusher);
handler.push('\n');
- writeln!(&mut handler, "s.push('&')")?;
+ writeln!(&mut handler, "write!(f, '&')?;")?;
writeln!(&mut handler, "}}")?;
writeln!(&mut handler, "}}")?;
}
@@ -1023,16 +1026,36 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
imp.push_str(&handler);
}
}
- if fields.is_empty() {
- return Ok(String::new());
+
+ let result = if fields.is_empty() {
+ String::new()
} else {
let op_name = op
.operation_id
.as_ref()
.ok_or_else(|| eyre::eyre!("no op id found"))?
.to_pascal_case();
- return Ok(format!("pub struct {op_name}Query {{\n{fields}\n}}\n\nimpl {op_name}Query {{\npub(crate) fn to_string(self) -> String {{\n{imp}\n}}\n}}"));
- }
+ // human readable format string + added whitespace so that the generate text is human
+ // readable as well
+ format!(
+ "
+pub struct {op_name}Query {{
+ {fields}
+}}
+
+impl std::fmt::Display for {op_name}Query {{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
+ {imp}
+ Ok(())
+ }}
+}}
+",
+ fields = fields.replace('\n', "\n ").trim(),
+ imp = imp.replace('\n', "\n ").trim()
+ )
+ };
+
+ Ok(result)
}
fn simple_query_array(
@@ -1042,17 +1065,19 @@ fn simple_query_array(
sep: &str,
) -> eyre::Result<String> {
let mut out = String::new();
- writeln!(&mut out, "s.push_str(\"{}=\");", param.name)?;
- writeln!(&mut out, "")?;
+
+ writeln!(&mut out)?;
writeln!(&mut out, "if !{name}.is_empty() {{")?;
- writeln!(&mut out, "for (i, item) in {name}.iter().enumerate() {{")?;
+ writeln!(&mut out, "for item in {name} {{")?;
+ writeln!(&mut out, "write!(f, \"{}=\")?;", param.name)?;
out.push_str(item_pusher);
out.push('\n');
writeln!(&mut out, "if i < {name}.len() - 1 {{")?;
writeln!(&mut out, "s.push('{sep}')")?;
writeln!(&mut out, "}}")?;
writeln!(&mut out, "}}")?;
- writeln!(&mut out, "s.push('&')")?;
+ writeln!(&mut out, "write!(f, '&')?;")?;
writeln!(&mut out, "}}")?;
+
Ok(out)
}