Initial macro support for deserializing structs and tuple structs

This commit is contained in:
Erick Tryzelaar 2015-02-10 21:08:04 -08:00
parent 044825829a
commit 3fd42e616c
3 changed files with 316 additions and 64 deletions

View File

@ -12,30 +12,28 @@ use syntax::ast::{
//LitNil, //LitNil,
}; };
use syntax::ast; use syntax::ast;
use syntax::codemap::Span; use syntax::codemap::{Span, respan};
use syntax::ext::base::{ExtCtxt, Decorator, ItemDecorator}; use syntax::ext::base::{ExtCtxt, Decorator, ItemDecorator};
use syntax::ext::build::AstBuilder; use syntax::ext::build::AstBuilder;
use syntax::ext::deriving::generic::{ use syntax::ext::deriving::generic::{
EnumMatching, EnumMatching,
FieldInfo, FieldInfo,
MethodDef, MethodDef,
//Named, Named,
//StaticFields, StaticFields,
//StaticStruct, StaticStruct,
//StaticEnum, //StaticEnum,
Struct, Struct,
Substructure, Substructure,
TraitDef, TraitDef,
//Unnamed, Unnamed,
combine_substructure, combine_substructure,
}; };
use syntax::ext::deriving::generic::ty::{ use syntax::ext::deriving::generic::ty::{
Borrowed, Borrowed,
LifetimeBounds, LifetimeBounds,
Literal, Ty,
Path, Path,
Ptr,
//Self,
//Tuple, //Tuple,
borrowed_explicit_self, borrowed_explicit_self,
}; };
@ -51,11 +49,9 @@ pub fn plugin_registrar(reg: &mut Registry) {
token::intern("derive_serialize"), token::intern("derive_serialize"),
Decorator(Box::new(expand_derive_serialize))); Decorator(Box::new(expand_derive_serialize)));
/*
reg.register_syntax_extension( reg.register_syntax_extension(
token::intern("derive_deserialize"), token::intern("derive_deserialize"),
ItemDecorator(Box::new(expand_derive_deserialize))); Decorator(Box::new(expand_derive_deserialize)));
*/
} }
fn expand_derive_serialize<>(cx: &mut ExtCtxt, fn expand_derive_serialize<>(cx: &mut ExtCtxt,
@ -85,24 +81,24 @@ fn expand_derive_serialize<>(cx: &mut ExtCtxt,
}, },
explicit_self: borrowed_explicit_self(), explicit_self: borrowed_explicit_self(),
args: vec![ args: vec![
Ptr( Ty::Ptr(
Box::new(Literal(Path::new_local("__V"))), Box::new(Ty::Literal(Path::new_local("__V"))),
Borrowed(None, MutMutable), Borrowed(None, MutMutable),
), ),
], ],
ret_ty: Literal( ret_ty: Ty::Literal(
Path::new_( Path::new_(
vec!("std", "result", "Result"), vec!("std", "result", "Result"),
None, None,
vec![ vec![
Box::new(Literal(Path::new_(vec!["__V", "Value"], Box::new(Ty::Literal(Path::new_(vec!["__V", "Value"],
None, None,
vec![], vec![],
false))), false))),
Box::new(Literal(Path::new_(vec!["__V", "Error"], Box::new(Ty::Literal(Path::new_(vec!["__V", "Error"],
None, None,
vec![], vec![],
false))), false))),
], ],
true true
) )
@ -115,7 +111,7 @@ fn expand_derive_serialize<>(cx: &mut ExtCtxt,
] ]
}; };
trait_def.expand(cx, mitem, item, |item| push.call_mut((item,))) trait_def.expand(cx, mitem, item, |item| push(item))
} }
fn serialize_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> { fn serialize_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
@ -241,64 +237,64 @@ fn serialize_enum(cx: &ExtCtxt,
}) })
} }
/*
pub fn expand_derive_deserialize(cx: &mut ExtCtxt, pub fn expand_derive_deserialize(cx: &mut ExtCtxt,
span: Span, sp: Span,
mitem: &MetaItem, mitem: &MetaItem,
item: &Item, item: &Item,
push: |P<Item>|) { mut push: Box<FnMut(P<ast::Item>)>)
{
let inline = cx.meta_word(sp, token::InternedString::new("inline"));
let attrs = vec!(cx.attribute(sp, inline));
let trait_def = TraitDef { let trait_def = TraitDef {
span: span, span: sp,
attributes: Vec::new(), attributes: Vec::new(),
path: Path::new_(vec!("serde2", "de", "Deserialize"), None, path: Path::new(vec!["serde2", "de", "Deserialize"]),
vec!(Box::new(Literal(Path::new_local("__D"))),
Box::new(Literal(Path::new_local("__E")))), true),
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: LifetimeBounds { generics: LifetimeBounds::empty(),
lifetimes: Vec::new(),
bounds: vec!(("__D", None, vec!(Path::new_(
vec!("serde2", "de", "Deserializer"), None,
vec!(Box::new(Literal(Path::new_local("__E")))), true))),
("__E", None, vec!()))
},
associated_types: vec![], associated_types: vec![],
methods: vec!( methods: vec!(
MethodDef { MethodDef {
name: "deserialize_token", name: "deserialize",
generics: LifetimeBounds::empty(), generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec![
("__S", vec![Path::new(vec!["serde2", "de", "Deserializer"])]),
],
},
explicit_self: None, explicit_self: None,
args: vec!( args: vec![
Ptr( Ty::Ptr(
Box::new(Literal(Path::new_local("__D"))), Box::new(Ty::Literal(Path::new_local("__S"))),
Borrowed(None, MutMutable) Borrowed(None, MutMutable)
), ),
Literal(Path::new(vec!("serde2", "de", "Token"))), ],
), ret_ty: Ty::Literal(
ret_ty: Literal(
Path::new_( Path::new_(
vec!("std", "result", "Result"), vec!["std", "result", "Result"],
None, None,
vec!( vec![
Box::new(Self), Box::new(Ty::Self),
Box::new(Literal(Path::new_local("__E"))) Box::new(Ty::Literal(Path::new_(vec!["__S", "Error"],
), None,
vec![],
false))),
],
true true
) )
), ),
attributes: Vec::new(), attributes: attrs,
combine_substructure: combine_substructure(Box::new(|a, b, c| { combine_substructure: combine_substructure(Box::new(|a, b, c| {
deserialize_substructure(a, b, c) deserialize_substructure(a, b, c)
})), })),
}) })
}; };
trait_def.expand(cx, mitem, item, push) trait_def.expand(cx, mitem, item, |item| push(item))
} }
fn deserialize_substructure(cx: &mut ExtCtxt, span: Span, fn deserialize_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
substr: &Substructure) -> P<Expr> { let state = substr.nonself_args[0].clone();
let deserializer = substr.nonself_args[0];
let token = substr.nonself_args[1];
match *substr.fields { match *substr.fields {
StaticStruct(_, ref fields) => { StaticStruct(_, ref fields) => {
@ -307,9 +303,9 @@ fn deserialize_substructure(cx: &mut ExtCtxt, span: Span,
span, span,
substr.type_ident, substr.type_ident,
fields, fields,
deserializer, state)
token)
} }
/*
StaticEnum(_, ref fields) => { StaticEnum(_, ref fields) => {
deserialize_enum( deserialize_enum(
cx, cx,
@ -319,10 +315,250 @@ fn deserialize_substructure(cx: &mut ExtCtxt, span: Span,
deserializer, deserializer,
token) token)
} }
*/
_ => cx.bug("expected StaticEnum or StaticStruct in derive(Deserialize)") _ => cx.bug("expected StaticEnum or StaticStruct in derive(Deserialize)")
} }
} }
fn deserialize_struct(
cx: &ExtCtxt,
span: Span,
type_ident: Ident,
fields: &StaticFields,
state: P<ast::Expr>,
) -> P<ast::Expr> {
match *fields {
Unnamed(ref fields) => {
deserialize_struct_unnamed_fields(
cx,
span,
type_ident,
&fields[],
state)
}
Named(ref fields) => {
deserialize_struct_named_fields(
cx,
span,
type_ident,
&fields[],
state)
}
}
}
fn deserialize_struct_unnamed_fields(
cx: &ExtCtxt,
span: Span,
type_ident: Ident,
fields: &[Span],
state: P<ast::Expr>,
) -> P<ast::Expr> {
let type_name = cx.expr_str(span, token::get_ident(type_ident));
let field_names: Vec<ast::Ident> = (0 .. fields.len())
.map(|i| token::str_to_ident(&format!("__field{}", i)))
.collect();
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
.map(|name| {
quote_stmt!(cx,
let $name = match try!(visitor.visit()) {
Some(value) => value,
None => {
return Err(::serde2::de::Error::end_of_stream_error());
}
};
)
})
.collect();
let result = cx.expr_call_ident(
span,
type_ident,
field_names.iter().map(|name| cx.expr_ident(span, *name)).collect());
quote_expr!(cx, {
struct __Visitor;
impl ::serde2::de::Visitor for __Visitor {
type Value = $type_ident;
fn visit_seq<
__V: ::serde2::de::SeqVisitor,
>(&mut self, mut visitor: __V) -> Result<$type_ident, __V::Error> {
$let_values
try!(visitor.end());
Ok($result)
}
fn visit_named_seq<
__V: ::serde2::de::SeqVisitor,
>(&mut self, name: &str, visitor: __V) -> Result<$type_ident, __V::Error> {
if name == $type_name {
self.visit_seq(visitor)
} else {
Err(::serde2::de::Error::syntax_error())
}
}
}
$state.visit(&mut __Visitor)
})
}
fn deserialize_struct_named_fields(
cx: &ExtCtxt,
span: Span,
type_ident: Ident,
fields: &[(Ident, Span)],
state: P<ast::Expr>,
) -> P<ast::Expr> {
let type_name = cx.expr_str(span, token::get_ident(type_ident));
// Create the field names for the fields.
let field_names: Vec<ast::Ident> = (0 .. fields.len())
.map(|i| token::str_to_ident(&format!("__field{}", i)))
.collect();
// Create the field names for the fields.
let field_variants: Vec<P<ast::Variant>> = field_names.iter()
.map(|field| {
P(respan(
span,
ast::Variant_ {
name: *field,
attrs: Vec::new(),
kind: ast::TupleVariantKind(Vec::new()),
id: ast::DUMMY_NODE_ID,
disr_expr: None,
vis: ast::Inherited,
}))
})
.collect();
let field_enum = cx.item_enum(
span,
token::str_to_ident("__Field"),
ast::EnumDef { variants: field_variants });
// Match arms to extract a field from a string
let field_arms: Vec<ast::Arm> = fields.iter()
.zip(field_names.iter())
.map(|(&(name, span), field)| {
let s = cx.expr_str(span, token::get_ident(name));
quote_arm!(cx, $s => Ok(__Field::$field),)
})
.collect();
// Declare each field.
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
.map(|field| {
quote_stmt!(cx, let mut $field = None;)
})
.collect();
// Match arms to extract a value for a field.
let value_arms: Vec<ast::Arm> = field_names.iter()
.map(|field| {
quote_arm!(cx, __Field::$field => {
$field = Some(try!(visitor.visit_value()));
})
})
.collect();
let extract_values: Vec<P<ast::Stmt>> = fields.iter()
.zip(field_names.iter())
.map(|(&(name, span), field)| {
let name_str = cx.expr_str(span, token::get_ident(name));
quote_stmt!(cx,
let $field = match $field {
Some($field) => $field,
None => {
return Err(::serde2::de::Error::missing_field_error($name_str));
}
};
)
})
.collect();
let result = cx.expr_struct_ident(
span,
type_ident,
fields.iter()
.zip(field_names.iter())
.map(|(&(name, span), field)| {
cx.field_imm(span, name, cx.expr_ident(span, *field))
})
.collect()
);
quote_expr!(cx, {
#[allow(non_camel_case_types)]
$field_enum
struct __FieldVisitor;
impl ::serde2::de::Visitor for __FieldVisitor {
type Value = __Field;
fn visit_str<
E: ::serde2::de::Error,
>(&mut self, value: &str) -> Result<__Field, E> {
match value {
$field_arms
_ => Err(::serde2::de::Error::syntax_error()),
}
}
}
impl ::serde2::de::Deserialize for __Field {
#[inline]
fn deserialize<
__S: ::serde2::de::Deserializer,
>(state: &mut __S) -> Result<__Field, __S::Error> {
state.visit(&mut __FieldVisitor)
}
}
struct __Visitor;
impl ::serde2::de::Visitor for __Visitor {
type Value = $type_ident;
fn visit_map<
__V: ::serde2::de::MapVisitor,
>(&mut self, mut visitor: __V) -> Result<$type_ident, __V::Error> {
$let_values
while let Some(key) = try!(visitor.visit_key()) {
match key {
$value_arms
}
}
$extract_values
Ok($result)
}
fn visit_named_map<
__V: ::serde2::de::MapVisitor,
>(&mut self, name: &str, visitor: __V) -> Result<$type_ident, __V::Error> {
if name == $type_name {
self.visit_map(visitor)
} else {
Err(::serde2::de::Error::syntax_error())
}
}
}
$state.visit(&mut __Visitor)
})
}
/*
fn deserialize_struct( fn deserialize_struct(
cx: &ExtCtxt, cx: &ExtCtxt,
span: Span, span: Span,
@ -331,6 +567,7 @@ fn deserialize_struct(
deserializer: P<ast::Expr>, deserializer: P<ast::Expr>,
token: P<ast::Expr> token: P<ast::Expr>
) -> P<ast::Expr> { ) -> P<ast::Expr> {
/*
let struct_block = deserialize_struct_from_struct( let struct_block = deserialize_struct_from_struct(
cx, cx,
span, span,
@ -338,6 +575,7 @@ fn deserialize_struct(
fields, fields,
deserializer deserializer
); );
*/
let map_block = deserialize_struct_from_map( let map_block = deserialize_struct_from_map(
cx, cx,
@ -363,6 +601,7 @@ fn deserialize_struct(
) )
} }
/*
fn deserialize_struct_from_struct( fn deserialize_struct_from_struct(
cx: &ExtCtxt, cx: &ExtCtxt,
span: Span, span: Span,
@ -392,6 +631,7 @@ fn deserialize_struct_from_struct(
Ok(result) Ok(result)
}) })
} }
*/
fn deserialize_struct_from_map( fn deserialize_struct_from_map(
cx: &ExtCtxt, cx: &ExtCtxt,
@ -401,7 +641,7 @@ fn deserialize_struct_from_map(
deserializer: P<ast::Expr> deserializer: P<ast::Expr>
) -> P<ast::Expr> { ) -> P<ast::Expr> {
let fields = match *fields { let fields = match *fields {
Unnamed(_) => fail!(), Unnamed(_) => panic!(),
Named(ref fields) => &fields[], Named(ref fields) => &fields[],
}; };

View File

@ -10,6 +10,8 @@ pub trait Error {
fn syntax_error() -> Self; fn syntax_error() -> Self;
fn end_of_stream_error() -> Self; fn end_of_stream_error() -> Self;
fn missing_field_error(&'static str) -> Self;
} }
pub trait Deserialize { pub trait Deserialize {

View File

@ -84,7 +84,9 @@ pub enum Error {
IoError(old_io::IoError), IoError(old_io::IoError),
/* /*
ExpectedError(String, String), ExpectedError(String, String),
MissingFieldError(String), */
MissingFieldError(&'static str),
/*
UnknownVariantError(String), UnknownVariantError(String),
*/ */
} }
@ -96,7 +98,9 @@ impl error::Error for Error {
Error::IoError(ref error) => error.description().as_slice(), Error::IoError(ref error) => error.description().as_slice(),
/* /*
Error::ExpectedError(ref expected, _) => &expected, Error::ExpectedError(ref expected, _) => &expected,
*/
Error::MissingFieldError(_) => "missing field", Error::MissingFieldError(_) => "missing field",
/*
Error::UnknownVariantError(_) => "unknown variant", Error::UnknownVariantError(_) => "unknown variant",
*/ */
} }
@ -115,16 +119,18 @@ impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Error::SyntaxError(ref code, line, col) => { Error::SyntaxError(ref code, line, col) => {
write!(fmt, "{:?} at line {:?} column {:?}", code, line, col) write!(fmt, "{:?} at line {} column {}", code, line, col)
} }
Error::IoError(ref error) => fmt::Display::fmt(error, fmt), Error::IoError(ref error) => fmt::Display::fmt(error, fmt),
/* /*
Error::ExpectedError(ref expected, ref found) => { Error::ExpectedError(ref expected, ref found) => {
Some(format!("expected {}, found {}", expected, found)) Some(format!("expected {}, found {}", expected, found))
} }
*/
Error::MissingFieldError(ref field) => { Error::MissingFieldError(ref field) => {
Some(format!("missing field {}", field)) write!(fmt, "missing field {}", field)
} }
/*
Error::UnknownVariantError(ref variant) => { Error::UnknownVariantError(ref variant) => {
Some(format!("unknown variant {}", variant)) Some(format!("unknown variant {}", variant))
} }
@ -147,4 +153,8 @@ impl de::Error for Error {
fn end_of_stream_error() -> Error { fn end_of_stream_error() -> Error {
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0) Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0)
} }
fn missing_field_error(field: &'static str) -> Error {
Error::MissingFieldError(field)
}
} }