2015-04-27 17:05:54 -05:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
use aster;
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ast::{
|
2015-05-18 01:14:38 -05:00
|
|
|
self,
|
2015-10-17 21:44:07 -05:00
|
|
|
EnumDef,
|
2015-03-14 15:09:37 -05:00
|
|
|
Ident,
|
|
|
|
Item,
|
2015-10-17 21:44:07 -05:00
|
|
|
MetaItem,
|
2015-03-14 15:09:37 -05:00
|
|
|
};
|
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};
|
2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
|
2015-05-01 11:53:59 -05:00
|
|
|
use attr;
|
2015-03-15 17:31:31 -05:00
|
|
|
use field;
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
let builder = aster::AstBuilder::new().span(span);
|
|
|
|
|
|
|
|
let generics = match item.node {
|
2016-02-12 23:06:47 -06:00
|
|
|
ast::ItemKind::Struct(_, ref generics) => generics,
|
|
|
|
ast::ItemKind::Enum(_, ref generics) => generics,
|
2016-01-16 16:51:11 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
|
|
|
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
};
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let impl_generics = builder.from_generics(generics.clone())
|
2015-03-14 15:09:37 -05:00
|
|
|
.add_ty_param_bound(
|
|
|
|
builder.path().global().ids(&["serde", "de", "Deserialize"]).build()
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
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-01-18 14:39:46 -06:00
|
|
|
let body = match deserialize_body(cx, &builder, &item, &impl_generics, ty.clone()) {
|
|
|
|
Ok(body) => body,
|
|
|
|
Err(()) => {
|
|
|
|
// An error occured, but it should have been reported already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let impl_item = quote_item!(cx,
|
|
|
|
impl $impl_generics ::serde::de::Deserialize for $ty $where_clause {
|
2015-03-20 06:01:06 -05:00
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
2015-03-15 19:47:25 -05:00
|
|
|
where __D: ::serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
$body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap();
|
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
push(Annotatable::Item(impl_item))
|
2015-03-15 19:47:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_body(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
item: &Item,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
|
|
|
// Note: While we don't have any container attributes, we still want to try to
|
|
|
|
// parse them so we can report a proper error if we get passed an unknown attribute.
|
|
|
|
let _ = try!(field::container_attrs(cx, item));
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
match item.node {
|
2016-02-12 23:06:47 -06:00
|
|
|
ast::ItemKind::Struct(ref variant_data, _) => {
|
2015-03-15 19:47:25 -05:00
|
|
|
deserialize_item_struct(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder,
|
|
|
|
item,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
2016-01-16 16:51:11 -06:00
|
|
|
item.span,
|
2015-10-17 21:44:07 -05:00
|
|
|
variant_data,
|
2015-03-14 15:09:37 -05:00
|
|
|
)
|
|
|
|
}
|
2016-02-12 23:06:47 -06:00
|
|
|
ast::ItemKind::Enum(ref enum_def, _) => {
|
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,
|
2015-03-14 15:09:37 -05:00
|
|
|
enum_def,
|
|
|
|
)
|
|
|
|
}
|
2016-01-16 16:51:11 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_bug(item.span,
|
|
|
|
"expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_item_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
item: &Item,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-01-16 16:51:11 -06:00
|
|
|
span: Span,
|
2015-10-17 21:44:07 -05:00
|
|
|
variant_data: &ast::VariantData,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-10-17 21:44:07 -05:00
|
|
|
match *variant_data {
|
|
|
|
ast::VariantData::Unit(_) => {
|
2015-03-15 19:47:25 -05:00
|
|
|
deserialize_unit_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
)
|
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
2015-07-30 11:38:09 -05:00
|
|
|
deserialize_newtype_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
|
|
|
)
|
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref fields, _) => {
|
|
|
|
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
2016-01-16 16:51:11 -06:00
|
|
|
cx.span_bug(span, "tuple struct has named fields")
|
2015-10-17 21:44:07 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
deserialize_tuple_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.len(),
|
2015-03-15 19:47:25 -05:00
|
|
|
)
|
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Struct(ref fields, _) => {
|
|
|
|
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
2016-01-16 16:51:11 -06:00
|
|
|
cx.span_bug(span, "struct has unnamed fields")
|
2015-10-17 21:44:07 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
deserialize_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
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-01-18 14:39:46 -06:00
|
|
|
) -> Result<(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-01-18 14:39:46 -06:00
|
|
|
Ok((
|
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-01-18 14:39:46 -06: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-01-18 14:39:46 -06:00
|
|
|
Ok((
|
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-01-18 14:39:46 -06: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()
|
2015-05-01 16:59:49 -05:00
|
|
|
.global()
|
2015-05-01 11:53:59 -05:00
|
|
|
.segment("serde").build()
|
|
|
|
.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,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
struct __Visitor;
|
|
|
|
|
|
|
|
impl ::serde::de::Visitor for __Visitor {
|
|
|
|
type Value = $type_ident;
|
|
|
|
|
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit_unit<E>(&mut self) -> ::std::result::Result<$type_ident, E>
|
2015-03-14 15:09:37 -05:00
|
|
|
where E: ::serde::de::Error,
|
|
|
|
{
|
2015-03-15 19:47:25 -05:00
|
|
|
Ok($type_ident)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit_seq<V>(&mut self, mut visitor: V) -> ::std::result::Result<$type_ident, V::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
try!(visitor.end());
|
|
|
|
self.visit_unit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 14:11:20 -05:00
|
|
|
deserializer.visit_unit_struct($type_name, __Visitor)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-07-30 11:38:09 -05:00
|
|
|
fn deserialize_newtype_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-07-30 11:38:09 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
|
|
|
));
|
2015-07-30 11:38:09 -05:00
|
|
|
|
|
|
|
let visit_seq_expr = deserialize_seq(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
builder.path().id(type_ident).build(),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
|
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-07-30 11:38:09 -05:00
|
|
|
$visitor_item
|
|
|
|
|
|
|
|
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
|
|
|
type Value = $ty;
|
|
|
|
|
|
|
|
#[inline]
|
2015-08-08 05:33:29 -05:00
|
|
|
fn visit_newtype_struct<D>(&mut self, deserializer: &mut D) -> ::std::result::Result<Self::Value, D::Error>
|
2015-07-30 11:38:09 -05:00
|
|
|
where D: ::serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
let value = try!(::serde::de::Deserialize::deserialize(deserializer));
|
|
|
|
Ok($type_ident(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.visit_newtype_struct($type_name, $visitor_expr)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-07-30 11:38:09 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
fn deserialize_tuple_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,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
fields: usize,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<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-01-18 14:39:46 -06:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
|
|
|
));
|
2015-03-15 19:47:25 -05:00
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
let visit_seq_expr = deserialize_seq(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.path().id(type_ident).build(),
|
2015-03-15 20:37:26 -05:00
|
|
|
fields,
|
2015-03-14 15:09:37 -05:00
|
|
|
);
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
$visitor_item
|
|
|
|
|
2015-05-01 11:53:59 -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-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>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:04:31 -05:00
|
|
|
deserializer.visit_tuple_struct($type_name, $fields, $visitor_expr)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
fn deserialize_seq(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
struct_path: ast::Path,
|
2015-03-15 20:37:26 -05:00
|
|
|
fields: usize,
|
2015-03-14 15:09:37 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-03-19 09:35:07 -05:00
|
|
|
let let_values: Vec<P<ast::Stmt>> = (0 .. fields)
|
2015-03-15 20:37:26 -05:00
|
|
|
.map(|i| {
|
|
|
|
let name = builder.id(format!("__field{}", i));
|
2015-03-14 15:09:37 -05:00
|
|
|
quote_stmt!(cx,
|
|
|
|
let $name = match try!(visitor.visit()) {
|
2015-05-14 16:28:44 -05:00
|
|
|
Some(value) => { value },
|
2015-03-14 15:09:37 -05:00
|
|
|
None => {
|
2015-08-06 19:47:44 -05:00
|
|
|
return Err(::serde::de::Error::end_of_stream());
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
};
|
2015-03-19 09:35:07 -05:00
|
|
|
).unwrap()
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let result = builder.expr().call()
|
|
|
|
.build_path(struct_path)
|
2015-03-15 20:37:26 -05:00
|
|
|
.with_args((0 .. fields).map(|i| builder.expr().id(format!("__field{}", i))))
|
2015-03-14 15:09:37 -05:00
|
|
|
.build();
|
|
|
|
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$let_values
|
|
|
|
|
|
|
|
try!(visitor.end());
|
|
|
|
|
|
|
|
Ok($result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-21 23:35:20 -05:00
|
|
|
fn deserialize_struct_as_seq(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
struct_path: ast::Path,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-10-17 21:44:07 -05:00
|
|
|
let let_values: Vec<P<ast::Stmt>> = (0 .. fields.len())
|
2015-07-21 23:35:20 -05:00
|
|
|
.map(|i| {
|
|
|
|
let name = builder.id(format!("__field{}", i));
|
|
|
|
quote_stmt!(cx,
|
|
|
|
let $name = match try!(visitor.visit()) {
|
|
|
|
Some(value) => { value },
|
|
|
|
None => {
|
2015-08-06 19:47:44 -05:00
|
|
|
return Err(::serde::de::Error::end_of_stream());
|
2015-07-21 23:35:20 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
).unwrap()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let result = builder.expr().struct_path(struct_path)
|
|
|
|
.with_id_exprs(
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.iter()
|
2015-07-21 23:35:20 -05:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, field)| {
|
|
|
|
(
|
|
|
|
match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => name.clone(),
|
2016-01-16 16:51:11 -06:00
|
|
|
ast::UnnamedField(_) => {
|
|
|
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
|
|
|
}
|
2015-07-21 23:35:20 -05:00
|
|
|
},
|
|
|
|
builder.expr().id(format!("__field{}", i)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-07-21 23:35:20 -05:00
|
|
|
$let_values
|
|
|
|
|
|
|
|
try!(visitor.end());
|
|
|
|
|
|
|
|
Ok($result)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-07-21 23:35: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,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
2015-07-03 21:39:27 -05:00
|
|
|
builder,
|
|
|
|
&impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-07-21 23:35:20 -05:00
|
|
|
let type_path = builder.path().id(type_ident).build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
2015-07-21 23:35:20 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_path.clone(),
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-07-21 23:35:20 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-10-17 21:44:07 -05:00
|
|
|
type_path.clone(),
|
|
|
|
fields,
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 23:47:23 -05:00
|
|
|
$field_visitor
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
$visitor_item
|
|
|
|
|
2015-05-01 11:53:59 -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>
|
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
$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>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::MapVisitor,
|
|
|
|
{
|
|
|
|
$visit_map_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
$fields_stmt
|
|
|
|
|
|
|
|
deserializer.visit_struct($type_name, FIELDS, $visitor_expr)
|
2016-01-18 14:39:46 -06: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>,
|
2015-03-14 15:09:37 -05:00
|
|
|
enum_def: &EnumDef,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<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
|
|
|
|
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2015-03-16 00:04:17 -05:00
|
|
|
let variant_visitor = deserialize_field_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
enum_def.variants.iter()
|
2015-09-07 15:13:32 -05:00
|
|
|
.map(|variant| {
|
|
|
|
let expr = builder.expr().str(variant.node.name);
|
2016-01-18 14:39:46 -06:00
|
|
|
attr::FieldAttrsBuilder::new(cx, builder)
|
2015-09-07 15:13:32 -05:00
|
|
|
.name(expr)
|
|
|
|
.default()
|
|
|
|
.build()
|
|
|
|
})
|
2015-03-16 00:04:17 -05:00
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
|
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(
|
|
|
|
enum_def.variants.iter()
|
|
|
|
.map(|variant| {
|
|
|
|
builder.expr().str(variant.node.name)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let variants_stmt = quote_stmt!(cx,
|
|
|
|
const VARIANTS: &'static [&'static str] = $variants_expr;
|
|
|
|
).unwrap();
|
|
|
|
|
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![];
|
|
|
|
for (i, variant) in enum_def.variants.iter().enumerate() {
|
|
|
|
let variant_name = builder.pat().enum_()
|
|
|
|
.id("__Field").id(format!("__field{}", i)).build()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let expr = try!(deserialize_variant(
|
|
|
|
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,
|
|
|
|
));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
|
|
|
));
|
|
|
|
|
|
|
|
Ok(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
|
|
|
|
2015-05-01 11:53:59 -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>
|
2015-03-16 00:04:17 -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
|
|
|
|
|
|
|
|
deserializer.visit_enum($type_name, VARIANTS, $visitor_expr)
|
2016-01-18 14:39:46 -06: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>,
|
|
|
|
variant: &ast::Variant,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let variant_ident = variant.node.name;
|
|
|
|
|
2015-11-01 15:24:26 -06:00
|
|
|
match variant.node.data {
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Unit(_) => {
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(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-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref args, _) if args.len() == 1 => {
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-07-29 17:41:37 -05:00
|
|
|
let val = try!(visitor.visit_newtype());
|
2015-07-14 04:12:01 -05:00
|
|
|
Ok($type_ident::$variant_ident(val))
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-07-14 04:12:01 -05:00
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref fields, _) => {
|
2015-03-16 00:04:17 -05:00
|
|
|
deserialize_tuple_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident,
|
|
|
|
variant_ident,
|
|
|
|
generics,
|
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.len(),
|
2015-03-16 00:04:17 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Struct(ref fields, _) => {
|
2015-03-16 00:04:17 -05:00
|
|
|
deserialize_struct_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident,
|
|
|
|
variant_ident,
|
|
|
|
generics,
|
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2015-03-16 00:04:17 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_tuple_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident: ast::Ident,
|
|
|
|
variant_ident: ast::Ident,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2015-03-15 20:37:26 -05:00
|
|
|
fields: usize,
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &generics.where_clause;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
|
|
|
));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
let visit_seq_expr = deserialize_seq(
|
|
|
|
cx,
|
2015-03-14 15:09:37 -05:00
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.path().id(type_ident).id(variant_ident).build(),
|
2015-03-15 20:37:26 -05:00
|
|
|
fields,
|
2015-03-14 15:09:37 -05:00
|
|
|
);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 17:20:00 -05:00
|
|
|
$visitor_item
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-05-01 11:53:59 -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-03-20 06:01:06 -05:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
2015-03-18 09:35:05 -05:00
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
2015-03-14 15:09:37 -05:00
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 14:24:16 -05:00
|
|
|
visitor.visit_tuple($fields, $visitor_expr)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn deserialize_struct_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-03-15 19:47:25 -05:00
|
|
|
type_ident: ast::Ident,
|
|
|
|
variant_ident: ast::Ident,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &generics.where_clause;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-07-21 23:35:20 -05:00
|
|
|
let type_path = builder.path()
|
|
|
|
.id(type_ident)
|
|
|
|
.id(variant_ident)
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
2015-07-21 23:35:20 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_path.clone(),
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-07-21 23:35:20 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (field_visitor, fields_stmt, field_expr) = try!(deserialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-10-17 21:44:07 -05:00
|
|
|
type_path,
|
|
|
|
fields,
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
generics,
|
|
|
|
vec![deserializer_ty_param(builder)],
|
|
|
|
vec![deserializer_ty_arg(builder)],
|
|
|
|
));
|
2015-03-15 19:47:25 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 23:47:23 -05:00
|
|
|
$field_visitor
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-14 17:20:00 -05:00
|
|
|
$visitor_item
|
|
|
|
|
2015-05-01 11:53:59 -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>
|
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
|
2015-07-21 23:40:39 -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>
|
2015-03-18 09:35:05 -05:00
|
|
|
where __V: ::serde::de::MapVisitor,
|
|
|
|
{
|
2015-03-15 19:47:25 -05:00
|
|
|
$field_expr
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
$fields_stmt
|
|
|
|
|
2015-07-29 14:24:16 -05:00
|
|
|
visitor.visit_struct(FIELDS, $visitor_expr)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2015-03-15 17:31:31 -05:00
|
|
|
|
|
|
|
fn deserialize_field_visitor(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-05-01 11:53:59 -05:00
|
|
|
field_attrs: Vec<attr::FieldAttrs>,
|
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.
|
2015-05-01 11:53:59 -05:00
|
|
|
let field_idents: Vec<ast::Ident> = (0 .. field_attrs.len())
|
2015-03-15 20:37:26 -05:00
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
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
|
|
|
})
|
|
|
|
)
|
|
|
|
.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();
|
|
|
|
|
|
|
|
let index_body = quote_expr!(cx,
|
|
|
|
match value {
|
|
|
|
$index_field_arms
|
2015-08-07 10:08:56 -05:00
|
|
|
_ => { Err(::serde::de::Error::syntax("expected a field")) }
|
2015-07-03 21:39:27 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-05-01 11:53:59 -05:00
|
|
|
// A set of all the formats that have specialized field attributes
|
|
|
|
let formats = field_attrs.iter()
|
|
|
|
.fold(HashSet::new(), |mut set, field_expr| {
|
|
|
|
set.extend(field_expr.formats());
|
|
|
|
set
|
|
|
|
});
|
2015-04-27 17:05:54 -05:00
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
// Match arms to extract a field from a string
|
2015-04-27 17:05:54 -05:00
|
|
|
let default_field_arms: Vec<_> = field_idents.iter()
|
2015-05-01 11:53:59 -05:00
|
|
|
.zip(field_attrs.iter())
|
2015-03-16 00:04:17 -05:00
|
|
|
.map(|(field_ident, field_expr)| {
|
2015-05-01 11:53:59 -05:00
|
|
|
let expr = field_expr.default_key_expr();
|
|
|
|
quote_arm!(cx, $expr => { Ok(__Field::$field_ident) })
|
2015-03-15 17:31:31 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
let str_body = if formats.is_empty() {
|
2015-05-01 11:53:59 -05:00
|
|
|
// No formats specific attributes, so no match on format required
|
2015-04-27 17:05:54 -05:00
|
|
|
quote_expr!(cx,
|
2015-07-29 15:46:45 -05:00
|
|
|
match value {
|
|
|
|
$default_field_arms
|
2015-08-06 19:47:44 -05:00
|
|
|
_ => { Err(::serde::de::Error::unknown_field(value)) }
|
2015-07-29 15:46:45 -05:00
|
|
|
})
|
2015-04-27 17:05:54 -05:00
|
|
|
} else {
|
2015-07-03 21:39:27 -05:00
|
|
|
let field_arms: Vec<_> = formats.iter()
|
2015-04-27 17:05:54 -05:00
|
|
|
.map(|fmt| {
|
|
|
|
field_idents.iter()
|
2015-05-01 11:53:59 -05:00
|
|
|
.zip(field_attrs.iter())
|
2015-04-27 17:05:54 -05:00
|
|
|
.map(|(field_ident, field_expr)| {
|
2015-05-01 11:53:59 -05:00
|
|
|
let expr = field_expr.key_expr(fmt);
|
|
|
|
quote_arm!(cx, $expr => { Ok(__Field::$field_ident) })
|
2015-04-27 17:05:54 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
let fmt_matches: Vec<_> = formats.iter()
|
2015-04-27 17:05:54 -05:00
|
|
|
.zip(field_arms.iter())
|
|
|
|
.map(|(ref fmt, ref arms)| {
|
|
|
|
quote_arm!(cx, $fmt => {
|
|
|
|
match value {
|
2015-05-14 16:28:44 -05:00
|
|
|
$arms
|
2015-04-27 17:05:54 -05:00
|
|
|
_ => {
|
2015-08-06 19:47:44 -05:00
|
|
|
Err(::serde::de::Error::unknown_field(value))
|
2015-04-27 17:05:54 -05:00
|
|
|
}
|
|
|
|
}})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
quote_expr!(cx,
|
2015-07-03 21:39:27 -05:00
|
|
|
match __D::format() {
|
|
|
|
$fmt_matches
|
|
|
|
_ => match value {
|
|
|
|
$default_field_arms
|
2015-08-06 19:47:44 -05:00
|
|
|
_ => { Err(::serde::de::Error::unknown_field(value)) }
|
2015-07-03 21:39:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2015-04-27 17:05:54 -05:00
|
|
|
};
|
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
let impl_item = quote_item!(cx,
|
|
|
|
impl ::serde::de::Deserialize for __Field {
|
|
|
|
#[inline]
|
|
|
|
fn deserialize<D>(deserializer: &mut D) -> ::std::result::Result<__Field, D::Error>
|
|
|
|
where D: ::serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
struct __FieldVisitor<D> {
|
|
|
|
phantom: PhantomData<D>
|
|
|
|
}
|
2015-03-15 17:31:31 -05:00
|
|
|
|
2015-05-18 01:14:38 -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
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
fn visit_usize<E>(&mut self, value: usize) -> ::std::result::Result<__Field, E>
|
|
|
|
where E: ::serde::de::Error,
|
|
|
|
{
|
|
|
|
$index_body
|
|
|
|
}
|
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
fn visit_str<E>(&mut self, value: &str) -> ::std::result::Result<__Field, E>
|
|
|
|
where E: ::serde::de::Error,
|
|
|
|
{
|
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
|
|
|
|
2015-05-18 01:14:38 -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
|
|
|
{
|
2015-05-18 01:14:38 -05:00
|
|
|
// TODO: would be better to generate a byte string literal match
|
|
|
|
match ::std::str::from_utf8(value) {
|
|
|
|
Ok(s) => self.visit_str(s),
|
2015-08-07 10:08:56 -05:00
|
|
|
_ => {
|
|
|
|
Err(
|
|
|
|
::serde::de::Error::syntax(
|
|
|
|
"could not convert a byte string to a String"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2015-05-08 19:18:13 -05:00
|
|
|
}
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 01:14:38 -05:00
|
|
|
|
2016-01-28 10:38:07 -06:00
|
|
|
deserializer.visit_struct_key(__FieldVisitor::<D>{ phantom: 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,
|
|
|
|
struct_path: ast::Path,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<(Vec<P<ast::Item>>, P<ast::Stmt>, P<ast::Expr>), ()> {
|
2015-03-15 17:31:31 -05:00
|
|
|
let field_visitor = deserialize_field_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2016-01-18 14:39:46 -06:00
|
|
|
try!(field::struct_field_attrs(cx, builder, fields)),
|
2015-03-15 17:31:31 -05:00
|
|
|
);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let visit_map_expr = try!(deserialize_map(
|
2015-03-15 17:31:31 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
struct_path,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2016-01-18 14:39:46 -06: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()
|
2015-07-03 21:39:27 -05:00
|
|
|
.map(|field| {
|
|
|
|
match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => builder.expr().str(name),
|
2016-01-16 16:51:11 -06:00
|
|
|
ast::UnnamedField(_) => {
|
|
|
|
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-01-18 14:39:46 -06:00
|
|
|
Ok((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,
|
|
|
|
struct_path: ast::Path,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-01-18 14:39:46 -06:00
|
|
|
) -> Result<P<ast::Expr>, ()> {
|
2015-03-15 20:38:52 -05:00
|
|
|
// Create the field names for the fields.
|
2015-10-17 21:44:07 -05:00
|
|
|
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
2015-03-15 20:38:52 -05:00
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Declare each field.
|
|
|
|
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
|
2015-03-19 09:35:07 -05:00
|
|
|
.map(|field_name| quote_stmt!(cx, let mut $field_name = None;).unwrap())
|
2015-03-15 20:38:52 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Match arms to extract a value for a field.
|
|
|
|
let value_arms: Vec<ast::Arm> = field_names.iter()
|
|
|
|
.map(|field_name| {
|
|
|
|
quote_arm!(cx,
|
|
|
|
__Field::$field_name => {
|
|
|
|
$field_name = Some(try!(visitor.visit_value()));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let field_attrs = try!(field::struct_field_attrs(cx, builder, fields));
|
|
|
|
|
2015-03-15 20:38:52 -05:00
|
|
|
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
2016-01-18 14:39:46 -06:00
|
|
|
.zip(field_attrs.iter())
|
2015-05-01 14:36:50 -05:00
|
|
|
.map(|(field_name, field_attr)| {
|
|
|
|
let missing_expr = if field_attr.use_default() {
|
2015-03-15 20:38:52 -05:00
|
|
|
quote_expr!(cx, ::std::default::Default::default())
|
|
|
|
} else {
|
2015-05-01 11:53:59 -05:00
|
|
|
let formats = field_attr.formats();
|
|
|
|
let arms : Vec<_> = formats.iter()
|
|
|
|
.map(|format| {
|
|
|
|
let key_expr = field_attr.key_expr(format);
|
|
|
|
quote_arm!(cx, $format => { $key_expr })
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let default = field_attr.default_key_expr();
|
|
|
|
if arms.is_empty() {
|
|
|
|
quote_expr!(cx, try!(visitor.missing_field($default)))
|
|
|
|
} else {
|
|
|
|
quote_expr!(
|
|
|
|
cx,
|
|
|
|
try!(visitor.missing_field(
|
|
|
|
match __D::format() {
|
2015-05-14 16:28:44 -05:00
|
|
|
$arms
|
|
|
|
_ => { $default }
|
2015-05-01 11:53:59 -05:00
|
|
|
})))
|
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
quote_stmt!(cx,
|
|
|
|
let $field_name = match $field_name {
|
|
|
|
Some($field_name) => $field_name,
|
2015-05-14 16:28:44 -05:00
|
|
|
None => $missing_expr
|
2015-03-15 20:38:52 -05:00
|
|
|
};
|
2015-03-19 09:35:07 -05:00
|
|
|
).unwrap()
|
2015-03-15 20:38:52 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let result = builder.expr().struct_path(struct_path)
|
|
|
|
.with_id_exprs(
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.iter()
|
2015-03-15 20:38:52 -05:00
|
|
|
.zip(field_names.iter())
|
|
|
|
.map(|(field, field_name)| {
|
|
|
|
(
|
|
|
|
match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => name.clone(),
|
2016-01-16 16:51:11 -06:00
|
|
|
ast::UnnamedField(_) => {
|
|
|
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
},
|
|
|
|
builder.expr().id(field_name),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-15 20:38:52 -05:00
|
|
|
$let_values
|
|
|
|
|
|
|
|
while let Some(key) = try!(visitor.visit_key()) {
|
|
|
|
match key {
|
|
|
|
$value_arms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$extract_values
|
2015-03-16 07:20:34 -05:00
|
|
|
|
|
|
|
try!(visitor.end());
|
|
|
|
|
2015-03-15 20:38:52 -05:00
|
|
|
Ok($result)
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-15 20:38:52 -05:00
|
|
|
}
|