Skip to content

Commit

Permalink
Remove temporary variables in generated code
Browse files Browse the repository at this point in the history
This further reduces MIR output size
  • Loading branch information
GREsau committed May 6, 2024
1 parent 1960b2d commit 9a1539e
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions schemars_derive/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,32 @@ pub struct SchemaMetadata<'a> {

impl<'a> SchemaMetadata<'a> {
pub fn apply_to_schema(&self, schema_expr: &mut TokenStream) {
let setters = self.make_setters();
if !setters.is_empty() {
*schema_expr = quote! {{
let schema = #schema_expr;
#(let schema = schemars::_private::metadata::#setters;)*
schema
}}
}
}

fn make_setters(&self) -> Vec<TokenStream> {
let mut setters = Vec::<TokenStream>::new();

if let Some(title) = &self.title {
setters.push(quote! {
add_title(schema, #title)
});
*schema_expr = quote! {
schemars::_private::metadata::add_title(#schema_expr, #title)
};
}
if let Some(description) = &self.description {
setters.push(quote! {
add_description(schema, #description)
});
*schema_expr = quote! {
schemars::_private::metadata::add_description(#schema_expr, #description)
};
}

if self.deprecated {
setters.push(quote! {
add_deprecated(schema, true)
});
*schema_expr = quote! {
schemars::_private::metadata::add_deprecated(#schema_expr, true)
};
}

if self.read_only {
setters.push(quote! {
add_read_only(schema, true)
});
*schema_expr = quote! {
schemars::_private::metadata::add_read_only(#schema_expr, true)
};
}
if self.write_only {
setters.push(quote! {
add_write_only(schema, true)
});
*schema_expr = quote! {
schemars::_private::metadata::add_write_only(#schema_expr, true)
};
}

if !self.examples.is_empty() {
Expand All @@ -60,17 +47,16 @@ impl<'a> SchemaMetadata<'a> {
schemars::_serde_json::value::to_value(#eg())
}
});
setters.push(quote! {
add_examples(schema, [#(#examples),*].into_iter().flatten())
});

*schema_expr = quote! {
schemars::_private::metadata::add_examples(#schema_expr, [#(#examples),*].into_iter().flatten())
};
}

if let Some(default) = &self.default {
setters.push(quote! {
add_default(schema, #default.and_then(|d| schemars::_schemars_maybe_to_value!(d)))
});
*schema_expr = quote! {
schemars::_private::metadata::add_default(#schema_expr, #default.and_then(|d| schemars::_schemars_maybe_to_value!(d)))
};
}

setters
}
}

0 comments on commit 9a1539e

Please sign in to comment.