2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ast::{
|
|
|
|
Ident,
|
|
|
|
MetaItem,
|
|
|
|
Item,
|
|
|
|
Expr,
|
|
|
|
StructDef,
|
|
|
|
EnumDef,
|
|
|
|
};
|
|
|
|
use syntax::ast;
|
2015-03-15 16:31:05 -05:00
|
|
|
use syntax::codemap::Span;
|
2015-03-15 19:47:25 -05:00
|
|
|
use syntax::ext::base::ExtCtxt;
|
2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
|
|
|
|
use aster;
|
|
|
|
|
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,
|
|
|
|
_mitem: &MetaItem,
|
2015-03-14 15:09:37 -05:00
|
|
|
item: &Item,
|
|
|
|
push: &mut FnMut(P<ast::Item>)
|
|
|
|
) {
|
|
|
|
let builder = aster::AstBuilder::new().span(span);
|
|
|
|
|
|
|
|
let generics = match item.node {
|
|
|
|
ast::ItemStruct(_, ref generics) => generics,
|
|
|
|
ast::ItemEnum(_, ref generics) => generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive_deserialize]")
|
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();
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let body = deserialize_body(
|
|
|
|
cx,
|
2015-03-14 15:09:37 -05:00
|
|
|
&builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
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
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let impl_item = quote_item!(cx,
|
|
|
|
#[automatically_derived]
|
|
|
|
impl $impl_generics ::serde::de::Deserialize for $ty $where_clause {
|
|
|
|
fn deserialize<__D>(deserializer: &mut __D) -> Result<$ty, __D::Error>
|
|
|
|
where __D: ::serde::de::Deserializer,
|
|
|
|
{
|
|
|
|
$body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
push(impl_item)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_body(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
item: &Item,
|
|
|
|
impl_generics: &ast::Generics,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
) -> P<ast::Expr> {
|
|
|
|
match item.node {
|
|
|
|
ast::ItemStruct(ref struct_def, _) => {
|
|
|
|
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,
|
2015-03-14 15:09:37 -05:00
|
|
|
struct_def,
|
|
|
|
)
|
|
|
|
}
|
2015-03-15 19:47:25 -05:00
|
|
|
ast::ItemEnum(ref enum_def, _) => {
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
2015-03-15 19:47:25 -05:00
|
|
|
_ => cx.bug("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>,
|
|
|
|
struct_def: &ast::StructDef,
|
|
|
|
) -> P<ast::Expr> {
|
|
|
|
let mut named_fields = vec![];
|
|
|
|
let mut unnamed_fields = 0;
|
|
|
|
|
|
|
|
for field in struct_def.fields.iter() {
|
|
|
|
match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => { named_fields.push(name); }
|
|
|
|
ast::UnnamedField(_) => { unnamed_fields += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match (named_fields.is_empty(), unnamed_fields == 0) {
|
|
|
|
(true, true) => {
|
|
|
|
deserialize_unit_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(true, false) => {
|
|
|
|
deserialize_tuple_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
|
|
|
unnamed_fields,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(false, true) => {
|
|
|
|
deserialize_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
|
|
|
ty,
|
|
|
|
struct_def,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(false, false) => {
|
|
|
|
cx.bug("struct has named and unnamed fields")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03-15 19:47:25 -05:00
|
|
|
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>) {
|
2015-03-14 15:09:37 -05:00
|
|
|
if trait_generics.ty_params.is_empty() {
|
|
|
|
(
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.item().tuple_struct("__Visitor").build(),
|
|
|
|
builder.ty().id("__Visitor"),
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.expr().id("__Visitor"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
builder.item().tuple_struct("__Visitor")
|
|
|
|
.generics().with(trait_generics.clone()).build()
|
|
|
|
.with_tys(
|
|
|
|
trait_generics.ty_params.iter().map(|ty_param| {
|
|
|
|
builder.ty().phantom_data().id(ty_param.ident)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build(),
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.ty().path()
|
|
|
|
.segment("__Visitor").with_generics(trait_generics.clone()).build()
|
|
|
|
.build(),
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.expr().call().id("__Visitor")
|
|
|
|
.with_args(
|
|
|
|
trait_generics.ty_params.iter().map(|_| {
|
|
|
|
builder.expr().phantom_data()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
) -> 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
|
|
|
|
|
|
|
quote_expr!(cx, {
|
|
|
|
struct __Visitor;
|
|
|
|
|
|
|
|
impl ::serde::de::Visitor for __Visitor {
|
|
|
|
type Value = $type_ident;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn visit_unit<E>(&mut self) -> Result<$type_ident, E>
|
|
|
|
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]
|
|
|
|
fn visit_named_unit<
|
|
|
|
E: ::serde::de::Error,
|
|
|
|
>(&mut self, name: &str) -> Result<$type_ident, E> {
|
2015-03-15 19:47:25 -05:00
|
|
|
if name == $type_name {
|
2015-03-14 15:09:37 -05:00
|
|
|
self.visit_unit()
|
|
|
|
} else {
|
|
|
|
Err(::serde::de::Error::syntax_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<$type_ident, V::Error>
|
|
|
|
where V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
try!(visitor.end());
|
|
|
|
self.visit_unit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
deserializer.visit(__Visitor)
|
2015-03-14 15:09:37 -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,
|
2015-03-14 15:09:37 -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
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$visitor_item
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
impl $impl_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
fn visit_seq<__V>(&mut self, mut visitor: __V) -> Result<$ty, __V::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_named_seq<__V>(&mut self,
|
|
|
|
name: &str,
|
2015-03-15 19:47:25 -05:00
|
|
|
visitor: __V) -> Result<$ty, __V::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::SeqVisitor,
|
|
|
|
{
|
2015-03-15 19:47:25 -05:00
|
|
|
if name == $type_name {
|
2015-03-14 15:09:37 -05:00
|
|
|
self.visit_seq(visitor)
|
|
|
|
} else {
|
|
|
|
Err(::serde::de::Error::syntax_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
deserializer.visit($visitor_expr)
|
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-15 20:37:26 -05:00
|
|
|
let let_values: Vec<P<_>> = (0 .. fields)
|
|
|
|
.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()) {
|
|
|
|
Some(value) => value,
|
|
|
|
None => {
|
|
|
|
return Err(::serde::de::Error::end_of_stream_error());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.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-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-03-14 15:09:37 -05:00
|
|
|
struct_def: &StructDef,
|
|
|
|
) -> P<ast::Expr> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 17:31:31 -05:00
|
|
|
let (field_visitor, visit_map_expr) = deserialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
struct_def,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.path().id(type_ident).build(),
|
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
|
|
|
|
|
|
|
quote_expr!(cx, {
|
2015-03-14 23:47:23 -05:00
|
|
|
$field_visitor
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
$visitor_item
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
impl $impl_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
#[inline]
|
2015-03-15 19:47:25 -05:00
|
|
|
fn visit_map<__V>(&mut self, mut visitor: __V) -> Result<$ty, __V::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::MapVisitor,
|
|
|
|
{
|
|
|
|
$visit_map_expr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn visit_named_map<__V>(&mut self,
|
|
|
|
name: &str,
|
2015-03-15 19:47:25 -05:00
|
|
|
visitor: __V) -> Result<$ty, __V::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where __V: ::serde::de::MapVisitor,
|
|
|
|
{
|
2015-03-15 19:47:25 -05:00
|
|
|
if name == $type_name {
|
2015-03-14 15:09:37 -05:00
|
|
|
self.visit_map(visitor)
|
|
|
|
} else {
|
|
|
|
Err(::serde::de::Error::syntax_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
deserializer.visit($visitor_expr)
|
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,
|
|
|
|
) -> 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()
|
|
|
|
.map(|variant| builder.expr().str(variant.node.name))
|
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
// Match arms to extract a variant from a string
|
2015-03-15 19:47:25 -05:00
|
|
|
let variant_arms: Vec<_> = enum_def.variants.iter()
|
2015-03-16 00:04:17 -05:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, variant)| {
|
|
|
|
let variant_name = builder.expr().path()
|
|
|
|
.id("__Field").id(format!("__field{}", i))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let expr = deserialize_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_ident,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl_generics,
|
|
|
|
ty.clone(),
|
2015-03-14 15:09:37 -05:00
|
|
|
variant,
|
2015-03-16 00:04:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
quote_arm!(cx, $variant_name => { $expr })
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
impl_generics,
|
|
|
|
);
|
|
|
|
|
2015-03-14 15:09:37 -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
|
|
|
|
2015-03-16 00:04:17 -05:00
|
|
|
impl $impl_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-16 00:04:17 -05:00
|
|
|
fn visit<__V>(&mut self, mut visitor: __V) -> Result<$ty, __V::Error>
|
|
|
|
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-03-16 00:04:17 -05:00
|
|
|
deserializer.visit_enum($type_name, $visitor_expr)
|
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,
|
2015-03-16 00:04:17 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-03-15 19:47:25 -05:00
|
|
|
let variant_ident = variant.node.name;
|
|
|
|
|
|
|
|
match variant.node.kind {
|
|
|
|
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
2015-03-16 00:04:17 -05:00
|
|
|
quote_expr!(cx, {
|
|
|
|
try!(visitor.visit_unit());
|
|
|
|
Ok($type_ident::$variant_ident)
|
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2015-03-15 19:47:25 -05:00
|
|
|
ast::TupleVariantKind(ref args) => {
|
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-03-15 20:37:26 -05:00
|
|
|
args.len(),
|
2015-03-16 00:04:17 -05:00
|
|
|
)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2015-03-15 19:47:25 -05:00
|
|
|
ast::StructVariantKind(ref struct_def) => {
|
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,
|
|
|
|
struct_def,
|
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,
|
2015-03-14 15:09:37 -05:00
|
|
|
) -> 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-03-15 19:47:25 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
builder,
|
2015-03-15 19:47:25 -05:00
|
|
|
generics,
|
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
|
|
|
);
|
|
|
|
|
|
|
|
quote_expr!(cx, {
|
2015-03-14 17:20:00 -05:00
|
|
|
$visitor_item
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
impl $generics ::serde::de::EnumSeqVisitor for $visitor_ty $where_clause {
|
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
fn visit<
|
|
|
|
V: ::serde::de::SeqVisitor,
|
2015-03-15 19:47:25 -05:00
|
|
|
>(&mut self, mut visitor: V) -> Result<$ty, V::Error> {
|
2015-03-14 15:09:37 -05:00
|
|
|
$visit_seq_expr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
visitor.visit_seq($visitor_expr)
|
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>,
|
|
|
|
struct_def: &ast::StructDef,
|
2015-03-14 15:09:37 -05:00
|
|
|
) -> 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-03-15 19:47:25 -05:00
|
|
|
let (field_visitor, field_expr) = deserialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-15 17:31:31 -05:00
|
|
|
struct_def,
|
2015-03-15 19:47:25 -05:00
|
|
|
builder.path().id(type_ident).id(variant_ident).build(),
|
2015-03-14 15:09:37 -05:00
|
|
|
);
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
|
|
|
builder,
|
|
|
|
generics,
|
|
|
|
);
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
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-03-15 19:47:25 -05:00
|
|
|
impl $generics ::serde::de::EnumMapVisitor for $visitor_ty $where_clause {
|
|
|
|
type Value = $ty;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
fn visit<
|
|
|
|
V: ::serde::de::MapVisitor,
|
2015-03-15 19:47:25 -05:00
|
|
|
>(&mut self, mut visitor: V) -> Result<$ty, V::Error> {
|
|
|
|
$field_expr
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
visitor.visit_map($visitor_expr)
|
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-03-16 00:04:17 -05:00
|
|
|
field_exprs: Vec<P<ast::Expr>>,
|
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-03-16 00:04:17 -05:00
|
|
|
let field_idents: Vec<ast::Ident> = (0 .. field_exprs.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| {
|
|
|
|
builder.variant(field_ident).tuple().build()
|
2015-03-15 17:31:31 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Match arms to extract a field from a string
|
2015-03-16 00:04:17 -05:00
|
|
|
let field_arms: Vec<_> = field_idents.iter()
|
|
|
|
.zip(field_exprs.into_iter())
|
|
|
|
.map(|(field_ident, field_expr)| {
|
|
|
|
quote_arm!(cx, $field_expr => { Ok(__Field::$field_ident) })
|
2015-03-15 17:31:31 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
vec![
|
|
|
|
field_enum,
|
|
|
|
|
|
|
|
quote_item!(cx,
|
|
|
|
impl ::serde::de::Deserialize for __Field {
|
|
|
|
#[inline]
|
2015-03-16 23:11:30 -05:00
|
|
|
fn deserialize<D>(deserializer: &mut D) -> Result<__Field, D::Error>
|
|
|
|
where D: ::serde::de::Deserializer,
|
2015-03-15 17:31:31 -05:00
|
|
|
{
|
|
|
|
struct __FieldVisitor;
|
|
|
|
|
|
|
|
impl ::serde::de::Visitor for __FieldVisitor {
|
|
|
|
type Value = __Field;
|
|
|
|
|
|
|
|
fn visit_str<E>(&mut self, value: &str) -> Result<__Field, E>
|
|
|
|
where E: ::serde::de::Error,
|
|
|
|
{
|
|
|
|
match value {
|
|
|
|
$field_arms
|
|
|
|
_ => Err(::serde::de::Error::syntax_error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:11:30 -05:00
|
|
|
deserializer.visit(__FieldVisitor)
|
2015-03-15 17:31:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_struct_visitor(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
struct_def: &ast::StructDef,
|
|
|
|
struct_path: ast::Path,
|
|
|
|
) -> (Vec<P<ast::Item>>, P<ast::Expr>) {
|
|
|
|
let field_visitor = deserialize_field_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2015-03-16 00:04:17 -05:00
|
|
|
field::struct_field_strs(cx, builder, struct_def),
|
2015-03-15 17:31:31 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let visit_map_expr = deserialize_map(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
struct_path,
|
|
|
|
struct_def,
|
|
|
|
);
|
|
|
|
|
|
|
|
(field_visitor, visit_map_expr)
|
|
|
|
}
|
2015-03-15 20:38:52 -05:00
|
|
|
|
|
|
|
fn deserialize_map(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
struct_path: ast::Path,
|
|
|
|
struct_def: &StructDef,
|
|
|
|
) -> P<ast::Expr> {
|
|
|
|
// Create the field names for the fields.
|
|
|
|
let field_names: Vec<ast::Ident> = (0 .. struct_def.fields.len())
|
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Declare each field.
|
|
|
|
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
|
|
|
|
.map(|field_name| quote_stmt!(cx, let mut $field_name = None;))
|
|
|
|
.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();
|
|
|
|
|
|
|
|
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
|
|
|
.zip(struct_def.fields.iter())
|
|
|
|
.map(|(field_name, field)| {
|
|
|
|
let name_str = match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => builder.expr().str(name),
|
|
|
|
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let missing_expr = if field::default_value(field) {
|
|
|
|
quote_expr!(cx, ::std::default::Default::default())
|
|
|
|
} else {
|
|
|
|
quote_expr!(cx, try!(visitor.missing_field($name_str)))
|
|
|
|
};
|
|
|
|
|
|
|
|
quote_stmt!(cx,
|
|
|
|
let $field_name = match $field_name {
|
|
|
|
Some($field_name) => $field_name,
|
|
|
|
None => $missing_expr,
|
|
|
|
};
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let result = builder.expr().struct_path(struct_path)
|
|
|
|
.with_id_exprs(
|
|
|
|
struct_def.fields.iter()
|
|
|
|
.zip(field_names.iter())
|
|
|
|
.map(|(field, field_name)| {
|
|
|
|
(
|
|
|
|
match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => name.clone(),
|
|
|
|
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
|
|
|
},
|
|
|
|
builder.expr().id(field_name),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$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)
|
|
|
|
})
|
|
|
|
}
|