Gather attrs at the beginning
This commit is contained in:
parent
28589620f6
commit
ac69524258
@ -119,7 +119,7 @@ pub struct ContainerAttrs {
|
|||||||
|
|
||||||
impl ContainerAttrs {
|
impl ContainerAttrs {
|
||||||
/// Extract out the `#[serde(...)]` attributes from an item.
|
/// Extract out the `#[serde(...)]` attributes from an item.
|
||||||
pub fn from_item(cx: &ExtCtxt, item: &ast::Item) -> Result<Self, Error> {
|
pub fn from_item(cx: &ExtCtxt, item: &ast::Item) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
let mut deny_unknown_fields = BoolAttr::none(cx, "deny_unknown_fields");
|
let mut deny_unknown_fields = BoolAttr::none(cx, "deny_unknown_fields");
|
||||||
@ -132,17 +132,19 @@ impl ContainerAttrs {
|
|||||||
match meta_item.node {
|
match meta_item.node {
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
||||||
let s = try!(get_str_from_lit(cx, name, lit));
|
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(span, s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(span, s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
||||||
let (ser, de) = try!(get_renames(cx, meta_items));
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deny_unknown_fields)]`
|
// Parse `#[serde(deny_unknown_fields)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"deny_unknown_fields" => {
|
ast::MetaItemKind::Word(ref name) if name == &"deny_unknown_fields" => {
|
||||||
@ -151,31 +153,31 @@ impl ContainerAttrs {
|
|||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
||||||
let where_predicates = try!(parse_lit_into_where(cx, name, lit));
|
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
|
||||||
ser_bound.set(span, where_predicates.clone());
|
ser_bound.set(span, where_predicates.clone());
|
||||||
de_bound.set(span, where_predicates);
|
de_bound.set(span, where_predicates);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
||||||
let (ser, de) = try!(get_where_predicates(cx, meta_items));
|
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
||||||
ser_bound.set_opt(ser);
|
ser_bound.set_opt(ser);
|
||||||
de_bound.set_opt(de);
|
de_bound.set_opt(de);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.span_err(
|
||||||
meta_item.span,
|
meta_item.span,
|
||||||
&format!("unknown serde container attribute `{}`",
|
&format!("unknown serde container attribute `{}`",
|
||||||
meta_item_to_string(meta_item)));
|
meta_item_to_string(meta_item)));
|
||||||
|
|
||||||
return Err(Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ContainerAttrs {
|
ContainerAttrs {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: item.ident,
|
ident: item.ident,
|
||||||
serialize_name: ser_name.get(),
|
serialize_name: ser_name.get(),
|
||||||
@ -184,7 +186,7 @@ impl ContainerAttrs {
|
|||||||
deny_unknown_fields: deny_unknown_fields.get(),
|
deny_unknown_fields: deny_unknown_fields.get(),
|
||||||
ser_bound: ser_bound.get(),
|
ser_bound: ser_bound.get(),
|
||||||
de_bound: de_bound.get(),
|
de_bound: de_bound.get(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &Name {
|
||||||
@ -211,7 +213,7 @@ pub struct VariantAttrs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl VariantAttrs {
|
impl VariantAttrs {
|
||||||
pub fn from_variant(cx: &ExtCtxt, variant: &ast::Variant) -> Result<Self, Error> {
|
pub fn from_variant(cx: &ExtCtxt, variant: &ast::Variant) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
|
|
||||||
@ -221,37 +223,37 @@ impl VariantAttrs {
|
|||||||
match meta_item.node {
|
match meta_item.node {
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
||||||
let s = try!(get_str_from_lit(cx, name, lit));
|
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(span, s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(span, s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
||||||
let (ser, de) = try!(get_renames(cx, meta_items));
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.span_err(
|
||||||
meta_item.span,
|
meta_item.span,
|
||||||
&format!("unknown serde variant attribute `{}`",
|
&format!("unknown serde variant attribute `{}`",
|
||||||
meta_item_to_string(meta_item)));
|
meta_item_to_string(meta_item)));
|
||||||
|
|
||||||
return Err(Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(VariantAttrs {
|
VariantAttrs {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: variant.node.name,
|
ident: variant.node.name,
|
||||||
serialize_name: ser_name.get(),
|
serialize_name: ser_name.get(),
|
||||||
deserialize_name: de_name.get(),
|
deserialize_name: de_name.get(),
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &Name {
|
||||||
@ -288,7 +290,7 @@ impl FieldAttrs {
|
|||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
||||||
pub fn from_field(cx: &ExtCtxt,
|
pub fn from_field(cx: &ExtCtxt,
|
||||||
index: usize,
|
index: usize,
|
||||||
field: &ast::StructField) -> Result<Self, Error> {
|
field: &ast::StructField) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
|
let mut skip_serializing = BoolAttr::none(cx, "skip_serializing");
|
||||||
@ -311,17 +313,19 @@ impl FieldAttrs {
|
|||||||
match meta_item.node {
|
match meta_item.node {
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
||||||
let s = try!(get_str_from_lit(cx, name, lit));
|
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(span, s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(span, s);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
||||||
let (ser, de) = try!(get_renames(cx, meta_items));
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(default)]`
|
// Parse `#[serde(default)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
||||||
@ -330,9 +334,10 @@ impl FieldAttrs {
|
|||||||
|
|
||||||
// Parse `#[serde(default="...")]`
|
// Parse `#[serde(default="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
|
||||||
let path = try!(parse_lit_into_path(cx, name, lit));
|
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
||||||
default.set(span, FieldDefault::Path(path));
|
default.set(span, FieldDefault::Path(path));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(skip_serializing)]`
|
// Parse `#[serde(skip_serializing)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
||||||
@ -346,43 +351,46 @@ impl FieldAttrs {
|
|||||||
|
|
||||||
// Parse `#[serde(skip_serializing_if="...")]`
|
// Parse `#[serde(skip_serializing_if="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
||||||
let path = try!(parse_lit_into_path(cx, name, lit));
|
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
||||||
skip_serializing_if.set(span, path);
|
skip_serializing_if.set(span, path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(serialize_with="...")]`
|
// Parse `#[serde(serialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
||||||
let path = try!(parse_lit_into_path(cx, name, lit));
|
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
||||||
serialize_with.set(span, path);
|
serialize_with.set(span, path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deserialize_with="...")]`
|
// Parse `#[serde(deserialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
||||||
let path = try!(parse_lit_into_path(cx, name, lit));
|
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
||||||
deserialize_with.set(span, path);
|
deserialize_with.set(span, path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
||||||
let where_predicates = try!(parse_lit_into_where(cx, name, lit));
|
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
|
||||||
ser_bound.set(span, where_predicates.clone());
|
ser_bound.set(span, where_predicates.clone());
|
||||||
de_bound.set(span, where_predicates);
|
de_bound.set(span, where_predicates);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
||||||
let (ser, de) = try!(get_where_predicates(cx, meta_items));
|
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
||||||
ser_bound.set_opt(ser);
|
ser_bound.set_opt(ser);
|
||||||
de_bound.set_opt(de);
|
de_bound.set_opt(de);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.span_err(
|
||||||
meta_item.span,
|
meta_item.span,
|
||||||
&format!("unknown serde field attribute `{}`",
|
&format!("unknown serde field attribute `{}`",
|
||||||
meta_item_to_string(meta_item)));
|
meta_item_to_string(meta_item)));
|
||||||
|
|
||||||
return Err(Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -394,7 +402,7 @@ impl FieldAttrs {
|
|||||||
default.set_if_none(span, FieldDefault::Default);
|
default.set_if_none(span, FieldDefault::Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(FieldAttrs {
|
FieldAttrs {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: field_ident,
|
ident: field_ident,
|
||||||
serialize_name: ser_name.get(),
|
serialize_name: ser_name.get(),
|
||||||
@ -408,7 +416,7 @@ impl FieldAttrs {
|
|||||||
deserialize_with: deserialize_with.get(),
|
deserialize_with: deserialize_with.get(),
|
||||||
ser_bound: ser_bound.get(),
|
ser_bound: ser_bound.get(),
|
||||||
de_bound: de_bound.get(),
|
de_bound: de_bound.get(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &Name {
|
||||||
@ -448,21 +456,6 @@ impl FieldAttrs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Zip together fields and `#[serde(...)]` attributes on those fields.
|
|
||||||
pub fn fields_with_attrs(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
fields: &[ast::StructField],
|
|
||||||
) -> Result<Vec<(ast::StructField, FieldAttrs)>, Error> {
|
|
||||||
fields.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, field)| {
|
|
||||||
let attrs = try!(FieldAttrs::from_field(cx, i, field));
|
|
||||||
Ok((field.clone(), attrs))
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_ser_and_de<T, F>(
|
fn get_ser_and_de<T, F>(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
attribute: &str,
|
attribute: &str,
|
||||||
|
@ -3,12 +3,11 @@ use std::collections::HashSet;
|
|||||||
use aster::AstBuilder;
|
use aster::AstBuilder;
|
||||||
|
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::ext::base::ExtCtxt;
|
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
use syntax::visit;
|
use syntax::visit;
|
||||||
|
|
||||||
use attr;
|
use attr;
|
||||||
use error::Error;
|
use item::Item;
|
||||||
|
|
||||||
// Remove the default from every type parameter because in the generated impls
|
// Remove the default from every type parameter because in the generated impls
|
||||||
// they look like associated types: "error: associated type bindings are not
|
// they look like associated types: "error: associated type bindings are not
|
||||||
@ -35,39 +34,35 @@ pub fn with_where_predicates(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_where_predicates_from_fields<F>(
|
pub fn with_where_predicates_from_fields<F>(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &AstBuilder,
|
builder: &AstBuilder,
|
||||||
item: &ast::Item,
|
item: &Item,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
from_field: F,
|
from_field: F,
|
||||||
) -> Result<ast::Generics, Error>
|
) -> ast::Generics
|
||||||
where F: Fn(&attr::FieldAttrs) -> Option<&[ast::WherePredicate]>,
|
where F: Fn(&attr::FieldAttrs) -> Option<&[ast::WherePredicate]>,
|
||||||
{
|
{
|
||||||
Ok(builder.from_generics(generics.clone())
|
builder.from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
try!(all_fields_with_attrs(cx, item))
|
item.body.all_fields()
|
||||||
.iter()
|
.flat_map(|field| from_field(&field.attrs))
|
||||||
.flat_map(|&(_, ref attrs)| from_field(attrs))
|
|
||||||
.flat_map(|predicates| predicates.to_vec()))
|
.flat_map(|predicates| predicates.to_vec()))
|
||||||
.build())
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_bound<F>(
|
pub fn with_bound<F>(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &AstBuilder,
|
builder: &AstBuilder,
|
||||||
item: &ast::Item,
|
item: &Item,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
filter: F,
|
filter: F,
|
||||||
bound: &ast::Path,
|
bound: &ast::Path,
|
||||||
) -> Result<ast::Generics, Error>
|
) -> ast::Generics
|
||||||
where F: Fn(&attr::FieldAttrs) -> bool,
|
where F: Fn(&attr::FieldAttrs) -> bool,
|
||||||
{
|
{
|
||||||
Ok(builder.from_generics(generics.clone())
|
builder.from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
try!(all_fields_with_attrs(cx, item))
|
item.body.all_fields()
|
||||||
.iter()
|
.filter(|&field| filter(&field.attrs))
|
||||||
.filter(|&&(_, ref attrs)| filter(attrs))
|
.map(|field| &field.ty)
|
||||||
.map(|&(ref field, _)| &field.ty)
|
|
||||||
// TODO this filter can be removed later, see comment on function
|
// TODO this filter can be removed later, see comment on function
|
||||||
.filter(|ty| contains_generic(ty, generics))
|
.filter(|ty| contains_generic(ty, generics))
|
||||||
.filter(|ty| !contains_recursion(ty, item.ident))
|
.filter(|ty| !contains_recursion(ty, item.ident))
|
||||||
@ -78,48 +73,7 @@ pub fn with_bound<F>(
|
|||||||
// the bound e.g. Serialize
|
// the bound e.g. Serialize
|
||||||
.bound().trait_(bound.clone()).build()
|
.bound().trait_(bound.clone()).build()
|
||||||
.build()))
|
.build()))
|
||||||
.build())
|
.build()
|
||||||
}
|
|
||||||
|
|
||||||
fn all_fields_with_attrs(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
item: &ast::Item,
|
|
||||||
) -> Result<Vec<(ast::StructField, attr::FieldAttrs)>, Error> {
|
|
||||||
let fields: Vec<ast::StructField> =
|
|
||||||
all_variants(cx, item).iter()
|
|
||||||
.flat_map(|variant_data| all_struct_fields(variant_data))
|
|
||||||
.cloned()
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
attr::fields_with_attrs(cx, &fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn all_variants<'a>(cx: &ExtCtxt, item: &'a ast::Item) -> Vec<&'a ast::VariantData> {
|
|
||||||
match item.node {
|
|
||||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
|
||||||
vec![variant_data]
|
|
||||||
}
|
|
||||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
|
||||||
enum_def.variants.iter()
|
|
||||||
.map(|variant| &variant.node.data)
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
cx.span_bug(item.span, "expected Item to be Struct or Enum");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn all_struct_fields(variant_data: &ast::VariantData) -> &[ast::StructField] {
|
|
||||||
match *variant_data {
|
|
||||||
ast::VariantData::Struct(ref fields, _) |
|
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
|
||||||
fields
|
|
||||||
}
|
|
||||||
ast::VariantData::Unit(_) => {
|
|
||||||
&[]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rust <1.7 enforces that `where` clauses involve generic type parameters. The
|
// Rust <1.7 enforces that `where` clauses involve generic type parameters. The
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
use aster;
|
use aster;
|
||||||
|
|
||||||
use syntax::ast::{
|
use syntax::ast::{self, Ident, MetaItem};
|
||||||
self,
|
|
||||||
EnumDef,
|
|
||||||
Ident,
|
|
||||||
Item,
|
|
||||||
MetaItem,
|
|
||||||
};
|
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
use syntax::parse::token::InternedString;
|
use syntax::parse::token::InternedString;
|
||||||
@ -15,6 +9,7 @@ use syntax::ptr::P;
|
|||||||
use attr;
|
use attr;
|
||||||
use bound;
|
use bound;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
use item;
|
||||||
|
|
||||||
pub fn expand_derive_deserialize(
|
pub fn expand_derive_deserialize(
|
||||||
cx: &mut ExtCtxt,
|
cx: &mut ExtCtxt,
|
||||||
@ -49,33 +44,22 @@ pub fn expand_derive_deserialize(
|
|||||||
fn deserialize_item(
|
fn deserialize_item(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &ast::Item,
|
||||||
) -> Result<P<ast::Item>, Error> {
|
) -> Result<P<ast::Item>, Error> {
|
||||||
let generics = match item.node {
|
let item = try!(item::Item::from_ast(cx, "Deserialize", item));
|
||||||
ast::ItemKind::Struct(_, ref generics) => generics,
|
try!(check_no_str(cx, &item));
|
||||||
ast::ItemKind::Enum(_, ref generics) => generics,
|
|
||||||
_ => {
|
|
||||||
cx.span_err(
|
|
||||||
item.span,
|
|
||||||
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
|
||||||
return Err(Error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let container_attrs = try!(attr::ContainerAttrs::from_item(cx, item));
|
let impl_generics = build_impl_generics(builder, &item);
|
||||||
|
|
||||||
let impl_generics = try!(build_impl_generics(cx, &builder, item, generics, &container_attrs));
|
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
let ty = builder.ty().path()
|
||||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let body = try!(deserialize_body(cx,
|
let body = deserialize_body(cx,
|
||||||
&builder,
|
builder,
|
||||||
&item,
|
&item,
|
||||||
&impl_generics,
|
&impl_generics,
|
||||||
ty.clone(),
|
ty.clone());
|
||||||
&container_attrs));
|
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@ -101,31 +85,27 @@ fn deserialize_item(
|
|||||||
// field type that will be deserialized by us, plus a bound `T: Default` for
|
// field type that will be deserialized by us, plus a bound `T: Default` for
|
||||||
// each generic field type that will be set to a default value.
|
// each generic field type that will be set to a default value.
|
||||||
fn build_impl_generics(
|
fn build_impl_generics(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &item::Item,
|
||||||
generics: &ast::Generics,
|
) -> ast::Generics {
|
||||||
container_attrs: &attr::ContainerAttrs,
|
let generics = bound::without_defaults(item.generics);
|
||||||
) -> Result<ast::Generics, Error> {
|
|
||||||
let generics = bound::without_defaults(generics);
|
|
||||||
|
|
||||||
let generics = try!(bound::with_where_predicates_from_fields(
|
let generics = bound::with_where_predicates_from_fields(
|
||||||
cx, builder, item, &generics,
|
builder, item, &generics,
|
||||||
|attrs| attrs.de_bound()));
|
|attrs| attrs.de_bound());
|
||||||
|
|
||||||
match container_attrs.de_bound() {
|
match item.attrs.de_bound() {
|
||||||
Some(predicates) => {
|
Some(predicates) => {
|
||||||
let generics = bound::with_where_predicates(builder, &generics, predicates);
|
bound::with_where_predicates(builder, &generics, predicates)
|
||||||
Ok(generics)
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let generics = try!(bound::with_bound(cx, builder, item, &generics,
|
let generics = bound::with_bound(builder, item, &generics,
|
||||||
needs_deserialize_bound,
|
needs_deserialize_bound,
|
||||||
&builder.path().ids(&["_serde", "de", "Deserialize"]).build()));
|
&builder.path().ids(&["_serde", "de", "Deserialize"]).build());
|
||||||
let generics = try!(bound::with_bound(cx, builder, item, &generics,
|
let generics = bound::with_bound(builder, item, &generics,
|
||||||
requires_default,
|
requires_default,
|
||||||
&builder.path().global().ids(&["std", "default", "Default"]).build()));
|
&builder.path().global().ids(&["std", "default", "Default"]).build());
|
||||||
Ok(generics)
|
generics
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,95 +129,60 @@ fn requires_default(attrs: &attr::FieldAttrs) -> bool {
|
|||||||
fn deserialize_body(
|
fn deserialize_body(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &item::Item,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
) -> P<ast::Expr> {
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
match item.body {
|
||||||
match item.node {
|
item::Body::Enum(ref variants) => {
|
||||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
|
||||||
deserialize_item_struct(
|
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
item,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
item.span,
|
|
||||||
variant_data,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
|
||||||
deserialize_item_enum(
|
deserialize_item_enum(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
enum_def,
|
variants,
|
||||||
container_attrs,
|
&item.attrs)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
_ => {
|
item::Body::Struct(item::Style::Struct, ref fields) => {
|
||||||
cx.span_bug(item.span,
|
|
||||||
"expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_item_struct(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
item: &Item,
|
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
span: Span,
|
|
||||||
variant_data: &ast::VariantData,
|
|
||||||
container_attrs: &attr::ContainerAttrs,
|
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
|
||||||
match *variant_data {
|
|
||||||
ast::VariantData::Unit(_) => {
|
|
||||||
deserialize_unit_struct(
|
|
||||||
cx,
|
|
||||||
item.ident,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
|
||||||
if fields.iter().any(|field| field.ident.is_some()) {
|
|
||||||
cx.span_bug(span, "tuple struct has named fields")
|
|
||||||
}
|
|
||||||
|
|
||||||
deserialize_tuple(
|
|
||||||
cx,
|
|
||||||
&builder,
|
|
||||||
item.ident,
|
|
||||||
None,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
fields,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
|
||||||
if fields.iter().any(|field| field.ident.is_none()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
cx.span_bug(span, "struct has unnamed fields")
|
cx.span_bug(item.span, "struct has unnamed fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
deserialize_struct(
|
deserialize_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
None,
|
None,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
container_attrs,
|
&item.attrs)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
item::Body::Struct(item::Style::Tuple, ref fields) |
|
||||||
|
item::Body::Struct(item::Style::Newtype, ref fields) => {
|
||||||
|
if fields.iter().any(|field| field.ident.is_some()) {
|
||||||
|
cx.span_bug(item.span, "tuple struct has named fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deserialize_tuple(
|
||||||
|
cx,
|
||||||
|
builder,
|
||||||
|
item.ident,
|
||||||
|
None,
|
||||||
|
impl_generics,
|
||||||
|
ty,
|
||||||
|
fields,
|
||||||
|
&item.attrs)
|
||||||
|
}
|
||||||
|
item::Body::Struct(item::Style::Unit, _) => {
|
||||||
|
deserialize_unit_struct(
|
||||||
|
cx,
|
||||||
|
item.ident,
|
||||||
|
&item.attrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
||||||
fn deserialize_visitor(
|
fn deserialize_visitor(
|
||||||
@ -245,14 +190,14 @@ fn deserialize_visitor(
|
|||||||
trait_generics: &ast::Generics,
|
trait_generics: &ast::Generics,
|
||||||
forward_ty_params: Vec<ast::TyParam>,
|
forward_ty_params: Vec<ast::TyParam>,
|
||||||
forward_tys: Vec<P<ast::Ty>>
|
forward_tys: Vec<P<ast::Ty>>
|
||||||
) -> Result<(P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics), Error> {
|
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics) {
|
||||||
if trait_generics.ty_params.is_empty() && forward_tys.is_empty() {
|
if trait_generics.ty_params.is_empty() && forward_tys.is_empty() {
|
||||||
Ok((
|
(
|
||||||
builder.item().tuple_struct("__Visitor").build(),
|
builder.item().tuple_struct("__Visitor").build(),
|
||||||
builder.ty().id("__Visitor"),
|
builder.ty().id("__Visitor"),
|
||||||
builder.expr().id("__Visitor"),
|
builder.expr().id("__Visitor"),
|
||||||
trait_generics.clone(),
|
trait_generics.clone(),
|
||||||
))
|
)
|
||||||
} else {
|
} else {
|
||||||
let placeholders : Vec<_> = trait_generics.ty_params.iter()
|
let placeholders : Vec<_> = trait_generics.ty_params.iter()
|
||||||
.map(|t| builder.ty().id(t.ident))
|
.map(|t| builder.ty().id(t.ident))
|
||||||
@ -262,7 +207,7 @@ fn deserialize_visitor(
|
|||||||
ty_params.extend(trait_generics.ty_params.into_vec());
|
ty_params.extend(trait_generics.ty_params.into_vec());
|
||||||
trait_generics.ty_params = P::from_vec(ty_params);
|
trait_generics.ty_params = P::from_vec(ty_params);
|
||||||
|
|
||||||
Ok((
|
(
|
||||||
builder.item().tuple_struct("__Visitor")
|
builder.item().tuple_struct("__Visitor")
|
||||||
.generics().with(trait_generics.clone()).build()
|
.generics().with(trait_generics.clone()).build()
|
||||||
.with_tys({
|
.with_tys({
|
||||||
@ -300,7 +245,7 @@ fn deserialize_visitor(
|
|||||||
})
|
})
|
||||||
.build(),
|
.build(),
|
||||||
trait_generics,
|
trait_generics,
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,10 +268,10 @@ fn deserialize_unit_struct(
|
|||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = container_attrs.name().deserialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
struct __Visitor;
|
struct __Visitor;
|
||||||
|
|
||||||
impl _serde::de::Visitor for __Visitor {
|
impl _serde::de::Visitor for __Visitor {
|
||||||
@ -349,7 +294,7 @@ fn deserialize_unit_struct(
|
|||||||
}
|
}
|
||||||
|
|
||||||
deserializer.deserialize_unit_struct($type_name, __Visitor)
|
deserializer.deserialize_unit_struct($type_name, __Visitor)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_tuple(
|
fn deserialize_tuple(
|
||||||
@ -359,17 +304,17 @@ fn deserialize_tuple(
|
|||||||
variant_ident: Option<Ident>,
|
variant_ident: Option<Ident>,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||||
builder,
|
builder,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
vec![deserializer_ty_param(builder)],
|
vec![deserializer_ty_param(builder)],
|
||||||
vec![deserializer_ty_arg(builder)],
|
vec![deserializer_ty_arg(builder)],
|
||||||
));
|
);
|
||||||
|
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let type_path = match variant_ident {
|
let type_path = match variant_ident {
|
||||||
@ -378,31 +323,29 @@ fn deserialize_tuple(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let nfields = fields.len();
|
let nfields = fields.len();
|
||||||
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
|
||||||
try!(check_no_str(cx, &fields_with_attrs));
|
|
||||||
|
|
||||||
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
||||||
Some(try!(deserialize_newtype_struct(
|
Some(deserialize_newtype_struct(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
&type_path,
|
&type_path,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
&fields_with_attrs[0],
|
&fields[0],
|
||||||
)))
|
))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_seq_expr = try!(deserialize_seq(
|
let visit_seq_expr = deserialize_seq(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
type_path,
|
type_path,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
&fields_with_attrs,
|
fields,
|
||||||
false,
|
false,
|
||||||
));
|
);
|
||||||
|
|
||||||
let dispatch = if is_enum {
|
let dispatch = if is_enum {
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
@ -417,7 +360,7 @@ fn deserialize_tuple(
|
|||||||
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_item
|
$visitor_item
|
||||||
|
|
||||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||||
@ -434,7 +377,7 @@ fn deserialize_tuple(
|
|||||||
}
|
}
|
||||||
|
|
||||||
$dispatch
|
$dispatch
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq(
|
fn deserialize_seq(
|
||||||
@ -443,20 +386,20 @@ fn deserialize_seq(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
type_path: ast::Path,
|
type_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[(ast::StructField, attr::FieldAttrs)],
|
fields: &[item::Field],
|
||||||
is_struct: bool,
|
is_struct: bool,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let let_values: Vec<_> = fields.iter()
|
let let_values: Vec<_> = fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, &(ref field, ref attrs))| {
|
.map(|(i, field)| {
|
||||||
let name = builder.id(format!("__field{}", i));
|
let name = builder.id(format!("__field{}", i));
|
||||||
if attrs.skip_deserializing() {
|
if field.attrs.skip_deserializing() {
|
||||||
let default = expr_is_missing(cx, attrs);
|
let default = expr_is_missing(cx, &field.attrs);
|
||||||
quote_stmt!(cx,
|
quote_stmt!(cx,
|
||||||
let $name = $default;
|
let $name = $default;
|
||||||
).unwrap()
|
).unwrap()
|
||||||
} else {
|
} else {
|
||||||
let visit = match attrs.deserialize_with() {
|
let visit = match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
quote_expr!(cx, try!(visitor.visit::<$field_ty>()))
|
quote_expr!(cx, try!(visitor.visit::<$field_ty>()))
|
||||||
@ -488,7 +431,7 @@ fn deserialize_seq(
|
|||||||
.with_id_exprs(
|
.with_id_exprs(
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, &(ref field, _))| {
|
.map(|(i, field)| {
|
||||||
(
|
(
|
||||||
match field.ident {
|
match field.ident {
|
||||||
Some(name) => name.clone(),
|
Some(name) => name.clone(),
|
||||||
@ -508,13 +451,13 @@ fn deserialize_seq(
|
|||||||
.build()
|
.build()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$let_values
|
$let_values
|
||||||
|
|
||||||
try!(visitor.end());
|
try!(visitor.end());
|
||||||
|
|
||||||
Ok($result)
|
Ok($result)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_newtype_struct(
|
fn deserialize_newtype_struct(
|
||||||
@ -523,10 +466,9 @@ fn deserialize_newtype_struct(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
type_path: &ast::Path,
|
type_path: &ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
field: &(ast::StructField, attr::FieldAttrs),
|
field: &item::Field,
|
||||||
) -> Result<Vec<ast::TokenTree>, Error> {
|
) -> Vec<ast::TokenTree> {
|
||||||
let &(ref field, ref attrs) = field;
|
let value = match field.attrs.deserialize_with() {
|
||||||
let value = match attrs.deserialize_with() {
|
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
@ -542,14 +484,14 @@ fn deserialize_newtype_struct(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(quote_tokens!(cx,
|
quote_tokens!(cx,
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
||||||
where __E: _serde::de::Deserializer,
|
where __E: _serde::de::Deserializer,
|
||||||
{
|
{
|
||||||
Ok($type_path($value))
|
Ok($type_path($value))
|
||||||
}
|
}
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_struct(
|
fn deserialize_struct(
|
||||||
@ -559,45 +501,42 @@ fn deserialize_struct(
|
|||||||
variant_ident: Option<Ident>,
|
variant_ident: Option<Ident>,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||||
builder,
|
builder,
|
||||||
&impl_generics,
|
&impl_generics,
|
||||||
vec![deserializer_ty_param(builder)],
|
vec![deserializer_ty_param(builder)],
|
||||||
vec![deserializer_ty_arg(builder)],
|
vec![deserializer_ty_arg(builder)],
|
||||||
));
|
);
|
||||||
|
|
||||||
let type_path = match variant_ident {
|
let type_path = match variant_ident {
|
||||||
Some(variant_ident) => builder.path().id(type_ident).id(variant_ident).build(),
|
Some(variant_ident) => builder.path().id(type_ident).id(variant_ident).build(),
|
||||||
None => builder.path().id(type_ident).build(),
|
None => builder.path().id(type_ident).build(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
let visit_seq_expr = deserialize_seq(
|
||||||
try!(check_no_str(cx, &fields_with_attrs));
|
|
||||||
|
|
||||||
let visit_seq_expr = try!(deserialize_seq(
|
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
impl_generics,
|
impl_generics,
|
||||||
&fields_with_attrs,
|
fields,
|
||||||
true,
|
true,
|
||||||
));
|
);
|
||||||
|
|
||||||
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
impl_generics,
|
impl_generics,
|
||||||
&fields_with_attrs,
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
));
|
);
|
||||||
|
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
let dispatch = if is_enum {
|
let dispatch = if is_enum {
|
||||||
@ -609,7 +548,7 @@ fn deserialize_struct(
|
|||||||
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$field_visitor
|
$field_visitor
|
||||||
|
|
||||||
$visitor_item
|
$visitor_item
|
||||||
@ -635,7 +574,7 @@ fn deserialize_struct(
|
|||||||
$fields_stmt
|
$fields_stmt
|
||||||
|
|
||||||
$dispatch
|
$dispatch
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_item_enum(
|
fn deserialize_item_enum(
|
||||||
@ -644,9 +583,9 @@ fn deserialize_item_enum(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
enum_def: &EnumDef,
|
variants: &[item::Variant],
|
||||||
container_attrs: &attr::ContainerAttrs
|
container_attrs: &attr::ContainerAttrs
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = container_attrs.name().deserialize_name_expr();
|
||||||
@ -654,24 +593,16 @@ fn deserialize_item_enum(
|
|||||||
let variant_visitor = deserialize_field_visitor(
|
let variant_visitor = deserialize_field_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
try!(
|
variants.iter()
|
||||||
enum_def.variants.iter()
|
.map(|variant| variant.attrs.name().deserialize_name())
|
||||||
.map(|variant| {
|
.collect(),
|
||||||
let attrs = try!(attr::VariantAttrs::from_variant(cx, variant));
|
|
||||||
Ok(attrs.name().deserialize_name())
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
),
|
|
||||||
container_attrs,
|
container_attrs,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let variants_expr = builder.expr().ref_().slice()
|
let variants_expr = builder.expr().ref_().slice()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
enum_def.variants.iter()
|
variants.iter().map(|variant| builder.expr().str(variant.ident))
|
||||||
.map(|variant| {
|
|
||||||
builder.expr().str(variant.node.name)
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -687,12 +618,12 @@ fn deserialize_item_enum(
|
|||||||
|
|
||||||
// Match arms to extract a variant from a string
|
// Match arms to extract a variant from a string
|
||||||
let mut variant_arms = vec![];
|
let mut variant_arms = vec![];
|
||||||
for (i, variant) in enum_def.variants.iter().enumerate() {
|
for (i, variant) in variants.iter().enumerate() {
|
||||||
let variant_name = builder.pat().path()
|
let variant_name = builder.pat().path()
|
||||||
.id("__Field").id(format!("__field{}", i))
|
.id("__Field").id(format!("__field{}", i))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = try!(deserialize_variant(
|
let expr = deserialize_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
@ -700,21 +631,21 @@ fn deserialize_item_enum(
|
|||||||
ty.clone(),
|
ty.clone(),
|
||||||
variant,
|
variant,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
));
|
);
|
||||||
|
|
||||||
let arm = quote_arm!(cx, $variant_name => { $expr });
|
let arm = quote_arm!(cx, $variant_name => { $expr });
|
||||||
variant_arms.push(arm);
|
variant_arms.push(arm);
|
||||||
}
|
}
|
||||||
variant_arms.extend(ignored_arm.into_iter());
|
variant_arms.extend(ignored_arm.into_iter());
|
||||||
|
|
||||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||||
builder,
|
builder,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
vec![deserializer_ty_param(builder)],
|
vec![deserializer_ty_param(builder)],
|
||||||
vec![deserializer_ty_arg(builder)],
|
vec![deserializer_ty_arg(builder)],
|
||||||
));
|
);
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$variant_visitor
|
$variant_visitor
|
||||||
|
|
||||||
$visitor_item
|
$visitor_item
|
||||||
@ -734,7 +665,7 @@ fn deserialize_item_enum(
|
|||||||
$variants_stmt
|
$variants_stmt
|
||||||
|
|
||||||
deserializer.deserialize_enum($type_name, VARIANTS, $visitor_expr)
|
deserializer.deserialize_enum($type_name, VARIANTS, $visitor_expr)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_variant(
|
fn deserialize_variant(
|
||||||
@ -743,29 +674,29 @@ fn deserialize_variant(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variant: &ast::Variant,
|
variant: &item::Variant,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let variant_ident = variant.node.name;
|
let variant_ident = variant.ident;
|
||||||
|
|
||||||
match variant.node.data {
|
match variant.style {
|
||||||
ast::VariantData::Unit(_) => {
|
item::Style::Unit => {
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
try!(visitor.visit_unit());
|
try!(visitor.visit_unit());
|
||||||
Ok($type_ident::$variant_ident)
|
Ok($type_ident::$variant_ident)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
item::Style::Newtype => {
|
||||||
deserialize_newtype_variant(
|
deserialize_newtype_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
variant_ident,
|
variant_ident,
|
||||||
generics,
|
generics,
|
||||||
&fields[0],
|
&variant.fields[0],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
item::Style::Tuple => {
|
||||||
deserialize_tuple(
|
deserialize_tuple(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
@ -773,11 +704,11 @@ fn deserialize_variant(
|
|||||||
Some(variant_ident),
|
Some(variant_ident),
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
&variant.fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
item::Style::Struct => {
|
||||||
deserialize_struct(
|
deserialize_struct(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
@ -785,7 +716,7 @@ fn deserialize_variant(
|
|||||||
Some(variant_ident),
|
Some(variant_ident),
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
&variant.fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -798,10 +729,9 @@ fn deserialize_newtype_variant(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
variant_ident: Ident,
|
variant_ident: Ident,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
field: &ast::StructField,
|
field: &item::Field,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
let visit = match field.attrs.deserialize_with() {
|
||||||
let visit = match attrs.deserialize_with() {
|
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
quote_expr!(cx, try!(visitor.visit_newtype::<$field_ty>()))
|
quote_expr!(cx, try!(visitor.visit_newtype::<$field_ty>()))
|
||||||
@ -816,7 +746,7 @@ fn deserialize_newtype_variant(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(quote_expr!(cx, Ok($type_ident::$variant_ident($visit))))
|
quote_expr!(cx, Ok($type_ident::$variant_ident($visit)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_field_visitor(
|
fn deserialize_field_visitor(
|
||||||
@ -985,11 +915,11 @@ fn deserialize_struct_visitor(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[(ast::StructField, attr::FieldAttrs)],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<(Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>), Error> {
|
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
|
||||||
let field_exprs = fields.iter()
|
let field_exprs = fields.iter()
|
||||||
.map(|&(_, ref attrs)| attrs.name().deserialize_name())
|
.map(|field| field.attrs.name().deserialize_name())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let field_visitor = deserialize_field_visitor(
|
let field_visitor = deserialize_field_visitor(
|
||||||
@ -1000,7 +930,7 @@ fn deserialize_struct_visitor(
|
|||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let visit_map_expr = try!(deserialize_map(
|
let visit_map_expr = deserialize_map(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
@ -1008,12 +938,12 @@ fn deserialize_struct_visitor(
|
|||||||
impl_generics,
|
impl_generics,
|
||||||
fields,
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
));
|
);
|
||||||
|
|
||||||
let fields_expr = builder.expr().ref_().slice()
|
let fields_expr = builder.expr().ref_().slice()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.map(|&(ref field, _)| {
|
.map(|field| {
|
||||||
match field.ident {
|
match field.ident {
|
||||||
Some(name) => builder.expr().str(name),
|
Some(name) => builder.expr().str(name),
|
||||||
None => {
|
None => {
|
||||||
@ -1028,7 +958,7 @@ fn deserialize_struct_visitor(
|
|||||||
const FIELDS: &'static [&'static str] = $fields_expr;
|
const FIELDS: &'static [&'static str] = $fields_expr;
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
Ok((field_visitor, fields_stmt, visit_map_expr))
|
(field_visitor, fields_stmt, visit_map_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_map(
|
fn deserialize_map(
|
||||||
@ -1037,33 +967,33 @@ fn deserialize_map(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[(ast::StructField, attr::FieldAttrs)],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
// Create the field names for the fields.
|
// Create the field names for the fields.
|
||||||
let fields_attrs_names = fields.iter()
|
let fields_names = fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, &(ref field, ref attrs))|
|
.map(|(i, field)|
|
||||||
(field, attrs, builder.id(format!("__field{}", i))))
|
(field, builder.id(format!("__field{}", i))))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// Declare each field that will be deserialized.
|
// Declare each field that will be deserialized.
|
||||||
let let_values: Vec<ast::Stmt> = fields_attrs_names.iter()
|
let let_values: Vec<ast::Stmt> = fields_names.iter()
|
||||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing())
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
.map(|&(field, _, name)| {
|
.map(|&(field, name)| {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
quote_stmt!(cx, let mut $name: Option<$field_ty> = None;).unwrap()
|
quote_stmt!(cx, let mut $name: Option<$field_ty> = None;).unwrap()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Match arms to extract a value for a field.
|
// Match arms to extract a value for a field.
|
||||||
let value_arms = fields_attrs_names.iter()
|
let value_arms = fields_names.iter()
|
||||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing())
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
.map(|&(ref field, ref attrs, name)| {
|
.map(|&(ref field, name)| {
|
||||||
let deser_name = attrs.name().deserialize_name();
|
let deser_name = field.attrs.name().deserialize_name();
|
||||||
let name_str = builder.expr().lit().str(deser_name);
|
let name_str = builder.expr().lit().str(deser_name);
|
||||||
|
|
||||||
let visit = match attrs.deserialize_with() {
|
let visit = match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
let field_ty = &field.ty;
|
let field_ty = &field.ty;
|
||||||
quote_expr!(cx, try!(visitor.visit_value::<$field_ty>()))
|
quote_expr!(cx, try!(visitor.visit_value::<$field_ty>()))
|
||||||
@ -1091,9 +1021,9 @@ fn deserialize_map(
|
|||||||
|
|
||||||
// Match arms to ignore value for fields that have `skip_deserializing`.
|
// Match arms to ignore value for fields that have `skip_deserializing`.
|
||||||
// Ignored even if `deny_unknown_fields` is set.
|
// Ignored even if `deny_unknown_fields` is set.
|
||||||
let skipped_arms = fields_attrs_names.iter()
|
let skipped_arms = fields_names.iter()
|
||||||
.filter(|&&(_, ref attrs, _)| attrs.skip_deserializing())
|
.filter(|&&(field, _)| field.attrs.skip_deserializing())
|
||||||
.map(|&(_, _, name)| {
|
.map(|&(_, name)| {
|
||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
__Field::$name => {
|
__Field::$name => {
|
||||||
try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>());
|
try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>());
|
||||||
@ -1111,26 +1041,24 @@ fn deserialize_map(
|
|||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
let extract_values = fields_attrs_names.iter()
|
let extract_values = fields_names.iter()
|
||||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing())
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
.map(|&(_, ref attrs, name)| {
|
.map(|&(field, name)| {
|
||||||
let missing_expr = expr_is_missing(cx, attrs);
|
let missing_expr = expr_is_missing(cx, &field.attrs);
|
||||||
|
|
||||||
Ok(quote_stmt!(cx,
|
quote_stmt!(cx,
|
||||||
let $name = match $name {
|
let $name = match $name {
|
||||||
Some($name) => $name,
|
Some($name) => $name,
|
||||||
None => $missing_expr
|
None => $missing_expr
|
||||||
};
|
};
|
||||||
).unwrap())
|
).unwrap()
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let extract_values = try!(extract_values);
|
|
||||||
|
|
||||||
let result = builder.expr().struct_path(struct_path)
|
let result = builder.expr().struct_path(struct_path)
|
||||||
.with_id_exprs(
|
.with_id_exprs(
|
||||||
fields_attrs_names.iter()
|
fields_names.iter()
|
||||||
.map(|&(field, attrs, name)| {
|
.map(|&(field, name)| {
|
||||||
(
|
(
|
||||||
match field.ident {
|
match field.ident {
|
||||||
Some(name) => name.clone(),
|
Some(name) => name.clone(),
|
||||||
@ -1138,8 +1066,8 @@ fn deserialize_map(
|
|||||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
if attrs.skip_deserializing() {
|
if field.attrs.skip_deserializing() {
|
||||||
expr_is_missing(cx, attrs)
|
expr_is_missing(cx, &field.attrs)
|
||||||
} else {
|
} else {
|
||||||
builder.expr().id(name)
|
builder.expr().id(name)
|
||||||
}
|
}
|
||||||
@ -1148,7 +1076,7 @@ fn deserialize_map(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$let_values
|
$let_values
|
||||||
|
|
||||||
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
while let Some(key) = try!(visitor.visit_key::<__Field>()) {
|
||||||
@ -1164,7 +1092,7 @@ fn deserialize_map(
|
|||||||
try!(visitor.end());
|
try!(visitor.end());
|
||||||
|
|
||||||
Ok($result)
|
Ok($result)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
||||||
@ -1247,9 +1175,9 @@ fn expr_is_missing(
|
|||||||
|
|
||||||
fn check_no_str(
|
fn check_no_str(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
fields: &[(ast::StructField, attr::FieldAttrs)],
|
item: &item::Item,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let fail = |field: &ast::StructField| {
|
let fail = |field: &item::Field| {
|
||||||
cx.span_err(
|
cx.span_err(
|
||||||
field.span,
|
field.span,
|
||||||
"Serde does not support deserializing fields of type &str; \
|
"Serde does not support deserializing fields of type &str; \
|
||||||
@ -1257,9 +1185,9 @@ fn check_no_str(
|
|||||||
Err(Error)
|
Err(Error)
|
||||||
};
|
};
|
||||||
|
|
||||||
for &(ref field, ref attrs) in fields {
|
for field in item.body.all_fields() {
|
||||||
if attrs.skip_deserializing()
|
if field.attrs.skip_deserializing()
|
||||||
|| attrs.deserialize_with().is_some() { continue }
|
|| field.attrs.deserialize_with().is_some() { continue }
|
||||||
|
|
||||||
if let ast::TyKind::Rptr(_, ref inner) = field.ty.node {
|
if let ast::TyKind::Rptr(_, ref inner) = field.ty.node {
|
||||||
if let ast::TyKind::Path(_, ref path) = inner.ty.node {
|
if let ast::TyKind::Path(_, ref path) = inner.ty.node {
|
||||||
|
144
serde_codegen/src/item.rs
Normal file
144
serde_codegen/src/item.rs
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
use syntax::ast;
|
||||||
|
use syntax::codemap;
|
||||||
|
use syntax::ext::base::ExtCtxt;
|
||||||
|
use syntax::ptr::P;
|
||||||
|
|
||||||
|
use attr;
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
pub struct Item<'a> {
|
||||||
|
pub ident: ast::Ident,
|
||||||
|
pub span: codemap::Span,
|
||||||
|
pub attrs: attr::ContainerAttrs,
|
||||||
|
pub body: Body<'a>,
|
||||||
|
pub generics: &'a ast::Generics,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Body<'a> {
|
||||||
|
Enum(Vec<Variant<'a>>),
|
||||||
|
Struct(Style, Vec<Field<'a>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Variant<'a> {
|
||||||
|
pub ident: ast::Ident,
|
||||||
|
pub attrs: attr::VariantAttrs,
|
||||||
|
pub style: Style,
|
||||||
|
pub fields: Vec<Field<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Field<'a> {
|
||||||
|
pub ident: Option<ast::Ident>,
|
||||||
|
pub span: codemap::Span,
|
||||||
|
pub attrs: attr::FieldAttrs,
|
||||||
|
pub ty: &'a P<ast::Ty>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Style {
|
||||||
|
Struct,
|
||||||
|
Tuple,
|
||||||
|
Newtype,
|
||||||
|
Unit,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Item<'a> {
|
||||||
|
pub fn from_ast(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
derive_trait: &'static str,
|
||||||
|
item: &'a ast::Item,
|
||||||
|
) -> Result<Item<'a>, Error> {
|
||||||
|
let attrs = attr::ContainerAttrs::from_item(cx, item);
|
||||||
|
|
||||||
|
let (body, generics) = match item.node {
|
||||||
|
ast::ItemKind::Enum(ref enum_def, ref generics) => {
|
||||||
|
let variants = enum_from_ast(cx, enum_def);
|
||||||
|
(Body::Enum(variants), generics)
|
||||||
|
}
|
||||||
|
ast::ItemKind::Struct(ref variant_data, ref generics) => {
|
||||||
|
let (style, fields) = struct_from_ast(cx, variant_data);
|
||||||
|
(Body::Struct(style, fields), generics)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
cx.span_err(item.span, &format!(
|
||||||
|
"`#[derive({})]` may only be applied to structs and enums",
|
||||||
|
derive_trait));
|
||||||
|
return Err(Error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Item {
|
||||||
|
ident: item.ident,
|
||||||
|
span: item.span,
|
||||||
|
attrs: attrs,
|
||||||
|
body: body,
|
||||||
|
generics: generics,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enum_from_ast<'a>(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
enum_def: &'a ast::EnumDef,
|
||||||
|
) -> Vec<Variant<'a>> {
|
||||||
|
enum_def.variants.iter()
|
||||||
|
.map(|variant| {
|
||||||
|
let (style, fields) = struct_from_ast(cx, &variant.node.data);
|
||||||
|
Variant {
|
||||||
|
ident: variant.node.name,
|
||||||
|
attrs: attr::VariantAttrs::from_variant(cx, variant),
|
||||||
|
style: style,
|
||||||
|
fields: fields,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn struct_from_ast<'a>(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
variant_data: &'a ast::VariantData,
|
||||||
|
) -> (Style, Vec<Field<'a>>) {
|
||||||
|
match *variant_data {
|
||||||
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
|
(Style::Struct, fields_from_ast(cx, fields))
|
||||||
|
}
|
||||||
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
|
(Style::Newtype, fields_from_ast(cx, fields))
|
||||||
|
}
|
||||||
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
|
(Style::Tuple, fields_from_ast(cx, fields))
|
||||||
|
}
|
||||||
|
ast::VariantData::Unit(_) => {
|
||||||
|
(Style::Unit, Vec::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fields_from_ast<'a>(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
fields: &'a [ast::StructField],
|
||||||
|
) -> Vec<Field<'a>> {
|
||||||
|
fields.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, field)| {
|
||||||
|
Field {
|
||||||
|
ident: field.ident,
|
||||||
|
span: field.span,
|
||||||
|
attrs: attr::FieldAttrs::from_field(cx, i, field),
|
||||||
|
ty: &field.ty,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Body<'a> {
|
||||||
|
pub fn all_fields(&'a self) -> Box<Iterator<Item=&'a Field<'a>> + 'a> {
|
||||||
|
match *self {
|
||||||
|
Body::Enum(ref variants) => {
|
||||||
|
Box::new(variants.iter()
|
||||||
|
.flat_map(|variant| variant.fields.iter()))
|
||||||
|
}
|
||||||
|
Body::Struct(_, ref fields) => {
|
||||||
|
Box::new(fields.iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,4 +2,5 @@ mod attr;
|
|||||||
mod bound;
|
mod bound;
|
||||||
mod de;
|
mod de;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod item;
|
||||||
mod ser;
|
mod ser;
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
use aster;
|
use aster;
|
||||||
|
|
||||||
use syntax::ast::{
|
use syntax::ast::{self, Ident, MetaItem};
|
||||||
Ident,
|
|
||||||
MetaItem,
|
|
||||||
Item,
|
|
||||||
};
|
|
||||||
use syntax::ast;
|
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
@ -13,6 +8,7 @@ use syntax::ptr::P;
|
|||||||
use attr;
|
use attr;
|
||||||
use bound;
|
use bound;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
use item;
|
||||||
|
|
||||||
pub fn expand_derive_serialize(
|
pub fn expand_derive_serialize(
|
||||||
cx: &mut ExtCtxt,
|
cx: &mut ExtCtxt,
|
||||||
@ -47,33 +43,21 @@ pub fn expand_derive_serialize(
|
|||||||
fn serialize_item(
|
fn serialize_item(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &ast::Item,
|
||||||
) -> Result<P<ast::Item>, Error> {
|
) -> Result<P<ast::Item>, Error> {
|
||||||
let generics = match item.node {
|
let item = try!(item::Item::from_ast(cx, "Serialize", item));
|
||||||
ast::ItemKind::Struct(_, ref generics) => generics,
|
|
||||||
ast::ItemKind::Enum(_, ref generics) => generics,
|
|
||||||
_ => {
|
|
||||||
cx.span_err(
|
|
||||||
item.span,
|
|
||||||
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
|
||||||
return Err(Error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let container_attrs = try!(attr::ContainerAttrs::from_item(cx, item));
|
let impl_generics = build_impl_generics(builder, &item);
|
||||||
|
|
||||||
let impl_generics = try!(build_impl_generics(cx, builder, item, generics, &container_attrs));
|
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
let ty = builder.ty().path()
|
||||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let body = try!(serialize_body(cx,
|
let body = serialize_body(cx,
|
||||||
&builder,
|
builder,
|
||||||
&item,
|
&item,
|
||||||
&impl_generics,
|
&impl_generics,
|
||||||
ty.clone(),
|
ty.clone());
|
||||||
&container_attrs));
|
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@ -98,28 +82,23 @@ fn serialize_item(
|
|||||||
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
||||||
// field type that will be serialized by us.
|
// field type that will be serialized by us.
|
||||||
fn build_impl_generics(
|
fn build_impl_generics(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &item::Item,
|
||||||
generics: &ast::Generics,
|
) -> ast::Generics {
|
||||||
container_attrs: &attr::ContainerAttrs,
|
let generics = bound::without_defaults(item.generics);
|
||||||
) -> Result<ast::Generics, Error> {
|
|
||||||
let generics = bound::without_defaults(generics);
|
|
||||||
|
|
||||||
let generics = try!(bound::with_where_predicates_from_fields(
|
let generics = bound::with_where_predicates_from_fields(
|
||||||
cx, builder, item, &generics,
|
builder, item, &generics,
|
||||||
|attrs| attrs.ser_bound()));
|
|attrs| attrs.ser_bound());
|
||||||
|
|
||||||
match container_attrs.ser_bound() {
|
match item.attrs.ser_bound() {
|
||||||
Some(predicates) => {
|
Some(predicates) => {
|
||||||
let generics = bound::with_where_predicates(builder, &generics, predicates);
|
bound::with_where_predicates(builder, &generics, predicates)
|
||||||
Ok(generics)
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let generics = try!(bound::with_bound(cx, builder, item, &generics,
|
bound::with_bound(builder, item, &generics,
|
||||||
needs_serialize_bound,
|
needs_serialize_bound,
|
||||||
&builder.path().ids(&["_serde", "ser", "Serialize"]).build()));
|
&builder.path().ids(&["_serde", "ser", "Serialize"]).build())
|
||||||
Ok(generics)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,94 +116,60 @@ fn needs_serialize_bound(attrs: &attr::FieldAttrs) -> bool {
|
|||||||
fn serialize_body(
|
fn serialize_body(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &Item,
|
item: &item::Item,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
) -> P<ast::Expr> {
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
match item.body {
|
||||||
match item.node {
|
item::Body::Enum(ref variants) => {
|
||||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
|
||||||
serialize_item_struct(
|
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
item.span,
|
|
||||||
variant_data,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
|
||||||
serialize_item_enum(
|
serialize_item_enum(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
enum_def,
|
variants,
|
||||||
container_attrs,
|
&item.attrs)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
_ => {
|
item::Body::Struct(item::Style::Struct, ref fields) => {
|
||||||
cx.span_bug(item.span,
|
|
||||||
"expected ItemStruct or ItemEnum in #[derive(Serialize)]");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_item_struct(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
span: Span,
|
|
||||||
variant_data: &ast::VariantData,
|
|
||||||
container_attrs: &attr::ContainerAttrs,
|
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
|
||||||
match *variant_data {
|
|
||||||
ast::VariantData::Unit(_) => {
|
|
||||||
serialize_unit_struct(
|
|
||||||
cx,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
|
||||||
serialize_newtype_struct(
|
|
||||||
cx,
|
|
||||||
&builder,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
&fields[0],
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
|
||||||
if fields.iter().any(|field| field.ident.is_some()) {
|
|
||||||
cx.span_bug(span, "tuple struct has named fields")
|
|
||||||
}
|
|
||||||
|
|
||||||
serialize_tuple_struct(
|
|
||||||
cx,
|
|
||||||
&builder,
|
|
||||||
impl_generics,
|
|
||||||
ty,
|
|
||||||
fields,
|
|
||||||
container_attrs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
|
||||||
if fields.iter().any(|field| field.ident.is_none()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
cx.span_bug(span, "struct has unnamed fields")
|
cx.span_bug(item.span, "struct has unnamed fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_struct(
|
serialize_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
builder,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
container_attrs,
|
&item.attrs)
|
||||||
)
|
}
|
||||||
|
item::Body::Struct(item::Style::Tuple, ref fields) => {
|
||||||
|
if fields.iter().any(|field| field.ident.is_some()) {
|
||||||
|
cx.span_bug(item.span, "tuple struct has named fields")
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize_tuple_struct(
|
||||||
|
cx,
|
||||||
|
builder,
|
||||||
|
impl_generics,
|
||||||
|
ty,
|
||||||
|
fields,
|
||||||
|
&item.attrs)
|
||||||
|
}
|
||||||
|
item::Body::Struct(item::Style::Newtype, ref fields) => {
|
||||||
|
serialize_newtype_struct(
|
||||||
|
cx,
|
||||||
|
builder,
|
||||||
|
impl_generics,
|
||||||
|
ty,
|
||||||
|
&fields[0],
|
||||||
|
&item.attrs)
|
||||||
|
}
|
||||||
|
item::Body::Struct(item::Style::Unit, _) => {
|
||||||
|
serialize_unit_struct(
|
||||||
|
cx,
|
||||||
|
&item.attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -232,12 +177,12 @@ fn serialize_item_struct(
|
|||||||
fn serialize_unit_struct(
|
fn serialize_unit_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
_serializer.serialize_unit_struct($type_name)
|
_serializer.serialize_unit_struct($type_name)
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct(
|
fn serialize_newtype_struct(
|
||||||
@ -245,22 +190,20 @@ fn serialize_newtype_struct(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
container_ty: P<ast::Ty>,
|
container_ty: P<ast::Ty>,
|
||||||
field: &ast::StructField,
|
field: &item::Field,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
|
||||||
|
|
||||||
let mut field_expr = quote_expr!(cx, &self.0);
|
let mut field_expr = quote_expr!(cx, &self.0);
|
||||||
if let Some(path) = attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
&container_ty, impl_generics, &field.ty, path, field_expr);
|
&container_ty, impl_generics, &field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(
|
||||||
@ -268,10 +211,10 @@ fn serialize_tuple_struct(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
@ -283,11 +226,11 @@ fn serialize_tuple_struct(
|
|||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
));
|
);
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
_serializer.serialize_tuple_struct($type_name, Visitor {
|
_serializer.serialize_tuple_struct($type_name, Visitor {
|
||||||
@ -295,7 +238,7 @@ fn serialize_tuple_struct(
|
|||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||||
})
|
})
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct(
|
fn serialize_struct(
|
||||||
@ -303,10 +246,10 @@ fn serialize_struct(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
@ -318,11 +261,11 @@ fn serialize_struct(
|
|||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
));
|
);
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
_serializer.serialize_struct($type_name, Visitor {
|
_serializer.serialize_struct($type_name, Visitor {
|
||||||
@ -330,7 +273,7 @@ fn serialize_struct(
|
|||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||||
})
|
})
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_item_enum(
|
fn serialize_item_enum(
|
||||||
@ -339,11 +282,11 @@ fn serialize_item_enum(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
enum_def: &ast::EnumDef,
|
variants: &[item::Variant],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let arms: Vec<_> = try!(
|
let arms: Vec<_> =
|
||||||
enum_def.variants.iter()
|
variants.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(variant_index, variant)| {
|
.map(|(variant_index, variant)| {
|
||||||
serialize_variant(
|
serialize_variant(
|
||||||
@ -357,14 +300,13 @@ fn serialize_item_enum(
|
|||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect();
|
||||||
);
|
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
match *self {
|
match *self {
|
||||||
$arms
|
$arms
|
||||||
}
|
}
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_variant(
|
fn serialize_variant(
|
||||||
@ -373,19 +315,18 @@ fn serialize_variant(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variant: &ast::Variant,
|
variant: &item::Variant,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<ast::Arm, Error> {
|
) -> ast::Arm {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
let variant_ident = variant.node.name;
|
let variant_ident = variant.ident;
|
||||||
let variant_attrs = try!(attr::VariantAttrs::from_variant(cx, variant));
|
let variant_name = variant.attrs.name().serialize_name_expr();
|
||||||
let variant_name = variant_attrs.name().serialize_name_expr();
|
|
||||||
|
|
||||||
match variant.node.data {
|
match variant.style {
|
||||||
ast::VariantData::Unit(_) => {
|
item::Style::Unit => {
|
||||||
Ok(quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$type_ident::$variant_ident => {
|
$type_ident::$variant_ident => {
|
||||||
_serde::ser::Serializer::serialize_unit_variant(
|
_serde::ser::Serializer::serialize_unit_variant(
|
||||||
_serializer,
|
_serializer,
|
||||||
@ -394,10 +335,10 @@ fn serialize_variant(
|
|||||||
$variant_name,
|
$variant_name,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
))
|
)
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
item::Style::Newtype => {
|
||||||
let expr = try!(serialize_newtype_variant(
|
let expr = serialize_newtype_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_name,
|
type_name,
|
||||||
@ -405,15 +346,15 @@ fn serialize_variant(
|
|||||||
variant_name,
|
variant_name,
|
||||||
ty,
|
ty,
|
||||||
generics,
|
generics,
|
||||||
&fields[0],
|
&variant.fields[0],
|
||||||
));
|
);
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$type_ident::$variant_ident(ref __simple_value) => { $expr }
|
$type_ident::$variant_ident(ref __simple_value) => { $expr }
|
||||||
))
|
)
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
item::Style::Tuple => {
|
||||||
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
let field_names: Vec<ast::Ident> = (0 .. variant.fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -425,7 +366,7 @@ fn serialize_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = try!(serialize_tuple_variant(
|
let expr = serialize_tuple_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_name,
|
type_name,
|
||||||
@ -433,16 +374,16 @@ fn serialize_variant(
|
|||||||
variant_name,
|
variant_name,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
&variant.fields,
|
||||||
field_names,
|
field_names,
|
||||||
));
|
);
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$pat => { $expr }
|
$pat => { $expr }
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
item::Style::Struct => {
|
||||||
let field_names: Vec<_> = (0 .. fields.len())
|
let field_names: Vec<_> = (0 .. variant.fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -450,7 +391,7 @@ fn serialize_variant(
|
|||||||
.id(type_ident).id(variant_ident).build()
|
.id(type_ident).id(variant_ident).build()
|
||||||
.with_pats(
|
.with_pats(
|
||||||
field_names.iter()
|
field_names.iter()
|
||||||
.zip(fields.iter())
|
.zip(variant.fields.iter())
|
||||||
.map(|(id, field)| {
|
.map(|(id, field)| {
|
||||||
let name = match field.ident {
|
let name = match field.ident {
|
||||||
Some(name) => name,
|
Some(name) => name,
|
||||||
@ -464,21 +405,21 @@ fn serialize_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = try!(serialize_struct_variant(
|
let expr = serialize_struct_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
variant_index,
|
variant_index,
|
||||||
variant_name,
|
variant_name,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
&variant.fields,
|
||||||
field_names,
|
field_names,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
));
|
);
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$pat => { $expr }
|
$pat => { $expr }
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -491,17 +432,15 @@ fn serialize_newtype_variant(
|
|||||||
variant_name: P<ast::Expr>,
|
variant_name: P<ast::Expr>,
|
||||||
container_ty: P<ast::Ty>,
|
container_ty: P<ast::Ty>,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
field: &ast::StructField,
|
field: &item::Field,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
|
||||||
|
|
||||||
let mut field_expr = quote_expr!(cx, __simple_value);
|
let mut field_expr = quote_expr!(cx, __simple_value);
|
||||||
if let Some(path) = attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
&container_ty, generics, &field.ty, path, field_expr);
|
&container_ty, generics, &field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
_serde::ser::Serializer::serialize_newtype_variant(
|
_serde::ser::Serializer::serialize_newtype_variant(
|
||||||
_serializer,
|
_serializer,
|
||||||
$type_name,
|
$type_name,
|
||||||
@ -509,7 +448,7 @@ fn serialize_newtype_variant(
|
|||||||
$variant_name,
|
$variant_name,
|
||||||
$field_expr,
|
$field_expr,
|
||||||
)
|
)
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
@ -520,9 +459,9 @@ fn serialize_tuple_variant(
|
|||||||
variant_name: P<ast::Expr>,
|
variant_name: P<ast::Expr>,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
field_names: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let variant_ty = builder.ty().tuple()
|
let variant_ty = builder.ty().tuple()
|
||||||
.with_tys(
|
.with_tys(
|
||||||
fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
@ -534,7 +473,7 @@ fn serialize_tuple_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
structure_ty.clone(),
|
structure_ty.clone(),
|
||||||
@ -543,7 +482,7 @@ fn serialize_tuple_variant(
|
|||||||
fields,
|
fields,
|
||||||
generics,
|
generics,
|
||||||
true,
|
true,
|
||||||
));
|
);
|
||||||
|
|
||||||
let value_expr = builder.expr().tuple()
|
let value_expr = builder.expr().tuple()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
@ -553,7 +492,7 @@ fn serialize_tuple_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||||
@ -561,7 +500,7 @@ fn serialize_tuple_variant(
|
|||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||||
})
|
})
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(
|
||||||
@ -571,10 +510,10 @@ fn serialize_struct_variant(
|
|||||||
variant_name: P<ast::Expr>,
|
variant_name: P<ast::Expr>,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
field_names: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> P<ast::Expr> {
|
||||||
let variant_generics = builder.generics()
|
let variant_generics = builder.generics()
|
||||||
.with(generics.clone())
|
.with(generics.clone())
|
||||||
.add_lifetime_bound("'__serde_variant")
|
.add_lifetime_bound("'__serde_variant")
|
||||||
@ -586,7 +525,6 @@ fn serialize_struct_variant(
|
|||||||
.with_fields(
|
.with_fields(
|
||||||
fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
builder.struct_field(field.ident.expect("struct has unnamed fields"))
|
builder.struct_field(field.ident.expect("struct has unnamed fields"))
|
||||||
.with_attrs(field.attrs.iter().cloned())
|
|
||||||
.ty()
|
.ty()
|
||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__serde_variant")
|
.lifetime("'__serde_variant")
|
||||||
@ -623,7 +561,7 @@ fn serialize_struct_variant(
|
|||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
variant_ty.clone(),
|
variant_ty.clone(),
|
||||||
@ -632,11 +570,11 @@ fn serialize_struct_variant(
|
|||||||
fields,
|
fields,
|
||||||
&variant_generics,
|
&variant_generics,
|
||||||
true,
|
true,
|
||||||
));
|
);
|
||||||
|
|
||||||
let container_name = container_attrs.name().serialize_name_expr();
|
let container_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$variant_struct
|
$variant_struct
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
@ -650,7 +588,7 @@ fn serialize_struct_variant(
|
|||||||
_structure_ty: ::std::marker::PhantomData,
|
_structure_ty: ::std::marker::PhantomData,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct_visitor(
|
fn serialize_tuple_struct_visitor(
|
||||||
@ -659,24 +597,22 @@ fn serialize_tuple_struct_visitor(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
variant_ty: P<ast::Ty>,
|
variant_ty: P<ast::Ty>,
|
||||||
serializer_method: ast::Ident,
|
serializer_method: ast::Ident,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
) -> (P<ast::Item>, P<ast::Item>) {
|
||||||
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
let arms: Vec<_> = fields.iter()
|
||||||
|
|
||||||
let arms: Vec<_> = fields_with_attrs.iter()
|
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, &(ref field, ref attrs))| {
|
.map(|(i, field)| {
|
||||||
let mut field_expr = builder.expr().tup_field(i).field("value").self_();
|
let mut field_expr = builder.expr().tup_field(i).field("value").self_();
|
||||||
if !is_enum {
|
if !is_enum {
|
||||||
field_expr = quote_expr!(cx, &$field_expr);
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let continue_if_skip = attrs.skip_serializing_if()
|
let continue_if_skip = field.attrs.skip_serializing_if()
|
||||||
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
|
|
||||||
if let Some(path) = attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
&structure_ty, generics, &field.ty, path, field_expr);
|
&structure_ty, generics, &field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
@ -704,7 +640,7 @@ fn serialize_tuple_struct_visitor(
|
|||||||
|
|
||||||
let nfields = fields.len();
|
let nfields = fields.len();
|
||||||
|
|
||||||
Ok((
|
(
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
struct Visitor $visitor_impl_generics $where_clause {
|
struct Visitor $visitor_impl_generics $where_clause {
|
||||||
state: usize,
|
state: usize,
|
||||||
@ -733,7 +669,7 @@ fn serialize_tuple_struct_visitor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_visitor(
|
fn serialize_struct_visitor(
|
||||||
@ -742,28 +678,26 @@ fn serialize_struct_visitor(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
variant_ty: P<ast::Ty>,
|
variant_ty: P<ast::Ty>,
|
||||||
serializer_method: ast::Ident,
|
serializer_method: ast::Ident,
|
||||||
fields: &[ast::StructField],
|
fields: &[item::Field],
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
) -> (P<ast::Item>, P<ast::Item>) {
|
||||||
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
let arms: Vec<ast::Arm> = fields.iter()
|
||||||
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
let arms: Vec<ast::Arm> = fields_with_attrs.iter()
|
|
||||||
.filter(|&&(_, ref attrs)| !attrs.skip_serializing())
|
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, &(ref field, ref attrs))| {
|
.map(|(i, field)| {
|
||||||
let ident = field.ident.expect("struct has unnamed field");
|
let ident = field.ident.expect("struct has unnamed field");
|
||||||
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
if !is_enum {
|
if !is_enum {
|
||||||
field_expr = quote_expr!(cx, &$field_expr);
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let key_expr = attrs.name().serialize_name_expr();
|
let key_expr = field.attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
let continue_if_skip = attrs.skip_serializing_if()
|
let continue_if_skip = field.attrs.skip_serializing_if()
|
||||||
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
|
|
||||||
if let Some(path) = attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
&structure_ty, generics, &field.ty, path, field_expr)
|
&structure_ty, generics, &field.ty, path, field_expr)
|
||||||
}
|
}
|
||||||
@ -793,23 +727,23 @@ fn serialize_struct_visitor(
|
|||||||
.strip_bounds()
|
.strip_bounds()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let len = fields_with_attrs.iter()
|
let len = fields.iter()
|
||||||
.filter(|&&(_, ref attrs)| !attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
.map(|&(ref field, ref attrs)| {
|
.map(|field| {
|
||||||
let ident = field.ident.expect("struct has unnamed fields");
|
let ident = field.ident.expect("struct has unnamed fields");
|
||||||
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
if !is_enum {
|
if !is_enum {
|
||||||
field_expr = quote_expr!(cx, &$field_expr);
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
match attrs.skip_serializing_if() {
|
match field.attrs.skip_serializing_if() {
|
||||||
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
||||||
None => quote_expr!(cx, 1),
|
None => quote_expr!(cx, 1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||||
|
|
||||||
Ok((
|
(
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
struct Visitor $visitor_impl_generics $where_clause {
|
struct Visitor $visitor_impl_generics $where_clause {
|
||||||
state: usize,
|
state: usize,
|
||||||
@ -841,7 +775,7 @@ fn serialize_struct_visitor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wrap_serialize_with(
|
fn wrap_serialize_with(
|
||||||
|
@ -5,13 +5,11 @@
|
|||||||
struct S {
|
struct S {
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
#[serde(rename(serialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(serialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
||||||
a: (), //~^ ERROR: duplicate serde attribute `rename`
|
a: (),
|
||||||
//~^^ ERROR: duplicate serde attribute `rename`
|
|
||||||
|
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
#[serde(rename="y")] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename="y")] //~ ERROR: duplicate serde attribute `rename`
|
||||||
b: (), //~^ ERROR: duplicate serde attribute `rename`
|
b: (),
|
||||||
//~^^ ERROR: duplicate serde attribute `rename`
|
|
||||||
|
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
#[serde(rename(deserialize="y"))] // ok
|
#[serde(rename(deserialize="y"))] // ok
|
||||||
@ -19,7 +17,7 @@ struct S {
|
|||||||
|
|
||||||
#[serde(rename="x")]
|
#[serde(rename="x")]
|
||||||
#[serde(rename(deserialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(deserialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
||||||
d: (), //~^ ERROR: duplicate serde attribute `rename`
|
d: (),
|
||||||
} //~^^ ERROR: duplicate serde attribute `rename`
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user