summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2024-04-20 22:16:32 +0200
committerCyborus <cyborus@cyborus.xyz>2024-04-20 22:16:32 +0200
commitf029ba57375e480f8e4dbf6d5055b1e44211e8d4 (patch)
tree6797de2d10e02b6acb84659fbde6ba4f35e604f4 /generator
parentMerge pull request 'regenerate for 201 status code for tags' (#41) from gen-t... (diff)
downloadforgejo-api-f029ba57375e480f8e4dbf6d5055b1e44211e8d4.tar.xz
forgejo-api-f029ba57375e480f8e4dbf6d5055b1e44211e8d4.zip
add derives for query structs
Diffstat (limited to 'generator')
-rw-r--r--generator/src/structs.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/generator/src/structs.rs b/generator/src/structs.rs
index 0b4de08..3449f5f 100644
--- a/generator/src/structs.rs
+++ b/generator/src/structs.rs
@@ -241,6 +241,8 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String>
let mut enums = Vec::new();
let mut fields = String::new();
let mut imp = String::new();
+ // only derive default if every field is optional
+ let mut can_derive_default = true;
for param in params {
let param = param.deref(spec)?;
if let ParameterIn::Query { param: query_param } = &param._in {
@@ -287,6 +289,7 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String>
fields.push_str(&field_name);
fields.push_str(": ");
if query_param.required {
+ can_derive_default = false;
fields.push_str(&ty);
} else {
fields.push_str("Option<");
@@ -425,11 +428,18 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String>
}
}
+ let derives = if can_derive_default {
+ "Debug, Clone, PartialEq, Default"
+ } else {
+ "Debug, Clone, PartialEq"
+ };
+
let result = if fields.is_empty() {
String::new()
} else {
let mut out = format!(
"
+#[derive({derives})]
pub struct {op_name} {{
{fields}
}}