2015-05-18 01:14:38 -05:00
|
|
|
use aster;
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
use syntax::ast::{self, Ident, MetaItem};
|
2015-03-15 16:31:05 -05:00
|
|
|
use syntax::codemap::Span;
|
2015-05-18 01:14:38 -05:00
|
|
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
2016-02-23 21:51:53 -06:00
|
|
|
use syntax::parse::token::InternedString;
|
2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ptr::P;
|
2016-07-02 20:12:26 -05:00
|
|
|
use syntax::tokenstream::TokenTree;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-04-13 02:34:29 -05:00
|
|
|
use bound;
|
2016-07-03 12:51:55 -05:00
|
|
|
use span;
|
2016-06-30 21:53:57 -05:00
|
|
|
use internals::ast::{Body, Field, Item, Style, Variant};
|
|
|
|
use internals::{attr, Error};
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
pub fn expand_derive_deserialize(
|
|
|
|
cx: &mut ExtCtxt,
|
2015-03-15 19:47:25 -05:00
|
|
|
span: Span,
|
2015-05-18 01:14:38 -05:00
|
|
|
meta_item: &MetaItem,
|
2015-06-08 08:55:36 -05:00
|
|
|
annotatable: &Annotatable,
|
2015-05-18 01:14:38 -05:00
|
|
|
push: &mut FnMut(Annotatable)
|
2015-03-14 15:09:37 -05:00
|
|
|
) {
|
2015-06-08 08:55:36 -05:00
|
|
|
let item = match *annotatable {
|
|
|
|
Annotatable::Item(ref item) => item,
|
2015-05-18 01:14:38 -05:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
2016-01-16 16:51:11 -06:00
|
|
|
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
2015-05-18 01:14:38 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-21 21:12:08 -05:00
|
|
|
let item = match Item::from_ast(cx, item) {
|
2016-05-21 00:03:20 -05:00
|
|
|
Ok(item) => item,
|
2016-06-21 21:12:08 -05:00
|
|
|
Err(Error::UnexpectedItemKind) => {
|
2016-06-19 22:15:49 -05:00
|
|
|
cx.span_err(item.span,
|
|
|
|
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
2016-05-21 00:03:20 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
if check_no_str(cx, &item).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = aster::AstBuilder::new().span(span);
|
|
|
|
|
|
|
|
let impl_item = deserialize_item(cx, &builder, &item);
|
2016-07-03 12:51:55 -05:00
|
|
|
push(span::record_expansion(cx, impl_item, "Deserialize"))
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_item(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-06-21 21:12:08 -05:00
|
|
|
item: &Item,
|
2016-06-19 22:15:49 -05:00
|
|
|
) -> P<ast::Item> {
|
2016-06-15 01:37:20 -05:00
|
|
|
let impl_generics = build_impl_generics(builder, &item);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let ty = builder.ty().path()
|
|
|
|
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
2015-03-14 15:09:37 -05:00
|
|
|
.build();
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let body = deserialize_body(cx,
|
|
|
|
builder,
|
|
|
|
&item,
|
|
|
|
&impl_generics,
|
|
|
|
ty.clone());
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
let dummy_const = builder.id(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
quote_item!(cx,
|
2016-04-24 12:59:57 -05:00
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const $dummy_const: () = {
|
|
|
|
extern crate serde as _serde;
|
|
|
|
#[automatically_derived]
|
|
|
|
impl $impl_generics _serde::de::Deserialize for $ty $where_clause {
|
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
|
|
|
where __D: _serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
$body
|
|
|
|
}
|
2015-03-15 19:47:25 -05:00
|
|
|
}
|
2016-04-24 12:59:57 -05:00
|
|
|
};
|
2016-06-19 22:15:49 -05:00
|
|
|
).unwrap()
|
2015-03-15 19:47:25 -05:00
|
|
|
}
|
|
|
|
|
2016-04-13 02:34:29 -05: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.
|
|
|
|
fn build_impl_generics(
|
|
|
|
builder: &aster::AstBuilder,
|
2016-06-21 21:12:08 -05:00
|
|
|
item: &Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> ast::Generics {
|
|
|
|
let generics = bound::without_defaults(item.generics);
|
2016-05-21 00:03:20 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let generics = bound::with_where_predicates_from_fields(
|
|
|
|
builder, item, &generics,
|
|
|
|
|attrs| attrs.de_bound());
|
2016-05-21 00:03:20 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
match item.attrs.de_bound() {
|
2016-05-21 00:03:20 -05:00
|
|
|
Some(predicates) => {
|
2016-06-15 01:37:20 -05:00
|
|
|
bound::with_where_predicates(builder, &generics, predicates)
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
None => {
|
2016-06-15 01:37:20 -05:00
|
|
|
let generics = bound::with_bound(builder, item, &generics,
|
2016-06-05 12:47:40 -05:00
|
|
|
needs_deserialize_bound,
|
2016-06-15 01:37:20 -05:00
|
|
|
&builder.path().ids(&["_serde", "de", "Deserialize"]).build());
|
|
|
|
let generics = bound::with_bound(builder, item, &generics,
|
2016-05-21 00:03:20 -05:00
|
|
|
requires_default,
|
2016-06-15 01:37:20 -05:00
|
|
|
&builder.path().global().ids(&["std", "default", "Default"]).build());
|
|
|
|
generics
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-13 02:34:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields with a `skip_deserializing` or `deserialize_with` attribute are not
|
2016-06-05 12:47:40 -05: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 22:15:49 -05:00
|
|
|
fn needs_deserialize_bound(attrs: &attr::Field) -> bool {
|
2016-06-14 13:22:49 -05:00
|
|
|
!attrs.skip_deserializing()
|
2016-05-21 00:03:20 -05:00
|
|
|
&& attrs.deserialize_with().is_none()
|
2016-06-05 12:47:40 -05:00
|
|
|
&& attrs.de_bound().is_none()
|
2016-04-13 02:34:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields with a `default` attribute (not `default=...`), and fields with a
|
|
|
|
// `skip_deserializing` attribute that do not also have `default=...`.
|
2016-06-19 22:15:49 -05:00
|
|
|
fn requires_default(attrs: &attr::Field) -> bool {
|
2016-06-10 01:21:42 -05:00
|
|
|
attrs.default() == &attr::FieldDefault::Default
|
2016-04-13 02:34:29 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_body(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-06-21 21:12:08 -05:00
|
|
|
item: &Item,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
|
|
|
match item.body {
|
2016-06-21 21:12:08 -05:00
|
|
|
Body::Enum(ref variants) => {
|
2015-03-15 19:47:25 -05:00
|
|
|
deserialize_item_enum(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
2016-06-15 01:37:20 -05:00
|
|
|
variants,
|
|
|
|
&item.attrs)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Body::Struct(Style::Struct, ref fields) => {
|
2016-06-15 01:37:20 -05:00
|
|
|
if fields.iter().any(|field| field.ident.is_none()) {
|
|
|
|
cx.span_bug(item.span, "struct has unnamed fields")
|
2015-10-17 21:44:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
deserialize_struct(
|
2015-03-15 19:47:25 -05:00
|
|
|
cx,
|
2016-06-15 01:37:20 -05:00
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
item.ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
None,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics,
|
|
|
|
ty,
|
2016-05-15 17:54:20 -05:00
|
|
|
fields,
|
2016-06-15 01:37:20 -05:00
|
|
|
&item.attrs)
|
2015-03-15 19:47:25 -05:00
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Body::Struct(Style::Tuple, ref fields) |
|
|
|
|
Body::Struct(Style::Newtype, ref fields) => {
|
2016-06-15 01:37:20 -05:00
|
|
|
if fields.iter().any(|field| field.ident.is_some()) {
|
|
|
|
cx.span_bug(item.span, "tuple struct has named fields")
|
2015-10-17 21:44:07 -05:00
|
|
|
}
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
deserialize_tuple(
|
2015-03-15 19:47:25 -05:00
|
|
|
cx,
|
2016-06-15 01:37:20 -05:00
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
item.ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
None,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics,
|
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2016-06-15 01:37:20 -05:00
|
|
|
&item.attrs)
|
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Body::Struct(Style::Unit, _) => {
|
2016-06-15 01:37:20 -05:00
|
|
|
deserialize_unit_struct(
|
|
|
|
cx,
|
2016-06-19 22:15:49 -05:00
|
|
|
builder,
|
2016-06-15 01:37:20 -05:00
|
|
|
item.ident,
|
|
|
|
&item.attrs)
|
2015-03-15 19:47:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
|
|
|
fn deserialize_visitor(
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
trait_generics: &ast::Generics,
|
2015-05-01 11:53:59 -05:00
|
|
|
forward_ty_params: Vec<ast::TyParam>,
|
|
|
|
forward_tys: Vec<P<ast::Ty>>
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics) {
|
2015-05-01 11:53:59 -05:00
|
|
|
if trait_generics.ty_params.is_empty() && forward_tys.is_empty() {
|
2016-06-15 01:37:20 -05:00
|
|
|
(
|
2015-05-07 16:47:46 -05:00
|
|
|
builder.item().tuple_struct("__Visitor").build(),
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.ty().id("__Visitor"),
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.expr().id("__Visitor"),
|
2015-05-01 11:53:59 -05:00
|
|
|
trait_generics.clone(),
|
2016-06-15 01:37:20 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
} else {
|
2015-05-01 11:53:59 -05:00
|
|
|
let placeholders : Vec<_> = trait_generics.ty_params.iter()
|
2015-05-14 16:29:14 -05:00
|
|
|
.map(|t| builder.ty().id(t.ident))
|
2015-05-01 11:53:59 -05:00
|
|
|
.collect();
|
|
|
|
let mut trait_generics = trait_generics.clone();
|
|
|
|
let mut ty_params = forward_ty_params.clone();
|
|
|
|
ty_params.extend(trait_generics.ty_params.into_vec());
|
2015-12-24 09:54:02 -06:00
|
|
|
trait_generics.ty_params = P::from_vec(ty_params);
|
2015-05-01 11:53:59 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
(
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.item().tuple_struct("__Visitor")
|
|
|
|
.generics().with(trait_generics.clone()).build()
|
2015-07-03 21:39:27 -05:00
|
|
|
.with_tys({
|
|
|
|
let lifetimes = trait_generics.lifetimes.iter()
|
|
|
|
.map(|lifetime_def| {
|
|
|
|
builder.ty()
|
|
|
|
.phantom_data()
|
|
|
|
.ref_().lifetime(lifetime_def.lifetime.name)
|
|
|
|
.ty()
|
|
|
|
.unit()
|
|
|
|
});
|
|
|
|
|
|
|
|
let ty_params = trait_generics.ty_params.iter()
|
|
|
|
.map(|ty_param| {
|
|
|
|
builder.ty()
|
|
|
|
.phantom_data()
|
|
|
|
.id(ty_param.ident)
|
|
|
|
});
|
|
|
|
|
|
|
|
lifetimes.chain(ty_params)
|
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
.build(),
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.ty().path()
|
|
|
|
.segment("__Visitor").with_generics(trait_generics.clone()).build()
|
|
|
|
.build(),
|
2015-05-01 11:53:59 -05:00
|
|
|
builder.expr().call()
|
|
|
|
.path().segment("__Visitor")
|
|
|
|
.with_tys(forward_tys)
|
|
|
|
.with_tys(placeholders)
|
|
|
|
.build().build()
|
2015-07-03 21:39:27 -05:00
|
|
|
.with_args({
|
|
|
|
let len = trait_generics.lifetimes.len() + trait_generics.ty_params.len();
|
|
|
|
|
|
|
|
(0 .. len).map(|_| builder.expr().phantom_data())
|
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
.build(),
|
2015-05-01 11:53:59 -05:00
|
|
|
trait_generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 11:53:59 -05:00
|
|
|
fn deserializer_ty_param(builder: &aster::AstBuilder) -> ast::TyParam {
|
|
|
|
builder.ty_param("__D")
|
|
|
|
.trait_bound(builder.path()
|
2016-04-24 12:59:57 -05:00
|
|
|
.segment("_serde").build()
|
2015-05-01 11:53:59 -05:00
|
|
|
.segment("de").build()
|
|
|
|
.id("Deserializer")
|
|
|
|
.build())
|
|
|
|
.build()
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserializer_ty_arg(builder: &aster::AstBuilder) -> P<ast::Ty>{
|
|
|
|
builder.ty().id("__D")
|
|
|
|
}
|
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
fn deserialize_unit_struct(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
2016-06-19 22:15:49 -05:00
|
|
|
builder: &aster::AstBuilder,
|
2015-03-14 15:09:37 -05:00
|
|
|
type_ident: Ident,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2016-06-19 22:15:49 -05:00
|
|
|
let type_name = name_expr(builder, item_attrs.name());
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
struct __Visitor;
|
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
impl _serde::de::Visitor for __Visitor {
|
2015-03-14 15:09:37 -05:00
|
|
|
type Value = $type_ident;
|
|
|
|
|
|
|
|
#[inline]
|
2016-05-08 22:29:10 -05:00
|
|
|
fn visit_unit<__E>(&mut self) -> ::std::result::Result<$type_ident, __E>
|
|
|
|
where __E: _serde::de::Error,
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
2015-03-15 19:47:25 -05:00
|
|
|
Ok($type_ident)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-05-08 22:29:10 -05:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$type_ident, __V::Error>
|
|
|
|
where __V: _serde::de::SeqVisitor,
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
|
|
|
try!(visitor.end());
|
|
|
|
self.visit_unit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 05:41:29 -05:00
|
|
|
deserializer.deserialize_unit_struct($type_name, __Visitor)
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
fn deserialize_tuple(
|
2015-07-30 11:38:09 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
variant_ident: Option<Ident>,
|
2015-07-30 11:38:09 -05:00
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-06-21 21:12:08 -05:00
|
|
|
fields: &[Field],
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-07-30 11:38:09 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
2016-01-18 14:39:46 -06:00
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-07-30 11:38:09 -05:00
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
let is_enum = variant_ident.is_some();
|
|
|
|
let type_path = match variant_ident {
|
|
|
|
Some(variant_ident) => builder.path().id(type_ident).id(variant_ident).build(),
|
|
|
|
None => builder.path().id(type_ident).build(),
|
|
|
|
};
|
2015-07-30 11:38:09 -05:00
|
|
|
|
2016-05-15 17:54:20 -05:00
|
|
|
let nfields = fields.len();
|
|
|
|
|
|
|
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
2016-06-15 01:37:20 -05:00
|
|
|
Some(deserialize_newtype_struct(
|
2016-05-15 17:54:20 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_ident,
|
|
|
|
&type_path,
|
|
|
|
impl_generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
&fields[0],
|
|
|
|
))
|
2016-05-15 15:32:06 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2015-03-15 19:47:25 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let visit_seq_expr = deserialize_seq(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2016-05-15 17:54:20 -05:00
|
|
|
type_ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
type_path,
|
2016-05-15 17:54:20 -05:00
|
|
|
impl_generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
fields,
|
2016-05-15 17:54:20 -05:00
|
|
|
false,
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
let dispatch = if is_enum {
|
|
|
|
quote_expr!(cx,
|
2016-05-15 17:54:20 -05:00
|
|
|
visitor.visit_tuple($nfields, $visitor_expr))
|
|
|
|
} else if nfields == 1 {
|
2016-06-19 22:15:49 -05:00
|
|
|
let type_name = name_expr(builder, item_attrs.name());
|
2016-05-15 15:32:06 -05:00
|
|
|
quote_expr!(cx,
|
|
|
|
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
|
|
|
|
} else {
|
2016-06-19 22:15:49 -05:00
|
|
|
let type_name = name_expr(builder, item_attrs.name());
|
2016-05-15 15:32:06 -05:00
|
|
|
quote_expr!(cx,
|
2016-05-15 17:54:20 -05:00
|
|
|
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
2016-05-15 15:32:06 -05:00
|
|
|
};
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
$visitor_item
|
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
2015-03-15 19:47:25 -05:00
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
$visit_newtype_struct
|
|
|
|
|
2015-07-30 11:31:16 -05:00
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
2016-04-24 12:59:57 -05:00
|
|
|
where __V: _serde::de::SeqVisitor,
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
$dispatch
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
fn deserialize_seq(
|
2015-07-21 23:35:20 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident: Ident,
|
2016-05-15 17:54:20 -05:00
|
|
|
type_path: ast::Path,
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2016-06-21 21:12:08 -05:00
|
|
|
fields: &[Field],
|
2016-05-15 17:54:20 -05:00
|
|
|
is_struct: bool,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2016-07-05 02:52:19 -05:00
|
|
|
let mut index_in_seq = 0usize;
|
2016-03-07 01:27:12 -06:00
|
|
|
let let_values: Vec<_> = fields.iter()
|
|
|
|
.enumerate()
|
2016-06-15 01:37:20 -05:00
|
|
|
.map(|(i, field)| {
|
2015-07-21 23:35:20 -05:00
|
|
|
let name = builder.id(format!("__field{}", i));
|
2016-06-15 01:37:20 -05:00
|
|
|
if field.attrs.skip_deserializing() {
|
2016-06-19 22:15:49 -05:00
|
|
|
let default = expr_is_missing(cx, builder, &field.attrs);
|
2016-03-07 01:27:12 -06:00
|
|
|
quote_stmt!(cx,
|
|
|
|
let $name = $default;
|
|
|
|
).unwrap()
|
|
|
|
} else {
|
2016-06-15 01:37:20 -05:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-07 14:33:59 -05:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
|
|
|
quote_expr!(cx, try!(visitor.visit::<$field_ty>()))
|
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
|
|
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$wrapper
|
|
|
|
$wrapper_impl
|
|
|
|
try!(visitor.visit::<$wrapper_ty>()).map(|wrap| wrap.value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-07-05 02:52:19 -05:00
|
|
|
let assign = quote_stmt!(cx,
|
2016-05-07 14:33:59 -05:00
|
|
|
let $name = match $visit {
|
|
|
|
Some(value) => { value },
|
2016-03-07 01:27:12 -06:00
|
|
|
None => {
|
2016-07-05 12:08:27 -05:00
|
|
|
try!(visitor.end());
|
2016-07-05 02:52:19 -05:00
|
|
|
return Err(_serde::de::Error::invalid_length($index_in_seq));
|
2016-03-07 01:27:12 -06:00
|
|
|
}
|
|
|
|
};
|
2016-07-05 02:52:19 -05:00
|
|
|
).unwrap();
|
|
|
|
index_in_seq += 1;
|
|
|
|
assign
|
2016-03-07 01:27:12 -06:00
|
|
|
}
|
2015-07-21 23:35:20 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-05-15 17:54:20 -05:00
|
|
|
let result = if is_struct {
|
|
|
|
builder.expr().struct_path(type_path)
|
|
|
|
.with_id_exprs(
|
|
|
|
fields.iter()
|
|
|
|
.enumerate()
|
2016-06-15 01:37:20 -05:00
|
|
|
.map(|(i, field)| {
|
2016-05-15 17:54:20 -05:00
|
|
|
(
|
|
|
|
match field.ident {
|
|
|
|
Some(name) => name.clone(),
|
|
|
|
None => {
|
|
|
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builder.expr().id(format!("__field{}", i)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
} else {
|
|
|
|
builder.expr().call()
|
|
|
|
.build_path(type_path)
|
|
|
|
.with_args((0..fields.len()).map(|i| builder.expr().id(format!("__field{}", i))))
|
|
|
|
.build()
|
|
|
|
};
|
2015-07-21 23:35:20 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-07-21 23:35:20 -05:00
|
|
|
$let_values
|
|
|
|
|
|
|
|
try!(visitor.end());
|
|
|
|
|
|
|
|
Ok($result)
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-07-21 23:35:20 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 17:54:20 -05:00
|
|
|
fn deserialize_newtype_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
type_path: &ast::Path,
|
|
|
|
impl_generics: &ast::Generics,
|
2016-06-21 21:12:08 -05:00
|
|
|
field: &Field,
|
2016-07-02 20:12:26 -05:00
|
|
|
) -> Vec<TokenTree> {
|
2016-06-15 01:37:20 -05:00
|
|
|
let value = match field.attrs.deserialize_with() {
|
2016-05-15 17:54:20 -05:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
|
|
|
quote_expr!(cx,
|
|
|
|
try!(<$field_ty as _serde::Deserialize>::deserialize(__e)))
|
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
|
|
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$wrapper
|
|
|
|
$wrapper_impl
|
|
|
|
try!(<$wrapper_ty as _serde::Deserialize>::deserialize(__e)).value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_tokens!(cx,
|
2016-05-15 17:54:20 -05:00
|
|
|
#[inline]
|
|
|
|
fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
|
|
|
where __E: _serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
Ok($type_path($value))
|
|
|
|
}
|
2016-06-15 01:37:20 -05:00
|
|
|
)
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_struct(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident: Ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
variant_ident: Option<Ident>,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-06-21 21:12:08 -05:00
|
|
|
fields: &[Field],
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
2015-07-03 21:39:27 -05:00
|
|
|
builder,
|
|
|
|
&impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
let type_path = match variant_ident {
|
|
|
|
Some(variant_ident) => builder.path().id(type_ident).id(variant_ident).build(),
|
|
|
|
None => builder.path().id(type_ident).build(),
|
|
|
|
};
|
2015-07-21 23:35:20 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let visit_seq_expr = deserialize_seq(
|
2015-07-21 23:35:20 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident,
|
2015-07-21 23:35:20 -05:00
|
|
|
type_path.clone(),
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
fields,
|
2016-05-15 17:54:20 -05:00
|
|
|
true,
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-07-21 23:35:20 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident,
|
2015-10-17 21:44:07 -05:00
|
|
|
type_path.clone(),
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
fields,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-05-15 17:54:20 -05:00
|
|
|
let is_enum = variant_ident.is_some();
|
2016-05-15 15:32:06 -05:00
|
|
|
let dispatch = if is_enum {
|
|
|
|
quote_expr!(cx,
|
|
|
|
visitor.visit_struct(FIELDS, $visitor_expr))
|
|
|
|
} else {
|
2016-06-19 22:15:49 -05:00
|
|
|
let type_name = name_expr(builder, item_attrs.name());
|
2016-05-15 15:32:06 -05:00
|
|
|
quote_expr!(cx,
|
|
|
|
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
|
|
|
};
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-03-14 23:47:23 -05:00
|
|
|
$field_visitor
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
$visitor_item
|
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
2015-03-15 19:47:25 -05:00
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-07-21 23:35:20 -05:00
|
|
|
#[inline]
|
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
2016-04-24 12:59:57 -05:00
|
|
|
where __V: _serde::de::SeqVisitor,
|
2015-07-21 23:35:20 -05:00
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
2016-04-24 12:59:57 -05:00
|
|
|
where __V: _serde::de::MapVisitor,
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
|
|
|
$visit_map_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
$fields_stmt
|
|
|
|
|
2016-05-15 15:32:06 -05:00
|
|
|
$dispatch
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_item_enum(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-06-21 21:12:08 -05:00
|
|
|
variants: &[Variant],
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let type_name = name_expr(builder, item_attrs.name());
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-16 00:04:17 -05:00
|
|
|
let variant_visitor = deserialize_field_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2016-06-15 01:37:20 -05:00
|
|
|
variants.iter()
|
|
|
|
.map(|variant| variant.attrs.name().deserialize_name())
|
|
|
|
.collect(),
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2016-02-21 18:26:52 -06:00
|
|
|
true,
|
2015-03-16 00:04:17 -05:00
|
|
|
);
|
|
|
|
|
2016-02-12 23:06:47 -06:00
|
|
|
let variants_expr = builder.expr().ref_().slice()
|
2015-07-29 14:04:31 -05:00
|
|
|
.with_exprs(
|
2016-06-15 01:37:20 -05:00
|
|
|
variants.iter().map(|variant| builder.expr().str(variant.ident))
|
2015-07-29 14:04:31 -05:00
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let variants_stmt = quote_stmt!(cx,
|
|
|
|
const VARIANTS: &'static [&'static str] = $variants_expr;
|
|
|
|
).unwrap();
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
2016-01-10 21:34:48 -06:00
|
|
|
None
|
2016-04-10 21:54:54 -05:00
|
|
|
} else {
|
2016-04-24 12:59:57 -05:00
|
|
|
Some(quote_arm!(cx, __Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }))
|
2016-01-10 21:34:48 -06:00
|
|
|
};
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
// Match arms to extract a variant from a string
|
2016-01-18 14:39:46 -06:00
|
|
|
let mut variant_arms = vec![];
|
2016-06-15 01:37:20 -05:00
|
|
|
for (i, variant) in variants.iter().enumerate() {
|
2016-02-18 11:27:41 -06:00
|
|
|
let variant_name = builder.pat().path()
|
|
|
|
.id("__Field").id(format!("__field{}", i))
|
2016-01-18 14:39:46 -06:00
|
|
|
.build();
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let expr = deserialize_variant(
|
2016-01-18 14:39:46 -06:00
|
|
|
cx,
|
2015-05-01 11:53:59 -05:00
|
|
|
builder,
|
2016-01-18 14:39:46 -06:00
|
|
|
type_ident,
|
2015-05-01 11:53:59 -05:00
|
|
|
impl_generics,
|
2016-01-18 14:39:46 -06:00
|
|
|
ty.clone(),
|
|
|
|
variant,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-03-15 19:47:25 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let arm = quote_arm!(cx, $variant_name => { $expr });
|
|
|
|
variant_arms.push(arm);
|
|
|
|
}
|
2016-01-18 14:45:39 -06:00
|
|
|
variant_arms.extend(ignored_arm.into_iter());
|
2016-01-18 14:39:46 -06:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
2016-01-18 14:39:46 -06:00
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2016-01-18 14:39:46 -06:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-03-16 00:04:17 -05:00
|
|
|
$variant_visitor
|
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
$visitor_item
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
impl $visitor_generics _serde::de::EnumVisitor for $visitor_ty $where_clause {
|
2015-03-15 19:47:25 -05:00
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
2016-04-24 12:59:57 -05:00
|
|
|
where __V: _serde::de::VariantVisitor,
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
2015-03-16 00:04:17 -05:00
|
|
|
match try!(visitor.visit_variant()) {
|
2015-03-14 15:09:37 -05:00
|
|
|
$variant_arms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:04:31 -05:00
|
|
|
$variants_stmt
|
|
|
|
|
2015-10-08 05:41:29 -05:00
|
|
|
deserializer.deserialize_enum($type_name, VARIANTS, $visitor_expr)
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2015-03-15 19:47:25 -05:00
|
|
|
generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-06-21 21:12:08 -05:00
|
|
|
variant: &Variant,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
|
|
|
let variant_ident = variant.ident;
|
2015-03-15 19:47:25 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
match variant.style {
|
2016-06-21 21:12:08 -05:00
|
|
|
Style::Unit => {
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-04-06 22:04:01 -05:00
|
|
|
try!(visitor.visit_unit());
|
2015-03-16 00:04:17 -05:00
|
|
|
Ok($type_ident::$variant_ident)
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Style::Newtype => {
|
2016-05-15 17:54:20 -05:00
|
|
|
deserialize_newtype_variant(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_ident,
|
|
|
|
variant_ident,
|
|
|
|
generics,
|
2016-06-15 01:37:20 -05:00
|
|
|
&variant.fields[0],
|
2016-05-15 17:54:20 -05:00
|
|
|
)
|
2015-07-14 04:12:01 -05:00
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Style::Tuple => {
|
2016-05-15 15:32:06 -05:00
|
|
|
deserialize_tuple(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
Some(variant_ident),
|
2015-03-15 19:47:25 -05:00
|
|
|
generics,
|
|
|
|
ty,
|
2016-06-15 01:37:20 -05:00
|
|
|
&variant.fields,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2015-03-16 00:04:17 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2016-06-21 21:12:08 -05:00
|
|
|
Style::Struct => {
|
2016-05-15 15:32:06 -05:00
|
|
|
deserialize_struct(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident,
|
2016-05-15 15:32:06 -05:00
|
|
|
Some(variant_ident),
|
2015-03-15 19:47:25 -05:00
|
|
|
generics,
|
|
|
|
ty,
|
2016-06-15 01:37:20 -05:00
|
|
|
&variant.fields,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2015-03-16 00:04:17 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 17:54:20 -05:00
|
|
|
fn deserialize_newtype_variant(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
variant_ident: Ident,
|
|
|
|
impl_generics: &ast::Generics,
|
2016-06-21 21:12:08 -05:00
|
|
|
field: &Field,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-15 17:54:20 -05:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
|
|
|
quote_expr!(cx, try!(visitor.visit_newtype::<$field_ty>()))
|
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
|
|
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$wrapper
|
|
|
|
$wrapper_impl
|
|
|
|
try!(visitor.visit_newtype::<$wrapper_ty>()).value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, Ok($type_ident::$variant_ident($visit)))
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
fn deserialize_field_visitor(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-02-23 21:51:53 -06:00
|
|
|
field_names: Vec<InternedString>,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-02-21 18:26:52 -06:00
|
|
|
is_variant: bool,
|
2015-03-15 17:31:31 -05:00
|
|
|
) -> Vec<P<ast::Item>> {
|
2015-03-15 20:37:26 -05:00
|
|
|
// Create the field names for the fields.
|
2016-02-23 21:51:53 -06:00
|
|
|
let field_idents: Vec<_> = (0 .. field_names.len())
|
2015-03-15 20:37:26 -05:00
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let ignore_variant = if item_attrs.deny_unknown_fields() {
|
2016-04-10 21:54:54 -05:00
|
|
|
None
|
|
|
|
} else {
|
2016-01-10 21:34:48 -06:00
|
|
|
let skip_ident = builder.id("__ignore");
|
|
|
|
Some(builder.variant(skip_ident).unit())
|
|
|
|
};
|
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
let field_enum = builder.item()
|
|
|
|
.attr().allow(&["non_camel_case_types"])
|
|
|
|
.enum_("__Field")
|
|
|
|
.with_variants(
|
2015-03-16 00:04:17 -05:00
|
|
|
field_idents.iter().map(|field_ident| {
|
2015-10-17 21:44:07 -05:00
|
|
|
builder.variant(field_ident).unit()
|
2015-03-15 17:31:31 -05:00
|
|
|
})
|
|
|
|
)
|
2016-01-10 21:34:48 -06:00
|
|
|
.with_variants(ignore_variant.into_iter())
|
2015-03-15 17:31:31 -05:00
|
|
|
.build();
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
let index_field_arms: Vec<_> = field_idents.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(field_index, field_ident)| {
|
|
|
|
quote_arm!(cx, $field_index => { Ok(__Field::$field_ident) })
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-02-21 18:26:52 -06:00
|
|
|
let (index_error_msg, unknown_ident) = if is_variant {
|
|
|
|
(builder.expr().str("expected a variant"), builder.id("unknown_variant"))
|
|
|
|
} else {
|
|
|
|
(builder.expr().str("expected a field"), builder.id("unknown_field"))
|
|
|
|
};
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let fallthrough_index_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-02-23 21:51:53 -06:00
|
|
|
quote_expr!(cx, Ok(__Field::__ignore))
|
|
|
|
} else {
|
|
|
|
quote_expr!(cx, {
|
2016-04-24 12:59:57 -05:00
|
|
|
Err(_serde::de::Error::invalid_value($index_error_msg))
|
2016-02-23 21:51:53 -06:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
let index_body = quote_expr!(cx,
|
|
|
|
match value {
|
|
|
|
$index_field_arms
|
2016-02-23 21:51:53 -06:00
|
|
|
_ => $fallthrough_index_arm_expr
|
2015-07-03 21:39:27 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-02-23 21:51:53 -06:00
|
|
|
// Convert the field names into byte strings.
|
|
|
|
let str_field_names: Vec<_> = field_names.iter()
|
|
|
|
.map(|name| builder.expr().lit().str(&name))
|
|
|
|
.collect();
|
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
// Match arms to extract a field from a string
|
2016-02-23 21:51:53 -06:00
|
|
|
let str_field_arms: Vec<_> = field_idents.iter().zip(str_field_names.iter())
|
2016-02-08 10:03:46 -06:00
|
|
|
.map(|(field_ident, field_name)| {
|
|
|
|
quote_arm!(cx, $field_name => { Ok(__Field::$field_ident) })
|
2015-03-15 17:31:31 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let fallthrough_str_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-01-10 21:34:48 -06:00
|
|
|
quote_expr!(cx, Ok(__Field::__ignore))
|
|
|
|
} else {
|
2016-04-24 12:59:57 -05:00
|
|
|
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
|
2016-01-10 21:34:48 -06:00
|
|
|
};
|
|
|
|
|
2016-02-08 00:03:01 -06:00
|
|
|
let str_body = quote_expr!(cx,
|
|
|
|
match value {
|
2016-02-23 21:51:53 -06:00
|
|
|
$str_field_arms
|
|
|
|
_ => $fallthrough_str_arm_expr
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Convert the field names into byte strings.
|
|
|
|
let bytes_field_names: Vec<_> = field_names.iter()
|
|
|
|
.map(|name| {
|
|
|
|
let name: &str = name;
|
|
|
|
builder.expr().lit().byte_str(name)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Match arms to extract a field from a string
|
|
|
|
let bytes_field_arms: Vec<_> = field_idents.iter().zip(bytes_field_names.iter())
|
|
|
|
.map(|(field_ident, field_name)| {
|
|
|
|
quote_arm!(cx, $field_name => { Ok(__Field::$field_ident) })
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let fallthrough_bytes_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
2016-02-23 21:51:53 -06:00
|
|
|
quote_expr!(cx, Ok(__Field::__ignore))
|
|
|
|
} else {
|
|
|
|
quote_expr!(cx, {
|
|
|
|
let value = ::std::string::String::from_utf8_lossy(value);
|
2016-04-24 12:59:57 -05:00
|
|
|
Err(_serde::de::Error::$unknown_ident(&value))
|
2016-02-23 21:51:53 -06:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let bytes_body = quote_expr!(cx,
|
|
|
|
match value {
|
|
|
|
$bytes_field_arms
|
|
|
|
_ => $fallthrough_bytes_arm_expr
|
2016-02-08 00:03:01 -06:00
|
|
|
}
|
|
|
|
);
|
2015-04-27 17:05:54 -05:00
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
let impl_item = quote_item!(cx,
|
2016-04-24 12:59:57 -05:00
|
|
|
impl _serde::de::Deserialize for __Field {
|
2015-05-18 01:14:38 -05:00
|
|
|
#[inline]
|
2016-05-08 22:29:10 -05:00
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<__Field, __D::Error>
|
|
|
|
where __D: _serde::de::Deserializer,
|
2015-05-18 01:14:38 -05:00
|
|
|
{
|
2016-05-08 22:29:10 -05:00
|
|
|
struct __FieldVisitor<__D> {
|
|
|
|
phantom: ::std::marker::PhantomData<__D>
|
2015-05-18 01:14:38 -05:00
|
|
|
}
|
2015-03-15 17:31:31 -05:00
|
|
|
|
2016-04-24 12:59:57 -05:00
|
|
|
impl<__D> _serde::de::Visitor for __FieldVisitor<__D>
|
|
|
|
where __D: _serde::de::Deserializer
|
2015-03-15 17:31:31 -05:00
|
|
|
{
|
2015-05-18 01:14:38 -05:00
|
|
|
type Value = __Field;
|
2015-04-27 17:05:54 -05:00
|
|
|
|
2016-05-08 22:29:10 -05:00
|
|
|
fn visit_usize<__E>(&mut self, value: usize) -> ::std::result::Result<__Field, __E>
|
|
|
|
where __E: _serde::de::Error,
|
2015-07-03 21:39:27 -05:00
|
|
|
{
|
|
|
|
$index_body
|
|
|
|
}
|
|
|
|
|
2016-05-08 22:29:10 -05:00
|
|
|
fn visit_str<__E>(&mut self, value: &str) -> ::std::result::Result<__Field, __E>
|
|
|
|
where __E: _serde::de::Error,
|
2015-05-18 01:14:38 -05:00
|
|
|
{
|
2015-07-03 21:39:27 -05:00
|
|
|
$str_body
|
2015-04-27 17:05:54 -05:00
|
|
|
}
|
2015-03-15 17:31:31 -05:00
|
|
|
|
2016-05-08 22:29:10 -05:00
|
|
|
fn visit_bytes<__E>(&mut self, value: &[u8]) -> ::std::result::Result<__Field, __E>
|
|
|
|
where __E: _serde::de::Error,
|
2015-04-27 17:05:54 -05:00
|
|
|
{
|
2016-02-23 21:51:53 -06:00
|
|
|
$bytes_body
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 01:14:38 -05:00
|
|
|
|
2016-05-07 14:33:59 -05:00
|
|
|
deserializer.deserialize_struct_field(
|
2016-05-08 22:29:10 -05:00
|
|
|
__FieldVisitor::<__D>{
|
2016-05-07 14:33:59 -05:00
|
|
|
phantom: ::std::marker::PhantomData
|
|
|
|
}
|
|
|
|
)
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
2015-05-18 01:14:38 -05:00
|
|
|
}
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
vec![field_enum, impl_item]
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_struct_visitor(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident: Ident,
|
2015-03-15 17:31:31 -05:00
|
|
|
struct_path: ast::Path,
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2016-06-21 21:12:08 -05:00
|
|
|
fields: &[Field],
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
|
2016-02-12 23:53:35 -06:00
|
|
|
let field_exprs = fields.iter()
|
2016-06-15 01:37:20 -05:00
|
|
|
.map(|field| field.attrs.name().deserialize_name())
|
2016-02-12 23:53:35 -06:00
|
|
|
.collect();
|
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
let field_visitor = deserialize_field_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2016-03-07 01:27:12 -06:00
|
|
|
field_exprs,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2016-02-21 18:26:52 -06:00
|
|
|
false,
|
2015-03-15 17:31:31 -05:00
|
|
|
);
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let visit_map_expr = deserialize_map(
|
2015-03-15 17:31:31 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident,
|
2015-03-15 17:31:31 -05:00
|
|
|
struct_path,
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs,
|
2016-06-15 01:37:20 -05:00
|
|
|
);
|
2015-03-15 17:31:31 -05:00
|
|
|
|
2016-02-12 23:06:47 -06:00
|
|
|
let fields_expr = builder.expr().ref_().slice()
|
2015-07-03 21:39:27 -05:00
|
|
|
.with_exprs(
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.iter()
|
2016-06-15 01:37:20 -05:00
|
|
|
.map(|field| {
|
2016-04-10 21:54:54 -05:00
|
|
|
match field.ident {
|
|
|
|
Some(name) => builder.expr().str(name),
|
|
|
|
None => {
|
2016-01-16 16:51:11 -06:00
|
|
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
|
|
|
}
|
2015-07-03 21:39:27 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let fields_stmt = quote_stmt!(cx,
|
|
|
|
const FIELDS: &'static [&'static str] = $fields_expr;
|
|
|
|
).unwrap();
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
(field_visitor, fields_stmt, visit_map_expr)
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
|
|
|
|
fn deserialize_map(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2016-05-07 14:33:59 -05:00
|
|
|
type_ident: Ident,
|
2015-03-15 20:38:52 -05:00
|
|
|
struct_path: ast::Path,
|
2016-05-07 14:33:59 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2016-06-21 21:12:08 -05:00
|
|
|
fields: &[Field],
|
2016-06-19 22:15:49 -05:00
|
|
|
item_attrs: &attr::Item,
|
2016-06-15 01:37:20 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-03-15 20:38:52 -05:00
|
|
|
// Create the field names for the fields.
|
2016-06-15 01:37:20 -05:00
|
|
|
let fields_names = fields.iter()
|
2016-03-07 01:27:12 -06:00
|
|
|
.enumerate()
|
2016-06-15 01:37:20 -05:00
|
|
|
.map(|(i, field)|
|
|
|
|
(field, builder.id(format!("__field{}", i))))
|
2016-03-07 01:27:12 -06:00
|
|
|
.collect::<Vec<_>>();
|
2016-02-15 22:43:11 -06:00
|
|
|
|
2016-03-07 01:27:12 -06:00
|
|
|
// Declare each field that will be deserialized.
|
2016-06-15 01:37:20 -05:00
|
|
|
let let_values: Vec<ast::Stmt> = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
|
|
|
.map(|&(field, name)| {
|
2016-05-07 14:33:59 -05:00
|
|
|
let field_ty = &field.ty;
|
|
|
|
quote_stmt!(cx, let mut $name: Option<$field_ty> = None;).unwrap()
|
|
|
|
})
|
2015-03-15 20:38:52 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Match arms to extract a value for a field.
|
2016-06-15 01:37:20 -05:00
|
|
|
let value_arms = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
|
|
|
.map(|&(ref field, name)| {
|
|
|
|
let deser_name = field.attrs.name().deserialize_name();
|
2016-05-10 11:52:51 -05:00
|
|
|
let name_str = builder.expr().lit().str(deser_name);
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-07 14:33:59 -05:00
|
|
|
None => {
|
|
|
|
let field_ty = &field.ty;
|
|
|
|
quote_expr!(cx, try!(visitor.visit_value::<$field_ty>()))
|
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
|
|
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
|
|
|
quote_expr!(cx, ({
|
|
|
|
$wrapper
|
|
|
|
$wrapper_impl
|
|
|
|
try!(visitor.visit_value::<$wrapper_ty>()).value
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
2015-03-15 20:38:52 -05:00
|
|
|
quote_arm!(cx,
|
2016-03-07 01:27:12 -06:00
|
|
|
__Field::$name => {
|
2016-05-10 11:52:51 -05:00
|
|
|
if $name.is_some() {
|
|
|
|
return Err(<__V::Error as _serde::de::Error>::duplicate_field($name_str));
|
|
|
|
}
|
2016-05-07 14:33:59 -05:00
|
|
|
$name = Some($visit);
|
2016-03-07 01:27:12 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Match arms to ignore value for fields that have `skip_deserializing`.
|
|
|
|
// Ignored even if `deny_unknown_fields` is set.
|
2016-06-15 01:37:20 -05:00
|
|
|
let skipped_arms = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| field.attrs.skip_deserializing())
|
|
|
|
.map(|&(_, name)| {
|
2016-03-07 01:27:12 -06:00
|
|
|
quote_arm!(cx,
|
|
|
|
__Field::$name => {
|
2016-04-24 12:59:57 -05:00
|
|
|
try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>());
|
2015-03-15 20:38:52 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2016-02-15 22:43:11 -06:00
|
|
|
.collect::<Vec<_>>();
|
2015-03-15 20:38:52 -05:00
|
|
|
|
2016-03-07 01:27:12 -06:00
|
|
|
// Visit ignored values to consume them
|
2016-06-19 22:15:49 -05:00
|
|
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
2016-04-19 12:43:57 -05:00
|
|
|
None
|
|
|
|
} else {
|
2016-03-07 01:27:12 -06:00
|
|
|
Some(quote_arm!(cx,
|
2016-04-24 12:59:57 -05:00
|
|
|
_ => { try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>()); }
|
2016-03-07 01:27:12 -06:00
|
|
|
))
|
|
|
|
};
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
let extract_values = fields_names.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
|
|
|
.map(|&(field, name)| {
|
2016-06-19 22:15:49 -05:00
|
|
|
let missing_expr = expr_is_missing(cx, builder, &field.attrs);
|
2015-03-15 20:38:52 -05:00
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_stmt!(cx,
|
2016-03-07 01:27:12 -06:00
|
|
|
let $name = match $name {
|
|
|
|
Some($name) => $name,
|
2015-05-14 16:28:44 -05:00
|
|
|
None => $missing_expr
|
2015-03-15 20:38:52 -05:00
|
|
|
};
|
2016-06-15 01:37:20 -05:00
|
|
|
).unwrap()
|
2015-03-15 20:38:52 -05:00
|
|
|
})
|
2016-06-15 01:37:20 -05:00
|
|
|
.collect::<Vec<_>>();
|
2015-03-15 20:38:52 -05:00
|
|
|
|
|
|
|
let result = builder.expr().struct_path(struct_path)
|
|
|
|
.with_id_exprs(
|
2016-06-15 01:37:20 -05:00
|
|
|
fields_names.iter()
|
|
|
|
.map(|&(field, name)| {
|
2015-03-15 20:38:52 -05:00
|
|
|
(
|
2016-04-10 21:54:54 -05:00
|
|
|
match field.ident {
|
|
|
|
Some(name) => name.clone(),
|
|
|
|
None => {
|
2016-01-16 16:51:11 -06:00
|
|
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
},
|
2016-06-15 01:37:20 -05:00
|
|
|
if field.attrs.skip_deserializing() {
|
2016-06-19 22:15:49 -05:00
|
|
|
expr_is_missing(cx, builder, &field.attrs)
|
2016-03-07 01:27:12 -06:00
|
|
|
} else {
|
|
|
|
builder.expr().id(name)
|
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
quote_expr!(cx, {
|
2015-03-15 20:38:52 -05:00
|
|
|
$let_values
|
|
|
|
|
2016-05-07 14:33:59 -05:00
|
|
|
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
2015-03-15 20:38:52 -05:00
|
|
|
match key {
|
|
|
|
$value_arms
|
2016-03-07 01:27:12 -06:00
|
|
|
$skipped_arms
|
|
|
|
$ignored_arm
|
2015-03-15 20:38:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:20:34 -05:00
|
|
|
try!(visitor.end());
|
|
|
|
|
2016-07-05 03:42:38 -05:00
|
|
|
$extract_values
|
|
|
|
|
2015-03-15 20:38:52 -05:00
|
|
|
Ok($result)
|
2016-06-15 01:37:20 -05:00
|
|
|
})
|
2015-03-15 20:38:52 -05:00
|
|
|
}
|
2016-03-07 01:27:12 -06:00
|
|
|
|
2016-05-07 14:33:59 -05: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(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
field_ty: &P<ast::Ty>,
|
|
|
|
deserialize_with: &ast::Path,
|
|
|
|
) -> (ast::Stmt, ast::Stmt, ast::Path) {
|
|
|
|
// Quasi-quoting doesn't do a great job of expanding generics into paths,
|
|
|
|
// so manually build it.
|
|
|
|
let wrapper_ty = builder.path()
|
|
|
|
.segment("__SerdeDeserializeWithStruct")
|
|
|
|
.with_generics(impl_generics.clone())
|
|
|
|
.build()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
|
|
|
let phantom_ty = builder.path()
|
|
|
|
.segment(type_ident)
|
|
|
|
.with_generics(builder.from_generics(impl_generics.clone())
|
|
|
|
.strip_ty_params()
|
|
|
|
.build())
|
|
|
|
.build()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
(
|
|
|
|
quote_stmt!(cx,
|
|
|
|
struct __SerdeDeserializeWithStruct $impl_generics $where_clause {
|
|
|
|
value: $field_ty,
|
|
|
|
phantom: ::std::marker::PhantomData<$phantom_ty>,
|
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
quote_stmt!(cx,
|
|
|
|
impl $impl_generics _serde::de::Deserialize for $wrapper_ty $where_clause {
|
2016-05-08 22:29:10 -05:00
|
|
|
fn deserialize<__D>(__d: &mut __D) -> ::std::result::Result<Self, __D::Error>
|
|
|
|
where __D: _serde::de::Deserializer
|
2016-05-07 14:33:59 -05:00
|
|
|
{
|
|
|
|
let value = try!($deserialize_with(__d));
|
|
|
|
Ok(__SerdeDeserializeWithStruct {
|
|
|
|
value: value,
|
|
|
|
phantom: ::std::marker::PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
wrapper_ty,
|
|
|
|
)
|
|
|
|
}
|
2016-05-07 21:57:44 -05:00
|
|
|
|
|
|
|
fn expr_is_missing(
|
|
|
|
cx: &ExtCtxt,
|
2016-06-19 22:15:49 -05:00
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
attrs: &attr::Field,
|
2016-05-07 21:57:44 -05:00
|
|
|
) -> P<ast::Expr> {
|
2016-06-10 01:21:42 -05:00
|
|
|
match *attrs.default() {
|
|
|
|
attr::FieldDefault::Default => {
|
|
|
|
return quote_expr!(cx, ::std::default::Default::default());
|
|
|
|
}
|
|
|
|
attr::FieldDefault::Path(ref path) => {
|
|
|
|
return quote_expr!(cx, $path());
|
|
|
|
}
|
|
|
|
attr::FieldDefault::None => { /* below */ }
|
2016-05-07 21:57:44 -05:00
|
|
|
}
|
2016-06-10 01:21:42 -05:00
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
let name = name_expr(builder, attrs.name());
|
2016-05-07 21:57:44 -05:00
|
|
|
match attrs.deserialize_with() {
|
|
|
|
None => {
|
|
|
|
quote_expr!(cx, try!(visitor.missing_field($name)))
|
|
|
|
}
|
|
|
|
Some(_) => {
|
|
|
|
quote_expr!(cx, return Err(
|
|
|
|
<__V::Error as _serde::de::Error>::missing_field($name)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 23:27:49 -05:00
|
|
|
|
2016-06-19 22:15:49 -05:00
|
|
|
fn name_expr(
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
name: &attr::Name,
|
|
|
|
) -> P<ast::Expr> {
|
|
|
|
builder.expr().str(name.deserialize_name())
|
|
|
|
}
|
|
|
|
|
2016-06-11 23:27:49 -05:00
|
|
|
fn check_no_str(
|
|
|
|
cx: &ExtCtxt,
|
2016-06-21 21:12:08 -05:00
|
|
|
item: &Item,
|
|
|
|
) -> Result<(), ()> {
|
|
|
|
let fail = |field: &Field| {
|
2016-06-11 23:27:49 -05:00
|
|
|
cx.span_err(
|
|
|
|
field.span,
|
|
|
|
"Serde does not support deserializing fields of type &str; \
|
|
|
|
consider using String instead");
|
2016-06-21 21:12:08 -05:00
|
|
|
Err(())
|
2016-06-11 23:27:49 -05:00
|
|
|
};
|
|
|
|
|
2016-06-15 01:37:20 -05:00
|
|
|
for field in item.body.all_fields() {
|
|
|
|
if field.attrs.skip_deserializing()
|
|
|
|
|| field.attrs.deserialize_with().is_some() { continue }
|
2016-06-11 23:27:49 -05:00
|
|
|
|
|
|
|
if let ast::TyKind::Rptr(_, ref inner) = field.ty.node {
|
|
|
|
if let ast::TyKind::Path(_, ref path) = inner.ty.node {
|
|
|
|
if path.segments.len() == 1
|
|
|
|
&& path.segments[0].identifier.name.as_str() == "str"
|
|
|
|
{
|
|
|
|
return fail(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|