2017-01-12 21:15:13 -08:00
|
|
|
use syn::{self, aster, Ident};
|
2017-01-08 00:59:18 -08:00
|
|
|
use quote::Tokens;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
use bound;
|
2016-06-30 19:53:57 -07:00
|
|
|
use internals::ast::{Body, Field, Item, Style, Variant};
|
2016-09-12 00:04:21 -07:00
|
|
|
use internals::{self, attr};
|
2016-06-19 20:15:49 -07:00
|
|
|
|
2016-09-28 08:57:53 -07:00
|
|
|
use std::iter;
|
2016-06-19 20:15:49 -07:00
|
|
|
|
2016-09-27 00:11:37 -07:00
|
|
|
pub fn expand_derive_deserialize(item: &syn::MacroInput) -> Result<Tokens, String> {
|
2016-09-12 00:04:21 -07:00
|
|
|
let item = {
|
|
|
|
let ctxt = internals::Ctxt::new();
|
|
|
|
let item = Item::from_ast(&ctxt, item);
|
|
|
|
check_no_str(&ctxt, &item);
|
2016-09-27 00:11:37 -07:00
|
|
|
try!(ctxt.check());
|
2016-09-12 00:04:21 -07:00
|
|
|
item
|
2015-05-17 23:14:38 -07:00
|
|
|
};
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let impl_generics = build_impl_generics(&item);
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let ty = aster::ty().path()
|
|
|
|
.segment(item.ident.clone()).with_generics(impl_generics.clone()).build()
|
2015-03-14 13:09:37 -07:00
|
|
|
.build();
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let body = deserialize_body(&item,
|
2016-06-14 23:37:20 -07:00
|
|
|
&impl_generics,
|
|
|
|
ty.clone());
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2015-03-15 17:47:25 -07:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-12 21:15:13 -08:00
|
|
|
let dummy_const = Ident::new(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
|
2016-04-24 10:59:57 -07:00
|
|
|
|
2016-09-27 00:11:37 -07:00
|
|
|
Ok(quote! {
|
2016-04-24 10:59:57 -07:00
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
2016-09-12 00:04:21 -07:00
|
|
|
const #dummy_const: () = {
|
2016-04-24 10:59:57 -07:00
|
|
|
extern crate serde as _serde;
|
|
|
|
#[automatically_derived]
|
2017-01-08 00:47:37 -08:00
|
|
|
impl #impl_generics _serde::Deserialize for #ty #where_clause {
|
2016-09-12 00:04:21 -07:00
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<#ty, __D::Error>
|
2017-01-08 00:47:37 -08:00
|
|
|
where __D: _serde::Deserializer
|
2016-09-27 01:04:11 -07:00
|
|
|
#body
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
2016-04-24 10:59:57 -07:00
|
|
|
};
|
2016-09-27 00:11:37 -07:00
|
|
|
})
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
// All the generics in the input, plus a bound `T: Deserialize` for each generic
|
|
|
|
// field type that will be deserialized by us, plus a bound `T: Default` for
|
|
|
|
// each generic field type that will be set to a default value.
|
2016-09-12 00:04:21 -07:00
|
|
|
fn build_impl_generics(item: &Item) -> syn::Generics {
|
2016-06-14 23:37:20 -07:00
|
|
|
let generics = bound::without_defaults(item.generics);
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
let generics = bound::with_where_predicates_from_fields(
|
2016-09-12 00:04:21 -07:00
|
|
|
item, &generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
|attrs| attrs.de_bound());
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
match item.attrs.de_bound() {
|
2016-05-20 22:03:20 -07:00
|
|
|
Some(predicates) => {
|
2016-09-12 00:04:21 -07:00
|
|
|
bound::with_where_predicates(&generics, predicates)
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
None => {
|
2016-09-12 00:04:21 -07:00
|
|
|
let generics = bound::with_bound(item, &generics,
|
2016-06-05 10:47:40 -07:00
|
|
|
needs_deserialize_bound,
|
2017-01-12 21:24:51 -08:00
|
|
|
&aster::path().ids(&["_serde", "Deserialize"]).build());
|
2016-09-27 00:46:03 -07:00
|
|
|
bound::with_bound(item, &generics,
|
2016-05-20 22:03:20 -07:00
|
|
|
requires_default,
|
2016-09-27 00:46:03 -07:00
|
|
|
&aster::path().global().ids(&["std", "default", "Default"]).build())
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields with a `skip_deserializing` or `deserialize_with` attribute are not
|
2016-06-05 10:47:40 -07:00
|
|
|
// deserialized by us so we do not generate a bound. Fields with a `bound`
|
|
|
|
// attribute specify their own bound so we do not generate one. All other fields
|
|
|
|
// may need a `T: Deserialize` bound where T is the type of the field.
|
2016-06-19 20:15:49 -07:00
|
|
|
fn needs_deserialize_bound(attrs: &attr::Field) -> bool {
|
2016-06-14 11:22:49 -07:00
|
|
|
!attrs.skip_deserializing()
|
2016-05-20 22:03:20 -07:00
|
|
|
&& attrs.deserialize_with().is_none()
|
2016-06-05 10:47:40 -07:00
|
|
|
&& attrs.de_bound().is_none()
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields with a `default` attribute (not `default=...`), and fields with a
|
|
|
|
// `skip_deserializing` attribute that do not also have `default=...`.
|
2016-06-19 20:15:49 -07:00
|
|
|
fn requires_default(attrs: &attr::Field) -> bool {
|
2016-06-09 23:21:42 -07:00
|
|
|
attrs.default() == &attr::FieldDefault::Default
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 17:47:25 -07:00
|
|
|
fn deserialize_body(
|
2016-06-21 19:12:08 -07:00
|
|
|
item: &Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
impl_generics: &syn::Generics,
|
|
|
|
ty: syn::Ty,
|
|
|
|
) -> Tokens {
|
2016-06-14 23:37:20 -07:00
|
|
|
match item.body {
|
2016-06-21 19:12:08 -07:00
|
|
|
Body::Enum(ref variants) => {
|
2015-03-15 17:47:25 -07:00
|
|
|
deserialize_item_enum(
|
2016-09-12 00:04:21 -07:00
|
|
|
&item.ident,
|
2015-03-15 17:47:25 -07:00
|
|
|
impl_generics,
|
|
|
|
ty,
|
2016-06-14 23:37:20 -07:00
|
|
|
variants,
|
|
|
|
&item.attrs)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Body::Struct(Style::Struct, ref fields) => {
|
2016-06-14 23:37:20 -07:00
|
|
|
if fields.iter().any(|field| field.ident.is_none()) {
|
2016-09-12 00:04:21 -07:00
|
|
|
panic!("struct has unnamed fields");
|
2015-10-17 19:44:07 -07:00
|
|
|
}
|
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
deserialize_struct(
|
2016-09-12 00:04:21 -07:00
|
|
|
&item.ident,
|
2016-05-15 13:32:06 -07:00
|
|
|
None,
|
2015-03-15 17:47:25 -07:00
|
|
|
impl_generics,
|
|
|
|
ty,
|
2016-05-15 15:54:20 -07:00
|
|
|
fields,
|
2016-06-14 23:37:20 -07:00
|
|
|
&item.attrs)
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Body::Struct(Style::Tuple, ref fields) |
|
|
|
|
Body::Struct(Style::Newtype, ref fields) => {
|
2016-06-14 23:37:20 -07:00
|
|
|
if fields.iter().any(|field| field.ident.is_some()) {
|
2016-09-12 00:04:21 -07:00
|
|
|
panic!("tuple struct has named fields");
|
2015-10-17 19:44:07 -07:00
|
|
|
}
|
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
deserialize_tuple(
|
2016-09-12 00:04:21 -07:00
|
|
|
&item.ident,
|
2016-05-15 13:32:06 -07:00
|
|
|
None,
|
2015-03-15 17:47:25 -07:00
|
|
|
impl_generics,
|
|
|
|
ty,
|
2015-10-17 19:44:07 -07:00
|
|
|
fields,
|
2016-06-14 23:37:20 -07:00
|
|
|
&item.attrs)
|
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Body::Struct(Style::Unit, _) => {
|
2016-06-14 23:37:20 -07:00
|
|
|
deserialize_unit_struct(
|
2016-09-12 00:04:21 -07:00
|
|
|
&item.ident,
|
2016-06-14 23:37:20 -07:00
|
|
|
&item.attrs)
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
2016-09-12 00:04:21 -07:00
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// 1. the struct declaration
|
|
|
|
// 2. the visitor type, including generics
|
|
|
|
// 3. the expression for instantiating the visitor
|
|
|
|
fn deserialize_visitor(generics: &syn::Generics) -> (Tokens, Tokens, Tokens) {
|
2016-08-19 11:12:38 -04:00
|
|
|
if generics.lifetimes.is_empty() && generics.ty_params.is_empty() {
|
2016-06-14 23:37:20 -07:00
|
|
|
(
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
struct __Visitor;
|
|
|
|
},
|
|
|
|
quote!(__Visitor),
|
|
|
|
quote!(__Visitor),
|
2016-06-14 23:37:20 -07:00
|
|
|
)
|
2015-03-14 13:09:37 -07:00
|
|
|
} else {
|
2016-09-27 01:04:11 -07:00
|
|
|
let where_clause = &generics.where_clause;
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let num_phantoms = generics.lifetimes.len() + generics.ty_params.len();
|
|
|
|
|
|
|
|
let phantom_types = generics.lifetimes.iter()
|
|
|
|
.map(|lifetime_def| {
|
|
|
|
let lifetime = &lifetime_def.lifetime;
|
|
|
|
quote!(::std::marker::PhantomData<& #lifetime ()>)
|
|
|
|
}).chain(generics.ty_params.iter()
|
|
|
|
.map(|ty_param| {
|
|
|
|
let ident = &ty_param.ident;
|
|
|
|
quote!(::std::marker::PhantomData<#ident>)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let all_params = generics.lifetimes.iter()
|
|
|
|
.map(|lifetime_def| {
|
|
|
|
let lifetime = &lifetime_def.lifetime;
|
|
|
|
quote!(#lifetime)
|
|
|
|
}).chain(generics.ty_params.iter()
|
|
|
|
.map(|ty_param| {
|
|
|
|
let ident = &ty_param.ident;
|
|
|
|
quote!(#ident)
|
|
|
|
}));
|
|
|
|
|
2017-01-12 21:28:09 -08:00
|
|
|
let ty_param_idents = if generics.ty_params.is_empty() {
|
2016-09-12 00:04:21 -07:00
|
|
|
None
|
|
|
|
} else {
|
2017-01-12 21:28:09 -08:00
|
|
|
let ty_param_idents = generics.ty_params.iter().map(|t| &t.ident);
|
2016-10-03 21:09:52 -07:00
|
|
|
Some(quote!(::<#(#ty_param_idents),*>))
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
|
|
|
|
2016-09-28 08:57:53 -07:00
|
|
|
let phantom_exprs = iter::repeat(quote!(::std::marker::PhantomData)).take(num_phantoms);
|
2016-09-12 00:04:21 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
(
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
struct __Visitor #generics ( #(#phantom_types),* ) #where_clause;
|
2016-09-12 00:04:21 -07:00
|
|
|
},
|
2016-10-03 21:09:52 -07:00
|
|
|
quote!(__Visitor <#(#all_params),*> ),
|
|
|
|
quote!(__Visitor #ty_param_idents ( #(#phantom_exprs),* )),
|
2016-06-14 23:37:20 -07:00
|
|
|
)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 15:20:00 -07:00
|
|
|
fn deserialize_unit_struct(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
|
|
|
let type_name = item_attrs.name().deserialize_name();
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-27 01:04:11 -07:00
|
|
|
quote!({
|
2015-03-14 13:09:37 -07:00
|
|
|
struct __Visitor;
|
|
|
|
|
2016-04-24 10:59:57 -07:00
|
|
|
impl _serde::de::Visitor for __Visitor {
|
2016-09-12 00:04:21 -07:00
|
|
|
type Value = #type_ident;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
|
|
|
#[inline]
|
2016-09-12 00:04:21 -07:00
|
|
|
fn visit_unit<__E>(&mut self) -> ::std::result::Result<#type_ident, __E>
|
2016-05-08 20:29:10 -07:00
|
|
|
where __E: _serde::de::Error,
|
2015-03-14 13:09:37 -07:00
|
|
|
{
|
2016-09-12 00:04:21 -07:00
|
|
|
Ok(#type_ident)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-01-09 00:32:50 -08:00
|
|
|
fn visit_seq<__V>(&mut self, _: __V) -> ::std::result::Result<#type_ident, __V::Error>
|
2016-05-08 20:29:10 -07:00
|
|
|
where __V: _serde::de::SeqVisitor,
|
2015-03-14 13:09:37 -07:00
|
|
|
{
|
2017-01-09 00:32:50 -08:00
|
|
|
Ok(#type_ident)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
deserializer.deserialize_unit_struct(#type_name, __Visitor)
|
2016-09-27 01:04:11 -07:00
|
|
|
})
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2016-05-15 13:32:06 -07:00
|
|
|
fn deserialize_tuple(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
variant_ident: Option<&syn::Ident>,
|
|
|
|
impl_generics: &syn::Generics,
|
|
|
|
ty: syn::Ty,
|
2016-06-21 19:12:08 -07:00
|
|
|
fields: &[Field],
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2015-07-30 09:38:09 -07:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(impl_generics);
|
2015-07-30 09:38:09 -07:00
|
|
|
|
2016-05-15 13:32:06 -07:00
|
|
|
let is_enum = variant_ident.is_some();
|
|
|
|
let type_path = match variant_ident {
|
2016-09-12 00:04:21 -07:00
|
|
|
Some(variant_ident) => quote!(#type_ident::#variant_ident),
|
|
|
|
None => quote!(#type_ident),
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-07-30 09:38:09 -07:00
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
let nfields = fields.len();
|
|
|
|
|
|
|
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
2016-06-14 23:37:20 -07:00
|
|
|
Some(deserialize_newtype_struct(
|
2016-05-15 15:54:20 -07:00
|
|
|
type_ident,
|
|
|
|
&type_path,
|
|
|
|
impl_generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
&fields[0],
|
|
|
|
))
|
2016-05-15 13:32:06 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
let visit_seq = deserialize_seq(
|
2016-05-15 15:54:20 -07:00
|
|
|
type_ident,
|
2016-09-12 00:04:21 -07:00
|
|
|
&type_path,
|
2016-05-15 15:54:20 -07:00
|
|
|
impl_generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
fields,
|
2016-05-15 15:54:20 -07:00
|
|
|
false,
|
2016-06-14 23:37:20 -07:00
|
|
|
);
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-05-15 13:32:06 -07:00
|
|
|
let dispatch = if is_enum {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!(visitor.visit_tuple(#nfields, #visitor_expr))
|
2016-05-15 15:54:20 -07:00
|
|
|
} else if nfields == 1 {
|
2016-09-12 00:04:21 -07:00
|
|
|
let type_name = item_attrs.name().deserialize_name();
|
|
|
|
quote!(deserializer.deserialize_newtype_struct(#type_name, #visitor_expr))
|
2016-05-15 13:32:06 -07:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
let type_name = item_attrs.name().deserialize_name();
|
|
|
|
quote!(deserializer.deserialize_tuple_struct(#type_name, #nfields, #visitor_expr))
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-13 00:06:50 -08:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
|
|
|
let visitor_var = if all_skipped {
|
|
|
|
quote!(_)
|
|
|
|
} else {
|
|
|
|
quote!(mut visitor)
|
|
|
|
};
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
|
|
|
#visitor_item
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
impl #impl_generics _serde::de::Visitor for #visitor_ty #where_clause {
|
|
|
|
type Value = #ty;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#visit_newtype_struct
|
2016-05-15 13:32:06 -07:00
|
|
|
|
2015-07-30 09:31:16 -07:00
|
|
|
#[inline]
|
2017-01-13 00:06:50 -08:00
|
|
|
fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error>
|
2016-08-03 21:08:17 -07:00
|
|
|
where __V: _serde::de::SeqVisitor
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
|
|
|
#visit_seq
|
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#dispatch
|
|
|
|
})
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2015-03-14 15:20:00 -07:00
|
|
|
fn deserialize_seq(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
type_path: &Tokens,
|
|
|
|
impl_generics: &syn::Generics,
|
2016-06-21 19:12:08 -07:00
|
|
|
fields: &[Field],
|
2016-05-15 15:54:20 -07:00
|
|
|
is_struct: bool,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2017-01-12 21:41:27 -08:00
|
|
|
let vars = (0..fields.len()).map(field_i as fn(_) -> _);
|
|
|
|
|
2016-07-05 00:52:19 -07:00
|
|
|
let mut index_in_seq = 0usize;
|
2017-01-12 21:41:27 -08:00
|
|
|
let let_values = vars.clone().zip(fields)
|
|
|
|
.map(|(var, field)| {
|
2016-06-14 23:37:20 -07:00
|
|
|
if field.attrs.skip_deserializing() {
|
2016-09-12 00:04:21 -07:00
|
|
|
let default = expr_is_missing(&field.attrs);
|
|
|
|
quote! {
|
2017-01-12 21:41:27 -08:00
|
|
|
let #var = #default;
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-03-06 23:27:12 -08:00
|
|
|
} else {
|
2016-06-14 23:37:20 -07:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-07 12:33:59 -07:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!(try!(visitor.visit::<#field_ty>()))
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
2016-09-27 00:46:03 -07:00
|
|
|
type_ident, impl_generics, field.ty, path);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
|
|
|
#wrapper
|
|
|
|
#wrapper_impl
|
|
|
|
try!(visitor.visit::<#wrapper_ty>()).map(|wrap| wrap.value)
|
2016-05-07 12:33:59 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
let assign = quote! {
|
2017-01-12 21:41:27 -08:00
|
|
|
let #var = match #visit {
|
2016-05-07 12:33:59 -07:00
|
|
|
Some(value) => { value },
|
2016-03-06 23:27:12 -08:00
|
|
|
None => {
|
2016-09-12 00:04:21 -07:00
|
|
|
return Err(_serde::de::Error::invalid_length(#index_in_seq));
|
2016-03-06 23:27:12 -08:00
|
|
|
}
|
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
2016-07-05 00:52:19 -07:00
|
|
|
index_in_seq += 1;
|
|
|
|
assign
|
2016-03-06 23:27:12 -08:00
|
|
|
}
|
2017-01-12 21:29:50 -08:00
|
|
|
});
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
let result = if is_struct {
|
2017-01-12 21:41:27 -08:00
|
|
|
let names = fields.iter().map(|f| &f.ident);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2017-01-12 21:41:27 -08:00
|
|
|
#type_path { #( #names: #vars ),* }
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2017-01-12 21:41:27 -08:00
|
|
|
#type_path ( #(#vars),* )
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
};
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#let_values)*
|
2016-09-12 00:04:21 -07:00
|
|
|
Ok(#result)
|
|
|
|
}
|
2015-07-21 21:35:20 -07:00
|
|
|
}
|
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
fn deserialize_newtype_struct(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
type_path: &Tokens,
|
|
|
|
impl_generics: &syn::Generics,
|
2016-06-21 19:12:08 -07:00
|
|
|
field: &Field,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2016-06-14 23:37:20 -07:00
|
|
|
let value = match field.attrs.deserialize_with() {
|
2016-05-15 15:54:20 -07:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
try!(<#field_ty as _serde::Deserialize>::deserialize(__e))
|
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
2016-09-27 00:46:03 -07:00
|
|
|
type_ident, impl_generics, field.ty, path);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
|
|
|
#wrapper
|
|
|
|
#wrapper_impl
|
|
|
|
try!(<#wrapper_ty as _serde::Deserialize>::deserialize(__e)).value
|
2016-05-15 15:54:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2016-05-15 15:54:20 -07:00
|
|
|
#[inline]
|
|
|
|
fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
2017-01-08 00:47:37 -08:00
|
|
|
where __E: _serde::Deserializer,
|
2016-05-15 15:54:20 -07:00
|
|
|
{
|
2016-09-12 00:04:21 -07:00
|
|
|
Ok(#type_path(#value))
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 17:47:25 -07:00
|
|
|
fn deserialize_struct(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
variant_ident: Option<&syn::Ident>,
|
|
|
|
impl_generics: &syn::Generics,
|
|
|
|
ty: syn::Ty,
|
2016-06-21 19:12:08 -07:00
|
|
|
fields: &[Field],
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2015-03-15 17:47:25 -07:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(impl_generics);
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-05-15 13:32:06 -07:00
|
|
|
let type_path = match variant_ident {
|
2016-09-27 00:46:03 -07:00
|
|
|
Some(variant_ident) => quote!(#type_ident::#variant_ident),
|
2016-09-12 00:04:21 -07:00
|
|
|
None => quote!(#type_ident),
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
let visit_seq = deserialize_seq(
|
2016-05-07 12:33:59 -07:00
|
|
|
type_ident,
|
2016-09-12 00:04:21 -07:00
|
|
|
&type_path,
|
2016-05-07 12:33:59 -07:00
|
|
|
impl_generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
fields,
|
2016-05-15 15:54:20 -07:00
|
|
|
true,
|
2016-06-14 23:37:20 -07:00
|
|
|
);
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
let (field_visitor, fields_stmt, visit_map) = deserialize_struct_visitor(
|
2016-05-07 12:33:59 -07:00
|
|
|
type_ident,
|
2016-09-12 00:04:21 -07:00
|
|
|
type_path,
|
2016-05-07 12:33:59 -07:00
|
|
|
impl_generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
fields,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2016-06-14 23:37:20 -07:00
|
|
|
);
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
let is_enum = variant_ident.is_some();
|
2016-05-15 13:32:06 -07:00
|
|
|
let dispatch = if is_enum {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
visitor.visit_struct(FIELDS, #visitor_expr)
|
|
|
|
}
|
2016-05-15 13:32:06 -07:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
let type_name = item_attrs.name().deserialize_name();
|
|
|
|
quote! {
|
|
|
|
deserializer.deserialize_struct(#type_name, FIELDS, #visitor_expr)
|
|
|
|
}
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-13 00:06:50 -08:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
|
|
|
let visitor_var = if all_skipped {
|
|
|
|
quote!(_)
|
|
|
|
} else {
|
|
|
|
quote!(mut visitor)
|
|
|
|
};
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
|
|
|
#field_visitor
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#visitor_item
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
impl #impl_generics _serde::de::Visitor for #visitor_ty #where_clause {
|
|
|
|
type Value = #ty;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2015-07-21 21:35:20 -07:00
|
|
|
#[inline]
|
2017-01-13 00:06:50 -08:00
|
|
|
fn visit_seq<__V>(&mut self, #visitor_var: __V) -> ::std::result::Result<#ty, __V::Error>
|
2016-08-03 21:08:17 -07:00
|
|
|
where __V: _serde::de::SeqVisitor
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
|
|
|
#visit_seq
|
|
|
|
}
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
#[inline]
|
2016-09-12 00:04:21 -07:00
|
|
|
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error>
|
2016-08-03 21:08:17 -07:00
|
|
|
where __V: _serde::de::MapVisitor
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
|
|
|
#visit_map
|
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#fields_stmt
|
2015-07-03 19:39:27 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#dispatch
|
|
|
|
})
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 17:47:25 -07:00
|
|
|
fn deserialize_item_enum(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
impl_generics: &syn::Generics,
|
|
|
|
ty: syn::Ty,
|
2016-06-21 19:12:08 -07:00
|
|
|
variants: &[Variant],
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2015-03-15 17:47:25 -07:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let type_name = item_attrs.name().deserialize_name();
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-12 23:30:57 -08:00
|
|
|
let variant_names_idents = variants.iter()
|
2017-01-12 23:17:45 -08:00
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
|
|
|
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)))
|
|
|
|
.collect();
|
|
|
|
|
2015-03-15 22:04:17 -07:00
|
|
|
let variant_visitor = deserialize_field_visitor(
|
2017-01-12 23:30:57 -08:00
|
|
|
variant_names_idents,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2016-02-21 16:26:52 -08:00
|
|
|
true,
|
2015-03-15 22:04:17 -07:00
|
|
|
);
|
|
|
|
|
2017-01-12 20:21:59 +01:00
|
|
|
let variant_names = variants.iter().map(|variant| variant.attrs.name().deserialize_name());
|
2015-07-29 12:04:31 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let variants_stmt = quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
2015-07-29 12:04:31 -07:00
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
// Match arms to extract a variant from a string
|
2017-01-12 23:35:39 -08:00
|
|
|
let variant_arms = variants.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
|
|
|
.map(|(i, variant)| {
|
|
|
|
let variant_name = field_i(i);
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2017-01-12 23:35:39 -08:00
|
|
|
let block = deserialize_variant(
|
|
|
|
type_ident,
|
|
|
|
impl_generics,
|
|
|
|
ty.clone(),
|
|
|
|
variant,
|
|
|
|
item_attrs,
|
|
|
|
);
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2017-01-12 23:35:39 -08:00
|
|
|
quote! {
|
|
|
|
__Field::#variant_name => #block
|
|
|
|
}
|
|
|
|
});
|
2017-01-11 11:02:24 -08:00
|
|
|
|
2017-01-12 23:35:39 -08:00
|
|
|
let all_skipped = variants.iter().all(|variant| variant.attrs.skip_deserializing());
|
|
|
|
let match_variant = if all_skipped {
|
2017-01-11 11:02:24 -08:00
|
|
|
// This is an empty enum like `enum Impossible {}` or an enum in which
|
|
|
|
// all variants have `#[serde(skip_deserializing)]`.
|
|
|
|
quote! {
|
2017-01-11 11:34:47 -08:00
|
|
|
// FIXME: Once we drop support for Rust 1.15:
|
|
|
|
// let Err(err) = visitor.visit_variant::<__Field>();
|
|
|
|
// Err(err)
|
2017-01-11 11:02:24 -08:00
|
|
|
visitor.visit_variant::<__Field>().map(|impossible| match impossible {})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
match try!(visitor.visit_variant()) {
|
|
|
|
#(#variant_arms)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(impl_generics);
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2016-09-27 01:04:11 -07:00
|
|
|
quote!({
|
2016-09-12 00:04:21 -07:00
|
|
|
#variant_visitor
|
2015-03-15 22:04:17 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#visitor_item
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-08 00:23:58 -08:00
|
|
|
impl #impl_generics _serde::de::Visitor for #visitor_ty #where_clause {
|
2016-09-12 00:04:21 -07:00
|
|
|
type Value = #ty;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-01-08 00:23:58 -08:00
|
|
|
fn visit_enum<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<#ty, __V::Error>
|
2016-04-24 10:59:57 -07:00
|
|
|
where __V: _serde::de::VariantVisitor,
|
2015-03-14 13:09:37 -07:00
|
|
|
{
|
2017-01-11 11:02:24 -08:00
|
|
|
#match_variant
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#variants_stmt
|
2015-07-29 12:04:31 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
deserializer.deserialize_enum(#type_name, VARIANTS, #visitor_expr)
|
2016-09-27 01:04:11 -07:00
|
|
|
})
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 17:47:25 -07:00
|
|
|
fn deserialize_variant(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
generics: &syn::Generics,
|
|
|
|
ty: syn::Ty,
|
2016-06-21 19:12:08 -07:00
|
|
|
variant: &Variant,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
|
|
|
let variant_ident = &variant.ident;
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
match variant.style {
|
2016-06-21 19:12:08 -07:00
|
|
|
Style::Unit => {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
2015-04-06 20:04:01 -07:00
|
|
|
try!(visitor.visit_unit());
|
2016-09-12 00:04:21 -07:00
|
|
|
Ok(#type_ident::#variant_ident)
|
|
|
|
})
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Style::Newtype => {
|
2016-05-15 15:54:20 -07:00
|
|
|
deserialize_newtype_variant(
|
|
|
|
type_ident,
|
|
|
|
variant_ident,
|
|
|
|
generics,
|
2016-06-14 23:37:20 -07:00
|
|
|
&variant.fields[0],
|
2016-05-15 15:54:20 -07:00
|
|
|
)
|
2015-07-14 11:12:01 +02:00
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Style::Tuple => {
|
2016-05-15 13:32:06 -07:00
|
|
|
deserialize_tuple(
|
2015-03-15 17:47:25 -07:00
|
|
|
type_ident,
|
2016-05-15 13:32:06 -07:00
|
|
|
Some(variant_ident),
|
2015-03-15 17:47:25 -07:00
|
|
|
generics,
|
|
|
|
ty,
|
2016-06-14 23:37:20 -07:00
|
|
|
&variant.fields,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2015-03-15 22:04:17 -07:00
|
|
|
)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
2016-06-21 19:12:08 -07:00
|
|
|
Style::Struct => {
|
2016-05-15 13:32:06 -07:00
|
|
|
deserialize_struct(
|
2015-03-15 17:47:25 -07:00
|
|
|
type_ident,
|
2016-05-15 13:32:06 -07:00
|
|
|
Some(variant_ident),
|
2015-03-15 17:47:25 -07:00
|
|
|
generics,
|
|
|
|
ty,
|
2016-06-14 23:37:20 -07:00
|
|
|
&variant.fields,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2015-03-15 22:04:17 -07:00
|
|
|
)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
fn deserialize_newtype_variant(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
variant_ident: &syn::Ident,
|
|
|
|
impl_generics: &syn::Generics,
|
2016-06-21 19:12:08 -07:00
|
|
|
field: &Field,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2016-06-14 23:37:20 -07:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-15 15:54:20 -07:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!(try!(visitor.visit_newtype::<#field_ty>()))
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
2016-09-27 00:46:03 -07:00
|
|
|
type_ident, impl_generics, field.ty, path);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
|
|
|
#wrapper
|
|
|
|
#wrapper_impl
|
|
|
|
try!(visitor.visit_newtype::<#wrapper_ty>()).value
|
2016-05-15 15:54:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-09-28 09:18:18 -07:00
|
|
|
quote! {
|
|
|
|
Ok(#type_ident::#variant_ident(#visit)),
|
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
|
2015-03-15 15:31:31 -07:00
|
|
|
fn deserialize_field_visitor(
|
2017-01-12 23:17:45 -08:00
|
|
|
fields: Vec<(String, Ident)>,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-02-21 16:26:52 -08:00
|
|
|
is_variant: bool,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2017-01-12 23:17:45 -08:00
|
|
|
let field_names = fields.iter().map(|&(ref name, _)| name);
|
|
|
|
let field_idents: &Vec<_> = &fields.iter().map(|&(_, ref ident)| ident).collect();
|
2015-03-15 18:37:26 -07:00
|
|
|
|
2017-01-11 11:02:24 -08:00
|
|
|
let ignore_variant = if is_variant || item_attrs.deny_unknown_fields() {
|
2016-04-10 19:54:54 -07:00
|
|
|
None
|
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
Some(quote!(__ignore,))
|
2016-01-10 19:34:48 -08:00
|
|
|
};
|
|
|
|
|
2017-01-09 00:37:37 -08:00
|
|
|
let fallthrough_arm = if is_variant {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2017-01-08 00:59:18 -08:00
|
|
|
Err(_serde::de::Error::unknown_variant(value))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2017-01-08 00:59:18 -08:00
|
|
|
} else if item_attrs.deny_unknown_fields() {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2017-01-08 00:59:18 -08:00
|
|
|
Err(_serde::de::Error::unknown_field(value))
|
2016-02-23 19:51:53 -08:00
|
|
|
}
|
2017-01-08 00:59:18 -08:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Ok(__Field::__ignore)
|
|
|
|
}
|
|
|
|
};
|
2015-04-27 18:05:54 -04:00
|
|
|
|
2016-10-03 13:45:11 -07:00
|
|
|
quote! {
|
2016-10-03 13:37:46 -07:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum __Field {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#field_idents,)*
|
2016-10-03 13:37:46 -07:00
|
|
|
#ignore_variant
|
2016-02-07 22:03:01 -08:00
|
|
|
}
|
2015-04-27 18:05:54 -04:00
|
|
|
|
2017-01-08 00:47:37 -08:00
|
|
|
impl _serde::Deserialize for __Field {
|
2015-05-17 23:14:38 -07:00
|
|
|
#[inline]
|
2016-05-08 20:29:10 -07:00
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<__Field, __D::Error>
|
2017-01-08 00:47:37 -08:00
|
|
|
where __D: _serde::Deserializer,
|
2015-05-17 23:14:38 -07:00
|
|
|
{
|
2016-08-03 19:53:41 -07:00
|
|
|
struct __FieldVisitor;
|
2015-03-15 15:31:31 -07:00
|
|
|
|
2016-08-03 19:53:41 -07:00
|
|
|
impl _serde::de::Visitor for __FieldVisitor {
|
2015-05-17 23:14:38 -07:00
|
|
|
type Value = __Field;
|
2015-04-27 18:05:54 -04:00
|
|
|
|
2016-05-08 20:29:10 -07:00
|
|
|
fn visit_str<__E>(&mut self, value: &str) -> ::std::result::Result<__Field, __E>
|
2016-08-03 21:08:17 -07:00
|
|
|
where __E: _serde::de::Error
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
2017-01-08 00:59:18 -08:00
|
|
|
match value {
|
2017-01-09 00:37:37 -08:00
|
|
|
#(
|
|
|
|
#field_names => Ok(__Field::#field_idents),
|
|
|
|
)*
|
|
|
|
_ => #fallthrough_arm
|
2017-01-08 00:59:18 -08:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
2015-05-17 23:14:38 -07:00
|
|
|
|
2016-08-03 19:53:41 -07:00
|
|
|
deserializer.deserialize_struct_field(__FieldVisitor)
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
2015-05-17 23:14:38 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_struct_visitor(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
struct_path: Tokens,
|
|
|
|
impl_generics: &syn::Generics,
|
2016-06-21 19:12:08 -07:00
|
|
|
fields: &[Field],
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> (Tokens, Tokens, Tokens) {
|
2017-01-12 23:30:57 -08:00
|
|
|
let field_names_idents = fields.iter()
|
2017-01-12 23:17:45 -08:00
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
|
|
|
.map(|(i, field)| (field.attrs.name().deserialize_name(), field_i(i)))
|
2016-02-12 21:53:35 -08:00
|
|
|
.collect();
|
|
|
|
|
2015-03-15 15:31:31 -07:00
|
|
|
let field_visitor = deserialize_field_visitor(
|
2017-01-12 23:30:57 -08:00
|
|
|
field_names_idents,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2016-02-21 16:26:52 -08:00
|
|
|
false,
|
2015-03-15 15:31:31 -07:00
|
|
|
);
|
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
let visit_map = deserialize_map(
|
2016-05-07 12:33:59 -07:00
|
|
|
type_ident,
|
2015-03-15 15:31:31 -07:00
|
|
|
struct_path,
|
2016-05-07 12:33:59 -07:00
|
|
|
impl_generics,
|
2015-10-17 19:44:07 -07:00
|
|
|
fields,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2016-06-14 23:37:20 -07:00
|
|
|
);
|
2015-03-15 15:31:31 -07:00
|
|
|
|
2017-01-13 01:27:07 -08:00
|
|
|
let field_names = fields.iter().map(|field| field.attrs.name().deserialize_name());
|
2016-09-12 00:04:21 -07:00
|
|
|
let fields_stmt = quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
2015-07-03 19:39:27 -07:00
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
(field_visitor, fields_stmt, visit_map)
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
2015-03-15 18:38:52 -07:00
|
|
|
|
|
|
|
fn deserialize_map(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
struct_path: Tokens,
|
|
|
|
impl_generics: &syn::Generics,
|
2016-06-21 19:12:08 -07:00
|
|
|
fields: &[Field],
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs: &attr::Item,
|
2016-09-12 00:04:21 -07:00
|
|
|
) -> Tokens {
|
2015-03-15 18:38:52 -07:00
|
|
|
// Create the field names for the fields.
|
2017-01-12 21:52:56 -08:00
|
|
|
let fields_names: Vec<_> = fields.iter()
|
2016-03-06 23:27:12 -08:00
|
|
|
.enumerate()
|
2017-01-12 21:49:20 -08:00
|
|
|
.map(|(i, field)| (field, field_i(i)))
|
2017-01-12 21:52:56 -08:00
|
|
|
.collect();
|
2016-02-15 20:43:11 -08:00
|
|
|
|
2016-03-06 23:27:12 -08:00
|
|
|
// Declare each field that will be deserialized.
|
2017-01-12 21:52:56 -08:00
|
|
|
let let_values = fields_names.iter()
|
2016-06-14 23:37:20 -07:00
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2016-09-12 00:04:21 -07:00
|
|
|
.map(|&(field, ref name)| {
|
2016-05-07 12:33:59 -07:00
|
|
|
let field_ty = &field.ty;
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
let mut #name: Option<#field_ty> = None;
|
|
|
|
}
|
2017-01-12 21:52:56 -08:00
|
|
|
});
|
2015-03-15 18:38:52 -07:00
|
|
|
|
|
|
|
// Match arms to extract a value for a field.
|
2016-06-14 23:37:20 -07:00
|
|
|
let value_arms = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2016-09-27 00:46:03 -07:00
|
|
|
.map(|&(field, ref name)| {
|
2016-06-14 23:37:20 -07:00
|
|
|
let deser_name = field.attrs.name().deserialize_name();
|
2016-05-10 09:52:51 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-07 12:33:59 -07:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
try!(visitor.visit_value::<#field_ty>())
|
|
|
|
}
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
2016-09-27 00:46:03 -07:00
|
|
|
type_ident, impl_generics, field.ty, path);
|
2016-09-28 09:18:18 -07:00
|
|
|
quote!({
|
2016-09-12 00:04:21 -07:00
|
|
|
#wrapper
|
|
|
|
#wrapper_impl
|
|
|
|
try!(visitor.visit_value::<#wrapper_ty>()).value
|
2016-09-28 09:18:18 -07:00
|
|
|
})
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
__Field::#name => {
|
|
|
|
if #name.is_some() {
|
|
|
|
return Err(<__V::Error as _serde::de::Error>::duplicate_field(#deser_name));
|
2016-05-10 09:52:51 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
#name = Some(#visit);
|
2016-03-06 23:27:12 -08:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2017-01-12 21:52:56 -08:00
|
|
|
});
|
2016-03-06 23:27:12 -08:00
|
|
|
|
|
|
|
// Visit ignored values to consume them
|
2016-06-19 20:15:49 -07:00
|
|
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
2016-04-19 12:43:57 -05:00
|
|
|
None
|
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
Some(quote! {
|
2016-10-22 08:00:44 +02:00
|
|
|
_ => { let _ = try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>()); }
|
2016-09-12 00:04:21 -07:00
|
|
|
})
|
2016-03-06 23:27:12 -08:00
|
|
|
};
|
|
|
|
|
2017-01-12 23:17:45 -08:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
|
|
|
let match_keys = if item_attrs.deny_unknown_fields() && all_skipped {
|
|
|
|
quote! {
|
|
|
|
// FIXME: Once we drop support for Rust 1.15:
|
|
|
|
// let None::<__Field> = try!(visitor.visit_key());
|
|
|
|
try!(visitor.visit_key::<__Field>()).map(|impossible| match impossible {});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
|
|
|
match key {
|
|
|
|
#(#value_arms)*
|
|
|
|
#ignored_arm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
let extract_values = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2016-09-12 00:04:21 -07:00
|
|
|
.map(|&(field, ref name)| {
|
|
|
|
let missing_expr = expr_is_missing(&field.attrs);
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
let #name = match #name {
|
|
|
|
Some(#name) => #name,
|
|
|
|
None => #missing_expr
|
2015-03-15 18:38:52 -07:00
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2017-01-12 21:52:56 -08:00
|
|
|
});
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let result = fields_names.iter()
|
|
|
|
.map(|&(field, ref name)| {
|
|
|
|
let ident = field.ident.clone().expect("struct contains unnamed fields");
|
|
|
|
let value = if field.attrs.skip_deserializing() {
|
|
|
|
expr_is_missing(&field.attrs)
|
|
|
|
} else {
|
|
|
|
quote!(#name)
|
|
|
|
};
|
|
|
|
quote!(#ident: #value)
|
|
|
|
});
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#let_values)*
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2017-01-12 23:17:45 -08:00
|
|
|
#match_keys
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#extract_values)*
|
2016-07-05 01:42:38 -07:00
|
|
|
|
2016-10-03 21:09:52 -07:00
|
|
|
Ok(#struct_path { #(#result),* })
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 18:38:52 -07:00
|
|
|
}
|
2016-03-06 23:27:12 -08:00
|
|
|
|
2017-01-12 21:41:27 -08:00
|
|
|
fn field_i(i: usize) -> Ident {
|
|
|
|
Ident::new(format!("__field{}", i))
|
|
|
|
}
|
|
|
|
|
2016-05-07 12:33:59 -07:00
|
|
|
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
|
|
|
/// a trait to prevent it from accessing the internal `Deserialize` state.
|
|
|
|
fn wrap_deserialize_with(
|
2016-09-12 00:04:21 -07:00
|
|
|
type_ident: &syn::Ident,
|
|
|
|
impl_generics: &syn::Generics,
|
|
|
|
field_ty: &syn::Ty,
|
|
|
|
deserialize_with: &syn::Path,
|
|
|
|
) -> (Tokens, Tokens, syn::Path) {
|
2016-05-07 12:33:59 -07:00
|
|
|
// Quasi-quoting doesn't do a great job of expanding generics into paths,
|
|
|
|
// so manually build it.
|
2016-09-12 00:04:21 -07:00
|
|
|
let wrapper_ty = aster::path()
|
2016-05-07 12:33:59 -07:00
|
|
|
.segment("__SerdeDeserializeWithStruct")
|
|
|
|
.with_generics(impl_generics.clone())
|
|
|
|
.build()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let phantom_ty = aster::path()
|
2016-05-07 12:33:59 -07:00
|
|
|
.segment(type_ident)
|
2016-09-12 00:04:21 -07:00
|
|
|
.with_generics(aster::from_generics(impl_generics.clone())
|
2016-05-07 12:33:59 -07:00
|
|
|
.strip_ty_params()
|
|
|
|
.build())
|
|
|
|
.build()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
(
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
struct __SerdeDeserializeWithStruct #impl_generics #where_clause {
|
|
|
|
value: #field_ty,
|
|
|
|
phantom: ::std::marker::PhantomData<#phantom_ty>,
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
},
|
|
|
|
quote! {
|
2017-01-08 00:47:37 -08:00
|
|
|
impl #impl_generics _serde::Deserialize for #wrapper_ty #where_clause {
|
2016-05-08 20:29:10 -07:00
|
|
|
fn deserialize<__D>(__d: &mut __D) -> ::std::result::Result<Self, __D::Error>
|
2017-01-08 00:47:37 -08:00
|
|
|
where __D: _serde::Deserializer
|
2016-05-07 12:33:59 -07:00
|
|
|
{
|
2016-09-12 00:04:21 -07:00
|
|
|
let value = try!(#deserialize_with(__d));
|
2016-05-07 12:33:59 -07:00
|
|
|
Ok(__SerdeDeserializeWithStruct {
|
|
|
|
value: value,
|
|
|
|
phantom: ::std::marker::PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
},
|
2016-05-07 12:33:59 -07:00
|
|
|
wrapper_ty,
|
|
|
|
)
|
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
fn expr_is_missing(attrs: &attr::Field) -> Tokens {
|
2016-06-09 23:21:42 -07:00
|
|
|
match *attrs.default() {
|
|
|
|
attr::FieldDefault::Default => {
|
2016-09-12 00:04:21 -07:00
|
|
|
return quote!(::std::default::Default::default());
|
2016-06-09 23:21:42 -07:00
|
|
|
}
|
|
|
|
attr::FieldDefault::Path(ref path) => {
|
2016-09-12 00:04:21 -07:00
|
|
|
return quote!(#path());
|
2016-06-09 23:21:42 -07:00
|
|
|
}
|
|
|
|
attr::FieldDefault::None => { /* below */ }
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
2016-06-09 23:21:42 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let name = attrs.name().deserialize_name();
|
2016-05-07 19:57:44 -07:00
|
|
|
match attrs.deserialize_with() {
|
|
|
|
None => {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
try!(visitor.missing_field(#name))
|
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
|
|
|
Some(_) => {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
return Err(<__V::Error as _serde::de::Error>::missing_field(#name))
|
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 21:27:49 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
fn check_no_str(cx: &internals::Ctxt, item: &Item) {
|
|
|
|
let fail = || {
|
|
|
|
cx.error(
|
2016-06-11 21:27:49 -07:00
|
|
|
"Serde does not support deserializing fields of type &str; \
|
|
|
|
consider using String instead");
|
|
|
|
};
|
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
for field in item.body.all_fields() {
|
|
|
|
if field.attrs.skip_deserializing()
|
|
|
|
|| field.attrs.deserialize_with().is_some() { continue }
|
2016-06-11 21:27:49 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
if let syn::Ty::Rptr(_, ref inner) = *field.ty {
|
|
|
|
if let syn::Ty::Path(_, ref path) = inner.ty {
|
|
|
|
if path.segments.len() == 1 && path.segments[0].ident == "str" {
|
|
|
|
fail();
|
|
|
|
return;
|
2016-06-11 21:27:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|