Merge pull request 'autogen improvements' (#39) from Aviac/forgejo-api:aviac/autogen into autogen
Reviewed-on: https://codeberg.org/Cyborus/forgejo-api/pulls/39
This commit is contained in:
commit
bcfe3e1e6c
|
@ -1,4 +1,4 @@
|
||||||
use std::ffi::OsString;
|
use std::ffi::{OsStr, OsString};
|
||||||
|
|
||||||
mod openapi;
|
mod openapi;
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ fn main() -> eyre::Result<()> {
|
||||||
s.push_str(&strukt);
|
s.push_str(&strukt);
|
||||||
}
|
}
|
||||||
s.push_str("\n}");
|
s.push_str("\n}");
|
||||||
save_generated(&mut s)?;
|
save_generated(&s)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,10 +49,23 @@ fn get_spec() -> eyre::Result<OpenApiV2> {
|
||||||
fn save_generated(contents: &str) -> eyre::Result<()> {
|
fn save_generated(contents: &str) -> eyre::Result<()> {
|
||||||
let path = std::env::var_os("FORGEJO_API_GENERATED_PATH")
|
let path = std::env::var_os("FORGEJO_API_GENERATED_PATH")
|
||||||
.unwrap_or_else(|| OsString::from("./src/generated.rs"));
|
.unwrap_or_else(|| OsString::from("./src/generated.rs"));
|
||||||
std::fs::write(path, contents)?;
|
std::fs::write(path.as_os_str(), contents)?;
|
||||||
|
run_rustfmt_on(path.as_os_str());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_rustfmt_on(path: &OsStr) {
|
||||||
|
let mut rustfmt = std::process::Command::new("rustfmt");
|
||||||
|
|
||||||
|
rustfmt.arg(path);
|
||||||
|
rustfmt.args(["--edition", "2021"]);
|
||||||
|
|
||||||
|
if let Err(e) = rustfmt.status() {
|
||||||
|
println!("Tried to format {path:?}, but failed to do so! :(");
|
||||||
|
println!("Error:\n{e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn create_methods_for_path(
|
fn create_methods_for_path(
|
||||||
spec: &OpenApiV2,
|
spec: &OpenApiV2,
|
||||||
path: &str,
|
path: &str,
|
||||||
|
@ -925,20 +938,23 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
|
||||||
match ty {
|
match ty {
|
||||||
ParameterType::String => match param.format.as_deref() {
|
ParameterType::String => match param.format.as_deref() {
|
||||||
Some("date-time" | "date") => {
|
Some("date-time" | "date") => {
|
||||||
writeln!(&mut handler, "s.push_str(\"{}=\");", param.name)?;
|
writeln!(
|
||||||
writeln!(&mut handler, "s.push_str(&{field_name}.format(&time::format_description::well_known::Rfc3339).unwrap());")?;
|
&mut handler,
|
||||||
writeln!(&mut handler, "s.push('&');")?;
|
"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!(
|
||||||
writeln!(&mut handler, "s.push_str(&{field_name});")?;
|
&mut handler,
|
||||||
writeln!(&mut handler, "s.push('&');")?;
|
"write!(f, \"{}={{{}}}&\")?;",
|
||||||
|
param.name, field_name
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ParameterType::Number | ParameterType::Integer | ParameterType::Boolean => {
|
ParameterType::Number | ParameterType::Integer | ParameterType::Boolean => {
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut handler,
|
&mut handler,
|
||||||
"write!(&mut s, \"{}={{}}&\", {field_name}).unwrap();",
|
"write!(f, \"{}={{{field_name}}}&\")?;",
|
||||||
param.name
|
param.name
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
@ -952,17 +968,17 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
|
||||||
ParameterType::String => {
|
ParameterType::String => {
|
||||||
match param.format.as_deref() {
|
match param.format.as_deref() {
|
||||||
Some("date-time" | "date") => {
|
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::Number |
|
||||||
ParameterType::Integer |
|
ParameterType::Integer |
|
||||||
ParameterType::Boolean => {
|
ParameterType::Boolean => {
|
||||||
"write!(&mut s, \"{item}\").unwrap();"
|
"write!(f, \"{item}\")?;"
|
||||||
},
|
},
|
||||||
ParameterType::Array => {
|
ParameterType::Array => {
|
||||||
eyre::bail!("nested arrays not supported in query");
|
eyre::bail!("nested arrays not supported in query");
|
||||||
|
@ -1003,13 +1019,13 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
|
||||||
)?);
|
)?);
|
||||||
}
|
}
|
||||||
CollectionFormat::Multi => {
|
CollectionFormat::Multi => {
|
||||||
writeln!(&mut handler, "")?;
|
writeln!(&mut handler)?;
|
||||||
writeln!(&mut handler, "if !{field_name}.is_empty() {{")?;
|
writeln!(&mut handler, "if !{field_name}.is_empty() {{")?;
|
||||||
writeln!(&mut handler, "for item in {field_name} {{")?;
|
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_str(item_pusher);
|
||||||
handler.push('\n');
|
handler.push('\n');
|
||||||
writeln!(&mut handler, "s.push('&')")?;
|
writeln!(&mut handler, "write!(f, '&')?;")?;
|
||||||
writeln!(&mut handler, "}}")?;
|
writeln!(&mut handler, "}}")?;
|
||||||
writeln!(&mut handler, "}}")?;
|
writeln!(&mut handler, "}}")?;
|
||||||
}
|
}
|
||||||
|
@ -1023,17 +1039,32 @@ fn create_query_struct(spec: &OpenApiV2, path: &str, op: &Operation) -> eyre::Re
|
||||||
imp.push_str(&handler);
|
imp.push_str(&handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imp.push_str("s\n");
|
|
||||||
if fields.is_empty() {
|
let result = if fields.is_empty() {
|
||||||
return Ok(String::new());
|
String::new()
|
||||||
} else {
|
} else {
|
||||||
let op_name = op
|
let op_name = op
|
||||||
.operation_id
|
.operation_id
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| eyre::eyre!("no op id found"))?
|
.ok_or_else(|| eyre::eyre!("no op id found"))?
|
||||||
.to_pascal_case();
|
.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}}"));
|
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(())
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simple_query_array(
|
fn simple_query_array(
|
||||||
|
@ -1043,17 +1074,22 @@ fn simple_query_array(
|
||||||
sep: &str,
|
sep: &str,
|
||||||
) -> eyre::Result<String> {
|
) -> eyre::Result<String> {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
writeln!(&mut out, "s.push_str(\"{}=\");", param.name)?;
|
|
||||||
writeln!(&mut out, "")?;
|
writeln!(
|
||||||
writeln!(&mut out, "if !{name}.is_empty() {{")?;
|
&mut out,
|
||||||
writeln!(&mut out, "for (i, item) in {name}.iter().enumerate() {{")?;
|
"
|
||||||
out.push_str(item_pusher);
|
if !{name}.is_empty() {{
|
||||||
out.push('\n');
|
write!(f, \"{}=\")?;
|
||||||
writeln!(&mut out, "if i < {name}.len() - 1 {{")?;
|
for (item, i) in {name}.iter().enumerate() {{
|
||||||
writeln!(&mut out, "s.push('{sep}')")?;
|
{item_pusher}
|
||||||
writeln!(&mut out, "}}")?;
|
if i < {name}.len() - 1 {{
|
||||||
writeln!(&mut out, "}}")?;
|
write!(f, '{sep}')?;
|
||||||
writeln!(&mut out, "s.push('&')")?;
|
}}
|
||||||
writeln!(&mut out, "}}")?;
|
}}
|
||||||
|
write!(f, '&')?;
|
||||||
|
}}",
|
||||||
|
param.name
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue