Optimise applying metadata
This reduces the output MIR size of the example code from https://github.com/GREsau/schemars/issues/246 by ~50% (from 18k to 9k lines)
This commit is contained in:
parent
9921d2859f
commit
eb3077742f
2 changed files with 12 additions and 9 deletions
|
@ -135,6 +135,10 @@ pub fn insert_object_property<T: ?Sized + JsonSchema>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert_metadata_property(schema: &mut Schema, key: &str, value: impl Into<Value>) {
|
||||||
|
schema.ensure_object().insert(key.to_owned(), value.into());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn insert_validation_property(
|
pub fn insert_validation_property(
|
||||||
schema: &mut Schema,
|
schema: &mut Schema,
|
||||||
required_type: &str,
|
required_type: &str,
|
||||||
|
|
|
@ -20,7 +20,6 @@ impl<'a> SchemaMetadata<'a> {
|
||||||
if !setters.is_empty() {
|
if !setters.is_empty() {
|
||||||
*schema_expr = quote! {{
|
*schema_expr = quote! {{
|
||||||
let mut schema = #schema_expr;
|
let mut schema = #schema_expr;
|
||||||
let obj = schema.ensure_object();
|
|
||||||
#(#setters)*
|
#(#setters)*
|
||||||
schema
|
schema
|
||||||
}}
|
}}
|
||||||
|
@ -44,29 +43,29 @@ impl<'a> SchemaMetadata<'a> {
|
||||||
|
|
||||||
if let Some(title) = &self.title {
|
if let Some(title) = &self.title {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("title".to_owned(), #title.into());
|
schemars::_private::insert_metadata_property(&mut schema, "title", #title);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if let Some(description) = &self.description {
|
if let Some(description) = &self.description {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("description".to_owned(), #description.into());
|
schemars::_private::insert_metadata_property(&mut schema, "description", #description);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.deprecated {
|
if self.deprecated {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("deprecated".to_owned(), true.into());
|
schemars::_private::insert_metadata_property(&mut schema, "deprecated", true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.read_only {
|
if self.read_only {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("readOnly".to_owned(), true.into());
|
schemars::_private::insert_metadata_property(&mut schema, "readOnly", true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if self.write_only {
|
if self.write_only {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("writeOnly".to_owned(), true.into());
|
schemars::_private::insert_metadata_property(&mut schema, "writeOnly", true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,21 +76,21 @@ impl<'a> SchemaMetadata<'a> {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert("examples".to_owned(), schemars::_serde_json::Value::Array([#(#examples),*].into_iter().flatten().collect()));
|
schemars::_private::insert_metadata_property(&mut schema, "examples", schemars::_serde_json::Value::Array([#(#examples),*].into_iter().flatten().collect()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(default) = &self.default {
|
if let Some(default) = &self.default {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
if let Some(default) = #default.and_then(|d| schemars::_schemars_maybe_to_value!(d)) {
|
if let Some(default) = #default.and_then(|d| schemars::_schemars_maybe_to_value!(d)) {
|
||||||
obj.insert("default".to_owned(), default);
|
schemars::_private::insert_metadata_property(&mut schema, "default", default);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k, v) in self.extensions {
|
for (k, v) in self.extensions {
|
||||||
setters.push(quote! {
|
setters.push(quote! {
|
||||||
obj.insert(#k.to_owned(), schemars::_serde_json::json!(#v));
|
schemars::_private::insert_metadata_property(&mut schema, #k, schemars::_serde_json::json!(#v));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue