2016-09-12 00:04:21 -07:00
|
|
|
use syn::{self, aster};
|
|
|
|
use quote::{self, 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
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let dummy_const = aster::id(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,
|
2016-09-12 00:04:21 -07:00
|
|
|
&aster::path().ids(&["_serde", "de", "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)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let ty_param_idents: Vec<_> = generics.ty_params.iter()
|
|
|
|
.map(|t| {
|
|
|
|
let ident = &t.ident;
|
|
|
|
quote!(#ident)
|
|
|
|
})
|
2015-05-01 12:53:59 -04:00
|
|
|
.collect();
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let ty_param_idents = if ty_param_idents.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
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]
|
2016-09-12 00:04:21 -07:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __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
|
|
|
{
|
|
|
|
try!(visitor.end());
|
|
|
|
self.visit_unit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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]
|
2016-09-12 00:04:21 -07:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __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 {
|
2016-07-05 00:52:19 -07:00
|
|
|
let mut index_in_seq = 0usize;
|
2016-03-06 23:27:12 -08:00
|
|
|
let let_values: Vec<_> = fields.iter()
|
|
|
|
.enumerate()
|
2016-06-14 23:37:20 -07:00
|
|
|
.map(|(i, field)| {
|
2016-09-12 00:04:21 -07:00
|
|
|
let name = aster::id(format!("__field{}", i));
|
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! {
|
|
|
|
let #name = #default;
|
|
|
|
}
|
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! {
|
|
|
|
let #name = match #visit {
|
2016-05-07 12:33:59 -07:00
|
|
|
Some(value) => { value },
|
2016-03-06 23:27:12 -08:00
|
|
|
None => {
|
2016-07-05 10:08:27 -07:00
|
|
|
try!(visitor.end());
|
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
|
|
|
}
|
2015-07-21 21:35:20 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
let result = if is_struct {
|
2016-09-12 00:04:21 -07:00
|
|
|
let args = fields.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, field)| {
|
|
|
|
let ident = field.ident.clone().expect("struct contains unnamed fields");
|
|
|
|
let value = aster::id(format!("__field{}", i));
|
|
|
|
quote!(#ident: #value)
|
|
|
|
});
|
|
|
|
quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
#type_path { #(#args),* }
|
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
|
|
|
let args = (0..fields.len()).map(|i| aster::id(format!("__field{}", i)));
|
|
|
|
quote! {
|
2016-10-03 21:09:52 -07:00
|
|
|
#type_path ( #(#args),* )
|
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)*
|
2015-07-21 21:35:20 -07:00
|
|
|
|
|
|
|
try!(visitor.end());
|
|
|
|
|
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
|
|
|
|
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]
|
2016-09-12 00:04:21 -07:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __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
|
|
|
|
2015-03-15 22:04:17 -07:00
|
|
|
let variant_visitor = deserialize_field_visitor(
|
2016-06-14 23:37:20 -07:00
|
|
|
variants.iter()
|
2016-12-15 22:23:52 +08:00
|
|
|
.filter(|variant| !variant.attrs.skip_deserializing())
|
2016-06-14 23:37:20 -07:00
|
|
|
.map(|variant| variant.attrs.name().deserialize_name())
|
|
|
|
.collect(),
|
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
|
|
|
);
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let variant_names = variants.iter().map(|variant| variant.ident.to_string());
|
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
|
|
|
|
2016-06-19 20:15:49 -07:00
|
|
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
2016-01-10 19:34:48 -08:00
|
|
|
None
|
2016-04-10 19:54:54 -07:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
Some(quote! {
|
|
|
|
__Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }
|
|
|
|
})
|
2016-01-10 19:34:48 -08:00
|
|
|
};
|
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
// Match arms to extract a variant from a string
|
2016-01-18 12:39:46 -08:00
|
|
|
let mut variant_arms = vec![];
|
2016-12-15 22:23:52 +08:00
|
|
|
for (i, variant) in variants.iter().filter(|variant| !variant.attrs.skip_deserializing()).enumerate() {
|
2016-09-12 00:04:21 -07:00
|
|
|
let variant_name = aster::id(format!("__field{}", i));
|
|
|
|
let variant_name = quote!(__Field::#variant_name);
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2016-08-03 21:08:17 -07:00
|
|
|
let block = deserialize_variant(
|
2016-01-18 12:39:46 -08:00
|
|
|
type_ident,
|
2015-05-01 12:53:59 -04:00
|
|
|
impl_generics,
|
2016-01-18 12:39:46 -08:00
|
|
|
ty.clone(),
|
|
|
|
variant,
|
2016-06-19 20:15:49 -07:00
|
|
|
item_attrs,
|
2016-06-14 23:37:20 -07:00
|
|
|
);
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let arm = quote! {
|
|
|
|
#variant_name => #block
|
|
|
|
};
|
2016-01-18 12:39:46 -08:00
|
|
|
variant_arms.push(arm);
|
|
|
|
}
|
2016-01-18 12:45:39 -08:00
|
|
|
variant_arms.extend(ignored_arm.into_iter());
|
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
|
|
|
{
|
2015-03-15 22:04:17 -07:00
|
|
|
match try!(visitor.visit_variant()) {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#variant_arms)*
|
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(
|
2016-09-12 00:04:21 -07:00
|
|
|
field_names: Vec<String>,
|
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 {
|
2015-03-15 18:37:26 -07:00
|
|
|
// Create the field names for the fields.
|
2016-02-23 19:51:53 -08:00
|
|
|
let field_idents: Vec<_> = (0 .. field_names.len())
|
2016-09-12 00:04:21 -07:00
|
|
|
.map(|i| aster::id(format!("__field{}", i)))
|
2015-03-15 18:37:26 -07:00
|
|
|
.collect();
|
|
|
|
|
2016-06-19 20:15:49 -07:00
|
|
|
let ignore_variant = if 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
|
|
|
};
|
|
|
|
|
2015-07-03 19:39:27 -07:00
|
|
|
let index_field_arms: Vec<_> = field_idents.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(field_index, field_ident)| {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
#field_index => { Ok(__Field::#field_ident) }
|
|
|
|
}
|
2015-07-03 19:39:27 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-02-21 16:26:52 -08:00
|
|
|
let (index_error_msg, unknown_ident) = if is_variant {
|
2016-09-12 00:04:21 -07:00
|
|
|
("expected a variant", aster::id("unknown_variant"))
|
2016-02-21 16:26:52 -08:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
("expected a field", aster::id("unknown_field"))
|
2016-02-21 16:26:52 -08:00
|
|
|
};
|
|
|
|
|
2016-06-19 20:15:49 -07:00
|
|
|
let fallthrough_index_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Ok(__Field::__ignore)
|
|
|
|
}
|
2016-02-23 19:51:53 -08:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Err(_serde::de::Error::invalid_value(#index_error_msg))
|
|
|
|
}
|
2016-02-23 19:51:53 -08:00
|
|
|
};
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let index_body = quote! {
|
2015-07-03 19:39:27 -07:00
|
|
|
match value {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#index_field_arms)*
|
2016-09-12 00:04:21 -07:00
|
|
|
_ => #fallthrough_index_arm_expr
|
2015-07-03 19:39:27 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
2016-02-23 19:51:53 -08:00
|
|
|
|
2015-03-15 15:31:31 -07:00
|
|
|
// Match arms to extract a field from a string
|
2016-09-12 00:04:21 -07:00
|
|
|
let str_field_arms: Vec<_> = field_idents.iter().zip(field_names.iter())
|
2016-02-08 08:03:46 -08:00
|
|
|
.map(|(field_ident, field_name)| {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
#field_name => { Ok(__Field::#field_ident) }
|
|
|
|
}
|
2015-03-15 15:31:31 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-06-19 20:15:49 -07:00
|
|
|
let fallthrough_str_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Ok(__Field::__ignore)
|
|
|
|
}
|
2016-01-10 19:34:48 -08:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Err(_serde::de::Error::#unknown_ident(value))
|
|
|
|
}
|
2016-01-10 19:34:48 -08:00
|
|
|
};
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let str_body = quote! {
|
2016-02-07 22:03:01 -08:00
|
|
|
match value {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#str_field_arms)*
|
2016-09-12 00:04:21 -07:00
|
|
|
_ => #fallthrough_str_arm_expr
|
2016-02-23 19:51:53 -08:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
2016-02-23 19:51:53 -08:00
|
|
|
|
|
|
|
// Match arms to extract a field from a string
|
2016-09-12 00:04:21 -07:00
|
|
|
let bytes_field_arms: Vec<_> = field_idents.iter().zip(field_names.iter())
|
2016-02-23 19:51:53 -08:00
|
|
|
.map(|(field_ident, field_name)| {
|
2016-09-27 00:46:03 -07:00
|
|
|
let bytes_field_name = quote::ByteStr(field_name);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
#bytes_field_name => { Ok(__Field::#field_ident) }
|
|
|
|
}
|
2016-02-23 19:51:53 -08:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-06-19 20:15:49 -07:00
|
|
|
let fallthrough_bytes_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
Ok(__Field::__ignore)
|
|
|
|
}
|
2016-02-23 19:51:53 -08:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
quote!({
|
2016-02-23 19:51:53 -08:00
|
|
|
let value = ::std::string::String::from_utf8_lossy(value);
|
2016-09-12 00:04:21 -07:00
|
|
|
Err(_serde::de::Error::#unknown_ident(&value))
|
2016-02-23 19:51:53 -08:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let bytes_body = quote! {
|
2016-02-23 19:51:53 -08:00
|
|
|
match value {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#bytes_field_arms)*
|
2016-09-12 00:04:21 -07:00
|
|
|
_ => #fallthrough_bytes_arm_expr
|
2016-02-07 22:03:01 -08:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
};
|
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_usize<__E>(&mut self, value: usize) -> ::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
|
|
|
{
|
|
|
|
#index_body
|
|
|
|
}
|
2015-07-03 19:39:27 -07: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
|
|
|
{
|
|
|
|
#str_body
|
|
|
|
}
|
2015-03-15 15:31:31 -07:00
|
|
|
|
2016-05-08 20:29:10 -07:00
|
|
|
fn visit_bytes<__E>(&mut self, value: &[u8]) -> ::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
|
|
|
{
|
|
|
|
#bytes_body
|
|
|
|
}
|
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) {
|
2016-02-12 21:53:35 -08:00
|
|
|
let field_exprs = fields.iter()
|
2016-06-14 23:37:20 -07:00
|
|
|
.map(|field| field.attrs.name().deserialize_name())
|
2016-02-12 21:53:35 -08:00
|
|
|
.collect();
|
|
|
|
|
2015-03-15 15:31:31 -07:00
|
|
|
let field_visitor = deserialize_field_visitor(
|
2016-03-06 23:27:12 -08:00
|
|
|
field_exprs,
|
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
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
let field_names = fields.iter().map(|field| {
|
|
|
|
field.ident.clone().expect("struct contains unnamed field").to_string()
|
|
|
|
});
|
2015-07-03 19:39:27 -07:00
|
|
|
|
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.
|
2016-06-14 23:37:20 -07:00
|
|
|
let fields_names = fields.iter()
|
2016-03-06 23:27:12 -08:00
|
|
|
.enumerate()
|
2016-06-14 23:37:20 -07:00
|
|
|
.map(|(i, field)|
|
2016-09-12 00:04:21 -07:00
|
|
|
(field, aster::id(format!("__field{}", i))))
|
2016-03-06 23:27:12 -08:00
|
|
|
.collect::<Vec<_>>();
|
2016-02-15 20:43:11 -08:00
|
|
|
|
2016-03-06 23:27:12 -08:00
|
|
|
// Declare each field that will be deserialized.
|
2016-09-12 00:04:21 -07:00
|
|
|
let let_values: Vec<_> = 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;
|
|
|
|
}
|
2016-05-07 12:33:59 -07:00
|
|
|
})
|
2015-03-15 18:38:52 -07:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2016-03-06 23:27:12 -08:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Match arms to ignore value for fields that have `skip_deserializing`.
|
|
|
|
// Ignored even if `deny_unknown_fields` is set.
|
2016-06-14 23:37:20 -07:00
|
|
|
let skipped_arms = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| field.attrs.skip_deserializing())
|
2016-09-12 00:04:21 -07:00
|
|
|
.map(|&(_, ref name)| {
|
|
|
|
quote! {
|
|
|
|
__Field::#name => {
|
2016-10-22 08:00:44 +02:00
|
|
|
let _ = try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>());
|
2015-03-15 18:38:52 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 18:38:52 -07:00
|
|
|
})
|
2016-02-15 20:43:11 -08:00
|
|
|
.collect::<Vec<_>>();
|
2015-03-15 18:38:52 -07: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
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
2015-03-15 18:38:52 -07:00
|
|
|
})
|
2016-06-14 23:37:20 -07:00
|
|
|
.collect::<Vec<_>>();
|
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
|
|
|
|
2016-05-07 12:33:59 -07:00
|
|
|
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
2015-03-15 18:38:52 -07:00
|
|
|
match key {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#value_arms)*
|
|
|
|
#(#skipped_arms)*
|
2016-09-12 00:04:21 -07:00
|
|
|
#ignored_arm
|
2015-03-15 18:38:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 13:20:34 +01:00
|
|
|
try!(visitor.end());
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|