2015-05-18 01:14:38 -05:00
|
|
|
use aster;
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
use syntax::ast::{
|
|
|
|
Ident,
|
|
|
|
MetaItem,
|
|
|
|
Item,
|
|
|
|
};
|
|
|
|
use syntax::ast;
|
|
|
|
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;
|
|
|
|
|
2016-02-05 20:11:58 -06:00
|
|
|
use attr;
|
|
|
|
use error::Error;
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
pub fn expand_derive_serialize(
|
|
|
|
cx: &mut ExtCtxt,
|
2015-03-14 16:57:05 -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(Serialize)]` may only be applied to structs and enums");
|
2015-05-18 01:14:38 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
let builder = aster::AstBuilder::new().span(span);
|
|
|
|
|
|
|
|
let generics = match item.node {
|
|
|
|
ast::ItemStruct(_, ref generics) => generics,
|
|
|
|
ast::ItemEnum(_, ref generics) => generics,
|
2016-01-16 16:51:11 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
|
|
|
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
};
|
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
let impl_generics = builder.from_generics(generics.clone())
|
|
|
|
.add_ty_param_bound(
|
|
|
|
builder.path().global().ids(&["serde", "ser", "Serialize"]).build()
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2015-03-15 19:47:25 -05:00
|
|
|
let ty = builder.ty().path()
|
2015-03-14 16:57:05 -05:00
|
|
|
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let body = match serialize_body(cx, &builder, &item, &impl_generics, ty.clone()) {
|
|
|
|
Ok(body) => body,
|
2016-02-05 20:11:58 -06:00
|
|
|
Err(Error) => {
|
2016-01-18 14:39:46 -06:00
|
|
|
// An error occured, but it should have been reported already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2015-03-14 16:57:05 -05:00
|
|
|
|
|
|
|
let where_clause = &impl_generics.where_clause;
|
|
|
|
|
|
|
|
let impl_item = quote_item!(cx,
|
2015-03-15 19:47:25 -05:00
|
|
|
impl $impl_generics ::serde::ser::Serialize for $ty $where_clause {
|
2015-03-20 06:01:06 -05:00
|
|
|
fn serialize<__S>(&self, serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
2015-03-15 15:02:34 -05:00
|
|
|
where __S: ::serde::ser::Serializer,
|
2015-03-14 16:57:05 -05:00
|
|
|
{
|
|
|
|
$body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap();
|
|
|
|
|
2015-05-18 01:14:38 -05:00
|
|
|
push(Annotatable::Item(impl_item))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
fn serialize_body(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
2015-03-14 16:57:05 -05:00
|
|
|
builder: &aster::AstBuilder,
|
2015-03-14 15:09:37 -05:00
|
|
|
item: &Item,
|
2015-03-14 16:57:05 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty: P<ast::Ty>,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2016-01-18 14:39:46 -06:00
|
|
|
// 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.
|
2016-02-05 20:11:58 -06:00
|
|
|
let _ = try!(attr::get_container_attrs(cx, item));
|
2016-01-18 14:39:46 -06:00
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
match item.node {
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::ItemStruct(ref variant_data, _) => {
|
2015-03-14 16:57:05 -05:00
|
|
|
serialize_item_struct(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
item,
|
|
|
|
impl_generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty,
|
2016-01-16 16:51:11 -06:00
|
|
|
item.span,
|
2015-10-17 21:44:07 -05:00
|
|
|
variant_data,
|
2015-03-14 16:57:05 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
ast::ItemEnum(ref enum_def, _) => {
|
|
|
|
serialize_item_enum(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty,
|
2015-03-14 16:57:05 -05:00
|
|
|
enum_def,
|
|
|
|
)
|
|
|
|
}
|
2016-01-16 16:51:11 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_bug(item.span,
|
2016-01-18 14:39:46 -06:00
|
|
|
"expected ItemStruct or ItemEnum in #[derive(Serialize)]");
|
2016-01-16 16:51:11 -06:00
|
|
|
}
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
fn serialize_item_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
item: &Item,
|
|
|
|
impl_generics: &ast::Generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
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-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2015-10-17 21:44:07 -05:00
|
|
|
match *variant_data {
|
|
|
|
ast::VariantData::Unit(_) => {
|
2015-03-14 16:57:05 -05:00
|
|
|
serialize_unit_struct(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
&builder,
|
2015-03-14 16:57:05 -05:00
|
|
|
item.ident,
|
2015-03-14 15:09:37 -05:00
|
|
|
)
|
|
|
|
}
|
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
|
|
|
serialize_newtype_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
)
|
|
|
|
}
|
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-14 16:57:05 -05:00
|
|
|
serialize_tuple_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.len(),
|
2015-03-14 16:57:05 -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-14 16:57:05 -05:00
|
|
|
serialize_struct(
|
|
|
|
cx,
|
|
|
|
&builder,
|
|
|
|
item.ident,
|
|
|
|
impl_generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2015-03-14 16:57:05 -05:00
|
|
|
)
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2015-03-14 15:09:37 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx,
|
2016-01-18 14:45:39 -06:00
|
|
|
serializer.serialize_unit_struct($type_name)
|
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 serialize_newtype_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2015-07-30 11:38:09 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx,
|
2016-01-18 14:45:39 -06:00
|
|
|
serializer.serialize_newtype_struct($type_name, &self.0)
|
2016-01-18 14:39:46 -06:00
|
|
|
))
|
2015-07-30 11:38:09 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2015-03-14 16:57:05 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty: P<ast::Ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
fields: usize,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2015-03-14 15:09:37 -05:00
|
|
|
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty.clone(),
|
2015-07-23 09:25:27 -05:00
|
|
|
builder.ty()
|
|
|
|
.ref_()
|
|
|
|
.lifetime("'__a")
|
|
|
|
.build_ty(ty.clone()),
|
2015-03-14 15:09:37 -05:00
|
|
|
fields,
|
2015-03-14 16:57:05 -05:00
|
|
|
impl_generics,
|
2015-03-14 15:09:37 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
$visitor_struct
|
|
|
|
$visitor_impl
|
2015-10-08 05:41:29 -05:00
|
|
|
serializer.serialize_tuple_struct($type_name, Visitor {
|
2015-03-14 15:09:37 -05:00
|
|
|
value: self,
|
|
|
|
state: 0,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
2015-03-14 16:57:05 -05:00
|
|
|
impl_generics: &ast::Generics,
|
2015-03-15 19:47:25 -05:00
|
|
|
ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2016-01-18 14:39:46 -06:00
|
|
|
let value_exprs = fields.iter().map(|field| {
|
|
|
|
let name = field.node.ident().expect("struct has unnamed field");
|
|
|
|
quote_expr!(cx, &self.value.$name)
|
|
|
|
});
|
|
|
|
|
|
|
|
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty.clone(),
|
2015-07-23 09:25:27 -05:00
|
|
|
builder.ty()
|
|
|
|
.ref_()
|
|
|
|
.lifetime("'__a")
|
|
|
|
.build_ty(ty.clone()),
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2015-03-14 16:57:05 -05:00
|
|
|
impl_generics,
|
2016-01-18 14:39:46 -06:00
|
|
|
value_exprs,
|
|
|
|
));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
|
|
|
let type_name = builder.expr().str(type_ident);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx, {
|
2015-03-14 15:09:37 -05:00
|
|
|
$visitor_struct
|
|
|
|
$visitor_impl
|
2015-10-08 05:41:29 -05:00
|
|
|
serializer.serialize_struct($type_name, Visitor {
|
2015-03-14 15:09:37 -05:00
|
|
|
value: self,
|
|
|
|
state: 0,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 16:57:05 -05:00
|
|
|
fn serialize_item_enum(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
impl_generics: &ast::Generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty: P<ast::Ty>,
|
2015-03-14 16:57:05 -05:00
|
|
|
enum_def: &ast::EnumDef,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2016-01-18 14:39:46 -06:00
|
|
|
let mut arms = vec![];
|
|
|
|
|
|
|
|
for (variant_index, variant) in enum_def.variants.iter().enumerate() {
|
|
|
|
let arm = try!(serialize_variant(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_ident,
|
|
|
|
impl_generics,
|
|
|
|
ty.clone(),
|
|
|
|
variant,
|
|
|
|
variant_index,
|
|
|
|
));
|
|
|
|
|
|
|
|
arms.push(arm);
|
|
|
|
}
|
2015-03-14 16:57:05 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_expr!(cx,
|
2015-03-14 16:57:05 -05:00
|
|
|
match *self {
|
|
|
|
$arms
|
|
|
|
}
|
2016-01-18 14:39:46 -06:00
|
|
|
))
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_variant(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_ident: Ident,
|
|
|
|
generics: &ast::Generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty: P<ast::Ty>,
|
2015-03-14 16:57:05 -05:00
|
|
|
variant: &ast::Variant,
|
2015-07-19 12:12:42 -05:00
|
|
|
variant_index: usize,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<ast::Arm, Error> {
|
2015-03-14 15:09:37 -05:00
|
|
|
let type_name = builder.expr().str(type_ident);
|
2015-03-14 16:57:05 -05:00
|
|
|
let variant_ident = variant.node.name;
|
|
|
|
let variant_name = builder.expr().str(variant_ident);
|
|
|
|
|
2015-11-01 15:24:26 -06:00
|
|
|
match variant.node.data {
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Unit(_) => {
|
2015-03-14 16:57:05 -05:00
|
|
|
let pat = builder.pat().enum_()
|
|
|
|
.id(type_ident).id(variant_ident).build()
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_arm!(cx,
|
2015-03-14 16:57:05 -05:00
|
|
|
$pat => {
|
2015-10-08 05:41:29 -05:00
|
|
|
::serde::ser::Serializer::serialize_unit_variant(
|
2015-03-14 16:57:05 -05:00
|
|
|
serializer,
|
|
|
|
$type_name,
|
2015-07-19 12:12:42 -05:00
|
|
|
$variant_index,
|
2015-03-14 16:57:05 -05:00
|
|
|
$variant_name,
|
|
|
|
)
|
2015-05-14 16:28:44 -05:00
|
|
|
}
|
2016-01-18 14:39:46 -06:00
|
|
|
))
|
2015-07-17 08:19:11 -05:00
|
|
|
},
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
2015-07-17 08:19:11 -05:00
|
|
|
let field = builder.id("__simple_value");
|
|
|
|
let field = builder.pat().ref_id(field);
|
|
|
|
let pat = builder.pat().enum_()
|
|
|
|
.id(type_ident).id(variant_ident).build()
|
|
|
|
.with_pats(Some(field).into_iter())
|
|
|
|
.build();
|
2016-01-18 14:39:46 -06:00
|
|
|
|
|
|
|
Ok(quote_arm!(cx,
|
2015-07-17 08:19:11 -05:00
|
|
|
$pat => {
|
2015-10-08 05:41:29 -05:00
|
|
|
::serde::ser::Serializer::serialize_newtype_variant(
|
2015-07-17 08:19:11 -05:00
|
|
|
serializer,
|
|
|
|
$type_name,
|
2015-07-29 16:05:51 -05:00
|
|
|
$variant_index,
|
2015-07-17 08:19:11 -05:00
|
|
|
$variant_name,
|
|
|
|
__simple_value,
|
|
|
|
)
|
|
|
|
}
|
2016-01-18 14:39:46 -06:00
|
|
|
))
|
2015-07-17 08:19:11 -05:00
|
|
|
},
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Tuple(ref fields, _) => {
|
|
|
|
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
2015-03-14 16:57:05 -05:00
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let pat = builder.pat().enum_()
|
|
|
|
.id(type_ident).id(variant_ident).build()
|
2015-10-17 21:44:07 -05:00
|
|
|
.with_pats(
|
|
|
|
field_names.iter()
|
|
|
|
.map(|field| builder.pat().ref_id(field))
|
|
|
|
)
|
2015-03-14 16:57:05 -05:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let expr = serialize_tuple_variant(
|
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_name,
|
2015-07-19 12:12:42 -05:00
|
|
|
variant_index,
|
2015-03-14 16:57:05 -05:00
|
|
|
variant_name,
|
|
|
|
generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty,
|
2015-03-14 16:57:05 -05:00
|
|
|
fields,
|
2015-10-17 21:44:07 -05:00
|
|
|
field_names,
|
2015-03-14 16:57:05 -05:00
|
|
|
);
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_arm!(cx,
|
|
|
|
$pat => { $expr }
|
|
|
|
))
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
2015-10-17 21:44:07 -05:00
|
|
|
ast::VariantData::Struct(ref fields, _) => {
|
|
|
|
let field_names: Vec<_> = (0 .. fields.len())
|
2015-03-14 16:57:05 -05:00
|
|
|
.map(|i| builder.id(format!("__field{}", i)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let pat = builder.pat().struct_()
|
|
|
|
.id(type_ident).id(variant_ident).build()
|
|
|
|
.with_pats(
|
2015-10-17 21:44:07 -05:00
|
|
|
field_names.iter()
|
|
|
|
.zip(fields.iter())
|
2015-03-14 16:57:05 -05:00
|
|
|
.map(|(id, field)| {
|
|
|
|
let name = match field.node.kind {
|
|
|
|
ast::NamedField(name, _) => name,
|
|
|
|
ast::UnnamedField(_) => {
|
2016-01-16 16:51:11 -06:00
|
|
|
cx.span_bug(field.span, "struct variant has unnamed fields")
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(name, builder.pat().ref_id(id))
|
|
|
|
})
|
2015-03-14 15:09:37 -05:00
|
|
|
)
|
2015-03-14 16:57:05 -05:00
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let expr = try!(serialize_struct_variant(
|
2015-03-14 16:57:05 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
|
|
|
type_name,
|
2015-07-19 12:12:42 -05:00
|
|
|
variant_index,
|
2015-03-14 16:57:05 -05:00
|
|
|
variant_name,
|
|
|
|
generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
ty,
|
2015-03-14 16:57:05 -05:00
|
|
|
fields,
|
2015-10-17 21:44:07 -05:00
|
|
|
field_names,
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
2015-03-14 16:57:05 -05:00
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok(quote_arm!(cx,
|
|
|
|
$pat => { $expr }
|
|
|
|
))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_variant(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_name: P<ast::Expr>,
|
2015-07-19 12:12:42 -05:00
|
|
|
variant_index: usize,
|
2015-03-14 15:09:37 -05:00
|
|
|
variant_name: P<ast::Expr>,
|
|
|
|
generics: &ast::Generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
structure_ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
field_names: Vec<Ident>,
|
2015-03-14 16:57:05 -05:00
|
|
|
) -> P<ast::Expr> {
|
2015-06-18 10:45:03 -05:00
|
|
|
let variant_ty = builder.ty().tuple()
|
2015-03-14 15:09:37 -05:00
|
|
|
.with_tys(
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.iter().map(|field| {
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.ty()
|
|
|
|
.ref_()
|
|
|
|
.lifetime("'__a")
|
2015-10-17 21:44:07 -05:00
|
|
|
.build_ty(field.node.ty.clone())
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2015-06-18 10:45:03 -05:00
|
|
|
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
|
|
|
cx,
|
|
|
|
builder,
|
2015-07-23 09:25:27 -05:00
|
|
|
structure_ty.clone(),
|
2015-06-18 10:45:03 -05:00
|
|
|
variant_ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.len(),
|
2015-06-18 10:45:03 -05:00
|
|
|
generics,
|
|
|
|
);
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
let value_expr = builder.expr().tuple()
|
|
|
|
.with_exprs(
|
2015-10-17 21:44:07 -05:00
|
|
|
field_names.iter().map(|field| {
|
2015-07-23 09:25:27 -05:00
|
|
|
builder.expr().id(field)
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
quote_expr!(cx, {
|
|
|
|
$visitor_struct
|
|
|
|
$visitor_impl
|
2015-10-08 05:41:29 -05:00
|
|
|
serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
2015-03-14 15:09:37 -05:00
|
|
|
value: $value_expr,
|
|
|
|
state: 0,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_variant(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
|
|
|
type_name: P<ast::Expr>,
|
2015-07-19 12:12:42 -05:00
|
|
|
variant_index: usize,
|
2015-03-14 15:09:37 -05:00
|
|
|
variant_name: P<ast::Expr>,
|
|
|
|
generics: &ast::Generics,
|
2015-06-18 10:45:03 -05:00
|
|
|
structure_ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
field_names: Vec<Ident>,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<P<ast::Expr>, Error> {
|
2015-03-14 15:09:37 -05:00
|
|
|
let value_ty = builder.ty().tuple()
|
|
|
|
.with_tys(
|
2015-10-17 21:44:07 -05:00
|
|
|
fields.iter().map(|field| {
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.ty()
|
|
|
|
.ref_()
|
|
|
|
.lifetime("'__a")
|
|
|
|
.build_ty(field.node.ty.clone())
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let value_expr = builder.expr().tuple()
|
|
|
|
.with_exprs(
|
2015-10-17 21:44:07 -05:00
|
|
|
field_names.iter().map(|field| {
|
2015-07-23 09:25:27 -05:00
|
|
|
builder.expr().id(field)
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
2015-03-14 15:09:37 -05:00
|
|
|
cx,
|
|
|
|
builder,
|
2015-07-23 09:25:27 -05:00
|
|
|
structure_ty.clone(),
|
2015-03-14 15:09:37 -05:00
|
|
|
value_ty,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields,
|
2015-03-14 15:09:37 -05:00
|
|
|
generics,
|
2015-10-17 21:44:07 -05:00
|
|
|
(0 .. field_names.len()).map(|i| {
|
2015-03-14 15:09:37 -05:00
|
|
|
builder.expr()
|
|
|
|
.tup_field(i)
|
|
|
|
.field("value").self_()
|
|
|
|
})
|
2016-01-18 14:39:46 -06:00
|
|
|
));
|
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_struct
|
|
|
|
$visitor_impl
|
2015-10-08 05:41:29 -05:00
|
|
|
serializer.serialize_struct_variant($type_name, $variant_index, $variant_name, Visitor {
|
2015-03-14 15:09:37 -05:00
|
|
|
value: $value_expr,
|
|
|
|
state: 0,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
})
|
2016-01-18 14:39:46 -06:00
|
|
|
}))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_struct_visitor(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-06-18 10:45:03 -05:00
|
|
|
structure_ty: P<ast::Ty>,
|
|
|
|
variant_ty: P<ast::Ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
fields: usize,
|
|
|
|
generics: &ast::Generics
|
|
|
|
) -> (P<ast::Item>, P<ast::Item>) {
|
|
|
|
let arms: Vec<ast::Arm> = (0 .. fields)
|
|
|
|
.map(|i| {
|
|
|
|
let expr = builder.expr()
|
|
|
|
.tup_field(i)
|
|
|
|
.field("value").self_();
|
|
|
|
|
|
|
|
quote_arm!(cx,
|
|
|
|
$i => {
|
|
|
|
self.state += 1;
|
2015-10-08 05:41:29 -05:00
|
|
|
let v = try!(serializer.serialize_tuple_struct_elt(&$expr));
|
2015-03-14 15:09:37 -05:00
|
|
|
Ok(Some(v))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let visitor_impl_generics = builder.from_generics(generics.clone())
|
|
|
|
.add_lifetime_bound("'__a")
|
|
|
|
.lifetime_name("'__a")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let where_clause = &visitor_impl_generics.where_clause;
|
|
|
|
|
|
|
|
let visitor_generics = builder.from_generics(visitor_impl_generics.clone())
|
|
|
|
.strip_bounds()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
(
|
|
|
|
quote_item!(cx,
|
|
|
|
struct Visitor $visitor_impl_generics $where_clause {
|
|
|
|
state: usize,
|
2015-06-18 10:45:03 -05:00
|
|
|
value: $variant_ty,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData<&'__a $structure_ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
|
|
|
|
quote_item!(cx,
|
|
|
|
impl $visitor_impl_generics ::serde::ser::SeqVisitor
|
|
|
|
for Visitor $visitor_generics
|
|
|
|
$where_clause {
|
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
2015-05-14 16:28:44 -05:00
|
|
|
where S: ::serde::ser::Serializer
|
2015-03-14 15:09:37 -05:00
|
|
|
{
|
|
|
|
match self.state {
|
|
|
|
$arms
|
2015-05-14 16:28:44 -05:00
|
|
|
_ => Ok(None)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> Option<usize> {
|
|
|
|
Some($fields)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_visitor<I>(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
builder: &aster::AstBuilder,
|
2015-06-18 10:45:03 -05:00
|
|
|
structure_ty: P<ast::Ty>,
|
|
|
|
variant_ty: P<ast::Ty>,
|
2015-10-17 21:44:07 -05:00
|
|
|
fields: &[ast::StructField],
|
2015-03-14 15:09:37 -05:00
|
|
|
generics: &ast::Generics,
|
|
|
|
value_exprs: I,
|
2016-02-05 20:11:58 -06:00
|
|
|
) -> Result<(P<ast::Item>, P<ast::Item>), Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where I: Iterator<Item=P<ast::Expr>>,
|
|
|
|
{
|
2015-09-07 18:44:56 -05:00
|
|
|
let value_exprs = value_exprs.collect::<Vec<_>>();
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-02-05 20:11:58 -06:00
|
|
|
let field_attrs = try!(attr::get_struct_field_attrs(cx, builder, fields));
|
2015-08-27 22:01:09 -05:00
|
|
|
|
2015-09-07 18:44:56 -05:00
|
|
|
let arms: Vec<ast::Arm> = field_attrs.iter()
|
|
|
|
.zip(value_exprs.iter())
|
2015-07-23 10:07:49 -05:00
|
|
|
.filter(|&(ref field, _)| !field.skip_serializing_field())
|
2015-03-14 15:09:37 -05:00
|
|
|
.enumerate()
|
2015-09-07 18:44:56 -05:00
|
|
|
.map(|(i, (ref field, value_expr))| {
|
2015-05-01 11:53:59 -05:00
|
|
|
let key_expr = field.serializer_key_expr(cx);
|
2015-09-07 18:44:56 -05:00
|
|
|
|
|
|
|
let stmt = if field.skip_serializing_field_if_empty() {
|
2015-10-12 04:04:50 -05:00
|
|
|
quote_stmt!(cx, if ($value_expr).is_empty() { continue; })
|
2015-09-07 18:44:56 -05:00
|
|
|
} else if field.skip_serializing_field_if_none() {
|
2015-10-12 04:04:50 -05:00
|
|
|
quote_stmt!(cx, if ($value_expr).is_none() { continue; })
|
2015-09-07 18:44:56 -05:00
|
|
|
} else {
|
|
|
|
quote_stmt!(cx, {})
|
|
|
|
};
|
|
|
|
|
2015-03-14 15:09:37 -05:00
|
|
|
quote_arm!(cx,
|
|
|
|
$i => {
|
|
|
|
self.state += 1;
|
2015-09-07 18:44:56 -05:00
|
|
|
$stmt
|
|
|
|
|
|
|
|
return Ok(
|
2015-03-14 15:09:37 -05:00
|
|
|
Some(
|
|
|
|
try!(
|
2015-10-08 05:41:29 -05:00
|
|
|
serializer.serialize_struct_elt(
|
2015-03-14 15:09:37 -05:00
|
|
|
$key_expr,
|
|
|
|
$value_expr,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2015-09-07 18:44:56 -05:00
|
|
|
);
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let visitor_impl_generics = builder.from_generics(generics.clone())
|
|
|
|
.add_lifetime_bound("'__a")
|
|
|
|
.lifetime_name("'__a")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let where_clause = &visitor_impl_generics.where_clause;
|
|
|
|
|
|
|
|
let visitor_generics = builder.from_generics(visitor_impl_generics.clone())
|
|
|
|
.strip_bounds()
|
|
|
|
.build();
|
|
|
|
|
2015-09-07 18:44:56 -05:00
|
|
|
let len = field_attrs.iter()
|
|
|
|
.zip(value_exprs.iter())
|
|
|
|
.map(|(field, value_expr)| {
|
|
|
|
if field.skip_serializing_field() {
|
|
|
|
quote_expr!(cx, 0)
|
|
|
|
} else if field.skip_serializing_field_if_empty() {
|
2015-10-12 04:04:50 -05:00
|
|
|
quote_expr!(cx, if ($value_expr).is_empty() { 0 } else { 1 })
|
2015-09-07 18:44:56 -05:00
|
|
|
} else if field.skip_serializing_field_if_none() {
|
2015-10-12 04:04:50 -05:00
|
|
|
quote_expr!(cx, if ($value_expr).is_none() { 0 } else { 1 })
|
2015-09-07 18:44:56 -05:00
|
|
|
} else {
|
|
|
|
quote_expr!(cx, 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
|
|
|
|
2016-01-18 14:39:46 -06:00
|
|
|
Ok((
|
2015-03-14 15:09:37 -05:00
|
|
|
quote_item!(cx,
|
|
|
|
struct Visitor $visitor_impl_generics $where_clause {
|
|
|
|
state: usize,
|
2015-06-18 10:45:03 -05:00
|
|
|
value: $variant_ty,
|
2015-07-23 09:25:27 -05:00
|
|
|
_structure_ty: ::std::marker::PhantomData<&'__a $structure_ty>,
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
).unwrap(),
|
|
|
|
|
|
|
|
quote_item!(cx,
|
|
|
|
impl $visitor_impl_generics
|
|
|
|
::serde::ser::MapVisitor
|
|
|
|
for Visitor $visitor_generics
|
|
|
|
$where_clause {
|
|
|
|
#[inline]
|
2015-03-20 06:01:06 -05:00
|
|
|
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
2015-03-14 15:09:37 -05:00
|
|
|
where S: ::serde::ser::Serializer,
|
|
|
|
{
|
2015-09-07 18:44:56 -05:00
|
|
|
loop {
|
|
|
|
match self.state {
|
|
|
|
$arms
|
|
|
|
_ => { return Ok(None); }
|
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> Option<usize> {
|
|
|
|
Some($len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).unwrap(),
|
2016-01-18 14:39:46 -06:00
|
|
|
))
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|