Skip to content

Commit

Permalink
Merge pull request #173 from erickt/update
Browse files Browse the repository at this point in the history
Update aster, quasi, and syntex
  • Loading branch information
erickt committed Oct 18, 2015
2 parents 612e384 + f872b3f commit 72de877
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 129 deletions.
5 changes: 2 additions & 3 deletions serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "0.6.0"
version = "0.6.1"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework"
Expand All @@ -10,8 +10,7 @@ readme = "../README.md"
keywords = ["serde", "serialization"]

[dependencies]
num = "*"
num = "^0.1.27"

[features]
nightly = []

16 changes: 8 additions & 8 deletions serde_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_codegen"
version = "0.5.3"
version = "0.6.1"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Macros to auto-generate implementations for the serde framework"
Expand All @@ -15,12 +15,12 @@ nightly = ["quasi_macros"]
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]

[build-dependencies]
quasi_codegen = { verision = "*", optional = true }
syntex = { version = "*", optional = true }
quasi_codegen = { verision = "^0.3.4", optional = true }
syntex = { version = "^0.18.0", optional = true }

[dependencies]
aster = { version = "*", default-features = false }
quasi = { verision = "*", default-features = false }
quasi_macros = { version = "*", optional = true }
syntex = { version = "*", optional = true }
syntex_syntax = { version = "*", optional = true }
aster = { version = "^0.5.0", default-features = false }
quasi = { verision = "^0.3.5", default-features = false }
quasi_macros = { version = "^0.3.5", optional = true }
syntex = { version = "^0.17.0", optional = true }
syntex_syntax = { version = "^0.18.0", optional = true }
110 changes: 52 additions & 58 deletions serde_codegen/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use aster;

use syntax::ast::{
self,
EnumDef,
Expr,
Ident,
MetaItem,
Item,
Expr,
StructDef,
EnumDef,
MetaItem,
};
use syntax::codemap::Span;
use syntax::ext::base::{Annotatable, ExtCtxt};
Expand Down Expand Up @@ -87,14 +86,14 @@ fn deserialize_body(
ty: P<ast::Ty>,
) -> P<ast::Expr> {
match item.node {
ast::ItemStruct(ref struct_def, _) => {
ast::ItemStruct(ref variant_data, _) => {
deserialize_item_struct(
cx,
builder,
item,
impl_generics,
ty,
struct_def,
variant_data,
)
}
ast::ItemEnum(ref enum_def, _) => {
Expand All @@ -117,27 +116,17 @@ fn deserialize_item_struct(
item: &Item,
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
struct_def: &ast::StructDef,
variant_data: &ast::VariantData,
) -> P<ast::Expr> {
let mut named_fields = vec![];
let mut unnamed_fields = 0;

for field in struct_def.fields.iter() {
match field.node.kind {
ast::NamedField(name, _) => { named_fields.push(name); }
ast::UnnamedField(_) => { unnamed_fields += 1; }
}
}

match (named_fields.is_empty(), unnamed_fields) {
(true, 0) => {
match *variant_data {
ast::VariantData::Unit(_) => {
deserialize_unit_struct(
cx,
&builder,
item.ident,
)
}
(true, 1) => {
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
deserialize_newtype_struct(
cx,
&builder,
Expand All @@ -146,29 +135,34 @@ fn deserialize_item_struct(
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(
cx,
&builder,
item.ident,
impl_generics,
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(
cx,
&builder,
item.ident,
impl_generics,
ty,
struct_def,
fields,
)
}
(false, _) => {
cx.bug("struct has named and unnamed fields")
}
}
}

Expand Down Expand Up @@ -422,9 +416,9 @@ fn deserialize_struct_as_seq(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
struct_path: ast::Path,
struct_def: &StructDef,
fields: &[ast::StructField],
) -> 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| {
let name = builder.id(format!("__field{}", i));
quote_stmt!(cx,
Expand All @@ -440,13 +434,13 @@ fn deserialize_struct_as_seq(

let result = builder.expr().struct_path(struct_path)
.with_id_exprs(
struct_def.fields.iter()
fields.iter()
.enumerate()
.map(|(i, field)| {
(
match field.node.kind {
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)),
)
Expand All @@ -469,7 +463,7 @@ fn deserialize_struct(
type_ident: Ident,
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
struct_def: &StructDef,
fields: &[ast::StructField],
) -> P<ast::Expr> {
let where_clause = &impl_generics.where_clause;

Expand All @@ -486,14 +480,14 @@ fn deserialize_struct(
cx,
builder,
type_path.clone(),
struct_def
fields,
);

let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
cx,
builder,
struct_def,
type_path.clone()
type_path.clone(),
fields,
);

let type_name = builder.expr().str(type_ident);
Expand Down Expand Up @@ -593,7 +587,7 @@ fn deserialize_item_enum(
impl_generics,
vec![deserializer_ty_param(builder)],
vec![deserializer_ty_arg(builder)],
);
);

quote_expr!(cx, {
$variant_visitor
Expand Down Expand Up @@ -628,39 +622,39 @@ fn deserialize_variant(
) -> P<ast::Expr> {
let variant_ident = variant.node.name;

match variant.node.kind {
ast::TupleVariantKind(ref args) if args.is_empty() => {
match *variant.node.data {
ast::VariantData::Unit(_) => {
quote_expr!(cx, {
try!(visitor.visit_unit());
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, {
let val = try!(visitor.visit_newtype());
Ok($type_ident::$variant_ident(val))
})
}
ast::TupleVariantKind(ref args) => {
ast::VariantData::Tuple(ref fields, _) => {
deserialize_tuple_variant(
cx,
builder,
type_ident,
variant_ident,
generics,
ty,
args.len(),
fields.len(),
)
}
ast::StructVariantKind(ref struct_def) => {
ast::VariantData::Struct(ref fields, _) => {
deserialize_struct_variant(
cx,
builder,
type_ident,
variant_ident,
generics,
ty,
struct_def,
fields,
)
}
}
Expand All @@ -683,7 +677,7 @@ fn deserialize_tuple_variant(
generics,
vec![deserializer_ty_param(builder)],
vec![deserializer_ty_arg(builder)],
);
);

let visit_seq_expr = deserialize_seq(
cx,
Expand Down Expand Up @@ -716,7 +710,7 @@ fn deserialize_struct_variant(
variant_ident: ast::Ident,
generics: &ast::Generics,
ty: P<ast::Ty>,
struct_def: &ast::StructDef,
fields: &[ast::StructField],
) -> P<ast::Expr> {
let where_clause = &generics.where_clause;

Expand All @@ -729,14 +723,14 @@ fn deserialize_struct_variant(
cx,
builder,
type_path.clone(),
struct_def
fields,
);

let (field_visitor, fields_stmt, field_expr) = deserialize_struct_visitor(
cx,
builder,
struct_def,
type_path
type_path,
fields,
);

let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
Expand All @@ -745,7 +739,7 @@ fn deserialize_struct_variant(
generics,
vec![deserializer_ty_param(builder)],
vec![deserializer_ty_arg(builder)],
);
);

quote_expr!(cx, {
$field_visitor
Expand Down Expand Up @@ -791,7 +785,7 @@ fn deserialize_field_visitor(
.enum_("__Field")
.with_variants(
field_idents.iter().map(|field_ident| {
builder.variant(field_ident).tuple().build()
builder.variant(field_ident).unit()
})
)
.build();
Expand Down Expand Up @@ -927,25 +921,25 @@ fn deserialize_field_visitor(
fn deserialize_struct_visitor(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
struct_def: &ast::StructDef,
struct_path: ast::Path,
fields: &[ast::StructField],
) -> (Vec<P<ast::Item>>, P<ast::Stmt>, P<ast::Expr>) {
let field_visitor = deserialize_field_visitor(
cx,
builder,
field::struct_field_attrs(cx, builder, struct_def),
field::struct_field_attrs(cx, builder, fields),
);

let visit_map_expr = deserialize_map(
cx,
builder,
struct_path,
struct_def,
fields,
);

let fields_expr = builder.expr().addr_of().slice()
.with_exprs(
struct_def.fields.iter()
fields.iter()
.map(|field| {
match field.node.kind {
ast::NamedField(name, _) => builder.expr().str(name),
Expand All @@ -966,10 +960,10 @@ fn deserialize_map(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
struct_path: ast::Path,
struct_def: &StructDef,
fields: &[ast::StructField],
) -> P<ast::Expr> {
// 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)))
.collect();

Expand All @@ -990,7 +984,7 @@ fn deserialize_map(
.collect();

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)| {
let missing_expr = if field_attr.use_default() {
quote_expr!(cx, ::std::default::Default::default())
Expand Down Expand Up @@ -1027,7 +1021,7 @@ fn deserialize_map(

let result = builder.expr().struct_path(struct_path)
.with_id_exprs(
struct_def.fields.iter()
fields.iter()
.zip(field_names.iter())
.map(|(field, field_name)| {
(
Expand Down
4 changes: 2 additions & 2 deletions serde_codegen/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use attr::{FieldAttrs, FieldAttrsBuilder};
pub fn struct_field_attrs(
_cx: &ExtCtxt,
builder: &aster::AstBuilder,
struct_def: &ast::StructDef,
fields: &[ast::StructField],
) -> Vec<FieldAttrs> {
struct_def.fields.iter()
fields.iter()
.map(|field| {
FieldAttrsBuilder::new(builder).field(field).build()
})
Expand Down
Loading

0 comments on commit 72de877

Please sign in to comment.