Factor attr parsing into serde_item crate

This commit is contained in:
David Tolnay
2016-06-19 20:15:49 -07:00
parent d4e1ef659a
commit 5c6a0e12e9
10 changed files with 226 additions and 165 deletions
-657
View File
@@ -1,657 +0,0 @@
use std::rc::Rc;
use syntax::ast::{self, TokenTree};
use syntax::attr;
use syntax::codemap::{Span, Spanned, respan};
use syntax::ext::base::ExtCtxt;
use syntax::fold::Folder;
use syntax::parse::parser::{Parser, PathStyle};
use syntax::parse::token::{self, InternedString};
use syntax::parse;
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
use syntax::ptr::P;
use aster::AstBuilder;
use aster::ident::ToIdent;
use error::Error;
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
// are `ContainerAttrs::from_item`, `VariantAttrs::from_variant`, and
// `FieldAttrs::from_field`. Each returns an instance of the corresponding
// struct. Note that none of them return a Result. Unrecognized, malformed, or
// duplicated attributes result in a span_err but otherwise are ignored. The
// user will see errors simultaneously for all bad attributes in the crate
// rather than just the first.
struct Attr<'a, 'b: 'a, T> {
cx: &'a ExtCtxt<'b>,
name: &'static str,
value: Option<Spanned<T>>,
}
impl<'a, 'b, T> Attr<'a, 'b, T> {
fn none(cx: &'a ExtCtxt<'b>, name: &'static str) -> Self {
Attr {
cx: cx,
name: name,
value: None,
}
}
fn set(&mut self, span: Span, t: T) {
if let Some(Spanned { span: prev_span, .. }) = self.value {
let mut err = self.cx.struct_span_err(
span,
&format!("duplicate serde attribute `{}`", self.name));
err.span_help(prev_span, "previously set here");
err.emit();
} else {
self.value = Some(respan(span, t));
}
}
fn set_opt(&mut self, v: Option<Spanned<T>>) {
if let Some(v) = v {
self.set(v.span, v.node);
}
}
fn set_if_none(&mut self, span: Span, t: T) {
if self.value.is_none() {
self.value = Some(respan(span, t));
}
}
fn get(self) -> Option<T> {
self.value.map(|spanned| spanned.node)
}
fn get_spanned(self) -> Option<Spanned<T>> {
self.value
}
}
struct BoolAttr<'a, 'b: 'a>(Attr<'a, 'b, ()>);
impl<'a, 'b> BoolAttr<'a, 'b> {
fn none(cx: &'a ExtCtxt<'b>, name: &'static str) -> Self {
BoolAttr(Attr::none(cx, name))
}
fn set_true(&mut self, span: Span) {
self.0.set(span, ());
}
fn get(&self) -> bool {
self.0.value.is_some()
}
}
#[derive(Debug)]
pub struct Name {
ident: ast::Ident,
serialize_name: Option<InternedString>,
deserialize_name: Option<InternedString>,
}
impl Name {
/// Return the container name for the container when serializing.
pub fn serialize_name(&self) -> InternedString {
match self.serialize_name {
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.
pub fn deserialize_name(&self) -> InternedString {
match self.deserialize_name {
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
#[derive(Debug)]
pub struct ContainerAttrs {
name: Name,
deny_unknown_fields: bool,
ser_bound: Option<Vec<ast::WherePredicate>>,
de_bound: Option<Vec<ast::WherePredicate>>,
}
impl ContainerAttrs {
/// Extract out the `#[serde(...)]` attributes from an item.
pub fn from_item(cx: &ExtCtxt, item: &ast::Item) -> Self {
let mut ser_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 ser_bound = Attr::none(cx, "bound");
let mut de_bound = Attr::none(cx, "bound");
for meta_items in item.attrs().iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
let span = meta_item.span;
match meta_item.node {
// Parse `#[serde(rename="foo")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
if let Ok(s) = get_str_from_lit(cx, name, lit) {
ser_name.set(span, s.clone());
de_name.set(span, s);
}
}
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
if let Ok((ser, de)) = get_renames(cx, meta_items) {
ser_name.set_opt(ser);
de_name.set_opt(de);
}
}
// Parse `#[serde(deny_unknown_fields)]`
ast::MetaItemKind::Word(ref name) if name == &"deny_unknown_fields" => {
deny_unknown_fields.set_true(span);
}
// Parse `#[serde(bound="D: Serialize")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
ser_bound.set(span, where_predicates.clone());
de_bound.set(span, where_predicates);
}
}
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
ser_bound.set_opt(ser);
de_bound.set_opt(de);
}
}
_ => {
cx.span_err(
meta_item.span,
&format!("unknown serde container attribute `{}`",
meta_item_to_string(meta_item)));
}
}
}
}
ContainerAttrs {
name: Name {
ident: item.ident,
serialize_name: ser_name.get(),
deserialize_name: de_name.get(),
},
deny_unknown_fields: deny_unknown_fields.get(),
ser_bound: ser_bound.get(),
de_bound: de_bound.get(),
}
}
pub fn name(&self) -> &Name {
&self.name
}
pub fn deny_unknown_fields(&self) -> bool {
self.deny_unknown_fields
}
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
self.ser_bound.as_ref().map(|vec| &vec[..])
}
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
self.de_bound.as_ref().map(|vec| &vec[..])
}
}
/// Represents variant attribute information
#[derive(Debug)]
pub struct VariantAttrs {
name: Name,
}
impl VariantAttrs {
pub fn from_variant(cx: &ExtCtxt, variant: &ast::Variant) -> Self {
let mut ser_name = Attr::none(cx, "rename");
let mut de_name = Attr::none(cx, "rename");
for meta_items in variant.node.attrs.iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
let span = meta_item.span;
match meta_item.node {
// Parse `#[serde(rename="foo")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
if let Ok(s) = get_str_from_lit(cx, name, lit) {
ser_name.set(span, s.clone());
de_name.set(span, s);
}
}
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
if let Ok((ser, de)) = get_renames(cx, meta_items) {
ser_name.set_opt(ser);
de_name.set_opt(de);
}
}
_ => {
cx.span_err(
meta_item.span,
&format!("unknown serde variant attribute `{}`",
meta_item_to_string(meta_item)));
}
}
}
}
VariantAttrs {
name: Name {
ident: variant.node.name,
serialize_name: ser_name.get(),
deserialize_name: de_name.get(),
},
}
}
pub fn name(&self) -> &Name {
&self.name
}
}
/// Represents field attribute information
#[derive(Debug)]
pub struct FieldAttrs {
name: Name,
skip_serializing: bool,
skip_deserializing: bool,
skip_serializing_if: Option<ast::Path>,
default: FieldDefault,
serialize_with: Option<ast::Path>,
deserialize_with: Option<ast::Path>,
ser_bound: Option<Vec<ast::WherePredicate>>,
de_bound: Option<Vec<ast::WherePredicate>>,
}
/// Represents the default to use for a field when deserializing.
#[derive(Debug, PartialEq)]
pub enum FieldDefault {
/// Field must always be specified because it does not have a default.
None,
/// The default is given by `std::default::Default::default()`.
Default,
/// The default is given by this function.
Path(ast::Path),
}
impl FieldAttrs {
/// Extract out the `#[serde(...)]` attributes from a struct field.
pub fn from_field(cx: &ExtCtxt,
index: usize,
field: &ast::StructField) -> Self {
let mut ser_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_deserializing = BoolAttr::none(cx, "skip_deserializing");
let mut skip_serializing_if = Attr::none(cx, "skip_serializing_if");
let mut default = Attr::none(cx, "default");
let mut serialize_with = Attr::none(cx, "serialize_with");
let mut deserialize_with = Attr::none(cx, "deserialize_with");
let mut ser_bound = Attr::none(cx, "bound");
let mut de_bound = Attr::none(cx, "bound");
let field_ident = match field.ident {
Some(ident) => ident,
None => index.to_string().to_ident(),
};
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
for meta_item in meta_items {
let span = meta_item.span;
match meta_item.node {
// Parse `#[serde(rename="foo")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
if let Ok(s) = get_str_from_lit(cx, name, lit) {
ser_name.set(span, s.clone());
de_name.set(span, s);
}
}
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
if let Ok((ser, de)) = get_renames(cx, meta_items) {
ser_name.set_opt(ser);
de_name.set_opt(de);
}
}
// Parse `#[serde(default)]`
ast::MetaItemKind::Word(ref name) if name == &"default" => {
default.set(span, FieldDefault::Default);
}
// Parse `#[serde(default="...")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
default.set(span, FieldDefault::Path(path));
}
}
// Parse `#[serde(skip_serializing)]`
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
skip_serializing.set_true(span);
}
// Parse `#[serde(skip_deserializing)]`
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
skip_deserializing.set_true(span);
}
// Parse `#[serde(skip_serializing_if="...")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
skip_serializing_if.set(span, path);
}
}
// Parse `#[serde(serialize_with="...")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
serialize_with.set(span, path);
}
}
// Parse `#[serde(deserialize_with="...")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
deserialize_with.set(span, path);
}
}
// Parse `#[serde(bound="D: Serialize")]`
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
ser_bound.set(span, where_predicates.clone());
de_bound.set(span, where_predicates);
}
}
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
ser_bound.set_opt(ser);
de_bound.set_opt(de);
}
}
_ => {
cx.span_err(
meta_item.span,
&format!("unknown serde field attribute `{}`",
meta_item_to_string(meta_item)));
}
}
}
}
// Is skip_deserializing, initialize the field to Default::default()
// unless a different default is specified by `#[serde(default="...")]`
if let Some(Spanned { span, .. }) = skip_deserializing.0.value {
default.set_if_none(span, FieldDefault::Default);
}
FieldAttrs {
name: Name {
ident: field_ident,
serialize_name: ser_name.get(),
deserialize_name: de_name.get(),
},
skip_serializing: skip_serializing.get(),
skip_deserializing: skip_deserializing.get(),
skip_serializing_if: skip_serializing_if.get(),
default: default.get().unwrap_or(FieldDefault::None),
serialize_with: serialize_with.get(),
deserialize_with: deserialize_with.get(),
ser_bound: ser_bound.get(),
de_bound: de_bound.get(),
}
}
pub fn name(&self) -> &Name {
&self.name
}
pub fn skip_serializing(&self) -> bool {
self.skip_serializing
}
pub fn skip_deserializing(&self) -> bool {
self.skip_deserializing
}
pub fn skip_serializing_if(&self) -> Option<&ast::Path> {
self.skip_serializing_if.as_ref()
}
pub fn default(&self) -> &FieldDefault {
&self.default
}
pub fn serialize_with(&self) -> Option<&ast::Path> {
self.serialize_with.as_ref()
}
pub fn deserialize_with(&self) -> Option<&ast::Path> {
self.deserialize_with.as_ref()
}
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
self.ser_bound.as_ref().map(|vec| &vec[..])
}
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
self.de_bound.as_ref().map(|vec| &vec[..])
}
}
fn get_ser_and_de<T, F>(
cx: &ExtCtxt,
attribute: &'static str,
items: &[P<ast::MetaItem>],
f: F
) -> Result<(Option<Spanned<T>>, Option<Spanned<T>>), Error>
where F: Fn(&ExtCtxt, &str, &ast::Lit) -> Result<T, Error>,
{
let mut ser_item = Attr::none(cx, attribute);
let mut de_item = Attr::none(cx, attribute);
for item in items {
match item.node {
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize" => {
if let Ok(v) = f(cx, name, lit) {
ser_item.set(item.span, v);
}
}
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize" => {
if let Ok(v) = f(cx, name, lit) {
de_item.set(item.span, v);
}
}
_ => {
cx.span_err(
item.span,
&format!("unknown {} attribute `{}`",
attribute,
meta_item_to_string(item)));
return Err(Error);
}
}
}
Ok((ser_item.get_spanned(), de_item.get_spanned()))
}
fn get_renames(
cx: &ExtCtxt,
items: &[P<ast::MetaItem>],
) -> Result<(Option<Spanned<InternedString>>, Option<Spanned<InternedString>>), Error> {
get_ser_and_de(cx, "rename", items, get_str_from_lit)
}
fn get_where_predicates(
cx: &ExtCtxt,
items: &[P<ast::MetaItem>],
) -> Result<(Option<Spanned<Vec<ast::WherePredicate>>>, Option<Spanned<Vec<ast::WherePredicate>>>), Error> {
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
}
pub fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
match attr.node.value.node {
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
attr::mark_used(&attr);
Some(items)
}
_ => None
}
}
/// This syntax folder rewrites tokens to say their spans are coming from a macro context.
struct Respanner<'a, 'b: 'a> {
cx: &'a ExtCtxt<'b>,
}
impl<'a, 'b> Folder for Respanner<'a, 'b> {
fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree {
match *tt {
TokenTree::Token(span, ref tok) => {
TokenTree::Token(
self.new_span(span),
self.fold_token(tok.clone())
)
}
TokenTree::Delimited(span, ref delimed) => {
TokenTree::Delimited(
self.new_span(span),
Rc::new(ast::Delimited {
delim: delimed.delim,
open_span: delimed.open_span,
tts: self.fold_tts(&delimed.tts),
close_span: delimed.close_span,
})
)
}
TokenTree::Sequence(span, ref seq) => {
TokenTree::Sequence(
self.new_span(span),
Rc::new(ast::SequenceRepetition {
tts: self.fold_tts(&seq.tts),
separator: seq.separator.clone().map(|tok| self.fold_token(tok)),
..**seq
})
)
}
}
}
fn new_span(&mut self, span: Span) -> Span {
Span {
lo: span.lo,
hi: span.hi,
expn_id: self.cx.backtrace(),
}
}
}
fn get_str_from_lit(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<InternedString, Error> {
match lit.node {
ast::LitKind::Str(ref s, _) => Ok(s.clone()),
_ => {
cx.span_err(
lit.span,
&format!("serde annotation `{}` must be a string, not `{}`",
name,
lit_to_string(lit)));
return Err(Error);
}
}
}
// If we just parse a string literal from an attibute, any syntax errors in the
// source will only have spans that point inside the string and not back to the
// attribute. So to have better error reporting, we'll first parse the string
// 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
// 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>
where F: for<'a> Fn(&'a mut Parser) -> parse::PResult<'a, T>,
{
let tts = panictry!(parse::parse_tts_from_source_str(
format!("<serde {} expansion>", name),
string,
cx.cfg(),
cx.parse_sess()));
// Respan the spans to say they are all coming from this macro.
let tts = Respanner { cx: cx }.fold_tts(&tts);
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
let path = match action(&mut parser) {
Ok(path) => path,
Err(mut e) => {
e.emit();
return Err(Error);
}
};
// Make sure to error out if there are trailing characters in the stream.
match parser.expect(&token::Eof) {
Ok(()) => { }
Err(mut e) => {
e.emit();
return Err(Error);
}
}
Ok(path)
}
fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::Path, Error> {
let string = try!(get_str_from_lit(cx, name, lit)).to_string();
parse_string_via_tts(cx, name, string, |parser| {
parser.parse_path(PathStyle::Type)
})
}
fn parse_lit_into_where(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<Vec<ast::WherePredicate>, Error> {
let string = try!(get_str_from_lit(cx, name, lit));
if string.is_empty() {
return Ok(Vec::new());
}
let where_string = format!("where {}", string);
parse_string_via_tts(cx, name, where_string, |parser| {
let where_clause = try!(parser.parse_where_clause());
Ok(where_clause.predicates)
})
}
+3 -4
View File
@@ -6,8 +6,7 @@ use syntax::ast;
use syntax::ptr::P;
use syntax::visit;
use attr;
use item::Item;
use item::{attr, Item};
// Remove the default from every type parameter because in the generated impls
// 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,
from_field: F,
) -> ast::Generics
where F: Fn(&attr::FieldAttrs) -> Option<&[ast::WherePredicate]>,
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
{
builder.from_generics(generics.clone())
.with_predicates(
@@ -56,7 +55,7 @@ pub fn with_bound<F>(
filter: F,
bound: &ast::Path,
) -> ast::Generics
where F: Fn(&attr::FieldAttrs) -> bool,
where F: Fn(&attr::Field) -> bool,
{
builder.from_generics(generics.clone())
.with_predicates(
+59 -47
View File
@@ -6,10 +6,9 @@ use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::parse::token::InternedString;
use syntax::ptr::P;
use attr;
use bound;
use error::Error;
use item;
use item::{self, attr};
pub fn expand_derive_deserialize(
cx: &mut ExtCtxt,
@@ -28,27 +27,30 @@ pub fn expand_derive_deserialize(
}
};
let builder = aster::AstBuilder::new().span(span);
let impl_item = match deserialize_item(cx, &builder, &item) {
let item = match item::Item::from_ast(cx, item) {
Ok(item) => item,
Err(Error) => {
// An error occured, but it should have been reported already.
Err(item::Error::UnexpectedItemKind) => {
cx.span_err(item.span,
"`#[derive(Deserialize)]` may only be applied to structs and enums");
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))
}
fn deserialize_item(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
item: &ast::Item,
) -> Result<P<ast::Item>, Error> {
let item = try!(item::Item::from_ast(cx, "Deserialize", item));
try!(check_no_str(cx, &item));
item: &item::Item,
) -> P<ast::Item> {
let impl_generics = build_impl_generics(builder, &item);
let ty = builder.ty().path()
@@ -65,7 +67,7 @@ fn deserialize_item(
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)]
const $dummy_const: () = {
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
@@ -114,7 +116,7 @@ fn build_impl_generics(
// 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
// 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.deserialize_with().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
// `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
}
@@ -178,6 +180,7 @@ fn deserialize_body(
item::Body::Struct(item::Style::Unit, _) => {
deserialize_unit_struct(
cx,
builder,
item.ident,
&item.attrs)
}
@@ -266,10 +269,11 @@ fn deserializer_ty_arg(builder: &aster::AstBuilder) -> P<ast::Ty>{
fn deserialize_unit_struct(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
type_ident: Ident,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let type_name = container_attrs.name().deserialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx, {
struct __Visitor;
@@ -305,7 +309,7 @@ fn deserialize_tuple(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let where_clause = &impl_generics.where_clause;
@@ -351,11 +355,11 @@ fn deserialize_tuple(
quote_expr!(cx,
visitor.visit_tuple($nfields, $visitor_expr))
} 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,
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
} else {
let type_name = container_attrs.name().deserialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx,
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
};
@@ -394,7 +398,7 @@ fn deserialize_seq(
.map(|(i, field)| {
let name = builder.id(format!("__field{}", i));
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,
let $name = $default;
).unwrap()
@@ -502,7 +506,7 @@ fn deserialize_struct(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let where_clause = &impl_generics.where_clause;
@@ -535,7 +539,7 @@ fn deserialize_struct(
type_path.clone(),
impl_generics,
fields,
container_attrs,
item_attrs,
);
let is_enum = variant_ident.is_some();
@@ -543,7 +547,7 @@ fn deserialize_struct(
quote_expr!(cx,
visitor.visit_struct(FIELDS, $visitor_expr))
} else {
let type_name = container_attrs.name().deserialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx,
deserializer.deserialize_struct($type_name, FIELDS, $visitor_expr))
};
@@ -584,11 +588,11 @@ fn deserialize_item_enum(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
variants: &[item::Variant],
container_attrs: &attr::ContainerAttrs
item_attrs: &attr::Item
) -> P<ast::Expr> {
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(
cx,
@@ -596,7 +600,7 @@ fn deserialize_item_enum(
variants.iter()
.map(|variant| variant.attrs.name().deserialize_name())
.collect(),
container_attrs,
item_attrs,
true,
);
@@ -610,7 +614,7 @@ fn deserialize_item_enum(
const VARIANTS: &'static [&'static str] = $variants_expr;
).unwrap();
let ignored_arm = if container_attrs.deny_unknown_fields() {
let ignored_arm = if item_attrs.deny_unknown_fields() {
None
} else {
Some(quote_arm!(cx, __Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }))
@@ -630,7 +634,7 @@ fn deserialize_item_enum(
impl_generics,
ty.clone(),
variant,
container_attrs,
item_attrs,
);
let arm = quote_arm!(cx, $variant_name => { $expr });
@@ -675,7 +679,7 @@ fn deserialize_variant(
generics: &ast::Generics,
ty: P<ast::Ty>,
variant: &item::Variant,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let variant_ident = variant.ident;
@@ -705,7 +709,7 @@ fn deserialize_variant(
generics,
ty,
&variant.fields,
container_attrs,
item_attrs,
)
}
item::Style::Struct => {
@@ -717,7 +721,7 @@ fn deserialize_variant(
generics,
ty,
&variant.fields,
container_attrs,
item_attrs,
)
}
}
@@ -753,7 +757,7 @@ fn deserialize_field_visitor(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
field_names: Vec<InternedString>,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
is_variant: bool,
) -> Vec<P<ast::Item>> {
// Create the field names for the fields.
@@ -761,7 +765,7 @@ fn deserialize_field_visitor(
.map(|i| builder.id(format!("__field{}", i)))
.collect();
let ignore_variant = if container_attrs.deny_unknown_fields() {
let ignore_variant = if item_attrs.deny_unknown_fields() {
None
} else {
let skip_ident = builder.id("__ignore");
@@ -792,7 +796,7 @@ fn deserialize_field_visitor(
(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))
} else {
quote_expr!(cx, {
@@ -819,7 +823,7 @@ fn deserialize_field_visitor(
})
.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))
} else {
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
@@ -847,7 +851,7 @@ fn deserialize_field_visitor(
})
.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))
} else {
quote_expr!(cx, {
@@ -916,7 +920,7 @@ fn deserialize_struct_visitor(
struct_path: ast::Path,
impl_generics: &ast::Generics,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> (Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>) {
let field_exprs = fields.iter()
.map(|field| field.attrs.name().deserialize_name())
@@ -926,7 +930,7 @@ fn deserialize_struct_visitor(
cx,
builder,
field_exprs,
container_attrs,
item_attrs,
false,
);
@@ -937,7 +941,7 @@ fn deserialize_struct_visitor(
struct_path,
impl_generics,
fields,
container_attrs,
item_attrs,
);
let fields_expr = builder.expr().ref_().slice()
@@ -968,7 +972,7 @@ fn deserialize_map(
struct_path: ast::Path,
impl_generics: &ast::Generics,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
// Create the field names for the fields.
let fields_names = fields.iter()
@@ -1033,7 +1037,7 @@ fn deserialize_map(
.collect::<Vec<_>>();
// 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
} else {
Some(quote_arm!(cx,
@@ -1044,7 +1048,7 @@ fn deserialize_map(
let extract_values = fields_names.iter()
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
.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,
let $name = match $name {
@@ -1067,7 +1071,7 @@ fn deserialize_map(
}
},
if field.attrs.skip_deserializing() {
expr_is_missing(cx, &field.attrs)
expr_is_missing(cx, builder, &field.attrs)
} else {
builder.expr().id(name)
}
@@ -1149,7 +1153,8 @@ fn wrap_deserialize_with(
fn expr_is_missing(
cx: &ExtCtxt,
attrs: &attr::FieldAttrs,
builder: &aster::AstBuilder,
attrs: &attr::Field,
) -> P<ast::Expr> {
match *attrs.default() {
attr::FieldDefault::Default => {
@@ -1161,7 +1166,7 @@ fn expr_is_missing(
attr::FieldDefault::None => { /* below */ }
}
let name = attrs.name().deserialize_name_expr();
let name = name_expr(builder, attrs.name());
match attrs.deserialize_with() {
None => {
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(
cx: &ExtCtxt,
item: &item::Item,
-144
View File
@@ -1,144 +0,0 @@
use syntax::ast;
use syntax::codemap;
use syntax::ext::base::ExtCtxt;
use syntax::ptr::P;
use attr;
use error::Error;
pub struct Item<'a> {
pub ident: ast::Ident,
pub span: codemap::Span,
pub attrs: attr::ContainerAttrs,
pub body: Body<'a>,
pub generics: &'a ast::Generics,
}
pub enum Body<'a> {
Enum(Vec<Variant<'a>>),
Struct(Style, Vec<Field<'a>>),
}
pub struct Variant<'a> {
pub ident: ast::Ident,
pub attrs: attr::VariantAttrs,
pub style: Style,
pub fields: Vec<Field<'a>>,
}
pub struct Field<'a> {
pub ident: Option<ast::Ident>,
pub span: codemap::Span,
pub attrs: attr::FieldAttrs,
pub ty: &'a P<ast::Ty>,
}
pub enum Style {
Struct,
Tuple,
Newtype,
Unit,
}
impl<'a> Item<'a> {
pub fn from_ast(
cx: &ExtCtxt,
derive_trait: &'static str,
item: &'a ast::Item,
) -> Result<Item<'a>, Error> {
let attrs = attr::ContainerAttrs::from_item(cx, item);
let (body, generics) = match item.node {
ast::ItemKind::Enum(ref enum_def, ref generics) => {
let variants = enum_from_ast(cx, enum_def);
(Body::Enum(variants), generics)
}
ast::ItemKind::Struct(ref variant_data, ref generics) => {
let (style, fields) = struct_from_ast(cx, variant_data);
(Body::Struct(style, fields), generics)
}
_ => {
cx.span_err(item.span, &format!(
"`#[derive({})]` may only be applied to structs and enums",
derive_trait));
return Err(Error);
}
};
Ok(Item {
ident: item.ident,
span: item.span,
attrs: attrs,
body: body,
generics: generics,
})
}
}
fn enum_from_ast<'a>(
cx: &ExtCtxt,
enum_def: &'a ast::EnumDef,
) -> Vec<Variant<'a>> {
enum_def.variants.iter()
.map(|variant| {
let (style, fields) = struct_from_ast(cx, &variant.node.data);
Variant {
ident: variant.node.name,
attrs: attr::VariantAttrs::from_variant(cx, variant),
style: style,
fields: fields,
}
})
.collect()
}
fn struct_from_ast<'a>(
cx: &ExtCtxt,
variant_data: &'a ast::VariantData,
) -> (Style, Vec<Field<'a>>) {
match *variant_data {
ast::VariantData::Struct(ref fields, _) => {
(Style::Struct, fields_from_ast(cx, fields))
}
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
(Style::Newtype, fields_from_ast(cx, fields))
}
ast::VariantData::Tuple(ref fields, _) => {
(Style::Tuple, fields_from_ast(cx, fields))
}
ast::VariantData::Unit(_) => {
(Style::Unit, Vec::new())
}
}
}
fn fields_from_ast<'a>(
cx: &ExtCtxt,
fields: &'a [ast::StructField],
) -> Vec<Field<'a>> {
fields.iter()
.enumerate()
.map(|(i, field)| {
Field {
ident: field.ident,
span: field.span,
attrs: attr::FieldAttrs::from_field(cx, i, field),
ty: &field.ty,
}
})
.collect()
}
impl<'a> Body<'a> {
pub fn all_fields(&'a self) -> Box<Iterator<Item=&'a Field<'a>> + 'a> {
match *self {
Body::Enum(ref variants) => {
Box::new(variants.iter()
.flat_map(|variant| variant.fields.iter()))
}
Body::Struct(_, ref fields) => {
Box::new(fields.iter())
}
}
}
}
+1
View File
@@ -7,6 +7,7 @@
extern crate aster;
extern crate quasi;
extern crate serde_item as item;
#[cfg(feature = "with-syntex")]
extern crate syntex;
-2
View File
@@ -1,6 +1,4 @@
mod attr;
mod bound;
mod de;
mod error;
mod item;
mod ser;
+47 -40
View File
@@ -5,10 +5,8 @@ use syntax::codemap::Span;
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ptr::P;
use attr;
use bound;
use error::Error;
use item;
use item::{self, attr};
pub fn expand_derive_serialize(
cx: &mut ExtCtxt,
@@ -27,26 +25,26 @@ pub fn expand_derive_serialize(
}
};
let builder = aster::AstBuilder::new().span(span);
let impl_item = match serialize_item(cx, &builder, &item) {
let item = match item::Item::from_ast(cx, item) {
Ok(item) => item,
Err(Error) => {
// An error occured, but it should have been reported already.
Err(item::Error::UnexpectedItemKind) => {
cx.span_err(item.span,
"`#[derive(Serialize)]` may only be applied to structs and enums");
return;
}
};
let builder = aster::AstBuilder::new().span(span);
let impl_item = serialize_item(cx, &builder, &item);
push(Annotatable::Item(impl_item))
}
fn serialize_item(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
item: &ast::Item,
) -> Result<P<ast::Item>, Error> {
let item = try!(item::Item::from_ast(cx, "Serialize", item));
item: &item::Item,
) -> P<ast::Item> {
let impl_generics = build_impl_generics(builder, &item);
let ty = builder.ty().path()
@@ -63,7 +61,7 @@ fn serialize_item(
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)]
const $dummy_const: () = {
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
@@ -107,7 +105,7 @@ fn build_impl_generics(
// 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
// 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.serialize_with().is_none()
&& attrs.ser_bound().is_none()
@@ -169,6 +167,7 @@ fn serialize_body(
item::Body::Struct(item::Style::Unit, _) => {
serialize_unit_struct(
cx,
builder,
&item.attrs)
}
}
@@ -176,9 +175,10 @@ fn serialize_body(
fn serialize_unit_struct(
cx: &ExtCtxt,
container_attrs: &attr::ContainerAttrs,
builder: &aster::AstBuilder,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let type_name = container_attrs.name().serialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx,
_serializer.serialize_unit_struct($type_name)
@@ -189,16 +189,16 @@ fn serialize_newtype_struct(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
impl_generics: &ast::Generics,
container_ty: P<ast::Ty>,
item_ty: P<ast::Ty>,
field: &item::Field,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> 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);
if let Some(path) = field.attrs.serialize_with() {
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,
@@ -212,7 +212,7 @@ fn serialize_tuple_struct(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
cx,
@@ -228,7 +228,7 @@ fn serialize_tuple_struct(
false,
);
let type_name = container_attrs.name().serialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx, {
$visitor_struct
@@ -247,7 +247,7 @@ fn serialize_struct(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
fields: &[item::Field],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
cx,
@@ -263,7 +263,7 @@ fn serialize_struct(
false,
);
let type_name = container_attrs.name().serialize_name_expr();
let type_name = name_expr(builder, item_attrs.name());
quote_expr!(cx, {
$visitor_struct
@@ -283,7 +283,7 @@ fn serialize_item_enum(
impl_generics: &ast::Generics,
ty: P<ast::Ty>,
variants: &[item::Variant],
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let arms: Vec<_> =
variants.iter()
@@ -297,7 +297,7 @@ fn serialize_item_enum(
ty.clone(),
variant,
variant_index,
container_attrs,
item_attrs,
)
})
.collect();
@@ -317,12 +317,12 @@ fn serialize_variant(
ty: P<ast::Ty>,
variant: &item::Variant,
variant_index: usize,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> 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_name = variant.attrs.name().serialize_name_expr();
let variant_name = name_expr(builder, variant.attrs.name());
match variant.style {
item::Style::Unit => {
@@ -414,7 +414,7 @@ fn serialize_variant(
ty,
&variant.fields,
field_names,
container_attrs,
item_attrs,
);
quote_arm!(cx,
@@ -430,14 +430,14 @@ fn serialize_newtype_variant(
type_name: P<ast::Expr>,
variant_index: usize,
variant_name: P<ast::Expr>,
container_ty: P<ast::Ty>,
item_ty: P<ast::Ty>,
generics: &ast::Generics,
field: &item::Field,
) -> P<ast::Expr> {
let mut field_expr = quote_expr!(cx, __simple_value);
if let Some(path) = field.attrs.serialize_with() {
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,
@@ -512,7 +512,7 @@ fn serialize_struct_variant(
ty: P<ast::Ty>,
fields: &[item::Field],
field_names: Vec<Ident>,
container_attrs: &attr::ContainerAttrs,
item_attrs: &attr::Item,
) -> P<ast::Expr> {
let variant_generics = builder.generics()
.with(generics.clone())
@@ -572,14 +572,14 @@ fn serialize_struct_variant(
true,
);
let container_name = container_attrs.name().serialize_name_expr();
let item_name = name_expr(builder, item_attrs.name());
quote_expr!(cx, {
$variant_struct
$visitor_struct
$visitor_impl
_serializer.serialize_struct_variant(
$container_name,
$item_name,
$variant_index,
$variant_name,
Visitor {
@@ -692,7 +692,7 @@ fn serialize_struct_visitor(
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()
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
@@ -781,7 +781,7 @@ fn serialize_struct_visitor(
fn wrap_serialize_with(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
container_ty: &P<ast::Ty>,
item_ty: &P<ast::Ty>,
generics: &ast::Generics,
field_ty: &P<ast::Ty>,
path: &ast::Path,
@@ -803,7 +803,7 @@ fn wrap_serialize_with(
quote_expr!(cx, {
struct __SerializeWith $wrapper_generics $where_clause {
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 {
@@ -816,7 +816,14 @@ fn wrap_serialize_with(
__SerializeWith {
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())
}