From f029ba57375e480f8e4dbf6d5055b1e44211e8d4 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Sat, 20 Apr 2024 16:16:32 -0400 Subject: add derives for query structs --- generator/src/structs.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'generator') 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 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 } = ¶m._in { @@ -287,6 +289,7 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result 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 } } + 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} }} -- cgit v1.2.3