Allow deriving MakeSchema on simple structs!

This commit is contained in:
Graham Esau 2019-08-04 16:44:09 +01:00
parent fcb9d5d1c0
commit 5228ec65e5
9 changed files with 90 additions and 38 deletions

3
schemars_derive/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

View file

@ -0,0 +1,16 @@
[package]
name = "schemars_derive"
version = "0.1.0"
authors = ["Graham Esau <gesau@hotmail.co.uk>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "0.4"
quote = "0.6.13"
syn = { version = "0.15.22", features = ["visit"] } # do we need visit?
serde_derive_internals = "0.24.1"

View file

@ -0,0 +1,56 @@
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro;
extern crate proc_macro2;
use proc_macro2::TokenStream;
use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Fields};
#[proc_macro_derive(MakeSchema, attributes(schemars, serde))]
pub fn derive_make_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let fn_contents = match input.data {
Data::Struct(data) => struct_implementation(&data.fields),
_ => unimplemented!("Only structs work so far!"),
};
let impl_block = quote! {
#[automatically_derived]
impl #impl_generics schemars::make_schema::MakeSchema for #name #ty_generics #where_clause {
fn make_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
let mut o = schemars::SchemaObject {
..Default::default()
};
#fn_contents
o.into()
}
};
};
proc_macro::TokenStream::from(impl_block)
}
fn struct_implementation(fields: &Fields) -> TokenStream {
match fields {
Fields::Named(ref fields) => {
let recurse = fields.named.iter().map(|f| {
let name = (&f.ident).as_ref().map(ToString::to_string);
let ty = &f.ty;
quote_spanned! {f.span()=>
o.properties.insert(#name.to_owned(), gen.subschema_for::<#ty>());
}
});
quote! {
#(#recurse)*
}
}
_ => unimplemented!("Only named fields work so far!"),
}
}