2016-02-21 09:54:03 -08:00
|
|
|
use std::rc::Rc;
|
|
|
|
use syntax::ast::{self, TokenTree};
|
2015-09-07 16:44:56 -07:00
|
|
|
use syntax::attr;
|
2016-02-21 09:54:03 -08:00
|
|
|
use syntax::codemap::Span;
|
2015-05-01 15:45:58 -04:00
|
|
|
use syntax::ext::base::ExtCtxt;
|
2016-02-21 09:54:03 -08:00
|
|
|
use syntax::fold::Folder;
|
2016-05-20 22:03:20 -07:00
|
|
|
use syntax::parse::parser::{Parser, PathStyle};
|
2016-02-23 19:51:53 -08:00
|
|
|
use syntax::parse::token::{self, InternedString};
|
2016-02-12 21:53:35 -08:00
|
|
|
use syntax::parse;
|
2016-02-21 09:54:03 -08:00
|
|
|
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
2015-05-01 15:45:58 -04:00
|
|
|
use syntax::ptr::P;
|
|
|
|
|
2016-02-08 09:51:07 -08:00
|
|
|
use aster::AstBuilder;
|
2015-09-07 13:13:32 -07:00
|
|
|
|
2016-02-05 18:11:58 -08:00
|
|
|
use error::Error;
|
|
|
|
|
2016-02-23 19:51:53 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Name {
|
|
|
|
ident: ast::Ident,
|
|
|
|
serialize_name: Option<InternedString>,
|
|
|
|
deserialize_name: Option<InternedString>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Name {
|
|
|
|
fn new(ident: ast::Ident) -> Self {
|
|
|
|
Name {
|
|
|
|
ident: ident,
|
|
|
|
serialize_name: None,
|
|
|
|
deserialize_name: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 08:30:29 -08:00
|
|
|
/// Represents container (e.g. struct) attribute information
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ContainerAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name,
|
2016-02-08 08:30:29 -08:00
|
|
|
deny_unknown_fields: bool,
|
2016-06-05 10:47:40 -07:00
|
|
|
ser_bound: Option<Vec<ast::WherePredicate>>,
|
|
|
|
de_bound: Option<Vec<ast::WherePredicate>>,
|
2016-02-08 08:30:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContainerAttrs {
|
2016-02-08 10:09:18 -08:00
|
|
|
/// Extract out the `#[serde(...)]` attributes from an item.
|
2016-02-23 19:51:53 -08:00
|
|
|
pub fn from_item(cx: &ExtCtxt, item: &ast::Item) -> Result<Self, Error> {
|
2016-02-08 10:09:18 -08:00
|
|
|
let mut container_attrs = ContainerAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name::new(item.ident),
|
2016-02-08 10:09:18 -08:00
|
|
|
deny_unknown_fields: false,
|
2016-06-05 10:47:40 -07:00
|
|
|
ser_bound: None,
|
|
|
|
de_bound: None,
|
2016-02-08 10:09:18 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
for meta_items in item.attrs().iter().filter_map(get_serde_meta_items) {
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item.node {
|
2016-02-08 08:00:38 -08:00
|
|
|
// Parse `#[serde(rename="foo")]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
2016-02-23 19:51:53 -08:00
|
|
|
let s = try!(get_str_from_lit(cx, name, lit));
|
|
|
|
container_attrs.name.serialize_name = Some(s.clone());
|
|
|
|
container_attrs.name.deserialize_name = Some(s);
|
2016-02-08 08:00:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
2016-02-08 08:00:38 -08:00
|
|
|
let (ser_name, de_name) = try!(get_renames(cx, meta_items));
|
2016-06-05 11:40:30 -07:00
|
|
|
if ser_name.is_some() {
|
|
|
|
container_attrs.name.serialize_name = ser_name;
|
|
|
|
}
|
|
|
|
if de_name.is_some() {
|
|
|
|
container_attrs.name.deserialize_name = de_name;
|
|
|
|
}
|
2016-02-08 08:00:38 -08:00
|
|
|
}
|
|
|
|
|
2016-02-08 10:09:18 -08:00
|
|
|
// Parse `#[serde(deny_unknown_fields)]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::Word(ref name) if name == &"deny_unknown_fields" => {
|
2016-02-08 10:09:18 -08:00
|
|
|
container_attrs.deny_unknown_fields = true;
|
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
// Parse `#[serde(bound="D: Serialize")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
2016-05-20 22:03:20 -07:00
|
|
|
let where_predicates = try!(parse_lit_into_where(cx, name, lit));
|
2016-06-05 10:47:40 -07:00
|
|
|
container_attrs.ser_bound = Some(where_predicates.clone());
|
2016-06-05 11:23:01 -07:00
|
|
|
container_attrs.de_bound = Some(where_predicates);
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
|
|
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
|
|
|
let (ser_bound, de_bound) = try!(get_where_predicates(cx, meta_items));
|
2016-06-05 11:40:30 -07:00
|
|
|
if ser_bound.is_some() {
|
|
|
|
container_attrs.ser_bound = ser_bound;
|
|
|
|
}
|
|
|
|
if de_bound.is_some() {
|
|
|
|
container_attrs.de_bound = de_bound;
|
|
|
|
}
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-02-08 10:09:18 -08:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
|
|
|
&format!("unknown serde container attribute `{}`",
|
|
|
|
meta_item_to_string(meta_item)));
|
|
|
|
|
|
|
|
return Err(Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(container_attrs)
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:51:53 -08:00
|
|
|
pub fn name(&self) -> &Name {
|
|
|
|
&self.name
|
2016-02-08 08:00:38 -08:00
|
|
|
}
|
|
|
|
|
2016-02-08 08:30:29 -08:00
|
|
|
pub fn deny_unknown_fields(&self) -> bool {
|
|
|
|
self.deny_unknown_fields
|
|
|
|
}
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
|
|
|
|
self.ser_bound.as_ref().map(|vec| &vec[..])
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
|
|
|
|
self.de_bound.as_ref().map(|vec| &vec[..])
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
2016-02-08 08:30:29 -08:00
|
|
|
}
|
|
|
|
|
2016-02-08 08:03:46 -08:00
|
|
|
/// Represents variant attribute information
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct VariantAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name,
|
2016-02-08 08:03:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantAttrs {
|
|
|
|
pub fn from_variant(cx: &ExtCtxt, variant: &ast::Variant) -> Result<Self, Error> {
|
|
|
|
let mut variant_attrs = VariantAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name::new(variant.node.name),
|
2016-02-08 08:03:46 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
for meta_items in variant.node.attrs.iter().filter_map(get_serde_meta_items) {
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item.node {
|
|
|
|
// Parse `#[serde(rename="foo")]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
2016-02-23 19:51:53 -08:00
|
|
|
let s = try!(get_str_from_lit(cx, name, lit));
|
|
|
|
variant_attrs.name.serialize_name = Some(s.clone());
|
|
|
|
variant_attrs.name.deserialize_name = Some(s);
|
2016-02-08 10:35:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
2016-02-08 10:35:42 -08:00
|
|
|
let (ser_name, de_name) = try!(get_renames(cx, meta_items));
|
2016-06-05 11:40:30 -07:00
|
|
|
if ser_name.is_some() {
|
|
|
|
variant_attrs.name.serialize_name = ser_name;
|
|
|
|
}
|
|
|
|
if de_name.is_some() {
|
|
|
|
variant_attrs.name.deserialize_name = de_name;
|
|
|
|
}
|
2016-02-08 08:03:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
|
|
|
&format!("unknown serde variant attribute `{}`",
|
|
|
|
meta_item_to_string(meta_item)));
|
|
|
|
|
|
|
|
return Err(Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(variant_attrs)
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:51:53 -08:00
|
|
|
pub fn name(&self) -> &Name {
|
|
|
|
&self.name
|
2016-02-08 08:03:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 15:45:58 -04:00
|
|
|
/// Represents field attribute information
|
2015-09-07 13:13:32 -07:00
|
|
|
#[derive(Debug)]
|
2015-05-01 15:45:58 -04:00
|
|
|
pub struct FieldAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name,
|
2015-07-23 08:07:49 -07:00
|
|
|
skip_serializing_field: bool,
|
2016-03-06 23:27:12 -08:00
|
|
|
skip_deserializing_field: bool,
|
2016-05-15 15:54:20 -07:00
|
|
|
skip_serializing_if: Option<ast::Path>,
|
2016-02-12 21:53:35 -08:00
|
|
|
default_expr_if_missing: Option<P<ast::Expr>>,
|
2016-05-15 15:54:20 -07:00
|
|
|
serialize_with: Option<ast::Path>,
|
2016-05-07 12:33:59 -07:00
|
|
|
deserialize_with: Option<ast::Path>,
|
2016-06-05 10:47:40 -07:00
|
|
|
ser_bound: Option<Vec<ast::WherePredicate>>,
|
|
|
|
de_bound: Option<Vec<ast::WherePredicate>>,
|
2015-05-01 15:45:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FieldAttrs {
|
2016-02-08 09:51:07 -08:00
|
|
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
2016-02-12 21:53:35 -08:00
|
|
|
pub fn from_field(cx: &ExtCtxt,
|
2016-05-15 15:54:20 -07:00
|
|
|
index: usize,
|
|
|
|
field: &ast::StructField) -> Result<Self, Error> {
|
2016-02-12 21:53:35 -08:00
|
|
|
let builder = AstBuilder::new();
|
|
|
|
|
2016-04-10 19:54:54 -07:00
|
|
|
let field_ident = match field.ident {
|
2016-02-08 09:51:07 -08:00
|
|
|
Some(ident) => ident,
|
2016-05-15 15:54:20 -07:00
|
|
|
None => builder.id(index.to_string()),
|
2016-02-08 09:51:07 -08:00
|
|
|
};
|
|
|
|
|
2016-02-08 10:35:42 -08:00
|
|
|
let mut field_attrs = FieldAttrs {
|
2016-02-23 19:51:53 -08:00
|
|
|
name: Name::new(field_ident),
|
2016-02-08 10:35:42 -08:00
|
|
|
skip_serializing_field: false,
|
2016-03-06 23:27:12 -08:00
|
|
|
skip_deserializing_field: false,
|
2016-05-15 15:54:20 -07:00
|
|
|
skip_serializing_if: None,
|
2016-02-12 21:53:35 -08:00
|
|
|
default_expr_if_missing: None,
|
2016-02-15 17:39:46 -08:00
|
|
|
serialize_with: None,
|
2016-05-07 12:33:59 -07:00
|
|
|
deserialize_with: None,
|
2016-06-05 10:47:40 -07:00
|
|
|
ser_bound: None,
|
|
|
|
de_bound: None,
|
2016-02-08 10:35:42 -08:00
|
|
|
};
|
2016-02-08 09:51:07 -08:00
|
|
|
|
2016-04-10 19:54:54 -07:00
|
|
|
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
2016-02-08 09:51:07 -08:00
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item.node {
|
|
|
|
// Parse `#[serde(rename="foo")]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
2016-02-23 19:51:53 -08:00
|
|
|
let s = try!(get_str_from_lit(cx, name, lit));
|
|
|
|
field_attrs.name.serialize_name = Some(s.clone());
|
|
|
|
field_attrs.name.deserialize_name = Some(s);
|
2016-02-08 10:35:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
2016-02-08 10:35:42 -08:00
|
|
|
let (ser_name, de_name) = try!(get_renames(cx, meta_items));
|
2016-06-05 11:40:30 -07:00
|
|
|
if ser_name.is_some() {
|
|
|
|
field_attrs.name.serialize_name = ser_name;
|
|
|
|
}
|
|
|
|
if de_name.is_some() {
|
|
|
|
field_attrs.name.deserialize_name = de_name;
|
|
|
|
}
|
2016-02-08 09:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(default)]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
2016-02-12 21:53:35 -08:00
|
|
|
let default_expr = builder.expr().default();
|
|
|
|
field_attrs.default_expr_if_missing = Some(default_expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(default="...")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
|
|
|
|
let wrapped_expr = wrap_default(
|
2016-02-21 15:28:25 -08:00
|
|
|
try!(parse_lit_into_path(cx, name, lit)),
|
2016-02-12 21:53:35 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
field_attrs.default_expr_if_missing = Some(wrapped_expr);
|
2016-02-08 09:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse `#[serde(skip_serializing)]`
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
2016-02-08 10:35:42 -08:00
|
|
|
field_attrs.skip_serializing_field = true;
|
2016-02-08 09:51:07 -08:00
|
|
|
}
|
|
|
|
|
2016-03-06 23:27:12 -08:00
|
|
|
// Parse `#[serde(skip_deserializing)]`
|
|
|
|
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
|
|
|
field_attrs.skip_deserializing_field = true;
|
2016-04-12 23:42:24 -07:00
|
|
|
|
|
|
|
// Initialize field to Default::default() unless a different
|
|
|
|
// default is specified by `#[serde(default="...")]`
|
|
|
|
if field_attrs.default_expr_if_missing.is_none() {
|
|
|
|
let default_expr = builder.expr().default();
|
|
|
|
field_attrs.default_expr_if_missing = Some(default_expr);
|
|
|
|
}
|
2016-03-06 23:27:12 -08:00
|
|
|
}
|
|
|
|
|
2016-02-12 22:05:02 -08:00
|
|
|
// Parse `#[serde(skip_serializing_if="...")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
2016-05-15 15:54:20 -07:00
|
|
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
|
|
|
field_attrs.skip_serializing_if = Some(path);
|
2016-02-12 22:05:02 -08:00
|
|
|
}
|
|
|
|
|
2016-02-15 17:39:46 -08:00
|
|
|
// Parse `#[serde(serialize_with="...")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
2016-05-15 15:54:20 -07:00
|
|
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
|
|
|
field_attrs.serialize_with = Some(path);
|
2016-02-15 17:39:46 -08:00
|
|
|
}
|
|
|
|
|
2016-02-15 20:43:11 -08:00
|
|
|
// Parse `#[serde(deserialize_with="...")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
2016-05-07 12:33:59 -07:00
|
|
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
|
|
|
field_attrs.deserialize_with = Some(path);
|
2016-02-15 20:43:11 -08:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
// Parse `#[serde(bound="D: Serialize")]`
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
2016-05-20 22:03:20 -07:00
|
|
|
let where_predicates = try!(parse_lit_into_where(cx, name, lit));
|
2016-06-05 10:47:40 -07:00
|
|
|
field_attrs.ser_bound = Some(where_predicates.clone());
|
2016-06-05 11:23:01 -07:00
|
|
|
field_attrs.de_bound = Some(where_predicates);
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
|
|
|
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
|
|
|
let (ser_bound, de_bound) = try!(get_where_predicates(cx, meta_items));
|
2016-06-05 11:40:30 -07:00
|
|
|
if ser_bound.is_some() {
|
|
|
|
field_attrs.ser_bound = ser_bound;
|
|
|
|
}
|
|
|
|
if de_bound.is_some() {
|
|
|
|
field_attrs.de_bound = de_bound;
|
|
|
|
}
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-02-08 09:51:07 -08:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
meta_item.span,
|
|
|
|
&format!("unknown serde field attribute `{}`",
|
|
|
|
meta_item_to_string(meta_item)));
|
|
|
|
|
|
|
|
return Err(Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 10:35:42 -08:00
|
|
|
Ok(field_attrs)
|
2016-02-08 09:51:07 -08:00
|
|
|
}
|
|
|
|
|
2016-02-23 19:51:53 -08:00
|
|
|
pub fn name(&self) -> &Name {
|
|
|
|
&self.name
|
2015-05-01 15:45:58 -04:00
|
|
|
}
|
|
|
|
|
2015-07-23 08:07:49 -07:00
|
|
|
pub fn skip_serializing_field(&self) -> bool {
|
|
|
|
self.skip_serializing_field
|
|
|
|
}
|
2015-09-07 16:44:56 -07:00
|
|
|
|
2016-03-06 23:27:12 -08:00
|
|
|
pub fn skip_deserializing_field(&self) -> bool {
|
|
|
|
self.skip_deserializing_field
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
pub fn skip_serializing_if(&self) -> Option<&ast::Path> {
|
|
|
|
self.skip_serializing_if.as_ref()
|
2016-02-12 22:05:02 -08:00
|
|
|
}
|
|
|
|
|
2016-05-07 19:57:44 -07:00
|
|
|
pub fn default_expr_if_missing(&self) -> Option<&P<ast::Expr>> {
|
|
|
|
self.default_expr_if_missing.as_ref()
|
|
|
|
}
|
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
pub fn serialize_with(&self) -> Option<&ast::Path> {
|
2016-02-15 17:39:46 -08:00
|
|
|
self.serialize_with.as_ref()
|
|
|
|
}
|
2016-02-15 20:43:11 -08:00
|
|
|
|
2016-05-07 12:33:59 -07:00
|
|
|
pub fn deserialize_with(&self) -> Option<&ast::Path> {
|
|
|
|
self.deserialize_with.as_ref()
|
2016-02-15 20:43:11 -08:00
|
|
|
}
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
|
|
|
|
self.ser_bound.as_ref().map(|vec| &vec[..])
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-05 10:47:40 -07:00
|
|
|
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
|
|
|
|
self.de_bound.as_ref().map(|vec| &vec[..])
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
2015-05-01 15:45:58 -04:00
|
|
|
}
|
2015-09-07 13:13:32 -07:00
|
|
|
|
2016-02-12 22:05:02 -08:00
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
/// Zip together fields and `#[serde(...)]` attributes on those fields.
|
2016-05-20 22:03:20 -07:00
|
|
|
pub fn fields_with_attrs(
|
2016-05-15 15:54:20 -07:00
|
|
|
cx: &ExtCtxt,
|
2016-05-20 22:03:20 -07:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
) -> Result<Vec<(ast::StructField, FieldAttrs)>, Error> {
|
2016-02-08 09:51:07 -08:00
|
|
|
fields.iter()
|
2016-05-15 15:54:20 -07:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, field)| {
|
|
|
|
let attrs = try!(FieldAttrs::from_field(cx, i, field));
|
2016-05-20 22:03:20 -07:00
|
|
|
Ok((field.clone(), attrs))
|
2016-05-15 15:54:20 -07:00
|
|
|
})
|
2016-02-08 09:51:07 -08:00
|
|
|
.collect()
|
2016-02-05 18:11:58 -08:00
|
|
|
}
|
2016-02-08 10:09:18 -08:00
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
fn get_ser_and_de<T, F>(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
attribute: &str,
|
|
|
|
items: &[P<ast::MetaItem>],
|
|
|
|
f: F
|
|
|
|
) -> Result<(Option<T>, Option<T>), Error>
|
|
|
|
where F: Fn(&ExtCtxt, &str, &ast::Lit) -> Result<T, Error>,
|
|
|
|
{
|
|
|
|
let mut ser_item = None;
|
|
|
|
let mut de_item = None;
|
2016-02-08 10:35:42 -08:00
|
|
|
|
|
|
|
for item in items {
|
|
|
|
match item.node {
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize" => {
|
2016-05-20 22:03:20 -07:00
|
|
|
let s = try!(f(cx, name, lit));
|
|
|
|
ser_item = Some(s);
|
2016-02-08 10:35:42 -08:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize" => {
|
2016-05-20 22:03:20 -07:00
|
|
|
let s = try!(f(cx, name, lit));
|
|
|
|
de_item = Some(s);
|
2016-02-08 10:35:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
item.span,
|
2016-05-20 22:03:20 -07:00
|
|
|
&format!("unknown {} attribute `{}`",
|
|
|
|
attribute,
|
2016-02-08 10:35:42 -08:00
|
|
|
meta_item_to_string(item)));
|
|
|
|
|
|
|
|
return Err(Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
Ok((ser_item, de_item))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_renames(
|
|
|
|
cx: &ExtCtxt,
|
|
|
|
items: &[P<ast::MetaItem>],
|
|
|
|
) -> Result<(Option<InternedString>, Option<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<Vec<ast::WherePredicate>>, Option<Vec<ast::WherePredicate>>), Error> {
|
2016-06-05 10:47:40 -07:00
|
|
|
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
2016-02-08 10:35:42 -08:00
|
|
|
}
|
|
|
|
|
feat(codegen): Inhibit generic bounds if skip_serializing
The generated code for a struct like:
struct Test<A, B, C> {
a: X<A>
#[serde(skip_serializing)]
b: B
#[serde(serialize_with="...")]
c: C
}
Used to be:
impl<A, B, C> Serialize for Test<A, B, C>
where A: Serialize,
B: Serialize,
C: Serialize,
{ ... }
Now it is:
impl<A, B, C> Serialize for Test<A, B, C>
where X<A>: Serialize,
{ ... }
Both `skip_serializing` and `serialize_with` mean the type does not need to
implement `Serialize`.
2016-02-28 12:17:29 -08:00
|
|
|
pub fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
|
2016-02-08 10:09:18 -08:00
|
|
|
match attr.node.value.node {
|
2016-02-12 21:43:23 -08:00
|
|
|
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
2016-02-08 10:09:18 -08:00
|
|
|
attr::mark_used(&attr);
|
|
|
|
Some(items)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 21:53:35 -08:00
|
|
|
|
2016-02-21 09:54:03 -08:00
|
|
|
/// 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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:51:53 -08:00
|
|
|
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()),
|
2016-02-12 21:53:35 -08:00
|
|
|
_ => {
|
|
|
|
cx.span_err(
|
|
|
|
lit.span,
|
2016-02-21 09:54:03 -08:00
|
|
|
&format!("serde annotation `{}` must be a string, not `{}`",
|
2016-02-12 21:53:35 -08:00
|
|
|
name,
|
|
|
|
lit_to_string(lit)));
|
|
|
|
|
|
|
|
return Err(Error);
|
|
|
|
}
|
2016-02-23 19:51:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
// 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>,
|
|
|
|
{
|
2016-03-16 23:33:23 -07:00
|
|
|
let tts = panictry!(parse::parse_tts_from_source_str(
|
2016-02-21 09:54:03 -08:00
|
|
|
format!("<serde {} expansion>", name),
|
2016-05-20 22:03:20 -07:00
|
|
|
string,
|
2016-02-21 09:54:03 -08:00
|
|
|
cx.cfg(),
|
2016-03-16 23:33:23 -07:00
|
|
|
cx.parse_sess()));
|
2016-02-21 09:54:03 -08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
let path = match action(&mut parser) {
|
2016-02-21 15:28:25 -08:00
|
|
|
Ok(path) => path,
|
2016-02-21 09:54:03 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 21:53:35 -08:00
|
|
|
|
2016-02-21 15:28:25 -08:00
|
|
|
Ok(path)
|
2016-02-12 21:53:35 -08:00
|
|
|
}
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-12 21:53:35 -08:00
|
|
|
/// This function wraps the expression in `#[serde(default="...")]` in a function to prevent it
|
|
|
|
/// from accessing the internal `Deserialize` state.
|
2016-02-21 15:28:25 -08:00
|
|
|
fn wrap_default(path: ast::Path) -> P<ast::Expr> {
|
|
|
|
AstBuilder::new().expr().call()
|
|
|
|
.build_path(path)
|
|
|
|
.build()
|
2016-02-12 21:53:35 -08:00
|
|
|
}
|