Skip to content

Commit

Permalink
better error message; add default to newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 19, 2023
1 parent d1fa491 commit d6917e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn extract_request_schema<'a>(
.content
.get("application/json")
.ok_or_else(|| anyhow!("No json body"))?;
Ok(content.schema.as_ref().unwrap().resolve(spec))
Ok(content.schema.as_ref().expect(&format!("Expecting a ref for {}", operation.operation_id.as_ref().map(|s| s.as_str()).unwrap_or_default())).resolve(spec))
}

pub fn extract_param(param: &ReferenceOr<oa::Parameter>, spec: &OpenAPI) -> Result<Parameter> {
Expand Down
12 changes: 7 additions & 5 deletions libninja/src/rust/lower_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl FieldExt for HirField {
#[serde(with = "rust_decimal::serde::str")]
});
}
},
}
_ => {}
}
decorators
Expand All @@ -99,7 +99,6 @@ pub trait StructExt {
}

impl StructExt for Struct {

fn implements_default(&self, spec: &HirSpec) -> bool {
self.fields.values().all(|f| f.implements_default(spec))
}
Expand Down Expand Up @@ -288,13 +287,16 @@ fn create_enum_struct(e: &StrEnum) -> TokenStream {
}


pub fn create_newtype_struct(schema: &NewType) -> TokenStream {
pub fn create_newtype_struct(schema: &NewType, spec: &HirSpec) -> TokenStream {
let name = schema.name.to_rust_struct();
let fields = schema.fields.iter().map(|f| {
f.ty.to_rust_type()
});
let default = schema.fields.iter().all(|f| f.implements_default(spec))
.then(|| { quote! { , Default } })
.unwrap_or_default();
quote! {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize #default)]
pub struct #name(#(pub #fields),*);
}
}
Expand All @@ -313,7 +315,7 @@ pub fn create_typealias(name: &str, schema: &HirField) -> TokenStream {
pub fn create_struct(record: &Record, config: &ConfigFlags, spec: &HirSpec) -> TokenStream {
match record {
Record::Struct(s) => create_sumtype_struct(s, config, spec),
Record::NewType(nt) => create_newtype_struct(nt),
Record::NewType(nt) => create_newtype_struct(nt, spec),
Record::Enum(en) => create_enum_struct(en),
Record::TypeAlias(name, field) => create_typealias(name, field),
}
Expand Down

0 comments on commit d6917e6

Please sign in to comment.