Merge pull request #173 from erickt/update
Update aster, quasi, and syntex
This commit is contained in:
commit
72de877ec3
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
@ -10,8 +10,7 @@ readme = "../README.md"
|
|||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num = "*"
|
num = "^0.1.27"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
nightly = []
|
nightly = []
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen"
|
name = "serde_codegen"
|
||||||
version = "0.5.3"
|
version = "0.6.1"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Macros to auto-generate implementations for the serde framework"
|
description = "Macros to auto-generate implementations for the serde framework"
|
||||||
@ -15,12 +15,12 @@ nightly = ["quasi_macros"]
|
|||||||
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", "syntex", "syntex_syntax"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
quasi_codegen = { verision = "*", optional = true }
|
quasi_codegen = { verision = "^0.3.4", optional = true }
|
||||||
syntex = { version = "*", optional = true }
|
syntex = { version = "^0.18.0", optional = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aster = { version = "*", default-features = false }
|
aster = { version = "^0.5.0", default-features = false }
|
||||||
quasi = { verision = "*", default-features = false }
|
quasi = { verision = "^0.3.5", default-features = false }
|
||||||
quasi_macros = { version = "*", optional = true }
|
quasi_macros = { version = "^0.3.5", optional = true }
|
||||||
syntex = { version = "*", optional = true }
|
syntex = { version = "^0.17.0", optional = true }
|
||||||
syntex_syntax = { version = "*", optional = true }
|
syntex_syntax = { version = "^0.18.0", optional = true }
|
||||||
|
@ -4,12 +4,11 @@ use aster;
|
|||||||
|
|
||||||
use syntax::ast::{
|
use syntax::ast::{
|
||||||
self,
|
self,
|
||||||
Ident,
|
|
||||||
MetaItem,
|
|
||||||
Item,
|
|
||||||
Expr,
|
|
||||||
StructDef,
|
|
||||||
EnumDef,
|
EnumDef,
|
||||||
|
Expr,
|
||||||
|
Ident,
|
||||||
|
Item,
|
||||||
|
MetaItem,
|
||||||
};
|
};
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||||
@ -87,14 +86,14 @@ fn deserialize_body(
|
|||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::ItemStruct(ref struct_def, _) => {
|
ast::ItemStruct(ref variant_data, _) => {
|
||||||
deserialize_item_struct(
|
deserialize_item_struct(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
item,
|
item,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
variant_data,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::ItemEnum(ref enum_def, _) => {
|
ast::ItemEnum(ref enum_def, _) => {
|
||||||
@ -117,27 +116,17 @@ fn deserialize_item_struct(
|
|||||||
item: &Item,
|
item: &Item,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
struct_def: &ast::StructDef,
|
variant_data: &ast::VariantData,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let mut named_fields = vec![];
|
match *variant_data {
|
||||||
let mut unnamed_fields = 0;
|
ast::VariantData::Unit(_) => {
|
||||||
|
|
||||||
for field in struct_def.fields.iter() {
|
|
||||||
match field.node.kind {
|
|
||||||
ast::NamedField(name, _) => { named_fields.push(name); }
|
|
||||||
ast::UnnamedField(_) => { unnamed_fields += 1; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match (named_fields.is_empty(), unnamed_fields) {
|
|
||||||
(true, 0) => {
|
|
||||||
deserialize_unit_struct(
|
deserialize_unit_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(true, 1) => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
deserialize_newtype_struct(
|
deserialize_newtype_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
@ -146,29 +135,34 @@ fn deserialize_item_struct(
|
|||||||
ty,
|
ty,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(true, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
|
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
||||||
|
cx.bug("tuple struct has named fields")
|
||||||
|
}
|
||||||
|
|
||||||
deserialize_tuple_struct(
|
deserialize_tuple_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
unnamed_fields,
|
fields.len(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(false, 0) => {
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
|
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
||||||
|
cx.bug("struct has unnamed fields")
|
||||||
|
}
|
||||||
|
|
||||||
deserialize_struct(
|
deserialize_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
fields,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(false, _) => {
|
|
||||||
cx.bug("struct has named and unnamed fields")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -422,9 +416,9 @@ fn deserialize_struct_as_seq(
|
|||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
struct_def: &StructDef,
|
fields: &[ast::StructField],
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let let_values: Vec<P<ast::Stmt>> = (0 .. struct_def.fields.len())
|
let let_values: Vec<P<ast::Stmt>> = (0 .. fields.len())
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
let name = builder.id(format!("__field{}", i));
|
let name = builder.id(format!("__field{}", i));
|
||||||
quote_stmt!(cx,
|
quote_stmt!(cx,
|
||||||
@ -440,13 +434,13 @@ fn deserialize_struct_as_seq(
|
|||||||
|
|
||||||
let result = builder.expr().struct_path(struct_path)
|
let result = builder.expr().struct_path(struct_path)
|
||||||
.with_id_exprs(
|
.with_id_exprs(
|
||||||
struct_def.fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, field)| {
|
.map(|(i, field)| {
|
||||||
(
|
(
|
||||||
match field.node.kind {
|
match field.node.kind {
|
||||||
ast::NamedField(name, _) => name.clone(),
|
ast::NamedField(name, _) => name.clone(),
|
||||||
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
ast::UnnamedField(_) => cx.bug("struct contains unnamed fields"),
|
||||||
},
|
},
|
||||||
builder.expr().id(format!("__field{}", i)),
|
builder.expr().id(format!("__field{}", i)),
|
||||||
)
|
)
|
||||||
@ -469,7 +463,7 @@ fn deserialize_struct(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
struct_def: &StructDef,
|
fields: &[ast::StructField],
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
@ -486,14 +480,14 @@ fn deserialize_struct(
|
|||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
struct_def
|
fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
struct_def,
|
type_path.clone(),
|
||||||
type_path.clone()
|
fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = builder.expr().str(type_ident);
|
let type_name = builder.expr().str(type_ident);
|
||||||
@ -628,20 +622,20 @@ fn deserialize_variant(
|
|||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let variant_ident = variant.node.name;
|
let variant_ident = variant.node.name;
|
||||||
|
|
||||||
match variant.node.kind {
|
match *variant.node.data {
|
||||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
ast::VariantData::Unit(_) => {
|
||||||
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::TupleVariantKind(ref args) if args.len() == 1 => {
|
ast::VariantData::Tuple(ref args, _) if args.len() == 1 => {
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
let val = try!(visitor.visit_newtype());
|
let val = try!(visitor.visit_newtype());
|
||||||
Ok($type_ident::$variant_ident(val))
|
Ok($type_ident::$variant_ident(val))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
ast::TupleVariantKind(ref args) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
deserialize_tuple_variant(
|
deserialize_tuple_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
@ -649,10 +643,10 @@ fn deserialize_variant(
|
|||||||
variant_ident,
|
variant_ident,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
args.len(),
|
fields.len(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::StructVariantKind(ref struct_def) => {
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
deserialize_struct_variant(
|
deserialize_struct_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
@ -660,7 +654,7 @@ fn deserialize_variant(
|
|||||||
variant_ident,
|
variant_ident,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
fields,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -716,7 +710,7 @@ fn deserialize_struct_variant(
|
|||||||
variant_ident: ast::Ident,
|
variant_ident: ast::Ident,
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
struct_def: &ast::StructDef,
|
fields: &[ast::StructField],
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let where_clause = &generics.where_clause;
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
@ -729,14 +723,14 @@ fn deserialize_struct_variant(
|
|||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
struct_def
|
fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let (field_visitor, fields_stmt, field_expr) = deserialize_struct_visitor(
|
let (field_visitor, fields_stmt, field_expr) = deserialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
struct_def,
|
type_path,
|
||||||
type_path
|
fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||||
@ -791,7 +785,7 @@ fn deserialize_field_visitor(
|
|||||||
.enum_("__Field")
|
.enum_("__Field")
|
||||||
.with_variants(
|
.with_variants(
|
||||||
field_idents.iter().map(|field_ident| {
|
field_idents.iter().map(|field_ident| {
|
||||||
builder.variant(field_ident).tuple().build()
|
builder.variant(field_ident).unit()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
@ -927,25 +921,25 @@ fn deserialize_field_visitor(
|
|||||||
fn deserialize_struct_visitor(
|
fn deserialize_struct_visitor(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
struct_def: &ast::StructDef,
|
|
||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
|
fields: &[ast::StructField],
|
||||||
) -> (Vec<P<ast::Item>>, P<ast::Stmt>, P<ast::Expr>) {
|
) -> (Vec<P<ast::Item>>, P<ast::Stmt>, P<ast::Expr>) {
|
||||||
let field_visitor = deserialize_field_visitor(
|
let field_visitor = deserialize_field_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
field::struct_field_attrs(cx, builder, struct_def),
|
field::struct_field_attrs(cx, builder, fields),
|
||||||
);
|
);
|
||||||
|
|
||||||
let visit_map_expr = deserialize_map(
|
let visit_map_expr = deserialize_map(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
struct_path,
|
struct_path,
|
||||||
struct_def,
|
fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
let fields_expr = builder.expr().addr_of().slice()
|
let fields_expr = builder.expr().addr_of().slice()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
struct_def.fields.iter()
|
fields.iter()
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
match field.node.kind {
|
match field.node.kind {
|
||||||
ast::NamedField(name, _) => builder.expr().str(name),
|
ast::NamedField(name, _) => builder.expr().str(name),
|
||||||
@ -966,10 +960,10 @@ fn deserialize_map(
|
|||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
struct_path: ast::Path,
|
struct_path: ast::Path,
|
||||||
struct_def: &StructDef,
|
fields: &[ast::StructField],
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
// Create the field names for the fields.
|
// Create the field names for the fields.
|
||||||
let field_names: Vec<ast::Ident> = (0 .. struct_def.fields.len())
|
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -990,7 +984,7 @@ fn deserialize_map(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||||
.zip(field::struct_field_attrs(cx, builder, struct_def).iter())
|
.zip(field::struct_field_attrs(cx, builder, fields).iter())
|
||||||
.map(|(field_name, field_attr)| {
|
.map(|(field_name, field_attr)| {
|
||||||
let missing_expr = if field_attr.use_default() {
|
let missing_expr = if field_attr.use_default() {
|
||||||
quote_expr!(cx, ::std::default::Default::default())
|
quote_expr!(cx, ::std::default::Default::default())
|
||||||
@ -1027,7 +1021,7 @@ fn deserialize_map(
|
|||||||
|
|
||||||
let result = builder.expr().struct_path(struct_path)
|
let result = builder.expr().struct_path(struct_path)
|
||||||
.with_id_exprs(
|
.with_id_exprs(
|
||||||
struct_def.fields.iter()
|
fields.iter()
|
||||||
.zip(field_names.iter())
|
.zip(field_names.iter())
|
||||||
.map(|(field, field_name)| {
|
.map(|(field, field_name)| {
|
||||||
(
|
(
|
||||||
|
@ -7,9 +7,9 @@ use attr::{FieldAttrs, FieldAttrsBuilder};
|
|||||||
pub fn struct_field_attrs(
|
pub fn struct_field_attrs(
|
||||||
_cx: &ExtCtxt,
|
_cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
struct_def: &ast::StructDef,
|
fields: &[ast::StructField],
|
||||||
) -> Vec<FieldAttrs> {
|
) -> Vec<FieldAttrs> {
|
||||||
struct_def.fields.iter()
|
fields.iter()
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
FieldAttrsBuilder::new(builder).field(field).build()
|
FieldAttrsBuilder::new(builder).field(field).build()
|
||||||
})
|
})
|
||||||
|
@ -5,7 +5,6 @@ use syntax::ast::{
|
|||||||
MetaItem,
|
MetaItem,
|
||||||
Item,
|
Item,
|
||||||
Expr,
|
Expr,
|
||||||
StructDef,
|
|
||||||
};
|
};
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
@ -82,14 +81,14 @@ fn serialize_body(
|
|||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
match item.node {
|
match item.node {
|
||||||
ast::ItemStruct(ref struct_def, _) => {
|
ast::ItemStruct(ref variant_data, _) => {
|
||||||
serialize_item_struct(
|
serialize_item_struct(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
item,
|
item,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
variant_data,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ast::ItemEnum(ref enum_def, _) => {
|
ast::ItemEnum(ref enum_def, _) => {
|
||||||
@ -112,57 +111,51 @@ fn serialize_item_struct(
|
|||||||
item: &Item,
|
item: &Item,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
struct_def: &ast::StructDef,
|
variant_data: &ast::VariantData,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let mut named_fields = vec![];
|
match *variant_data {
|
||||||
let mut unnamed_fields = 0;
|
ast::VariantData::Unit(_) => {
|
||||||
|
|
||||||
for field in struct_def.fields.iter() {
|
|
||||||
match field.node.kind {
|
|
||||||
ast::NamedField(name, _) => { named_fields.push(name); }
|
|
||||||
ast::UnnamedField(_) => { unnamed_fields += 1; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match (named_fields.is_empty(), unnamed_fields) {
|
|
||||||
(true, 0) => {
|
|
||||||
serialize_unit_struct(
|
serialize_unit_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(true, 1) => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
serialize_newtype_struct(
|
serialize_newtype_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(true, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
|
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
||||||
|
cx.bug("tuple struct has named fields")
|
||||||
|
}
|
||||||
|
|
||||||
serialize_tuple_struct(
|
serialize_tuple_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
unnamed_fields,
|
fields.len(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(false, 0) => {
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
|
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
||||||
|
cx.bug("struct has unnamed fields")
|
||||||
|
}
|
||||||
|
|
||||||
serialize_struct(
|
serialize_struct(
|
||||||
cx,
|
cx,
|
||||||
&builder,
|
&builder,
|
||||||
item.ident,
|
item.ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
fields,
|
||||||
named_fields,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
(false, _) => {
|
|
||||||
cx.bug("struct has named and unnamed fields")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,8 +218,7 @@ fn serialize_struct(
|
|||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
struct_def: &StructDef,
|
fields: &[ast::StructField],
|
||||||
fields: Vec<Ident>,
|
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
@ -236,9 +228,12 @@ fn serialize_struct(
|
|||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__a")
|
.lifetime("'__a")
|
||||||
.build_ty(ty.clone()),
|
.build_ty(ty.clone()),
|
||||||
struct_def,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
fields.iter().map(|field| quote_expr!(cx, &self.value.$field)),
|
fields.iter().map(|field| {
|
||||||
|
let name = field.node.ident().expect("struct has unnamed field");
|
||||||
|
quote_expr!(cx, &self.value.$name)
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = builder.expr().str(type_ident);
|
let type_name = builder.expr().str(type_ident);
|
||||||
@ -297,8 +292,8 @@ fn serialize_variant(
|
|||||||
let variant_ident = variant.node.name;
|
let variant_ident = variant.node.name;
|
||||||
let variant_name = builder.expr().str(variant_ident);
|
let variant_name = builder.expr().str(variant_ident);
|
||||||
|
|
||||||
match variant.node.kind {
|
match *variant.node.data {
|
||||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
ast::VariantData::Unit(_) => {
|
||||||
let pat = builder.pat().enum_()
|
let pat = builder.pat().enum_()
|
||||||
.id(type_ident).id(variant_ident).build()
|
.id(type_ident).id(variant_ident).build()
|
||||||
.build();
|
.build();
|
||||||
@ -314,7 +309,7 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
ast::TupleVariantKind(ref args) if args.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
let field = builder.id("__simple_value");
|
let field = builder.id("__simple_value");
|
||||||
let field = builder.pat().ref_id(field);
|
let field = builder.pat().ref_id(field);
|
||||||
let pat = builder.pat().enum_()
|
let pat = builder.pat().enum_()
|
||||||
@ -333,14 +328,17 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
ast::TupleVariantKind(ref args) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
let fields: Vec<ast::Ident> = (0 .. args.len())
|
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let pat = builder.pat().enum_()
|
let pat = builder.pat().enum_()
|
||||||
.id(type_ident).id(variant_ident).build()
|
.id(type_ident).id(variant_ident).build()
|
||||||
.with_pats(fields.iter().map(|field| builder.pat().ref_id(field)))
|
.with_pats(
|
||||||
|
field_names.iter()
|
||||||
|
.map(|field| builder.pat().ref_id(field))
|
||||||
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = serialize_tuple_variant(
|
let expr = serialize_tuple_variant(
|
||||||
@ -351,22 +349,22 @@ fn serialize_variant(
|
|||||||
variant_name,
|
variant_name,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
args,
|
|
||||||
fields,
|
fields,
|
||||||
|
field_names,
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx, $pat => { $expr })
|
quote_arm!(cx, $pat => { $expr })
|
||||||
}
|
}
|
||||||
ast::StructVariantKind(ref struct_def) => {
|
ast::VariantData::Struct(ref fields, _) => {
|
||||||
let fields: Vec<_> = (0 .. struct_def.fields.len())
|
let field_names: Vec<_> = (0 .. fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| builder.id(format!("__field{}", i)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let pat = builder.pat().struct_()
|
let pat = builder.pat().struct_()
|
||||||
.id(type_ident).id(variant_ident).build()
|
.id(type_ident).id(variant_ident).build()
|
||||||
.with_pats(
|
.with_pats(
|
||||||
fields.iter()
|
field_names.iter()
|
||||||
.zip(struct_def.fields.iter())
|
.zip(fields.iter())
|
||||||
.map(|(id, field)| {
|
.map(|(id, field)| {
|
||||||
let name = match field.node.kind {
|
let name = match field.node.kind {
|
||||||
ast::NamedField(name, _) => name,
|
ast::NamedField(name, _) => name,
|
||||||
@ -388,8 +386,8 @@ fn serialize_variant(
|
|||||||
variant_name,
|
variant_name,
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
struct_def,
|
|
||||||
fields,
|
fields,
|
||||||
|
field_names,
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx, $pat => { $expr })
|
quote_arm!(cx, $pat => { $expr })
|
||||||
@ -405,16 +403,16 @@ 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>,
|
||||||
args: &[ast::VariantArg],
|
fields: &[ast::StructField],
|
||||||
fields: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let variant_ty = builder.ty().tuple()
|
let variant_ty = builder.ty().tuple()
|
||||||
.with_tys(
|
.with_tys(
|
||||||
args.iter().map(|arg| {
|
fields.iter().map(|field| {
|
||||||
builder.ty()
|
builder.ty()
|
||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__a")
|
.lifetime("'__a")
|
||||||
.build_ty(arg.ty.clone())
|
.build_ty(field.node.ty.clone())
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
@ -424,13 +422,13 @@ fn serialize_tuple_variant(
|
|||||||
builder,
|
builder,
|
||||||
structure_ty.clone(),
|
structure_ty.clone(),
|
||||||
variant_ty,
|
variant_ty,
|
||||||
args.len(),
|
fields.len(),
|
||||||
generics,
|
generics,
|
||||||
);
|
);
|
||||||
|
|
||||||
let value_expr = builder.expr().tuple()
|
let value_expr = builder.expr().tuple()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
fields.iter().map(|field| {
|
field_names.iter().map(|field| {
|
||||||
builder.expr().id(field)
|
builder.expr().id(field)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -455,12 +453,12 @@ fn serialize_struct_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>,
|
||||||
struct_def: &ast::StructDef,
|
fields: &[ast::StructField],
|
||||||
fields: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
) -> P<ast::Expr> {
|
) -> P<ast::Expr> {
|
||||||
let value_ty = builder.ty().tuple()
|
let value_ty = builder.ty().tuple()
|
||||||
.with_tys(
|
.with_tys(
|
||||||
struct_def.fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
builder.ty()
|
builder.ty()
|
||||||
.ref_()
|
.ref_()
|
||||||
.lifetime("'__a")
|
.lifetime("'__a")
|
||||||
@ -471,7 +469,7 @@ fn serialize_struct_variant(
|
|||||||
|
|
||||||
let value_expr = builder.expr().tuple()
|
let value_expr = builder.expr().tuple()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
fields.iter().map(|field| {
|
field_names.iter().map(|field| {
|
||||||
builder.expr().id(field)
|
builder.expr().id(field)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -482,9 +480,9 @@ fn serialize_struct_variant(
|
|||||||
builder,
|
builder,
|
||||||
structure_ty.clone(),
|
structure_ty.clone(),
|
||||||
value_ty,
|
value_ty,
|
||||||
struct_def,
|
fields,
|
||||||
generics,
|
generics,
|
||||||
(0 .. fields.len()).map(|i| {
|
(0 .. field_names.len()).map(|i| {
|
||||||
builder.expr()
|
builder.expr()
|
||||||
.tup_field(i)
|
.tup_field(i)
|
||||||
.field("value").self_()
|
.field("value").self_()
|
||||||
@ -574,7 +572,7 @@ fn serialize_struct_visitor<I>(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
variant_ty: P<ast::Ty>,
|
variant_ty: P<ast::Ty>,
|
||||||
struct_def: &StructDef,
|
fields: &[ast::StructField],
|
||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
value_exprs: I,
|
value_exprs: I,
|
||||||
) -> (P<ast::Item>, P<ast::Item>)
|
) -> (P<ast::Item>, P<ast::Item>)
|
||||||
@ -582,7 +580,7 @@ fn serialize_struct_visitor<I>(
|
|||||||
{
|
{
|
||||||
let value_exprs = value_exprs.collect::<Vec<_>>();
|
let value_exprs = value_exprs.collect::<Vec<_>>();
|
||||||
|
|
||||||
let field_attrs = struct_field_attrs(cx, builder, struct_def);
|
let field_attrs = struct_field_attrs(cx, builder, fields);
|
||||||
|
|
||||||
let arms: Vec<ast::Arm> = field_attrs.iter()
|
let arms: Vec<ast::Arm> = field_attrs.iter()
|
||||||
.zip(value_exprs.iter())
|
.zip(value_exprs.iter())
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
version = "0.5.3"
|
version = "0.6.1"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Macros to auto-generate implementations for the serde framework"
|
description = "Macros to auto-generate implementations for the serde framework"
|
||||||
@ -16,6 +16,6 @@ plugin = true
|
|||||||
serde_codegen = { version = "*", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
serde_codegen = { version = "*", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
num = "*"
|
num = "^0.1.27"
|
||||||
rustc-serialize = "*"
|
rustc-serialize = "^0.3.16"
|
||||||
serde = { version = "*", path = "../serde", features = ["nightly"] }
|
serde = { version = "*", path = "../serde", features = ["nightly"] }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user