mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 10:11:01 +00:00
Auto merge of #397 - dtolnay:item, r=oli-obk
Factor attr parsing into serde_item crate Fixes #396. @KodrAus [let me know whether this fits the bill.](https://github.com/dtolnay/serde/tree/5c6a0e12e95974e3c131386e8e0528b6b9cfa6fa/serde_item/src) a few other changes to make the API a little more presentable: - Rename attr::{ContainerAttrs,VariantAttrs,FieldAttrs} to remove the "Attrs" (I see you worked on the corresponding [clippy lint](https://github.com/Manishearth/rust-clippy/issues/904)). - Rename attr::Container* to attr::Item to correspond with item::Item and ast::Item. The others already had a correspondence (attr::Variant/item::Variant/ast::Variant, attr::Field/item::Field/ast::Field). Also a unit struct isn't much of a "container." - Change item::Item::from_ast to return a meaningful error enum instead of printing a message that was hard to generalize to other uses. - Add item::Variant.span for consistency because Item and Field already had span. - Remove the "ident" field from attr::Name because we can just fold it into the other two fields. - Remove attr::Name::(de)serialize_name_expr because it wasn't using the right AstBuilder in the first place. - Rename the attr:: constructors from_item/from_variant/from_field to from_ast to line up with the item:: constructors; the signatures match. - Remove attr's dependency on aster because we were only using it for two very simple things.
This commit is contained in:
@@ -14,7 +14,14 @@ include = ["Cargo.toml", "build.rs", "src/**/*.rs", "src/lib.rs.in"]
|
|||||||
default = ["with-syntex"]
|
default = ["with-syntex"]
|
||||||
nightly = ["quasi_macros"]
|
nightly = ["quasi_macros"]
|
||||||
nightly-testing = ["clippy"]
|
nightly-testing = ["clippy"]
|
||||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
with-syntex = [
|
||||||
|
"quasi/with-syntex",
|
||||||
|
"quasi_codegen",
|
||||||
|
"quasi_codegen/with-syntex",
|
||||||
|
"serde_item/with-syntex",
|
||||||
|
"syntex",
|
||||||
|
"syntex_syntax",
|
||||||
|
]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
quasi_codegen = { version = "^0.12.0", optional = true }
|
quasi_codegen = { version = "^0.12.0", optional = true }
|
||||||
@@ -25,5 +32,6 @@ aster = { version = "^0.18.0", default-features = false }
|
|||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
quasi = { version = "^0.12.0", default-features = false }
|
quasi = { version = "^0.12.0", default-features = false }
|
||||||
quasi_macros = { version = "^0.12.0", optional = true }
|
quasi_macros = { version = "^0.12.0", optional = true }
|
||||||
|
serde_item = { version = "0.1", path = "../serde_item", default-features = false }
|
||||||
syntex = { version = "^0.35.0", optional = true }
|
syntex = { version = "^0.35.0", optional = true }
|
||||||
syntex_syntax = { version = "^0.35.0", optional = true }
|
syntex_syntax = { version = "^0.35.0", optional = true }
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ use syntax::ast;
|
|||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
use syntax::visit;
|
use syntax::visit;
|
||||||
|
|
||||||
use attr;
|
use item::{attr, Item};
|
||||||
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
|
||||||
@@ -39,7 +38,7 @@ pub fn with_where_predicates_from_fields<F>(
|
|||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
from_field: F,
|
from_field: F,
|
||||||
) -> ast::Generics
|
) -> ast::Generics
|
||||||
where F: Fn(&attr::FieldAttrs) -> Option<&[ast::WherePredicate]>,
|
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
|
||||||
{
|
{
|
||||||
builder.from_generics(generics.clone())
|
builder.from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
@@ -56,7 +55,7 @@ pub fn with_bound<F>(
|
|||||||
filter: F,
|
filter: F,
|
||||||
bound: &ast::Path,
|
bound: &ast::Path,
|
||||||
) -> ast::Generics
|
) -> ast::Generics
|
||||||
where F: Fn(&attr::FieldAttrs) -> bool,
|
where F: Fn(&attr::Field) -> bool,
|
||||||
{
|
{
|
||||||
builder.from_generics(generics.clone())
|
builder.from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
|
|||||||
+59
-47
@@ -6,10 +6,9 @@ use syntax::ext::base::{Annotatable, ExtCtxt};
|
|||||||
use syntax::parse::token::InternedString;
|
use syntax::parse::token::InternedString;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
use attr;
|
|
||||||
use bound;
|
use bound;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use item;
|
use item::{self, attr};
|
||||||
|
|
||||||
pub fn expand_derive_deserialize(
|
pub fn expand_derive_deserialize(
|
||||||
cx: &mut ExtCtxt,
|
cx: &mut ExtCtxt,
|
||||||
@@ -28,27 +27,30 @@ pub fn expand_derive_deserialize(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let builder = aster::AstBuilder::new().span(span);
|
let item = match item::Item::from_ast(cx, item) {
|
||||||
|
|
||||||
let impl_item = match deserialize_item(cx, &builder, &item) {
|
|
||||||
Ok(item) => item,
|
Ok(item) => item,
|
||||||
Err(Error) => {
|
Err(item::Error::UnexpectedItemKind) => {
|
||||||
// An error occured, but it should have been reported already.
|
cx.span_err(item.span,
|
||||||
|
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if check_no_str(cx, &item).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let builder = aster::AstBuilder::new().span(span);
|
||||||
|
|
||||||
|
let impl_item = deserialize_item(cx, &builder, &item);
|
||||||
push(Annotatable::Item(impl_item))
|
push(Annotatable::Item(impl_item))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_item(
|
fn deserialize_item(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &ast::Item,
|
item: &item::Item,
|
||||||
) -> Result<P<ast::Item>, Error> {
|
) -> P<ast::Item> {
|
||||||
let item = try!(item::Item::from_ast(cx, "Deserialize", item));
|
|
||||||
try!(check_no_str(cx, &item));
|
|
||||||
|
|
||||||
let impl_generics = build_impl_generics(builder, &item);
|
let impl_generics = build_impl_generics(builder, &item);
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
let ty = builder.ty().path()
|
||||||
@@ -65,7 +67,7 @@ fn deserialize_item(
|
|||||||
|
|
||||||
let dummy_const = builder.id(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
|
let dummy_const = builder.id(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
|
||||||
|
|
||||||
Ok(quote_item!(cx,
|
quote_item!(cx,
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||||
const $dummy_const: () = {
|
const $dummy_const: () = {
|
||||||
extern crate serde as _serde;
|
extern crate serde as _serde;
|
||||||
@@ -78,7 +80,7 @@ fn deserialize_item(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
).unwrap())
|
).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// All the generics in the input, plus a bound `T: Deserialize` for each generic
|
// All the generics in the input, plus a bound `T: Deserialize` for each generic
|
||||||
@@ -114,7 +116,7 @@ fn build_impl_generics(
|
|||||||
// deserialized by us so we do not generate a bound. Fields with a `bound`
|
// deserialized by us so we do not generate a bound. Fields with a `bound`
|
||||||
// attribute specify their own bound so we do not generate one. All other fields
|
// attribute specify their own bound so we do not generate one. All other fields
|
||||||
// may need a `T: Deserialize` bound where T is the type of the field.
|
// may need a `T: Deserialize` bound where T is the type of the field.
|
||||||
fn needs_deserialize_bound(attrs: &attr::FieldAttrs) -> bool {
|
fn needs_deserialize_bound(attrs: &attr::Field) -> bool {
|
||||||
!attrs.skip_deserializing()
|
!attrs.skip_deserializing()
|
||||||
&& attrs.deserialize_with().is_none()
|
&& attrs.deserialize_with().is_none()
|
||||||
&& attrs.de_bound().is_none()
|
&& attrs.de_bound().is_none()
|
||||||
@@ -122,7 +124,7 @@ fn needs_deserialize_bound(attrs: &attr::FieldAttrs) -> bool {
|
|||||||
|
|
||||||
// Fields with a `default` attribute (not `default=...`), and fields with a
|
// Fields with a `default` attribute (not `default=...`), and fields with a
|
||||||
// `skip_deserializing` attribute that do not also have `default=...`.
|
// `skip_deserializing` attribute that do not also have `default=...`.
|
||||||
fn requires_default(attrs: &attr::FieldAttrs) -> bool {
|
fn requires_default(attrs: &attr::Field) -> bool {
|
||||||
attrs.default() == &attr::FieldDefault::Default
|
attrs.default() == &attr::FieldDefault::Default
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +180,7 @@ fn deserialize_body(
|
|||||||
item::Body::Struct(item::Style::Unit, _) => {
|
item::Body::Struct(item::Style::Unit, _) => {
|
||||||
deserialize_unit_struct(
|
deserialize_unit_struct(
|
||||||
cx,
|
cx,
|
||||||
|
builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
&item.attrs)
|
&item.attrs)
|
||||||
}
|
}
|
||||||
@@ -266,10 +269,11 @@ fn deserializer_ty_arg(builder: &aster::AstBuilder) -> P<ast::Ty>{
|
|||||||
|
|
||||||
fn deserialize_unit_struct(
|
fn deserialize_unit_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
struct __Visitor;
|
struct __Visitor;
|
||||||
@@ -305,7 +309,7 @@ fn deserialize_tuple(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@@ -351,11 +355,11 @@ fn deserialize_tuple(
|
|||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
visitor.visit_tuple($nfields, $visitor_expr))
|
visitor.visit_tuple($nfields, $visitor_expr))
|
||||||
} else if nfields == 1 {
|
} else if nfields == 1 {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
|
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
|
||||||
} else {
|
} else {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
||||||
};
|
};
|
||||||
@@ -394,7 +398,7 @@ fn deserialize_seq(
|
|||||||
.map(|(i, field)| {
|
.map(|(i, field)| {
|
||||||
let name = builder.id(format!("__field{}", i));
|
let name = builder.id(format!("__field{}", i));
|
||||||
if field.attrs.skip_deserializing() {
|
if field.attrs.skip_deserializing() {
|
||||||
let default = expr_is_missing(cx, &field.attrs);
|
let default = expr_is_missing(cx, builder, &field.attrs);
|
||||||
quote_stmt!(cx,
|
quote_stmt!(cx,
|
||||||
let $name = $default;
|
let $name = $default;
|
||||||
).unwrap()
|
).unwrap()
|
||||||
@@ -502,7 +506,7 @@ fn deserialize_struct(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@@ -535,7 +539,7 @@ fn deserialize_struct(
|
|||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
impl_generics,
|
impl_generics,
|
||||||
fields,
|
fields,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
let is_enum = variant_ident.is_some();
|
let is_enum = variant_ident.is_some();
|
||||||
@@ -543,7 +547,7 @@ fn deserialize_struct(
|
|||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
visitor.visit_struct(FIELDS, $visitor_expr))
|
visitor.visit_struct(FIELDS, $visitor_expr))
|
||||||
} else {
|
} else {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
|
||||||
};
|
};
|
||||||
@@ -584,11 +588,11 @@ fn deserialize_item_enum(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variants: &[item::Variant],
|
variants: &[item::Variant],
|
||||||
container_attrs: &attr::ContainerAttrs
|
item_attrs: &attr::Item
|
||||||
) -> P<ast::Expr> {
|
) -> 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 = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
let variant_visitor = deserialize_field_visitor(
|
let variant_visitor = deserialize_field_visitor(
|
||||||
cx,
|
cx,
|
||||||
@@ -596,7 +600,7 @@ fn deserialize_item_enum(
|
|||||||
variants.iter()
|
variants.iter()
|
||||||
.map(|variant| variant.attrs.name().deserialize_name())
|
.map(|variant| variant.attrs.name().deserialize_name())
|
||||||
.collect(),
|
.collect(),
|
||||||
container_attrs,
|
item_attrs,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -610,7 +614,7 @@ fn deserialize_item_enum(
|
|||||||
const VARIANTS: &'static [&'static str] = $variants_expr;
|
const VARIANTS: &'static [&'static str] = $variants_expr;
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let ignored_arm = if container_attrs.deny_unknown_fields() {
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(quote_arm!(cx, __Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }))
|
Some(quote_arm!(cx, __Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }))
|
||||||
@@ -630,7 +634,7 @@ fn deserialize_item_enum(
|
|||||||
impl_generics,
|
impl_generics,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
variant,
|
variant,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
let arm = quote_arm!(cx, $variant_name => { $expr });
|
let arm = quote_arm!(cx, $variant_name => { $expr });
|
||||||
@@ -675,7 +679,7 @@ fn deserialize_variant(
|
|||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variant: &item::Variant,
|
variant: &item::Variant,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let variant_ident = variant.ident;
|
let variant_ident = variant.ident;
|
||||||
|
|
||||||
@@ -705,7 +709,7 @@ fn deserialize_variant(
|
|||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
item::Style::Struct => {
|
item::Style::Struct => {
|
||||||
@@ -717,7 +721,7 @@ fn deserialize_variant(
|
|||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -753,7 +757,7 @@ fn deserialize_field_visitor(
|
|||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
field_names: Vec<InternedString>,
|
field_names: Vec<InternedString>,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
is_variant: bool,
|
is_variant: bool,
|
||||||
) -> Vec<P<ast::Item>> {
|
) -> Vec<P<ast::Item>> {
|
||||||
// Create the field names for the fields.
|
// Create the field names for the fields.
|
||||||
@@ -761,7 +765,7 @@ fn deserialize_field_visitor(
|
|||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let ignore_variant = if container_attrs.deny_unknown_fields() {
|
let ignore_variant = if item_attrs.deny_unknown_fields() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let skip_ident = builder.id("__ignore");
|
let skip_ident = builder.id("__ignore");
|
||||||
@@ -792,7 +796,7 @@ fn deserialize_field_visitor(
|
|||||||
(builder.expr().str("expected a field"), builder.id("unknown_field"))
|
(builder.expr().str("expected a field"), builder.id("unknown_field"))
|
||||||
};
|
};
|
||||||
|
|
||||||
let fallthrough_index_arm_expr = if !is_variant && !container_attrs.deny_unknown_fields() {
|
let fallthrough_index_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
||||||
quote_expr!(cx, Ok(__Field::__ignore))
|
quote_expr!(cx, Ok(__Field::__ignore))
|
||||||
} else {
|
} else {
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
@@ -819,7 +823,7 @@ fn deserialize_field_visitor(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let fallthrough_str_arm_expr = if !is_variant && !container_attrs.deny_unknown_fields() {
|
let fallthrough_str_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
||||||
quote_expr!(cx, Ok(__Field::__ignore))
|
quote_expr!(cx, Ok(__Field::__ignore))
|
||||||
} else {
|
} else {
|
||||||
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
|
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
|
||||||
@@ -847,7 +851,7 @@ fn deserialize_field_visitor(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let fallthrough_bytes_arm_expr = if !is_variant && !container_attrs.deny_unknown_fields() {
|
let fallthrough_bytes_arm_expr = if !is_variant && !item_attrs.deny_unknown_fields() {
|
||||||
quote_expr!(cx, Ok(__Field::__ignore))
|
quote_expr!(cx, Ok(__Field::__ignore))
|
||||||
} else {
|
} else {
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
@@ -916,7 +920,7 @@ fn deserialize_struct_visitor(
|
|||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
|
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
|
||||||
let field_exprs = fields.iter()
|
let field_exprs = fields.iter()
|
||||||
.map(|field| field.attrs.name().deserialize_name())
|
.map(|field| field.attrs.name().deserialize_name())
|
||||||
@@ -926,7 +930,7 @@ fn deserialize_struct_visitor(
|
|||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
field_exprs,
|
field_exprs,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -937,7 +941,7 @@ fn deserialize_struct_visitor(
|
|||||||
struct_path,
|
struct_path,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
fields,
|
fields,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
let fields_expr = builder.expr().ref_().slice()
|
let fields_expr = builder.expr().ref_().slice()
|
||||||
@@ -968,7 +972,7 @@ fn deserialize_map(
|
|||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
// Create the field names for the fields.
|
// Create the field names for the fields.
|
||||||
let fields_names = fields.iter()
|
let fields_names = fields.iter()
|
||||||
@@ -1033,7 +1037,7 @@ fn deserialize_map(
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// Visit ignored values to consume them
|
// Visit ignored values to consume them
|
||||||
let ignored_arm = if container_attrs.deny_unknown_fields() {
|
let ignored_arm = if item_attrs.deny_unknown_fields() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(quote_arm!(cx,
|
Some(quote_arm!(cx,
|
||||||
@@ -1044,7 +1048,7 @@ fn deserialize_map(
|
|||||||
let extract_values = fields_names.iter()
|
let extract_values = fields_names.iter()
|
||||||
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
.map(|&(field, name)| {
|
.map(|&(field, name)| {
|
||||||
let missing_expr = expr_is_missing(cx, &field.attrs);
|
let missing_expr = expr_is_missing(cx, builder, &field.attrs);
|
||||||
|
|
||||||
quote_stmt!(cx,
|
quote_stmt!(cx,
|
||||||
let $name = match $name {
|
let $name = match $name {
|
||||||
@@ -1067,7 +1071,7 @@ fn deserialize_map(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
if field.attrs.skip_deserializing() {
|
if field.attrs.skip_deserializing() {
|
||||||
expr_is_missing(cx, &field.attrs)
|
expr_is_missing(cx, builder, &field.attrs)
|
||||||
} else {
|
} else {
|
||||||
builder.expr().id(name)
|
builder.expr().id(name)
|
||||||
}
|
}
|
||||||
@@ -1149,7 +1153,8 @@ fn wrap_deserialize_with(
|
|||||||
|
|
||||||
fn expr_is_missing(
|
fn expr_is_missing(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
attrs: &attr::FieldAttrs,
|
builder: &aster::AstBuilder,
|
||||||
|
attrs: &attr::Field,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
match *attrs.default() {
|
match *attrs.default() {
|
||||||
attr::FieldDefault::Default => {
|
attr::FieldDefault::Default => {
|
||||||
@@ -1161,7 +1166,7 @@ fn expr_is_missing(
|
|||||||
attr::FieldDefault::None => { /* below */ }
|
attr::FieldDefault::None => { /* below */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = attrs.name().deserialize_name_expr();
|
let name = name_expr(builder, attrs.name());
|
||||||
match attrs.deserialize_with() {
|
match attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
quote_expr!(cx, try!(visitor.missing_field($name)))
|
quote_expr!(cx, try!(visitor.missing_field($name)))
|
||||||
@@ -1173,6 +1178,13 @@ fn expr_is_missing(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name_expr(
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
name: &attr::Name,
|
||||||
|
) -> P<ast::Expr> {
|
||||||
|
builder.expr().str(name.deserialize_name())
|
||||||
|
}
|
||||||
|
|
||||||
fn check_no_str(
|
fn check_no_str(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
item: &item::Item,
|
item: &item::Item,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
extern crate aster;
|
extern crate aster;
|
||||||
extern crate quasi;
|
extern crate quasi;
|
||||||
|
extern crate serde_item as item;
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
#[cfg(feature = "with-syntex")]
|
||||||
extern crate syntex;
|
extern crate syntex;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
mod attr;
|
|
||||||
mod bound;
|
mod bound;
|
||||||
mod de;
|
mod de;
|
||||||
mod error;
|
mod error;
|
||||||
mod item;
|
|
||||||
mod ser;
|
mod ser;
|
||||||
|
|||||||
+47
-40
@@ -5,10 +5,8 @@ use syntax::codemap::Span;
|
|||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
use attr;
|
|
||||||
use bound;
|
use bound;
|
||||||
use error::Error;
|
use item::{self, attr};
|
||||||
use item;
|
|
||||||
|
|
||||||
pub fn expand_derive_serialize(
|
pub fn expand_derive_serialize(
|
||||||
cx: &mut ExtCtxt,
|
cx: &mut ExtCtxt,
|
||||||
@@ -27,26 +25,26 @@ pub fn expand_derive_serialize(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let builder = aster::AstBuilder::new().span(span);
|
let item = match item::Item::from_ast(cx, item) {
|
||||||
|
|
||||||
let impl_item = match serialize_item(cx, &builder, &item) {
|
|
||||||
Ok(item) => item,
|
Ok(item) => item,
|
||||||
Err(Error) => {
|
Err(item::Error::UnexpectedItemKind) => {
|
||||||
// An error occured, but it should have been reported already.
|
cx.span_err(item.span,
|
||||||
|
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let builder = aster::AstBuilder::new().span(span);
|
||||||
|
|
||||||
|
let impl_item = serialize_item(cx, &builder, &item);
|
||||||
push(Annotatable::Item(impl_item))
|
push(Annotatable::Item(impl_item))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_item(
|
fn serialize_item(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
item: &ast::Item,
|
item: &item::Item,
|
||||||
) -> Result<P<ast::Item>, Error> {
|
) -> P<ast::Item> {
|
||||||
let item = try!(item::Item::from_ast(cx, "Serialize", item));
|
|
||||||
|
|
||||||
let impl_generics = build_impl_generics(builder, &item);
|
let impl_generics = build_impl_generics(builder, &item);
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
let ty = builder.ty().path()
|
||||||
@@ -63,7 +61,7 @@ fn serialize_item(
|
|||||||
|
|
||||||
let dummy_const = builder.id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
let dummy_const = builder.id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
||||||
|
|
||||||
Ok(quote_item!(cx,
|
quote_item!(cx,
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||||
const $dummy_const: () = {
|
const $dummy_const: () = {
|
||||||
extern crate serde as _serde;
|
extern crate serde as _serde;
|
||||||
@@ -76,7 +74,7 @@ fn serialize_item(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
).unwrap())
|
).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -107,7 +105,7 @@ fn build_impl_generics(
|
|||||||
// serialized by us so we do not generate a bound. Fields with a `bound`
|
// serialized by us so we do not generate a bound. Fields with a `bound`
|
||||||
// attribute specify their own bound so we do not generate one. All other fields
|
// attribute specify their own bound so we do not generate one. All other fields
|
||||||
// may need a `T: Serialize` bound where T is the type of the field.
|
// may need a `T: Serialize` bound where T is the type of the field.
|
||||||
fn needs_serialize_bound(attrs: &attr::FieldAttrs) -> bool {
|
fn needs_serialize_bound(attrs: &attr::Field) -> bool {
|
||||||
!attrs.skip_serializing()
|
!attrs.skip_serializing()
|
||||||
&& attrs.serialize_with().is_none()
|
&& attrs.serialize_with().is_none()
|
||||||
&& attrs.ser_bound().is_none()
|
&& attrs.ser_bound().is_none()
|
||||||
@@ -169,6 +167,7 @@ fn serialize_body(
|
|||||||
item::Body::Struct(item::Style::Unit, _) => {
|
item::Body::Struct(item::Style::Unit, _) => {
|
||||||
serialize_unit_struct(
|
serialize_unit_struct(
|
||||||
cx,
|
cx,
|
||||||
|
builder,
|
||||||
&item.attrs)
|
&item.attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,9 +175,10 @@ fn serialize_body(
|
|||||||
|
|
||||||
fn serialize_unit_struct(
|
fn serialize_unit_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
builder: &aster::AstBuilder,
|
||||||
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
_serializer.serialize_unit_struct($type_name)
|
_serializer.serialize_unit_struct($type_name)
|
||||||
@@ -189,16 +189,16 @@ fn serialize_newtype_struct(
|
|||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
container_ty: P<ast::Ty>,
|
item_ty: P<ast::Ty>,
|
||||||
field: &item::Field,
|
field: &item::Field,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
let mut field_expr = quote_expr!(cx, &self.0);
|
let mut field_expr = quote_expr!(cx, &self.0);
|
||||||
if let Some(path) = field.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);
|
&item_ty, impl_generics, &field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
@@ -212,7 +212,7 @@ fn serialize_tuple_struct(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
@@ -228,7 +228,7 @@ fn serialize_tuple_struct(
|
|||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
@@ -247,7 +247,7 @@ fn serialize_struct(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
@@ -263,7 +263,7 @@ fn serialize_struct(
|
|||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
@@ -283,7 +283,7 @@ fn serialize_item_enum(
|
|||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variants: &[item::Variant],
|
variants: &[item::Variant],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let arms: Vec<_> =
|
let arms: Vec<_> =
|
||||||
variants.iter()
|
variants.iter()
|
||||||
@@ -297,7 +297,7 @@ fn serialize_item_enum(
|
|||||||
ty.clone(),
|
ty.clone(),
|
||||||
variant,
|
variant,
|
||||||
variant_index,
|
variant_index,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -317,12 +317,12 @@ fn serialize_variant(
|
|||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
variant: &item::Variant,
|
variant: &item::Variant,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> ast::Arm {
|
) -> ast::Arm {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
let variant_ident = variant.ident;
|
let variant_ident = variant.ident;
|
||||||
let variant_name = variant.attrs.name().serialize_name_expr();
|
let variant_name = name_expr(builder, variant.attrs.name());
|
||||||
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
item::Style::Unit => {
|
item::Style::Unit => {
|
||||||
@@ -414,7 +414,7 @@ fn serialize_variant(
|
|||||||
ty,
|
ty,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
field_names,
|
field_names,
|
||||||
container_attrs,
|
item_attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
@@ -430,14 +430,14 @@ fn serialize_newtype_variant(
|
|||||||
type_name: P<ast::Expr>,
|
type_name: P<ast::Expr>,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant_name: P<ast::Expr>,
|
variant_name: P<ast::Expr>,
|
||||||
container_ty: P<ast::Ty>,
|
item_ty: P<ast::Ty>,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
field: &item::Field,
|
field: &item::Field,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let mut field_expr = quote_expr!(cx, __simple_value);
|
let mut field_expr = quote_expr!(cx, __simple_value);
|
||||||
if let Some(path) = field.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);
|
&item_ty, generics, &field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
@@ -512,7 +512,7 @@ fn serialize_struct_variant(
|
|||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: &[item::Field],
|
fields: &[item::Field],
|
||||||
field_names: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let variant_generics = builder.generics()
|
let variant_generics = builder.generics()
|
||||||
.with(generics.clone())
|
.with(generics.clone())
|
||||||
@@ -572,14 +572,14 @@ fn serialize_struct_variant(
|
|||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let container_name = container_attrs.name().serialize_name_expr();
|
let item_name = name_expr(builder, item_attrs.name());
|
||||||
|
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$variant_struct
|
$variant_struct
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
_serializer.serialize_struct_variant(
|
_serializer.serialize_struct_variant(
|
||||||
$container_name,
|
$item_name,
|
||||||
$variant_index,
|
$variant_index,
|
||||||
$variant_name,
|
$variant_name,
|
||||||
Visitor {
|
Visitor {
|
||||||
@@ -692,7 +692,7 @@ fn serialize_struct_visitor(
|
|||||||
field_expr = quote_expr!(cx, &$field_expr);
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let key_expr = field.attrs.name().serialize_name_expr();
|
let key_expr = name_expr(builder, field.attrs.name());
|
||||||
|
|
||||||
let continue_if_skip = field.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 }));
|
||||||
@@ -781,7 +781,7 @@ fn serialize_struct_visitor(
|
|||||||
fn wrap_serialize_with(
|
fn wrap_serialize_with(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
container_ty: &P<ast::Ty>,
|
item_ty: &P<ast::Ty>,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
field_ty: &P<ast::Ty>,
|
field_ty: &P<ast::Ty>,
|
||||||
path: &ast::Path,
|
path: &ast::Path,
|
||||||
@@ -803,7 +803,7 @@ fn wrap_serialize_with(
|
|||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
struct __SerializeWith $wrapper_generics $where_clause {
|
struct __SerializeWith $wrapper_generics $where_clause {
|
||||||
value: &'__a $field_ty,
|
value: &'__a $field_ty,
|
||||||
phantom: ::std::marker::PhantomData<$container_ty>,
|
phantom: ::std::marker::PhantomData<$item_ty>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $wrapper_generics _serde::ser::Serialize for $wrapper_ty $where_clause {
|
impl $wrapper_generics _serde::ser::Serialize for $wrapper_ty $where_clause {
|
||||||
@@ -816,7 +816,14 @@ fn wrap_serialize_with(
|
|||||||
|
|
||||||
__SerializeWith {
|
__SerializeWith {
|
||||||
value: $value,
|
value: $value,
|
||||||
phantom: ::std::marker::PhantomData::<$container_ty>,
|
phantom: ::std::marker::PhantomData::<$item_ty>,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn name_expr(
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
name: &attr::Name,
|
||||||
|
) -> P<ast::Expr> {
|
||||||
|
builder.expr().str(name.serialize_name())
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "serde_item"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
description = "AST representation used by Serde codegen. Unstable."
|
||||||
|
repository = "https://github.com/serde-rs/serde"
|
||||||
|
documentation = "https://github.com/serde-rs/serde"
|
||||||
|
keywords = ["serde", "serialization"]
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["with-syntex"]
|
||||||
|
nightly-testing = ["clippy"]
|
||||||
|
with-syntex = ["syntex_syntax"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clippy = { version = "^0.*", optional = true }
|
||||||
|
syntex_syntax = { version = "^0.35.0", optional = true }
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use syntax::ast::{self, TokenTree};
|
use syntax::ast::{self, TokenTree};
|
||||||
use syntax::attr;
|
use syntax::attr;
|
||||||
use syntax::codemap::{Span, Spanned, respan};
|
use syntax::codemap::{Span, Spanned, respan};
|
||||||
@@ -10,14 +11,9 @@ use syntax::parse;
|
|||||||
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
use aster::AstBuilder;
|
|
||||||
use aster::ident::ToIdent;
|
|
||||||
|
|
||||||
use error::Error;
|
|
||||||
|
|
||||||
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
||||||
// are `ContainerAttrs::from_item`, `VariantAttrs::from_variant`, and
|
// are `attr::Item::from_ast`, `attr::Variant::from_ast`, and
|
||||||
// `FieldAttrs::from_field`. Each returns an instance of the corresponding
|
// `attr::Field::from_ast`. Each returns an instance of the corresponding
|
||||||
// struct. Note that none of them return a Result. Unrecognized, malformed, or
|
// struct. Note that none of them return a Result. Unrecognized, malformed, or
|
||||||
// duplicated attributes result in a span_err but otherwise are ignored. The
|
// duplicated attributes result in a span_err but otherwise are ignored. The
|
||||||
// user will see errors simultaneously for all bad attributes in the crate
|
// user will see errors simultaneously for all bad attributes in the crate
|
||||||
@@ -87,57 +83,42 @@ impl<'a, 'b> BoolAttr<'a, 'b> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Name {
|
pub struct Name {
|
||||||
ident: ast::Ident,
|
serialize: InternedString,
|
||||||
serialize_name: Option<InternedString>,
|
deserialize: InternedString,
|
||||||
deserialize_name: Option<InternedString>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Name {
|
impl Name {
|
||||||
/// Return the container name for the container when serializing.
|
/// Return the container name for the container when serializing.
|
||||||
pub fn serialize_name(&self) -> InternedString {
|
pub fn serialize_name(&self) -> InternedString {
|
||||||
match self.serialize_name {
|
self.serialize.clone()
|
||||||
Some(ref name) => name.clone(),
|
|
||||||
None => self.ident.name.as_str(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the container name expression for the container when deserializing.
|
|
||||||
pub fn serialize_name_expr(&self) -> P<ast::Expr> {
|
|
||||||
AstBuilder::new().expr().str(self.serialize_name())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the container name for the container when deserializing.
|
/// Return the container name for the container when deserializing.
|
||||||
pub fn deserialize_name(&self) -> InternedString {
|
pub fn deserialize_name(&self) -> InternedString {
|
||||||
match self.deserialize_name {
|
self.deserialize.clone()
|
||||||
Some(ref name) => name.clone(),
|
|
||||||
None => self.ident.name.as_str(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the container name expression for the container when deserializing.
|
|
||||||
pub fn deserialize_name_expr(&self) -> P<ast::Expr> {
|
|
||||||
AstBuilder::new().expr().str(self.deserialize_name())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents container (e.g. struct) attribute information
|
/// Represents container (e.g. struct) attribute information
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ContainerAttrs {
|
pub struct Item {
|
||||||
name: Name,
|
name: Name,
|
||||||
deny_unknown_fields: bool,
|
deny_unknown_fields: bool,
|
||||||
ser_bound: Option<Vec<ast::WherePredicate>>,
|
ser_bound: Option<Vec<ast::WherePredicate>>,
|
||||||
de_bound: Option<Vec<ast::WherePredicate>>,
|
de_bound: Option<Vec<ast::WherePredicate>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContainerAttrs {
|
impl Item {
|
||||||
/// 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) -> Self {
|
pub fn from_ast(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");
|
||||||
let mut ser_bound = Attr::none(cx, "bound");
|
let mut ser_bound = Attr::none(cx, "bound");
|
||||||
let mut de_bound = Attr::none(cx, "bound");
|
let mut de_bound = Attr::none(cx, "bound");
|
||||||
|
|
||||||
|
let ident = item.ident.name.as_str();
|
||||||
|
|
||||||
for meta_items in item.attrs().iter().filter_map(get_serde_meta_items) {
|
for meta_items in item.attrs().iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
let span = meta_item.span;
|
let span = meta_item.span;
|
||||||
@@ -189,11 +170,10 @@ impl ContainerAttrs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ContainerAttrs {
|
Item {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: item.ident,
|
serialize: ser_name.get().unwrap_or(ident.clone()),
|
||||||
serialize_name: ser_name.get(),
|
deserialize: de_name.get().unwrap_or(ident),
|
||||||
deserialize_name: de_name.get(),
|
|
||||||
},
|
},
|
||||||
deny_unknown_fields: deny_unknown_fields.get(),
|
deny_unknown_fields: deny_unknown_fields.get(),
|
||||||
ser_bound: ser_bound.get(),
|
ser_bound: ser_bound.get(),
|
||||||
@@ -220,15 +200,17 @@ impl ContainerAttrs {
|
|||||||
|
|
||||||
/// Represents variant attribute information
|
/// Represents variant attribute information
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct VariantAttrs {
|
pub struct Variant {
|
||||||
name: Name,
|
name: Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VariantAttrs {
|
impl Variant {
|
||||||
pub fn from_variant(cx: &ExtCtxt, variant: &ast::Variant) -> Self {
|
pub fn from_ast(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");
|
||||||
|
|
||||||
|
let ident = variant.node.name.name.as_str();
|
||||||
|
|
||||||
for meta_items in variant.node.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in variant.node.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
let span = meta_item.span;
|
let span = meta_item.span;
|
||||||
@@ -259,11 +241,10 @@ impl VariantAttrs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VariantAttrs {
|
Variant {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: variant.node.name,
|
serialize: ser_name.get().unwrap_or(ident.clone()),
|
||||||
serialize_name: ser_name.get(),
|
deserialize: de_name.get().unwrap_or(ident),
|
||||||
deserialize_name: de_name.get(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -275,7 +256,7 @@ impl VariantAttrs {
|
|||||||
|
|
||||||
/// Represents field attribute information
|
/// Represents field attribute information
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FieldAttrs {
|
pub struct Field {
|
||||||
name: Name,
|
name: Name,
|
||||||
skip_serializing: bool,
|
skip_serializing: bool,
|
||||||
skip_deserializing: bool,
|
skip_deserializing: bool,
|
||||||
@@ -298,11 +279,11 @@ pub enum FieldDefault {
|
|||||||
Path(ast::Path),
|
Path(ast::Path),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FieldAttrs {
|
impl Field {
|
||||||
/// 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_ast(cx: &ExtCtxt,
|
||||||
index: usize,
|
index: usize,
|
||||||
field: &ast::StructField) -> Self {
|
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");
|
||||||
@@ -314,9 +295,9 @@ impl FieldAttrs {
|
|||||||
let mut ser_bound = Attr::none(cx, "bound");
|
let mut ser_bound = Attr::none(cx, "bound");
|
||||||
let mut de_bound = Attr::none(cx, "bound");
|
let mut de_bound = Attr::none(cx, "bound");
|
||||||
|
|
||||||
let field_ident = match field.ident {
|
let ident = match field.ident {
|
||||||
Some(ident) => ident,
|
Some(ident) => ident.name.as_str(),
|
||||||
None => index.to_string().to_ident(),
|
None => token::intern_and_get_ident(&index.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
@@ -414,11 +395,10 @@ impl FieldAttrs {
|
|||||||
default.set_if_none(span, FieldDefault::Default);
|
default.set_if_none(span, FieldDefault::Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
FieldAttrs {
|
Field {
|
||||||
name: Name {
|
name: Name {
|
||||||
ident: field_ident,
|
serialize: ser_name.get().unwrap_or(ident.clone()),
|
||||||
serialize_name: ser_name.get(),
|
deserialize: de_name.get().unwrap_or(ident),
|
||||||
deserialize_name: de_name.get(),
|
|
||||||
},
|
},
|
||||||
skip_serializing: skip_serializing.get(),
|
skip_serializing: skip_serializing.get(),
|
||||||
skip_deserializing: skip_deserializing.get(),
|
skip_deserializing: skip_deserializing.get(),
|
||||||
@@ -473,8 +453,8 @@ fn get_ser_and_de<T, F>(
|
|||||||
attribute: &'static str,
|
attribute: &'static str,
|
||||||
items: &[P<ast::MetaItem>],
|
items: &[P<ast::MetaItem>],
|
||||||
f: F
|
f: F
|
||||||
) -> Result<(Option<Spanned<T>>, Option<Spanned<T>>), Error>
|
) -> Result<(Option<Spanned<T>>, Option<Spanned<T>>), ()>
|
||||||
where F: Fn(&ExtCtxt, &str, &ast::Lit) -> Result<T, Error>,
|
where F: Fn(&ExtCtxt, &str, &ast::Lit) -> Result<T, ()>,
|
||||||
{
|
{
|
||||||
let mut ser_item = Attr::none(cx, attribute);
|
let mut ser_item = Attr::none(cx, attribute);
|
||||||
let mut de_item = Attr::none(cx, attribute);
|
let mut de_item = Attr::none(cx, attribute);
|
||||||
@@ -500,7 +480,7 @@ fn get_ser_and_de<T, F>(
|
|||||||
attribute,
|
attribute,
|
||||||
meta_item_to_string(item)));
|
meta_item_to_string(item)));
|
||||||
|
|
||||||
return Err(Error);
|
return Err(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -511,14 +491,14 @@ fn get_ser_and_de<T, F>(
|
|||||||
fn get_renames(
|
fn get_renames(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
items: &[P<ast::MetaItem>],
|
items: &[P<ast::MetaItem>],
|
||||||
) -> Result<(Option<Spanned<InternedString>>, Option<Spanned<InternedString>>), Error> {
|
) -> Result<(Option<Spanned<InternedString>>, Option<Spanned<InternedString>>), ()> {
|
||||||
get_ser_and_de(cx, "rename", items, get_str_from_lit)
|
get_ser_and_de(cx, "rename", items, get_str_from_lit)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_where_predicates(
|
fn get_where_predicates(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
items: &[P<ast::MetaItem>],
|
items: &[P<ast::MetaItem>],
|
||||||
) -> Result<(Option<Spanned<Vec<ast::WherePredicate>>>, Option<Spanned<Vec<ast::WherePredicate>>>), Error> {
|
) -> Result<(Option<Spanned<Vec<ast::WherePredicate>>>, Option<Spanned<Vec<ast::WherePredicate>>>), ()> {
|
||||||
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -579,7 +559,7 @@ impl<'a, 'b> Folder for Respanner<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_str_from_lit(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<InternedString, Error> {
|
fn get_str_from_lit(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<InternedString, ()> {
|
||||||
match lit.node {
|
match lit.node {
|
||||||
ast::LitKind::Str(ref s, _) => Ok(s.clone()),
|
ast::LitKind::Str(ref s, _) => Ok(s.clone()),
|
||||||
_ => {
|
_ => {
|
||||||
@@ -589,7 +569,7 @@ fn get_str_from_lit(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Interned
|
|||||||
name,
|
name,
|
||||||
lit_to_string(lit)));
|
lit_to_string(lit)));
|
||||||
|
|
||||||
return Err(Error);
|
return Err(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -600,7 +580,7 @@ fn get_str_from_lit(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Interned
|
|||||||
// into a token tree. Then we'll update those spans to say they're coming from a
|
// into a token tree. Then we'll update those spans to say they're coming from a
|
||||||
// macro context that originally came from the attribnute, and then finally
|
// macro context that originally came from the attribnute, and then finally
|
||||||
// parse them into an expression or where-clause.
|
// parse them into an expression or where-clause.
|
||||||
fn parse_string_via_tts<T, F>(cx: &ExtCtxt, name: &str, string: String, action: F) -> Result<T, Error>
|
fn parse_string_via_tts<T, F>(cx: &ExtCtxt, name: &str, string: String, action: F) -> Result<T, ()>
|
||||||
where F: for<'a> Fn(&'a mut Parser) -> parse::PResult<'a, T>,
|
where F: for<'a> Fn(&'a mut Parser) -> parse::PResult<'a, T>,
|
||||||
{
|
{
|
||||||
let tts = panictry!(parse::parse_tts_from_source_str(
|
let tts = panictry!(parse::parse_tts_from_source_str(
|
||||||
@@ -618,7 +598,7 @@ fn parse_string_via_tts<T, F>(cx: &ExtCtxt, name: &str, string: String, action:
|
|||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(mut e) => {
|
Err(mut e) => {
|
||||||
e.emit();
|
e.emit();
|
||||||
return Err(Error);
|
return Err(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -627,14 +607,14 @@ fn parse_string_via_tts<T, F>(cx: &ExtCtxt, name: &str, string: String, action:
|
|||||||
Ok(()) => { }
|
Ok(()) => { }
|
||||||
Err(mut e) => {
|
Err(mut e) => {
|
||||||
e.emit();
|
e.emit();
|
||||||
return Err(Error);
|
return Err(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::Path, Error> {
|
fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::Path, ()> {
|
||||||
let string = try!(get_str_from_lit(cx, name, lit)).to_string();
|
let string = try!(get_str_from_lit(cx, name, lit)).to_string();
|
||||||
|
|
||||||
parse_string_via_tts(cx, name, string, |parser| {
|
parse_string_via_tts(cx, name, string, |parser| {
|
||||||
@@ -642,7 +622,7 @@ fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_lit_into_where(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Vec<ast::WherePredicate>, Error> {
|
fn parse_lit_into_where(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Vec<ast::WherePredicate>, ()> {
|
||||||
let string = try!(get_str_from_lit(cx, name, lit));
|
let string = try!(get_str_from_lit(cx, name, lit));
|
||||||
if string.is_empty() {
|
if string.is_empty() {
|
||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use std::error;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
UnexpectedItemKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "expected a struct or enum")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl error::Error for Error {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
"expected a struct or enum"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,29 @@
|
|||||||
|
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
||||||
|
#![cfg_attr(feature = "nightly-testing", feature(plugin))]
|
||||||
|
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
||||||
|
|
||||||
|
#[cfg(feature = "with-syntex")]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate syntex_syntax as syntax;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "with-syntex"))]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate syntax;
|
||||||
|
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap;
|
use syntax::codemap;
|
||||||
use syntax::ext::base::ExtCtxt;
|
use syntax::ext::base::ExtCtxt;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
|
|
||||||
use attr;
|
pub mod attr;
|
||||||
use error::Error;
|
|
||||||
|
mod error;
|
||||||
|
pub use error::Error;
|
||||||
|
|
||||||
pub struct Item<'a> {
|
pub struct Item<'a> {
|
||||||
pub ident: ast::Ident,
|
pub ident: ast::Ident,
|
||||||
pub span: codemap::Span,
|
pub span: codemap::Span,
|
||||||
pub attrs: attr::ContainerAttrs,
|
pub attrs: attr::Item,
|
||||||
pub body: Body<'a>,
|
pub body: Body<'a>,
|
||||||
pub generics: &'a ast::Generics,
|
pub generics: &'a ast::Generics,
|
||||||
}
|
}
|
||||||
@@ -21,7 +35,8 @@ pub enum Body<'a> {
|
|||||||
|
|
||||||
pub struct Variant<'a> {
|
pub struct Variant<'a> {
|
||||||
pub ident: ast::Ident,
|
pub ident: ast::Ident,
|
||||||
pub attrs: attr::VariantAttrs,
|
pub span: codemap::Span,
|
||||||
|
pub attrs: attr::Variant,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub fields: Vec<Field<'a>>,
|
pub fields: Vec<Field<'a>>,
|
||||||
}
|
}
|
||||||
@@ -29,7 +44,7 @@ pub struct Variant<'a> {
|
|||||||
pub struct Field<'a> {
|
pub struct Field<'a> {
|
||||||
pub ident: Option<ast::Ident>,
|
pub ident: Option<ast::Ident>,
|
||||||
pub span: codemap::Span,
|
pub span: codemap::Span,
|
||||||
pub attrs: attr::FieldAttrs,
|
pub attrs: attr::Field,
|
||||||
pub ty: &'a P<ast::Ty>,
|
pub ty: &'a P<ast::Ty>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,10 +58,9 @@ pub enum Style {
|
|||||||
impl<'a> Item<'a> {
|
impl<'a> Item<'a> {
|
||||||
pub fn from_ast(
|
pub fn from_ast(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
derive_trait: &'static str,
|
|
||||||
item: &'a ast::Item,
|
item: &'a ast::Item,
|
||||||
) -> Result<Item<'a>, Error> {
|
) -> Result<Item<'a>, Error> {
|
||||||
let attrs = attr::ContainerAttrs::from_item(cx, item);
|
let attrs = attr::Item::from_ast(cx, item);
|
||||||
|
|
||||||
let (body, generics) = match item.node {
|
let (body, generics) = match item.node {
|
||||||
ast::ItemKind::Enum(ref enum_def, ref generics) => {
|
ast::ItemKind::Enum(ref enum_def, ref generics) => {
|
||||||
@@ -58,10 +72,7 @@ impl<'a> Item<'a> {
|
|||||||
(Body::Struct(style, fields), generics)
|
(Body::Struct(style, fields), generics)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(item.span, &format!(
|
return Err(Error::UnexpectedItemKind);
|
||||||
"`#[derive({})]` may only be applied to structs and enums",
|
|
||||||
derive_trait));
|
|
||||||
return Err(Error);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -75,6 +86,20 @@ impl<'a> Item<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn enum_from_ast<'a>(
|
fn enum_from_ast<'a>(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
enum_def: &'a ast::EnumDef,
|
enum_def: &'a ast::EnumDef,
|
||||||
@@ -84,7 +109,8 @@ fn enum_from_ast<'a>(
|
|||||||
let (style, fields) = struct_from_ast(cx, &variant.node.data);
|
let (style, fields) = struct_from_ast(cx, &variant.node.data);
|
||||||
Variant {
|
Variant {
|
||||||
ident: variant.node.name,
|
ident: variant.node.name,
|
||||||
attrs: attr::VariantAttrs::from_variant(cx, variant),
|
span: variant.span,
|
||||||
|
attrs: attr::Variant::from_ast(cx, variant),
|
||||||
style: style,
|
style: style,
|
||||||
fields: fields,
|
fields: fields,
|
||||||
}
|
}
|
||||||
@@ -122,23 +148,9 @@ fn fields_from_ast<'a>(
|
|||||||
Field {
|
Field {
|
||||||
ident: field.ident,
|
ident: field.ident,
|
||||||
span: field.span,
|
span: field.span,
|
||||||
attrs: attr::FieldAttrs::from_field(cx, i, field),
|
attrs: attr::Field::from_ast(cx, i, field),
|
||||||
ty: &field.ty,
|
ty: &field.ty,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user