Merge branch 'master' into validate

This commit is contained in:
Graham Esau 2021-04-15 16:03:25 +01:00
commit b68132f17d
31 changed files with 440 additions and 143 deletions

View file

@ -14,6 +14,7 @@ mod schema_exprs;
use ast::*;
use proc_macro2::TokenStream;
use syn::spanned::Spanned;
#[proc_macro_derive(JsonSchema, attributes(schemars, serde, validate))]
pub fn derive_json_schema_wrapper(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
@ -40,14 +41,20 @@ fn derive_json_schema(
attr::process_serde_attrs(&mut input)?;
let cont = Container::from_ast(&input)?;
let crate_alias = cont.attrs.crate_name.as_ref().map(|path| {
quote_spanned! {path.span()=>
use #path as schemars;
}
});
let type_name = &cont.ident;
let (impl_generics, ty_generics, where_clause) = cont.generics.split_for_impl();
if let Some(transparent_field) = cont.transparent_field() {
let (ty, type_def) = schema_exprs::type_for_schema(transparent_field, 0);
let (ty, type_def) = schema_exprs::type_for_field_schema(transparent_field, 0);
return Ok(quote! {
const _: () = {
#crate_alias
#type_def
#[automatically_derived]
@ -64,18 +71,12 @@ fn derive_json_schema(
<#ty as schemars::JsonSchema>::json_schema(gen)
}
fn json_schema_for_flatten(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<#ty as schemars::JsonSchema>::json_schema_for_flatten(gen)
fn _schemars_private_non_optional_json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(gen)
}
fn add_schema_as_property(
gen: &mut schemars::gen::SchemaGenerator,
parent: &mut schemars::schema::SchemaObject,
name: String,
metadata: Option<schemars::schema::Metadata>,
required: Option<bool>,
) {
<#ty as schemars::JsonSchema>::add_schema_as_property(gen, parent, name, metadata, required)
fn _schemars_private_is_option() -> bool {
<#ty as schemars::JsonSchema>::_schemars_private_is_option()
}
};
};
@ -122,16 +123,20 @@ fn derive_json_schema(
};
Ok(quote! {
#[automatically_derived]
#[allow(unused_braces)]
impl #impl_generics schemars::JsonSchema for #type_name #ty_generics #where_clause {
fn schema_name() -> std::string::String {
#schema_name
}
const _: () = {
#crate_alias
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
#schema_expr
}
#[automatically_derived]
#[allow(unused_braces)]
impl #impl_generics schemars::JsonSchema for #type_name #ty_generics #where_clause {
fn schema_name() -> std::string::String {
#schema_name
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
#schema_expr
}
};
};
})
}