mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-25 12:47:56 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a2c098eeb | |||
| f68f32d3ee | |||
| 6ccb6c9130 | |||
| 49d24a1377 | |||
| b0eee50947 | |||
| 554b81d636 | |||
| 9fe16767c5 | |||
| 3a3777a2fb | |||
| effa298871 | |||
| 9a86e6818f | |||
| 7d09b1475c | |||
| 7e441e5110 | |||
| 40b874214a | |||
| 0c18c151e2 | |||
| 4ad6c4fd56 | |||
| 8ee8c07090 | |||
| 8e77960e3a | |||
| 819d47fea2 | |||
| 8e865f62c4 | |||
| 6cbf0d32da | |||
| 20afa85087 | |||
| da6d967776 | |||
| f47a50e996 | |||
| 1eca7766ba | |||
| 51f95575ec | |||
| a69b82c7c5 | |||
| 55e5f19437 | |||
| c34baa1e5f |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "0.8.9"
|
version = "0.8.10"
|
||||||
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"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen"
|
name = "serde_codegen"
|
||||||
version = "0.8.9"
|
version = "0.8.10"
|
||||||
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"
|
||||||
@@ -8,31 +8,22 @@ homepage = "https://serde.rs"
|
|||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
documentation = "https://serde.rs/codegen.html"
|
documentation = "https://serde.rs/codegen.html"
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
build = "build.rs"
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
include = ["Cargo.toml", "build.rs", "src/**/*.rs", "src/lib.rs.in"]
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["with-syntex"]
|
default = ["with-syntex"]
|
||||||
unstable = ["quasi_macros"]
|
unstable = []
|
||||||
unstable-testing = ["clippy"]
|
unstable-testing = ["clippy"]
|
||||||
with-syntex = [
|
with-syntex = [
|
||||||
"quasi/with-syntex",
|
|
||||||
"quasi_codegen",
|
|
||||||
"quasi_codegen/with-syntex",
|
|
||||||
"serde_codegen_internals/with-syntex",
|
|
||||||
"syntex",
|
"syntex",
|
||||||
"syntex_syntax",
|
"syntex_syntax",
|
||||||
]
|
]
|
||||||
|
with-syn = []
|
||||||
[build-dependencies]
|
|
||||||
quasi_codegen = { version = "^0.20.0", optional = true }
|
|
||||||
syntex = { version = "^0.44.0", optional = true }
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aster = { version = "^0.27.0", default-features = false }
|
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
quasi = { version = "^0.20.0", default-features = false }
|
quote = "0.2"
|
||||||
quasi_macros = { version = "^0.20.0", optional = true }
|
serde_codegen_internals = { version = "=0.9.0", default-features = false, path = "../serde_codegen_internals" }
|
||||||
serde_codegen_internals = { version = "=0.8.9", default-features = false, path = "../serde_codegen_internals" }
|
syn = { version = "0.8", features = ["aster", "visit"] }
|
||||||
syntex = { version = "^0.44.0", optional = true }
|
syntex = { version = "^0.44.0", optional = true }
|
||||||
syntex_syntax = { version = "^0.44.0", optional = true }
|
syntex_syntax = { version = "^0.44.0", optional = true }
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
#[cfg(feature = "with-syntex")]
|
|
||||||
mod inner {
|
|
||||||
extern crate quasi_codegen;
|
|
||||||
|
|
||||||
use std::env;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::thread::spawn;
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
// put everything into a thread, so users can use `RUST_MIN_STACK` to increase the amount of stack
|
|
||||||
spawn(|| {
|
|
||||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
|
||||||
|
|
||||||
let src = Path::new("src/lib.rs.in");
|
|
||||||
let dst = Path::new(&out_dir).join("lib.rs");
|
|
||||||
quasi_codegen::expand(&src, &dst).unwrap();
|
|
||||||
}).join().unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
|
||||||
mod inner {
|
|
||||||
pub fn main() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
inner::main();
|
|
||||||
}
|
|
||||||
+25
-31
@@ -1,9 +1,6 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use aster::AstBuilder;
|
use syn::{self, aster, visit};
|
||||||
|
|
||||||
use syntax::ast;
|
|
||||||
use syntax::visit;
|
|
||||||
|
|
||||||
use internals::ast::Item;
|
use internals::ast::Item;
|
||||||
use internals::attr;
|
use internals::attr;
|
||||||
@@ -11,10 +8,10 @@ use internals::attr;
|
|||||||
// Remove the default from every type parameter because in the generated impls
|
// Remove the default from every type parameter because in the generated impls
|
||||||
// they look like associated types: "error: associated type bindings are not
|
// they look like associated types: "error: associated type bindings are not
|
||||||
// allowed here".
|
// allowed here".
|
||||||
pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
|
||||||
ast::Generics {
|
syn::Generics {
|
||||||
ty_params: generics.ty_params.iter().map(|ty_param| {
|
ty_params: generics.ty_params.iter().map(|ty_param| {
|
||||||
ast::TyParam {
|
syn::TyParam {
|
||||||
default: None,
|
default: None,
|
||||||
.. ty_param.clone()
|
.. ty_param.clone()
|
||||||
}}).collect(),
|
}}).collect(),
|
||||||
@@ -23,24 +20,22 @@ pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_where_predicates(
|
pub fn with_where_predicates(
|
||||||
builder: &AstBuilder,
|
generics: &syn::Generics,
|
||||||
generics: &ast::Generics,
|
predicates: &[syn::WherePredicate],
|
||||||
predicates: &[ast::WherePredicate],
|
) -> syn::Generics {
|
||||||
) -> ast::Generics {
|
aster::from_generics(generics.clone())
|
||||||
builder.from_generics(generics.clone())
|
|
||||||
.with_predicates(predicates.to_vec())
|
.with_predicates(predicates.to_vec())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_where_predicates_from_fields<F>(
|
pub fn with_where_predicates_from_fields<F>(
|
||||||
builder: &AstBuilder,
|
|
||||||
item: &Item,
|
item: &Item,
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
from_field: F,
|
from_field: F,
|
||||||
) -> ast::Generics
|
) -> syn::Generics
|
||||||
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
|
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
|
||||||
{
|
{
|
||||||
builder.from_generics(generics.clone())
|
aster::from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
item.body.all_fields()
|
item.body.all_fields()
|
||||||
.flat_map(|field| from_field(&field.attrs))
|
.flat_map(|field| from_field(&field.attrs))
|
||||||
@@ -60,34 +55,33 @@ pub fn with_where_predicates_from_fields<F>(
|
|||||||
// c: C,
|
// c: C,
|
||||||
// }
|
// }
|
||||||
pub fn with_bound<F>(
|
pub fn with_bound<F>(
|
||||||
builder: &AstBuilder,
|
|
||||||
item: &Item,
|
item: &Item,
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
filter: F,
|
filter: F,
|
||||||
bound: &ast::Path,
|
bound: &syn::Path,
|
||||||
) -> ast::Generics
|
) -> syn::Generics
|
||||||
where F: Fn(&attr::Field) -> bool,
|
where F: Fn(&attr::Field) -> bool,
|
||||||
{
|
{
|
||||||
struct FindTyParams {
|
struct FindTyParams {
|
||||||
// Set of all generic type parameters on the current struct (A, B, C in
|
// Set of all generic type parameters on the current struct (A, B, C in
|
||||||
// the example). Initialized up front.
|
// the example). Initialized up front.
|
||||||
all_ty_params: HashSet<ast::Name>,
|
all_ty_params: HashSet<syn::Ident>,
|
||||||
// Set of generic type parameters used in fields for which filter
|
// Set of generic type parameters used in fields for which filter
|
||||||
// returns true (A and B in the example). Filled in as the visitor sees
|
// returns true (A and B in the example). Filled in as the visitor sees
|
||||||
// them.
|
// them.
|
||||||
relevant_ty_params: HashSet<ast::Name>,
|
relevant_ty_params: HashSet<syn::Ident>,
|
||||||
}
|
}
|
||||||
impl visit::Visitor for FindTyParams {
|
impl visit::Visitor for FindTyParams {
|
||||||
fn visit_path(&mut self, path: &ast::Path, _id: ast::NodeId) {
|
fn visit_path(&mut self, path: &syn::Path) {
|
||||||
if let Some(seg) = path.segments.last() {
|
if let Some(seg) = path.segments.last() {
|
||||||
if seg.identifier.name.as_str() == "PhantomData" {
|
if seg.ident == "PhantomData" {
|
||||||
// Hardcoded exception, because PhantomData<T> implements
|
// Hardcoded exception, because PhantomData<T> implements
|
||||||
// Serialize and Deserialize whether or not T implements it.
|
// Serialize and Deserialize whether or not T implements it.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !path.global && path.segments.len() == 1 {
|
if !path.global && path.segments.len() == 1 {
|
||||||
let id = path.segments[0].identifier.name;
|
let id = path.segments[0].ident.clone();
|
||||||
if self.all_ty_params.contains(&id) {
|
if self.all_ty_params.contains(&id) {
|
||||||
self.relevant_ty_params.insert(id);
|
self.relevant_ty_params.insert(id);
|
||||||
}
|
}
|
||||||
@@ -97,7 +91,7 @@ pub fn with_bound<F>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let all_ty_params: HashSet<_> = generics.ty_params.iter()
|
let all_ty_params: HashSet<_> = generics.ty_params.iter()
|
||||||
.map(|ty_param| ty_param.ident.name)
|
.map(|ty_param| ty_param.ident.clone())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let relevant_tys = item.body.all_fields()
|
let relevant_tys = item.body.all_fields()
|
||||||
@@ -112,14 +106,14 @@ pub fn with_bound<F>(
|
|||||||
visit::walk_ty(&mut visitor, ty);
|
visit::walk_ty(&mut visitor, ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.from_generics(generics.clone())
|
aster::from_generics(generics.clone())
|
||||||
.with_predicates(
|
.with_predicates(
|
||||||
generics.ty_params.iter()
|
generics.ty_params.iter()
|
||||||
.map(|ty_param| ty_param.ident.name)
|
.map(|ty_param| ty_param.ident.clone())
|
||||||
.filter(|id| visitor.relevant_ty_params.contains(id))
|
.filter(|id| visitor.relevant_ty_params.contains(id))
|
||||||
.map(|id| builder.where_predicate()
|
.map(|id| aster::where_predicate()
|
||||||
// the type parameter that is being bounded e.g. T
|
// the type parameter that is being bounded e.g. T
|
||||||
.bound().build(builder.ty().id(id))
|
.bound().build(aster::ty().id(id))
|
||||||
// the bound e.g. Serialize
|
// the bound e.g. Serialize
|
||||||
.bound().trait_(bound.clone()).build()
|
.bound().trait_(bound.clone()).build()
|
||||||
.build()))
|
.build()))
|
||||||
|
|||||||
+433
-556
File diff suppressed because it is too large
Load Diff
+187
-12
@@ -3,10 +3,10 @@
|
|||||||
#![cfg_attr(feature = "clippy", allow(too_many_arguments))]
|
#![cfg_attr(feature = "clippy", allow(too_many_arguments))]
|
||||||
#![cfg_attr(feature = "clippy", allow(used_underscore_binding))]
|
#![cfg_attr(feature = "clippy", allow(used_underscore_binding))]
|
||||||
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
||||||
#![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))]
|
|
||||||
|
|
||||||
extern crate aster;
|
// The `quote!` macro requires deep recursion.
|
||||||
extern crate quasi;
|
#![recursion_limit = "192"]
|
||||||
|
|
||||||
extern crate serde_codegen_internals as internals;
|
extern crate serde_codegen_internals as internals;
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
#[cfg(feature = "with-syntex")]
|
||||||
@@ -23,17 +23,19 @@ extern crate syntax;
|
|||||||
#[cfg(not(feature = "with-syntex"))]
|
#[cfg(not(feature = "with-syntex"))]
|
||||||
extern crate rustc_plugin;
|
extern crate rustc_plugin;
|
||||||
|
|
||||||
|
extern crate syn;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate quote;
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
#[cfg(feature = "with-syntex")]
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
#[cfg(not(feature = "with-syntex"))]
|
||||||
use syntax::feature_gate::AttributeType;
|
use syntax::feature_gate::AttributeType;
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
mod bound;
|
||||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
mod de;
|
||||||
|
mod ser;
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
|
||||||
include!("lib.rs.in");
|
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
#[cfg(feature = "with-syntex")]
|
||||||
fn syntex_registry() -> syntex::Registry {
|
fn syntex_registry() -> syntex::Registry {
|
||||||
@@ -68,8 +70,8 @@ fn syntex_registry() -> syntex::Registry {
|
|||||||
reg.add_attr("feature(custom_derive)");
|
reg.add_attr("feature(custom_derive)");
|
||||||
reg.add_attr("feature(custom_attribute)");
|
reg.add_attr("feature(custom_attribute)");
|
||||||
|
|
||||||
reg.add_decorator("derive_Serialize", ser::expand_derive_serialize);
|
reg.add_decorator("derive_Serialize", expand_derive_serialize);
|
||||||
reg.add_decorator("derive_Deserialize", de::expand_derive_deserialize);
|
reg.add_decorator("derive_Deserialize", expand_derive_deserialize);
|
||||||
|
|
||||||
reg.add_post_expansion_pass(strip_attributes);
|
reg.add_post_expansion_pass(strip_attributes);
|
||||||
|
|
||||||
@@ -107,12 +109,185 @@ pub fn register(reg: &mut rustc_plugin::Registry) {
|
|||||||
reg.register_syntax_extension(
|
reg.register_syntax_extension(
|
||||||
syntax::parse::token::intern("derive_Serialize"),
|
syntax::parse::token::intern("derive_Serialize"),
|
||||||
syntax::ext::base::MultiDecorator(
|
syntax::ext::base::MultiDecorator(
|
||||||
Box::new(ser::expand_derive_serialize)));
|
Box::new(expand_derive_serialize)));
|
||||||
|
|
||||||
reg.register_syntax_extension(
|
reg.register_syntax_extension(
|
||||||
syntax::parse::token::intern("derive_Deserialize"),
|
syntax::parse::token::intern("derive_Deserialize"),
|
||||||
syntax::ext::base::MultiDecorator(
|
syntax::ext::base::MultiDecorator(
|
||||||
Box::new(de::expand_derive_deserialize)));
|
Box::new(expand_derive_deserialize)));
|
||||||
|
|
||||||
reg.register_attribute("serde".to_owned(), AttributeType::Normal);
|
reg.register_attribute("serde".to_owned(), AttributeType::Normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! shim {
|
||||||
|
($name:ident $pkg:ident :: $func:ident) => {
|
||||||
|
fn $func(
|
||||||
|
cx: &mut ::syntax::ext::base::ExtCtxt,
|
||||||
|
span: ::syntax::codemap::Span,
|
||||||
|
meta_item: &::syntax::ast::MetaItem,
|
||||||
|
annotatable: &::syntax::ext::base::Annotatable,
|
||||||
|
push: &mut FnMut(::syntax::ext::base::Annotatable)
|
||||||
|
) {
|
||||||
|
let item = match *annotatable {
|
||||||
|
::syntax::ext::base::Annotatable::Item(ref item) => item,
|
||||||
|
_ => {
|
||||||
|
cx.span_err(
|
||||||
|
meta_item.span,
|
||||||
|
concat!("`#[derive(",
|
||||||
|
stringify!($name),
|
||||||
|
")]` may only be applied to structs and enums"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
use syntax::{attr, ast, visit};
|
||||||
|
struct MarkSerdeAttributesUsed;
|
||||||
|
impl visit::Visitor for MarkSerdeAttributesUsed {
|
||||||
|
fn visit_attribute(&mut self, attr: &ast::Attribute) {
|
||||||
|
match attr.node.value.node {
|
||||||
|
ast::MetaItemKind::List(ref name, _) if name == "serde" => {
|
||||||
|
attr::mark_used(attr);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
visit::walk_item(&mut MarkSerdeAttributesUsed, item);
|
||||||
|
|
||||||
|
use syntax::print::pprust;
|
||||||
|
let s = pprust::item_to_string(item);
|
||||||
|
|
||||||
|
let syn_item = syn::parse_macro_input(&s).unwrap();
|
||||||
|
let expanded = match $pkg::$func(&syn_item) {
|
||||||
|
Ok(expanded) => expanded.to_string(),
|
||||||
|
Err(msg) => {
|
||||||
|
cx.span_err(span, &msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
use syntax::parse;
|
||||||
|
let name = stringify!($name).to_string();
|
||||||
|
let cfg = Vec::new();
|
||||||
|
let sess = parse::ParseSess::new();
|
||||||
|
let impl_item = parse::parse_item_from_source_str(name, expanded, cfg, &sess);
|
||||||
|
push(::syntax::ext::base::Annotatable::Item(impl_item.unwrap().unwrap()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
shim!(Serialize ser::expand_derive_serialize);
|
||||||
|
shim!(Deserialize de::expand_derive_deserialize);
|
||||||
|
|
||||||
|
#[cfg(feature = "with-syn")]
|
||||||
|
pub fn expand_single_item(item: &str) -> Result<String, String> {
|
||||||
|
let syn_item = syn::parse_macro_input(item).unwrap();
|
||||||
|
let (ser, de, syn_item) = strip_serde_derives(syn_item);
|
||||||
|
let expanded_ser = if ser {
|
||||||
|
Some(try!(ser::expand_derive_serialize(&syn_item)))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let expanded_de = if de {
|
||||||
|
Some(try!(de::expand_derive_deserialize(&syn_item)))
|
||||||
|
} else {
|
||||||
|
None::<quote::Tokens>
|
||||||
|
};
|
||||||
|
let syn_item = strip_serde_attrs(syn_item);
|
||||||
|
return Ok(quote!(#expanded_ser #expanded_de #syn_item).to_string());
|
||||||
|
|
||||||
|
fn strip_serde_derives(item: syn::MacroInput) -> (bool, bool, syn::MacroInput) {
|
||||||
|
let mut ser = false;
|
||||||
|
let mut de = false;
|
||||||
|
let item = syn::MacroInput {
|
||||||
|
attrs: item.attrs.into_iter().flat_map(|attr| {
|
||||||
|
if attr.is_sugared_doc {
|
||||||
|
return Some(attr);
|
||||||
|
}
|
||||||
|
let (name, nested) = match attr.value {
|
||||||
|
syn::MetaItem::List(name, nested) => (name, nested),
|
||||||
|
_ => return Some(attr)
|
||||||
|
};
|
||||||
|
if name != "derive" {
|
||||||
|
return Some(syn::Attribute {
|
||||||
|
value: syn::MetaItem::List(name, nested),
|
||||||
|
is_sugared_doc: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let rest: Vec<_> = nested.into_iter().filter(|nested| {
|
||||||
|
match *nested {
|
||||||
|
syn::MetaItem::Word(ref word) if word == "Serialize" => {
|
||||||
|
ser = true;
|
||||||
|
false
|
||||||
|
}
|
||||||
|
syn::MetaItem::Word(ref word) if word == "Deserialize" => {
|
||||||
|
de = true;
|
||||||
|
false
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
|
if rest.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(syn::Attribute {
|
||||||
|
value: syn::MetaItem::List(name, rest),
|
||||||
|
is_sugared_doc: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}).collect(),
|
||||||
|
..item
|
||||||
|
};
|
||||||
|
(ser, de, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_serde_attrs(item: syn::MacroInput) -> syn::MacroInput {
|
||||||
|
syn::MacroInput {
|
||||||
|
attrs: strip_serde_from_attrs(item.attrs),
|
||||||
|
body: match item.body {
|
||||||
|
syn::Body::Enum(variants) => syn::Body::Enum(
|
||||||
|
variants.into_iter().map(|variant| {
|
||||||
|
syn::Variant {
|
||||||
|
ident: variant.ident,
|
||||||
|
attrs: strip_serde_from_attrs(variant.attrs),
|
||||||
|
data: strip_serde_from_variant_data(variant.data),
|
||||||
|
discriminant: variant.discriminant,
|
||||||
|
}
|
||||||
|
}).collect()
|
||||||
|
),
|
||||||
|
syn::Body::Struct(variant_data) => syn::Body::Struct(
|
||||||
|
strip_serde_from_variant_data(variant_data)
|
||||||
|
),
|
||||||
|
},
|
||||||
|
..item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_serde_from_variant_data(data: syn::VariantData) -> syn::VariantData {
|
||||||
|
match data {
|
||||||
|
syn::VariantData::Struct(fields) => syn::VariantData::Struct(
|
||||||
|
fields.into_iter().map(strip_serde_from_field).collect()
|
||||||
|
),
|
||||||
|
syn::VariantData::Tuple(fields) => syn::VariantData::Tuple(
|
||||||
|
fields.into_iter().map(strip_serde_from_field).collect()
|
||||||
|
),
|
||||||
|
syn::VariantData::Unit => syn::VariantData::Unit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_serde_from_field(field: syn::Field) -> syn::Field {
|
||||||
|
syn::Field {
|
||||||
|
attrs: strip_serde_from_attrs(field.attrs),
|
||||||
|
..field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_serde_from_attrs(attrs: Vec<syn::Attribute>) -> Vec<syn::Attribute> {
|
||||||
|
attrs.into_iter().filter(|attr| {
|
||||||
|
match attr.value {
|
||||||
|
syn::MetaItem::List(ref ident, _) => ident != "serde",
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
mod bound;
|
|
||||||
mod de;
|
|
||||||
mod ser;
|
|
||||||
mod span;
|
|
||||||
+218
-324
@@ -1,103 +1,62 @@
|
|||||||
use aster;
|
use syn::{self, aster};
|
||||||
|
use quote::Tokens;
|
||||||
use syntax::ast::{self, Ident, MetaItem};
|
|
||||||
use syntax::codemap::Span;
|
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
|
||||||
use syntax::ptr::P;
|
|
||||||
use syntax::tokenstream::TokenTree;
|
|
||||||
|
|
||||||
use bound;
|
use bound;
|
||||||
use span;
|
|
||||||
use internals::ast::{Body, Field, Item, Style, Variant};
|
use internals::ast::{Body, Field, Item, Style, Variant};
|
||||||
use internals::{attr, Error};
|
use internals::{self, attr};
|
||||||
|
|
||||||
pub fn expand_derive_serialize(
|
pub fn expand_derive_serialize(item: &syn::MacroInput) -> Result<Tokens, String> {
|
||||||
cx: &mut ExtCtxt,
|
let ctxt = internals::Ctxt::new();
|
||||||
span: Span,
|
let item = Item::from_ast(&ctxt, item);
|
||||||
meta_item: &MetaItem,
|
try!(ctxt.check());
|
||||||
annotatable: &Annotatable,
|
|
||||||
push: &mut FnMut(Annotatable)
|
|
||||||
) {
|
|
||||||
let item = match *annotatable {
|
|
||||||
Annotatable::Item(ref item) => item,
|
|
||||||
_ => {
|
|
||||||
cx.span_err(
|
|
||||||
meta_item.span,
|
|
||||||
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let item = match Item::from_ast(cx, item) {
|
let impl_generics = build_impl_generics(&item);
|
||||||
Ok(item) => item,
|
|
||||||
Err(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 ty = aster::ty().path()
|
||||||
|
.segment(item.ident.clone()).with_generics(impl_generics.clone()).build()
|
||||||
let impl_item = serialize_item(cx, &builder, &item);
|
|
||||||
push(span::record_expansion(cx, impl_item, "Serialize"))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_item(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
item: &Item,
|
|
||||||
) -> P<ast::Item> {
|
|
||||||
let impl_generics = build_impl_generics(builder, &item);
|
|
||||||
|
|
||||||
let ty = builder.ty().path()
|
|
||||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let body = serialize_body(cx,
|
let body = serialize_body(&item,
|
||||||
builder,
|
|
||||||
&item,
|
|
||||||
&impl_generics,
|
&impl_generics,
|
||||||
ty.clone());
|
ty.clone());
|
||||||
|
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
|
|
||||||
let dummy_const = builder.id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
let dummy_const = aster::id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
||||||
|
|
||||||
quote_item!(cx,
|
Ok(quote! {
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||||
const $dummy_const: () = {
|
const #dummy_const: () = {
|
||||||
extern crate serde as _serde;
|
extern crate serde as _serde;
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl $impl_generics _serde::ser::Serialize for $ty $where_clause {
|
impl #impl_generics _serde::ser::Serialize for #ty #where_clause {
|
||||||
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||||
where __S: _serde::ser::Serializer
|
where __S: _serde::ser::Serializer
|
||||||
$body
|
{
|
||||||
|
#body
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
).unwrap()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
||||||
// field type that will be serialized by us.
|
// field type that will be serialized by us.
|
||||||
fn build_impl_generics(
|
fn build_impl_generics(item: &Item) -> syn::Generics {
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
item: &Item,
|
|
||||||
) -> ast::Generics {
|
|
||||||
let generics = bound::without_defaults(item.generics);
|
let generics = bound::without_defaults(item.generics);
|
||||||
|
|
||||||
let generics = bound::with_where_predicates_from_fields(
|
let generics = bound::with_where_predicates_from_fields(
|
||||||
builder, item, &generics,
|
item, &generics,
|
||||||
|attrs| attrs.ser_bound());
|
|attrs| attrs.ser_bound());
|
||||||
|
|
||||||
match item.attrs.ser_bound() {
|
match item.attrs.ser_bound() {
|
||||||
Some(predicates) => {
|
Some(predicates) => {
|
||||||
bound::with_where_predicates(builder, &generics, predicates)
|
bound::with_where_predicates(&generics, predicates)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
bound::with_bound(builder, item, &generics,
|
bound::with_bound(item, &generics,
|
||||||
needs_serialize_bound,
|
needs_serialize_bound,
|
||||||
&builder.path().ids(&["_serde", "ser", "Serialize"]).build())
|
&aster::path().ids(&["_serde", "ser", "Serialize"]).build())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,18 +72,14 @@ fn needs_serialize_bound(attrs: &attr::Field) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_body(
|
fn serialize_body(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
item: &Item,
|
item: &Item,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &syn::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: syn::Ty,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
match item.body {
|
match item.body {
|
||||||
Body::Enum(ref variants) => {
|
Body::Enum(ref variants) => {
|
||||||
serialize_item_enum(
|
serialize_item_enum(
|
||||||
cx,
|
&item.ident,
|
||||||
builder,
|
|
||||||
item.ident,
|
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
variants,
|
variants,
|
||||||
@@ -132,12 +87,10 @@ fn serialize_body(
|
|||||||
}
|
}
|
||||||
Body::Struct(Style::Struct, ref fields) => {
|
Body::Struct(Style::Struct, ref fields) => {
|
||||||
if fields.iter().any(|field| field.ident.is_none()) {
|
if fields.iter().any(|field| field.ident.is_none()) {
|
||||||
cx.span_bug(item.span, "struct has unnamed fields")
|
panic!("struct has unnamed fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_struct(
|
serialize_struct(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
@@ -145,12 +98,10 @@ fn serialize_body(
|
|||||||
}
|
}
|
||||||
Body::Struct(Style::Tuple, ref fields) => {
|
Body::Struct(Style::Tuple, ref fields) => {
|
||||||
if fields.iter().any(|field| field.ident.is_some()) {
|
if fields.iter().any(|field| field.ident.is_some()) {
|
||||||
cx.span_bug(item.span, "tuple struct has named fields")
|
panic!("tuple struct has named fields");
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_tuple_struct(
|
serialize_tuple_struct(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
@@ -158,8 +109,6 @@ fn serialize_body(
|
|||||||
}
|
}
|
||||||
Body::Struct(Style::Newtype, ref fields) => {
|
Body::Struct(Style::Newtype, ref fields) => {
|
||||||
serialize_newtype_struct(
|
serialize_newtype_struct(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
&fields[0],
|
&fields[0],
|
||||||
@@ -167,136 +116,116 @@ fn serialize_body(
|
|||||||
}
|
}
|
||||||
Body::Struct(Style::Unit, _) => {
|
Body::Struct(Style::Unit, _) => {
|
||||||
serialize_unit_struct(
|
serialize_unit_struct(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
&item.attrs)
|
&item.attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(
|
fn serialize_unit_struct(item_attrs: &attr::Item) -> Tokens {
|
||||||
cx: &ExtCtxt,
|
let type_name = item_attrs.name().serialize_name();
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
item_attrs: &attr::Item,
|
|
||||||
) -> P<ast::Block> {
|
|
||||||
let type_name = name_expr(builder, item_attrs.name());
|
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
_serializer.serialize_unit_struct($type_name)
|
_serializer.serialize_unit_struct(#type_name)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct(
|
fn serialize_newtype_struct(
|
||||||
cx: &ExtCtxt,
|
impl_generics: &syn::Generics,
|
||||||
builder: &aster::AstBuilder,
|
item_ty: syn::Ty,
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
item_ty: P<ast::Ty>,
|
|
||||||
field: &Field,
|
field: &Field,
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let type_name = name_expr(builder, item_attrs.name());
|
let type_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
let mut field_expr = quote_expr!(cx, &self.0);
|
let mut field_expr = quote!(&self.0);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(
|
||||||
&item_ty, impl_generics, &field.ty, path, field_expr);
|
&item_ty, impl_generics, field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
_serializer.serialize_newtype_struct(#type_name, #field_expr)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(
|
||||||
cx: &ExtCtxt,
|
impl_generics: &syn::Generics,
|
||||||
builder: &aster::AstBuilder,
|
ty: syn::Ty,
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
let serialize_stmts = serialize_tuple_struct_visitor(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
cx.ident_of("serialize_tuple_struct_elt"),
|
aster::id("serialize_tuple_struct_elt"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = name_expr(builder, item_attrs.name());
|
let type_name = item_attrs.name().serialize_name();
|
||||||
let len = serialize_stmts.len();
|
let len = serialize_stmts.len();
|
||||||
let let_mut = mut_if(cx, len > 0);
|
let let_mut = mut_if(len > 0);
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
let $let_mut state = try!(_serializer.serialize_tuple_struct($type_name, $len));
|
let #let_mut __serde_state = try!(_serializer.serialize_tuple_struct(#type_name, #len));
|
||||||
$serialize_stmts
|
#(serialize_stmts)*
|
||||||
_serializer.serialize_tuple_struct_end(state)
|
_serializer.serialize_tuple_struct_end(__serde_state)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct(
|
fn serialize_struct(
|
||||||
cx: &ExtCtxt,
|
impl_generics: &syn::Generics,
|
||||||
builder: &aster::AstBuilder,
|
ty: syn::Ty,
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let serialize_fields = serialize_struct_visitor(
|
let serialize_fields = serialize_struct_visitor(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
false,
|
false,
|
||||||
cx.ident_of("serialize_struct_elt"),
|
aster::id("serialize_struct_elt"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let type_name = name_expr(builder, item_attrs.name());
|
let type_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
let mut serialized_fields = fields.iter()
|
let mut serialized_fields = fields.iter()
|
||||||
.filter(|&field| !field.attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
.peekable();
|
.peekable();
|
||||||
|
|
||||||
let let_mut = mut_if(cx, serialized_fields.peek().is_some());
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||||
|
|
||||||
let len = serialized_fields
|
let len = serialized_fields
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
let ident = field.ident.expect("struct has unnamed fields");
|
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||||
let field_expr = quote_expr!(cx, &self.$ident);
|
let field_expr = quote!(&self.#ident);
|
||||||
|
|
||||||
match field.attrs.skip_serializing_if() {
|
match field.attrs.skip_serializing_if() {
|
||||||
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
Some(path) => quote!(if #path(#field_expr) { 0 } else { 1 }),
|
||||||
None => quote_expr!(cx, 1),
|
None => quote!(1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
let $let_mut state = try!(_serializer.serialize_struct($type_name, $len));
|
let #let_mut __serde_state = try!(_serializer.serialize_struct(#type_name, #len));
|
||||||
$serialize_fields
|
#(serialize_fields)*
|
||||||
_serializer.serialize_struct_end(state)
|
_serializer.serialize_struct_end(__serde_state)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_item_enum(
|
fn serialize_item_enum(
|
||||||
cx: &ExtCtxt,
|
type_ident: &syn::Ident,
|
||||||
builder: &aster::AstBuilder,
|
impl_generics: &syn::Generics,
|
||||||
type_ident: Ident,
|
ty: syn::Ty,
|
||||||
impl_generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
variants: &[Variant],
|
variants: &[Variant],
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let arms: Vec<_> =
|
let arms: Vec<_> =
|
||||||
variants.iter()
|
variants.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(variant_index, variant)| {
|
.map(|(variant_index, variant)| {
|
||||||
serialize_variant(
|
serialize_variant(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
type_ident,
|
type_ident,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
@@ -307,44 +236,40 @@ fn serialize_item_enum(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
match *self {
|
match *self {
|
||||||
$arms
|
#(arms)*
|
||||||
}
|
}
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_variant(
|
fn serialize_variant(
|
||||||
cx: &ExtCtxt,
|
type_ident: &syn::Ident,
|
||||||
builder: &aster::AstBuilder,
|
generics: &syn::Generics,
|
||||||
type_ident: Ident,
|
ty: syn::Ty,
|
||||||
generics: &ast::Generics,
|
|
||||||
ty: P<ast::Ty>,
|
|
||||||
variant: &Variant,
|
variant: &Variant,
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> ast::Arm {
|
) -> Tokens {
|
||||||
let type_name = name_expr(builder, item_attrs.name());
|
let type_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
let variant_ident = variant.ident;
|
let variant_ident = variant.ident.clone();
|
||||||
let variant_name = name_expr(builder, variant.attrs.name());
|
let variant_name = variant.attrs.name().serialize_name();
|
||||||
|
|
||||||
match variant.style {
|
match variant.style {
|
||||||
Style::Unit => {
|
Style::Unit => {
|
||||||
quote_arm!(cx,
|
quote! {
|
||||||
$type_ident::$variant_ident =>
|
#type_ident::#variant_ident =>
|
||||||
_serde::ser::Serializer::serialize_unit_variant(
|
_serde::ser::Serializer::serialize_unit_variant(
|
||||||
_serializer,
|
_serializer,
|
||||||
$type_name,
|
#type_name,
|
||||||
$variant_index,
|
#variant_index,
|
||||||
$variant_name,
|
#variant_name,
|
||||||
),
|
),
|
||||||
)
|
}
|
||||||
},
|
},
|
||||||
Style::Newtype => {
|
Style::Newtype => {
|
||||||
let block = serialize_newtype_variant(
|
let block = serialize_newtype_variant(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
type_name,
|
type_name,
|
||||||
variant_index,
|
variant_index,
|
||||||
variant_name,
|
variant_name,
|
||||||
@@ -353,26 +278,21 @@ fn serialize_variant(
|
|||||||
&variant.fields[0],
|
&variant.fields[0],
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote! {
|
||||||
$type_ident::$variant_ident(ref __simple_value) => $block
|
#type_ident::#variant_ident(ref __simple_value) => #block,
|
||||||
)
|
}
|
||||||
},
|
},
|
||||||
Style::Tuple => {
|
Style::Tuple => {
|
||||||
let field_names: Vec<ast::Ident> = (0 .. variant.fields.len())
|
let field_names: Vec<Tokens> = (0 .. variant.fields.len())
|
||||||
.map(|i| builder.id(format!("__field{}", i)))
|
.map(|i| {
|
||||||
|
let id = aster::id(format!("__field{}", i));
|
||||||
|
quote!(ref #id)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let pat = builder.pat().enum_()
|
let pat = quote!(#type_ident::#variant_ident(#(field_names),*));
|
||||||
.id(type_ident).id(variant_ident).build()
|
|
||||||
.with_pats(
|
|
||||||
field_names.iter()
|
|
||||||
.map(|field| builder.pat().ref_id(field))
|
|
||||||
)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let block = serialize_tuple_variant(
|
let block = serialize_tuple_variant(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
type_name,
|
type_name,
|
||||||
variant_index,
|
variant_index,
|
||||||
variant_name,
|
variant_name,
|
||||||
@@ -381,28 +301,21 @@ fn serialize_variant(
|
|||||||
&variant.fields,
|
&variant.fields,
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote! {
|
||||||
$pat => $block
|
#pat => { #block }
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let mut pat = builder.pat().struct_().id(type_ident).id(variant_ident).build();
|
let fields = variant.fields.iter().map(|field| {
|
||||||
for field in &variant.fields {
|
let id = match field.ident {
|
||||||
let name = match field.ident {
|
Some(ref name) => name.clone(),
|
||||||
Some(name) => name,
|
None => panic!("struct variant has unnamed fields"),
|
||||||
None => cx.span_bug(field.span, "struct variant has unnamed fields"),
|
|
||||||
};
|
};
|
||||||
pat = pat.with_field_pat(ast::FieldPat {
|
quote!(ref #id)
|
||||||
ident: name,
|
});
|
||||||
pat: builder.pat().ref_id(name),
|
let pat = quote!(#type_ident::#variant_ident { #(fields),* });
|
||||||
is_shorthand: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let pat = pat.build();
|
|
||||||
|
|
||||||
let block = serialize_struct_variant(
|
let block = serialize_struct_variant(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
variant_index,
|
variant_index,
|
||||||
variant_name,
|
variant_name,
|
||||||
generics,
|
generics,
|
||||||
@@ -411,262 +324,243 @@ fn serialize_variant(
|
|||||||
item_attrs,
|
item_attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote! {
|
||||||
$pat => $block
|
#pat => { #block }
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant(
|
fn serialize_newtype_variant(
|
||||||
cx: &ExtCtxt,
|
type_name: String,
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
type_name: P<ast::Expr>,
|
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant_name: P<ast::Expr>,
|
variant_name: String,
|
||||||
item_ty: P<ast::Ty>,
|
item_ty: syn::Ty,
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
field: &Field,
|
field: &Field,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let mut field_expr = quote_expr!(cx, __simple_value);
|
let mut field_expr = quote!(__simple_value);
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(
|
||||||
&item_ty, generics, &field.ty, path, field_expr);
|
&item_ty, generics, field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
_serde::ser::Serializer::serialize_newtype_variant(
|
_serde::ser::Serializer::serialize_newtype_variant(
|
||||||
_serializer,
|
_serializer,
|
||||||
$type_name,
|
#type_name,
|
||||||
$variant_index,
|
#variant_index,
|
||||||
$variant_name,
|
#variant_name,
|
||||||
$field_expr,
|
#field_expr,
|
||||||
)
|
)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
cx: &ExtCtxt,
|
type_name: String,
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
type_name: P<ast::Expr>,
|
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant_name: P<ast::Expr>,
|
variant_name: String,
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
structure_ty: P<ast::Ty>,
|
structure_ty: syn::Ty,
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
let serialize_stmts = serialize_tuple_struct_visitor(
|
let serialize_stmts = serialize_tuple_struct_visitor(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
structure_ty,
|
structure_ty,
|
||||||
fields,
|
fields,
|
||||||
generics,
|
generics,
|
||||||
true,
|
true,
|
||||||
cx.ident_of("serialize_tuple_variant_elt"),
|
aster::id("serialize_tuple_variant_elt"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let len = serialize_stmts.len();
|
let len = serialize_stmts.len();
|
||||||
let let_mut = mut_if(cx, len > 0);
|
let let_mut = mut_if(len > 0);
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
let $let_mut state = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
let #let_mut __serde_state = try!(_serializer.serialize_tuple_variant(
|
||||||
$serialize_stmts
|
#type_name,
|
||||||
_serializer.serialize_tuple_variant_end(state)
|
#variant_index,
|
||||||
}).unwrap()
|
#variant_name,
|
||||||
|
#len));
|
||||||
|
#(serialize_stmts)*
|
||||||
|
_serializer.serialize_tuple_variant_end(__serde_state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
variant_index: usize,
|
variant_index: usize,
|
||||||
variant_name: P<ast::Expr>,
|
variant_name: String,
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: syn::Ty,
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
item_attrs: &attr::Item,
|
item_attrs: &attr::Item,
|
||||||
) -> P<ast::Block> {
|
) -> Tokens {
|
||||||
|
|
||||||
let serialize_fields = serialize_struct_visitor(
|
let serialize_fields = serialize_struct_visitor(
|
||||||
cx,
|
|
||||||
builder,
|
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
fields,
|
fields,
|
||||||
&generics,
|
generics,
|
||||||
true,
|
true,
|
||||||
cx.ident_of("serialize_struct_variant_elt"),
|
aster::id("serialize_struct_variant_elt"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let item_name = name_expr(builder, item_attrs.name());
|
let item_name = item_attrs.name().serialize_name();
|
||||||
|
|
||||||
let mut serialized_fields = fields.iter()
|
let mut serialized_fields = fields.iter()
|
||||||
.filter(|&field| !field.attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
.peekable();
|
.peekable();
|
||||||
|
|
||||||
let let_mut = mut_if(cx, serialized_fields.peek().is_some());
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
||||||
|
|
||||||
let len = serialized_fields
|
let len = serialized_fields
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
let ident = field.ident.expect("struct has unnamed fields");
|
let ident = field.ident.clone().expect("struct has unnamed fields");
|
||||||
let field_expr = quote_expr!(cx, $ident);
|
|
||||||
|
|
||||||
match field.attrs.skip_serializing_if() {
|
match field.attrs.skip_serializing_if() {
|
||||||
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
||||||
None => quote_expr!(cx, 1),
|
None => quote!(1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
||||||
|
|
||||||
quote_block!(cx, {
|
quote! {
|
||||||
let $let_mut state = try!(_serializer.serialize_struct_variant(
|
let #let_mut __serde_state = try!(_serializer.serialize_struct_variant(
|
||||||
$item_name,
|
#item_name,
|
||||||
$variant_index,
|
#variant_index,
|
||||||
$variant_name,
|
#variant_name,
|
||||||
$len,
|
#len,
|
||||||
));
|
));
|
||||||
$serialize_fields
|
#(serialize_fields)*
|
||||||
_serializer.serialize_struct_variant_end(state)
|
_serializer.serialize_struct_variant_end(__serde_state)
|
||||||
}).unwrap()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct_visitor(
|
fn serialize_tuple_struct_visitor(
|
||||||
cx: &ExtCtxt,
|
structure_ty: syn::Ty,
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
structure_ty: P<ast::Ty>,
|
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
func: ast::Ident,
|
func: syn::Ident,
|
||||||
) -> Vec<ast::Stmt> {
|
) -> Vec<Tokens> {
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, field)| {
|
.map(|(i, field)| {
|
||||||
let mut field_expr = if is_enum {
|
let mut field_expr = if is_enum {
|
||||||
builder.expr().path().id(format!("__field{}", i)).build()
|
let id = aster::id(format!("__field{}", i));
|
||||||
|
quote!(#id)
|
||||||
} else {
|
} else {
|
||||||
builder.expr().ref_().tup_field(i).self_()
|
let i = aster::id(i);
|
||||||
|
quote!(&self.#i)
|
||||||
};
|
};
|
||||||
|
|
||||||
let skip = field.attrs.skip_serializing_if()
|
let skip = field.attrs.skip_serializing_if()
|
||||||
.map(|path| quote_expr!(cx, $path($field_expr)));
|
.map(|path| quote!(#path(#field_expr)));
|
||||||
|
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(
|
||||||
&structure_ty, generics, &field.ty, path, field_expr);
|
&structure_ty, generics, field.ty, path, field_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote_expr!(cx,
|
let ser = quote! {
|
||||||
try!(_serializer.$func(&mut state, $field_expr));
|
try!(_serializer.#func(&mut __serde_state, #field_expr));
|
||||||
);
|
};
|
||||||
|
|
||||||
match skip {
|
match skip {
|
||||||
None => quote_stmt!(cx, $ser).unwrap(),
|
None => ser,
|
||||||
Some(skip) => quote_stmt!(cx, if !$skip { $ser }).unwrap(),
|
Some(skip) => quote!(if !#skip { #ser }),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_visitor(
|
fn serialize_struct_visitor(
|
||||||
cx: &ExtCtxt,
|
structure_ty: syn::Ty,
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
structure_ty: P<ast::Ty>,
|
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
generics: &ast::Generics,
|
generics: &syn::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
func: ast::Ident,
|
func: syn::Ident,
|
||||||
) -> Vec<ast::Stmt> {
|
) -> Vec<Tokens> {
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.filter(|&field| !field.attrs.skip_serializing())
|
.filter(|&field| !field.attrs.skip_serializing())
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
let ident = field.ident.expect("struct has unnamed field");
|
let ident = field.ident.clone().expect("struct has unnamed field");
|
||||||
let mut field_expr = if is_enum {
|
let mut field_expr = if is_enum {
|
||||||
quote_expr!(cx, $ident)
|
quote!(#ident)
|
||||||
} else {
|
} else {
|
||||||
quote_expr!(cx, &self.$ident)
|
quote!(&self.#ident)
|
||||||
};
|
};
|
||||||
|
|
||||||
let key_expr = name_expr(builder, field.attrs.name());
|
let key_expr = field.attrs.name().serialize_name();
|
||||||
|
|
||||||
let skip = field.attrs.skip_serializing_if()
|
let skip = field.attrs.skip_serializing_if()
|
||||||
.map(|path| quote_expr!(cx, $path($field_expr)));
|
.map(|path| quote!(#path(#field_expr)));
|
||||||
|
|
||||||
if let Some(path) = field.attrs.serialize_with() {
|
if let Some(path) = field.attrs.serialize_with() {
|
||||||
field_expr = wrap_serialize_with(cx, builder,
|
field_expr = wrap_serialize_with(
|
||||||
&structure_ty, generics, &field.ty, path, field_expr)
|
&structure_ty, generics, field.ty, path, field_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
let ser = quote_expr!(cx,
|
let ser = quote! {
|
||||||
try!(_serializer.$func(&mut state, $key_expr, $field_expr));
|
try!(_serializer.#func(&mut __serde_state, #key_expr, #field_expr));
|
||||||
);
|
};
|
||||||
|
|
||||||
match skip {
|
match skip {
|
||||||
None => quote_stmt!(cx, $ser).unwrap(),
|
None => ser,
|
||||||
Some(skip) => quote_stmt!(cx, if !$skip { $ser }).unwrap(),
|
Some(skip) => quote!(if !#skip { #ser }),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wrap_serialize_with(
|
fn wrap_serialize_with(
|
||||||
cx: &ExtCtxt,
|
item_ty: &syn::Ty,
|
||||||
builder: &aster::AstBuilder,
|
generics: &syn::Generics,
|
||||||
item_ty: &P<ast::Ty>,
|
field_ty: &syn::Ty,
|
||||||
generics: &ast::Generics,
|
path: &syn::Path,
|
||||||
field_ty: &P<ast::Ty>,
|
value: Tokens,
|
||||||
path: &ast::Path,
|
) -> Tokens {
|
||||||
value: P<ast::Expr>,
|
|
||||||
) -> P<ast::Expr> {
|
|
||||||
let where_clause = &generics.where_clause;
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
let wrapper_generics = builder.from_generics(generics.clone())
|
let wrapper_generics = aster::from_generics(generics.clone())
|
||||||
.add_lifetime_bound("'__a")
|
.add_lifetime_bound("'__a")
|
||||||
.lifetime_name("'__a")
|
.lifetime_name("'__a")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let wrapper_ty = builder.path()
|
let wrapper_ty = aster::path()
|
||||||
.segment("__SerializeWith")
|
.segment("__SerializeWith")
|
||||||
.with_generics(wrapper_generics.clone())
|
.with_generics(wrapper_generics.clone())
|
||||||
.build()
|
.build()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
quote_expr!(cx, {
|
quote!({
|
||||||
struct __SerializeWith $wrapper_generics $where_clause {
|
struct __SerializeWith #wrapper_generics #where_clause {
|
||||||
value: &'__a $field_ty,
|
value: &'__a #field_ty,
|
||||||
phantom: ::std::marker::PhantomData<$item_ty>,
|
phantom: ::std::marker::PhantomData<#item_ty>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $wrapper_generics _serde::ser::Serialize for $wrapper_ty $where_clause {
|
impl #wrapper_generics _serde::ser::Serialize for #wrapper_ty #where_clause {
|
||||||
fn serialize<__S>(&self, __s: &mut __S) -> ::std::result::Result<(), __S::Error>
|
fn serialize<__S>(&self, __s: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||||
where __S: _serde::ser::Serializer
|
where __S: _serde::ser::Serializer
|
||||||
{
|
{
|
||||||
$path(self.value, __s)
|
#path(self.value, __s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__SerializeWith {
|
__SerializeWith {
|
||||||
value: $value,
|
value: #value,
|
||||||
phantom: ::std::marker::PhantomData::<$item_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())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialization of an empty struct results in code like:
|
// Serialization of an empty struct results in code like:
|
||||||
//
|
//
|
||||||
// let mut state = try!(serializer.serialize_struct("S", 0));
|
// let mut __serde_state = try!(serializer.serialize_struct("S", 0));
|
||||||
// serializer.serialize_struct_end(state)
|
// serializer.serialize_struct_end(__serde_state)
|
||||||
//
|
//
|
||||||
// where we want to omit the `mut` to avoid a warning.
|
// where we want to omit the `mut` to avoid a warning.
|
||||||
fn mut_if(cx: &ExtCtxt, is_mut: bool) -> Vec<TokenTree> {
|
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
||||||
if is_mut {
|
if is_mut {
|
||||||
quote_tokens!(cx, mut)
|
Some(quote!(mut))
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
use syntax::ast;
|
|
||||||
use syntax::codemap::{self, ExpnId, Span};
|
|
||||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
|
||||||
use syntax::fold::{self, Folder};
|
|
||||||
use syntax::parse::token::intern;
|
|
||||||
use syntax::ptr::P;
|
|
||||||
|
|
||||||
pub fn record_expansion(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
item: P<ast::Item>,
|
|
||||||
derive: &str,
|
|
||||||
) -> Annotatable {
|
|
||||||
let info = codemap::ExpnInfo {
|
|
||||||
call_site: codemap::DUMMY_SP,
|
|
||||||
callee: codemap::NameAndSpan {
|
|
||||||
format: codemap::MacroAttribute(intern(&format!("derive({})", derive))),
|
|
||||||
span: None,
|
|
||||||
allow_internal_unstable: false,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let expn_id = cx.codemap().record_expansion(info);
|
|
||||||
|
|
||||||
let mut respanner = Respanner { expn_id: expn_id };
|
|
||||||
let item = item.map(|item| respanner.fold_item_simple(item));
|
|
||||||
Annotatable::Item(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Respanner {
|
|
||||||
expn_id: ExpnId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Folder for Respanner {
|
|
||||||
fn new_span(&mut self, span: Span) -> Span {
|
|
||||||
Span {
|
|
||||||
expn_id: self.expn_id,
|
|
||||||
.. span
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
||||||
fold::noop_fold_mac(mac, self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen_internals"
|
name = "serde_codegen_internals"
|
||||||
version = "0.8.9"
|
version = "0.9.0"
|
||||||
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 = "AST representation used by Serde codegen. Unstable."
|
description = "AST representation used by Serde codegen. Unstable."
|
||||||
@@ -11,11 +11,8 @@ keywords = ["serde", "serialization"]
|
|||||||
include = ["Cargo.toml", "src/**/*.rs"]
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["with-syntex"]
|
|
||||||
unstable-testing = ["clippy"]
|
unstable-testing = ["clippy"]
|
||||||
with-syntex = ["syntex_syntax", "syntex_errors"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
syntex_syntax = { version = "^0.44.0", optional = true }
|
syn = "0.8"
|
||||||
syntex_errors = { version = "^0.44.0", optional = true }
|
|
||||||
|
|||||||
@@ -1,17 +1,12 @@
|
|||||||
use syntax::ast;
|
use syn;
|
||||||
use syntax::codemap;
|
|
||||||
use syntax::ext::base::ExtCtxt;
|
|
||||||
use syntax::ptr::P;
|
|
||||||
|
|
||||||
use attr;
|
use attr;
|
||||||
use Error;
|
use Ctxt;
|
||||||
|
|
||||||
pub struct Item<'a> {
|
pub struct Item<'a> {
|
||||||
pub ident: ast::Ident,
|
pub ident: syn::Ident,
|
||||||
pub span: codemap::Span,
|
|
||||||
pub attrs: attr::Item,
|
pub attrs: attr::Item,
|
||||||
pub body: Body<'a>,
|
pub body: Body<'a>,
|
||||||
pub generics: &'a ast::Generics,
|
pub generics: &'a syn::Generics,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Body<'a> {
|
pub enum Body<'a> {
|
||||||
@@ -20,18 +15,16 @@ pub enum Body<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Variant<'a> {
|
pub struct Variant<'a> {
|
||||||
pub ident: ast::Ident,
|
pub ident: syn::Ident,
|
||||||
pub span: codemap::Span,
|
|
||||||
pub attrs: attr::Variant,
|
pub attrs: attr::Variant,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub fields: Vec<Field<'a>>,
|
pub fields: Vec<Field<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Field<'a> {
|
pub struct Field<'a> {
|
||||||
pub ident: Option<ast::Ident>,
|
pub ident: Option<syn::Ident>,
|
||||||
pub span: codemap::Span,
|
|
||||||
pub attrs: attr::Field,
|
pub attrs: attr::Field,
|
||||||
pub ty: &'a P<ast::Ty>,
|
pub ty: &'a syn::Ty,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Style {
|
pub enum Style {
|
||||||
@@ -42,33 +35,25 @@ pub enum Style {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Item<'a> {
|
impl<'a> Item<'a> {
|
||||||
pub fn from_ast(
|
pub fn from_ast(cx: &Ctxt, item: &'a syn::MacroInput) -> Item<'a> {
|
||||||
cx: &ExtCtxt,
|
|
||||||
item: &'a ast::Item,
|
|
||||||
) -> Result<Item<'a>, Error> {
|
|
||||||
let attrs = attr::Item::from_ast(cx, item);
|
let attrs = attr::Item::from_ast(cx, item);
|
||||||
|
|
||||||
let (body, generics) = match item.node {
|
let body = match item.body {
|
||||||
ast::ItemKind::Enum(ref enum_def, ref generics) => {
|
syn::Body::Enum(ref variants) => {
|
||||||
let variants = enum_from_ast(cx, enum_def);
|
Body::Enum(enum_from_ast(cx, variants))
|
||||||
(Body::Enum(variants), generics)
|
|
||||||
}
|
}
|
||||||
ast::ItemKind::Struct(ref variant_data, ref generics) => {
|
syn::Body::Struct(ref variant_data) => {
|
||||||
let (style, fields) = struct_from_ast(cx, variant_data);
|
let (style, fields) = struct_from_ast(cx, variant_data);
|
||||||
(Body::Struct(style, fields), generics)
|
Body::Struct(style, fields)
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(Error::UnexpectedItemKind);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Item {
|
Item {
|
||||||
ident: item.ident,
|
ident: item.ident.clone(),
|
||||||
span: item.span,
|
|
||||||
attrs: attrs,
|
attrs: attrs,
|
||||||
body: body,
|
body: body,
|
||||||
generics: generics,
|
generics: &item.generics,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,16 +71,12 @@ impl<'a> Body<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enum_from_ast<'a>(
|
fn enum_from_ast<'a>(cx: &Ctxt, variants: &'a [syn::Variant]) -> Vec<Variant<'a>> {
|
||||||
cx: &ExtCtxt,
|
variants.iter()
|
||||||
enum_def: &'a ast::EnumDef,
|
|
||||||
) -> Vec<Variant<'a>> {
|
|
||||||
enum_def.variants.iter()
|
|
||||||
.map(|variant| {
|
.map(|variant| {
|
||||||
let (style, fields) = struct_from_ast(cx, &variant.node.data);
|
let (style, fields) = struct_from_ast(cx, &variant.data);
|
||||||
Variant {
|
Variant {
|
||||||
ident: variant.node.name,
|
ident: variant.ident.clone(),
|
||||||
span: variant.span,
|
|
||||||
attrs: attr::Variant::from_ast(cx, variant),
|
attrs: attr::Variant::from_ast(cx, variant),
|
||||||
style: style,
|
style: style,
|
||||||
fields: fields,
|
fields: fields,
|
||||||
@@ -104,36 +85,29 @@ fn enum_from_ast<'a>(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_from_ast<'a>(
|
fn struct_from_ast<'a>(cx: &Ctxt, data: &'a syn::VariantData) -> (Style, Vec<Field<'a>>) {
|
||||||
cx: &ExtCtxt,
|
match *data {
|
||||||
variant_data: &'a ast::VariantData,
|
syn::VariantData::Struct(ref fields) => {
|
||||||
) -> (Style, Vec<Field<'a>>) {
|
|
||||||
match *variant_data {
|
|
||||||
ast::VariantData::Struct(ref fields, _) => {
|
|
||||||
(Style::Struct, fields_from_ast(cx, fields))
|
(Style::Struct, fields_from_ast(cx, fields))
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
syn::VariantData::Tuple(ref fields) if fields.len() == 1 => {
|
||||||
(Style::Newtype, fields_from_ast(cx, fields))
|
(Style::Newtype, fields_from_ast(cx, fields))
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
syn::VariantData::Tuple(ref fields) => {
|
||||||
(Style::Tuple, fields_from_ast(cx, fields))
|
(Style::Tuple, fields_from_ast(cx, fields))
|
||||||
}
|
}
|
||||||
ast::VariantData::Unit(_) => {
|
syn::VariantData::Unit => {
|
||||||
(Style::Unit, Vec::new())
|
(Style::Unit, Vec::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fields_from_ast<'a>(
|
fn fields_from_ast<'a>(cx: &Ctxt, fields: &'a [syn::Field]) -> Vec<Field<'a>> {
|
||||||
cx: &ExtCtxt,
|
|
||||||
fields: &'a [ast::StructField],
|
|
||||||
) -> Vec<Field<'a>> {
|
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, field)| {
|
.map(|(i, field)| {
|
||||||
Field {
|
Field {
|
||||||
ident: field.ident,
|
ident: field.ident.clone(),
|
||||||
span: field.span,
|
|
||||||
attrs: attr::Field::from_ast(cx, i, field),
|
attrs: attr::Field::from_ast(cx, i, field),
|
||||||
ty: &field.ty,
|
ty: &field.ty,
|
||||||
}
|
}
|
||||||
|
|||||||
+153
-306
@@ -1,15 +1,5 @@
|
|||||||
use std::rc::Rc;
|
use Ctxt;
|
||||||
|
use syn;
|
||||||
use syntax::ast;
|
|
||||||
use syntax::attr::{self, HasAttrs};
|
|
||||||
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, meta_list_item_to_string};
|
|
||||||
use syntax::tokenstream::{self, TokenTree};
|
|
||||||
|
|
||||||
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
||||||
// are `attr::Item::from_ast`, `attr::Variant::from_ast`, and
|
// are `attr::Item::from_ast`, `attr::Variant::from_ast`, and
|
||||||
@@ -19,13 +9,14 @@ use syntax::tokenstream::{self, TokenTree};
|
|||||||
// user will see errors simultaneously for all bad attributes in the crate
|
// user will see errors simultaneously for all bad attributes in the crate
|
||||||
// rather than just the first.
|
// rather than just the first.
|
||||||
|
|
||||||
struct Attr<'a, 'b: 'a, T> {
|
struct Attr<'c, T> {
|
||||||
cx: &'a ExtCtxt<'b>,
|
cx: &'c Ctxt,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
value: Option<Spanned<T>>,
|
value: Option<T>,
|
||||||
}
|
}
|
||||||
impl<'a, 'b, T> Attr<'a, 'b, T> {
|
|
||||||
fn none(cx: &'a ExtCtxt<'b>, name: &'static str) -> Self {
|
impl<'c, T> Attr<'c, T> {
|
||||||
|
fn none(cx: &'c Ctxt, name: &'static str) -> Self {
|
||||||
Attr {
|
Attr {
|
||||||
cx: cx,
|
cx: cx,
|
||||||
name: name,
|
name: name,
|
||||||
@@ -33,47 +24,40 @@ impl<'a, 'b, T> Attr<'a, 'b, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set(&mut self, span: Span, t: T) {
|
fn set(&mut self, value: T) {
|
||||||
if let Some(Spanned { span: prev_span, .. }) = self.value {
|
if self.value.is_some() {
|
||||||
let mut err = self.cx.struct_span_err(
|
self.cx.error(format!("duplicate serde attribute `{}`", self.name));
|
||||||
span,
|
|
||||||
&format!("duplicate serde attribute `{}`", self.name));
|
|
||||||
err.span_help(prev_span, "previously set here");
|
|
||||||
err.emit();
|
|
||||||
} else {
|
} else {
|
||||||
self.value = Some(respan(span, t));
|
self.value = Some(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_opt(&mut self, v: Option<Spanned<T>>) {
|
fn set_opt(&mut self, value: Option<T>) {
|
||||||
if let Some(v) = v {
|
if let Some(value) = value {
|
||||||
self.set(v.span, v.node);
|
self.set(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_if_none(&mut self, span: Span, t: T) {
|
fn set_if_none(&mut self, value: T) {
|
||||||
if self.value.is_none() {
|
if self.value.is_none() {
|
||||||
self.value = Some(respan(span, t));
|
self.value = Some(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self) -> Option<T> {
|
fn get(self) -> Option<T> {
|
||||||
self.value.map(|spanned| spanned.node)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_spanned(self) -> Option<Spanned<T>> {
|
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BoolAttr<'a, 'b: 'a>(Attr<'a, 'b, ()>);
|
struct BoolAttr<'c>(Attr<'c, ()>);
|
||||||
impl<'a, 'b> BoolAttr<'a, 'b> {
|
|
||||||
fn none(cx: &'a ExtCtxt<'b>, name: &'static str) -> Self {
|
impl<'c> BoolAttr<'c> {
|
||||||
|
fn none(cx: &'c Ctxt, name: &'static str) -> Self {
|
||||||
BoolAttr(Attr::none(cx, name))
|
BoolAttr(Attr::none(cx, name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_true(&mut self, span: Span) {
|
fn set_true(&mut self) {
|
||||||
self.0.set(span, ());
|
self.0.set(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self) -> bool {
|
fn get(&self) -> bool {
|
||||||
@@ -83,18 +67,18 @@ impl<'a, 'b> BoolAttr<'a, 'b> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Name {
|
pub struct Name {
|
||||||
serialize: InternedString,
|
serialize: String,
|
||||||
deserialize: InternedString,
|
deserialize: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Name {
|
impl Name {
|
||||||
/// Return the container name for the container when serializing.
|
/// Return the container name for the container when serializing.
|
||||||
pub fn serialize_name(&self) -> InternedString {
|
pub fn serialize_name(&self) -> String {
|
||||||
self.serialize.clone()
|
self.serialize.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the container name for the container when deserializing.
|
/// Return the container name for the container when deserializing.
|
||||||
pub fn deserialize_name(&self) -> InternedString {
|
pub fn deserialize_name(&self) -> String {
|
||||||
self.deserialize.clone()
|
self.deserialize.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,35 +88,32 @@ impl Name {
|
|||||||
pub struct Item {
|
pub struct Item {
|
||||||
name: Name,
|
name: Name,
|
||||||
deny_unknown_fields: bool,
|
deny_unknown_fields: bool,
|
||||||
ser_bound: Option<Vec<ast::WherePredicate>>,
|
ser_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
de_bound: Option<Vec<ast::WherePredicate>>,
|
de_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
/// Extract out the `#[serde(...)]` attributes from an item.
|
/// Extract out the `#[serde(...)]` attributes from an item.
|
||||||
pub fn from_ast(cx: &ExtCtxt, item: &ast::Item) -> Self {
|
pub fn from_ast(cx: &Ctxt, item: &syn::MacroInput) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_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 deny_unknown_fields = BoolAttr::none(cx, "deny_unknown_fields");
|
||||||
let mut ser_bound = Attr::none(cx, "bound");
|
let mut ser_bound = Attr::none(cx, "bound");
|
||||||
let mut de_bound = Attr::none(cx, "bound");
|
let mut de_bound = Attr::none(cx, "bound");
|
||||||
|
|
||||||
let ident = item.ident.name.as_str();
|
for meta_items in item.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
|
|
||||||
for meta_items in item.attrs().iter().filter_map(get_serde_meta_items) {
|
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
let span = meta_item.span;
|
match meta_item {
|
||||||
match meta_item.node {
|
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "rename" => {
|
||||||
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
syn::MetaItem::List(ref name, ref meta_items) if name == "rename" => {
|
||||||
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
@@ -140,20 +121,20 @@ impl Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deny_unknown_fields)]`
|
// Parse `#[serde(deny_unknown_fields)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"deny_unknown_fields" => {
|
syn::MetaItem::Word(ref name) if name == "deny_unknown_fields" => {
|
||||||
deny_unknown_fields.set_true(span);
|
deny_unknown_fields.set_true();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "bound" => {
|
||||||
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
|
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_bound.set(span, where_predicates.clone());
|
ser_bound.set(where_predicates.clone());
|
||||||
de_bound.set(span, where_predicates);
|
de_bound.set(where_predicates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
syn::MetaItem::List(ref name, ref meta_items) if name == "bound" => {
|
||||||
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
||||||
ser_bound.set_opt(ser);
|
ser_bound.set_opt(ser);
|
||||||
de_bound.set_opt(de);
|
de_bound.set_opt(de);
|
||||||
@@ -161,10 +142,8 @@ impl Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.error(format!("unknown serde container attribute `{}`",
|
||||||
meta_item.span,
|
meta_item.name()));
|
||||||
&format!("unknown serde container attribute `{}`",
|
|
||||||
meta_item_to_string(&meta_item)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,8 +151,8 @@ impl Item {
|
|||||||
|
|
||||||
Item {
|
Item {
|
||||||
name: Name {
|
name: Name {
|
||||||
serialize: ser_name.get().unwrap_or(ident.clone()),
|
serialize: ser_name.get().unwrap_or_else(|| item.ident.to_string()),
|
||||||
deserialize: de_name.get().unwrap_or(ident),
|
deserialize: de_name.get().unwrap_or_else(|| item.ident.to_string()),
|
||||||
},
|
},
|
||||||
deny_unknown_fields: deny_unknown_fields.get(),
|
deny_unknown_fields: deny_unknown_fields.get(),
|
||||||
ser_bound: ser_bound.get(),
|
ser_bound: ser_bound.get(),
|
||||||
@@ -189,11 +168,11 @@ impl Item {
|
|||||||
self.deny_unknown_fields
|
self.deny_unknown_fields
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
|
pub fn ser_bound(&self) -> Option<&[syn::WherePredicate]> {
|
||||||
self.ser_bound.as_ref().map(|vec| &vec[..])
|
self.ser_bound.as_ref().map(|vec| &vec[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
|
pub fn de_bound(&self) -> Option<&[syn::WherePredicate]> {
|
||||||
self.de_bound.as_ref().map(|vec| &vec[..])
|
self.de_bound.as_ref().map(|vec| &vec[..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,26 +184,23 @@ pub struct Variant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
pub fn from_ast(cx: &ExtCtxt, variant: &ast::Variant) -> Self {
|
pub fn from_ast(cx: &Ctxt, variant: &syn::Variant) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_name = Attr::none(cx, "rename");
|
let mut de_name = Attr::none(cx, "rename");
|
||||||
|
|
||||||
let ident = variant.node.name.name.as_str();
|
for meta_items in variant.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
|
|
||||||
for meta_items in variant.node.attrs.iter().filter_map(get_serde_meta_items) {
|
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
let span = meta_item.span;
|
match meta_item {
|
||||||
match meta_item.node {
|
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "rename" => {
|
||||||
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
syn::MetaItem::List(ref name, ref meta_items) if name == "rename" => {
|
||||||
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
@@ -232,10 +208,8 @@ impl Variant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.error(format!("unknown serde variant attribute `{}`",
|
||||||
meta_item.span,
|
meta_item.name()));
|
||||||
&format!("unknown serde variant attribute `{}`",
|
|
||||||
meta_item_to_string(&meta_item)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,8 +217,8 @@ impl Variant {
|
|||||||
|
|
||||||
Variant {
|
Variant {
|
||||||
name: Name {
|
name: Name {
|
||||||
serialize: ser_name.get().unwrap_or(ident.clone()),
|
serialize: ser_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
||||||
deserialize: de_name.get().unwrap_or(ident),
|
deserialize: de_name.get().unwrap_or_else(|| variant.ident.to_string()),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,12 +234,12 @@ pub struct Field {
|
|||||||
name: Name,
|
name: Name,
|
||||||
skip_serializing: bool,
|
skip_serializing: bool,
|
||||||
skip_deserializing: bool,
|
skip_deserializing: bool,
|
||||||
skip_serializing_if: Option<ast::Path>,
|
skip_serializing_if: Option<syn::Path>,
|
||||||
default: FieldDefault,
|
default: FieldDefault,
|
||||||
serialize_with: Option<ast::Path>,
|
serialize_with: Option<syn::Path>,
|
||||||
deserialize_with: Option<ast::Path>,
|
deserialize_with: Option<syn::Path>,
|
||||||
ser_bound: Option<Vec<ast::WherePredicate>>,
|
ser_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
de_bound: Option<Vec<ast::WherePredicate>>,
|
de_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the default to use for a field when deserializing.
|
/// Represents the default to use for a field when deserializing.
|
||||||
@@ -276,14 +250,14 @@ pub enum FieldDefault {
|
|||||||
/// The default is given by `std::default::Default::default()`.
|
/// The default is given by `std::default::Default::default()`.
|
||||||
Default,
|
Default,
|
||||||
/// The default is given by this function.
|
/// The default is given by this function.
|
||||||
Path(ast::Path),
|
Path(syn::Path),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
||||||
pub fn from_ast(cx: &ExtCtxt,
|
pub fn from_ast(cx: &Ctxt,
|
||||||
index: usize,
|
index: usize,
|
||||||
field: &ast::StructField) -> Self {
|
field: &syn::Field) -> Self {
|
||||||
let mut ser_name = Attr::none(cx, "rename");
|
let mut ser_name = Attr::none(cx, "rename");
|
||||||
let mut de_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_serializing = BoolAttr::none(cx, "skip_serializing");
|
||||||
@@ -296,24 +270,23 @@ impl Field {
|
|||||||
let mut de_bound = Attr::none(cx, "bound");
|
let mut de_bound = Attr::none(cx, "bound");
|
||||||
|
|
||||||
let ident = match field.ident {
|
let ident = match field.ident {
|
||||||
Some(ident) => ident.name.as_str(),
|
Some(ref ident) => ident.to_string(),
|
||||||
None => token::intern_and_get_ident(&index.to_string()),
|
None => index.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
let span = meta_item.span;
|
match meta_item {
|
||||||
match meta_item.node {
|
|
||||||
// Parse `#[serde(rename="foo")]`
|
// Parse `#[serde(rename="foo")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "rename" => {
|
||||||
if let Ok(s) = get_str_from_lit(cx, name, lit) {
|
if let Ok(s) = get_string_from_lit(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_name.set(span, s.clone());
|
ser_name.set(s.clone());
|
||||||
de_name.set(span, s);
|
de_name.set(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
// Parse `#[serde(rename(serialize="foo", deserialize="bar"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"rename" => {
|
syn::MetaItem::List(ref name, ref meta_items) if name == "rename" => {
|
||||||
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
if let Ok((ser, de)) = get_renames(cx, meta_items) {
|
||||||
ser_name.set_opt(ser);
|
ser_name.set_opt(ser);
|
||||||
de_name.set_opt(de);
|
de_name.set_opt(de);
|
||||||
@@ -321,58 +294,58 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(default)]`
|
// Parse `#[serde(default)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
syn::MetaItem::Word(ref name) if name == "default" => {
|
||||||
default.set(span, FieldDefault::Default);
|
default.set(FieldDefault::Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(default="...")]`
|
// Parse `#[serde(default="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"default" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "default" => {
|
||||||
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||||
default.set(span, FieldDefault::Path(path));
|
default.set(FieldDefault::Path(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(skip_serializing)]`
|
// Parse `#[serde(skip_serializing)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
syn::MetaItem::Word(ref name) if name == "skip_serializing" => {
|
||||||
skip_serializing.set_true(span);
|
skip_serializing.set_true();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(skip_deserializing)]`
|
// Parse `#[serde(skip_deserializing)]`
|
||||||
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
syn::MetaItem::Word(ref name) if name == "skip_deserializing" => {
|
||||||
skip_deserializing.set_true(span);
|
skip_deserializing.set_true();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(skip_serializing_if="...")]`
|
// Parse `#[serde(skip_serializing_if="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "skip_serializing_if" => {
|
||||||
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||||
skip_serializing_if.set(span, path);
|
skip_serializing_if.set(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(serialize_with="...")]`
|
// Parse `#[serde(serialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "serialize_with" => {
|
||||||
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||||
serialize_with.set(span, path);
|
serialize_with.set(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deserialize_with="...")]`
|
// Parse `#[serde(deserialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "deserialize_with" => {
|
||||||
if let Ok(path) = parse_lit_into_path(cx, name, lit) {
|
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||||
deserialize_with.set(span, path);
|
deserialize_with.set(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"bound" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "bound" => {
|
||||||
if let Ok(where_predicates) = parse_lit_into_where(cx, name, lit) {
|
if let Ok(where_predicates) = parse_lit_into_where(cx, name.as_ref(), name.as_ref(), lit) {
|
||||||
ser_bound.set(span, where_predicates.clone());
|
ser_bound.set(where_predicates.clone());
|
||||||
de_bound.set(span, where_predicates);
|
de_bound.set(where_predicates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
// Parse `#[serde(bound(serialize="D: Serialize", deserialize="D: Deserialize"))]`
|
||||||
ast::MetaItemKind::List(ref name, ref meta_items) if name == &"bound" => {
|
syn::MetaItem::List(ref name, ref meta_items) if name == "bound" => {
|
||||||
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
if let Ok((ser, de)) = get_where_predicates(cx, meta_items) {
|
||||||
ser_bound.set_opt(ser);
|
ser_bound.set_opt(ser);
|
||||||
de_bound.set_opt(de);
|
de_bound.set_opt(de);
|
||||||
@@ -380,10 +353,8 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.error(format!("unknown serde field attribute `{}`",
|
||||||
meta_item.span,
|
meta_item.name()));
|
||||||
&format!("unknown serde field attribute `{}`",
|
|
||||||
meta_item_to_string(&meta_item)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,8 +362,8 @@ impl Field {
|
|||||||
|
|
||||||
// Is skip_deserializing, initialize the field to Default::default()
|
// Is skip_deserializing, initialize the field to Default::default()
|
||||||
// unless a different default is specified by `#[serde(default="...")]`
|
// unless a different default is specified by `#[serde(default="...")]`
|
||||||
if let Some(Spanned { span, .. }) = skip_deserializing.0.value {
|
if skip_deserializing.0.value.is_some() {
|
||||||
default.set_if_none(span, FieldDefault::Default);
|
default.set_if_none(FieldDefault::Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field {
|
Field {
|
||||||
@@ -423,7 +394,7 @@ impl Field {
|
|||||||
self.skip_deserializing
|
self.skip_deserializing
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_serializing_if(&self) -> Option<&ast::Path> {
|
pub fn skip_serializing_if(&self) -> Option<&syn::Path> {
|
||||||
self.skip_serializing_if.as_ref()
|
self.skip_serializing_if.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,230 +402,106 @@ impl Field {
|
|||||||
&self.default
|
&self.default
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_with(&self) -> Option<&ast::Path> {
|
pub fn serialize_with(&self) -> Option<&syn::Path> {
|
||||||
self.serialize_with.as_ref()
|
self.serialize_with.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deserialize_with(&self) -> Option<&ast::Path> {
|
pub fn deserialize_with(&self) -> Option<&syn::Path> {
|
||||||
self.deserialize_with.as_ref()
|
self.deserialize_with.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ser_bound(&self) -> Option<&[ast::WherePredicate]> {
|
pub fn ser_bound(&self) -> Option<&[syn::WherePredicate]> {
|
||||||
self.ser_bound.as_ref().map(|vec| &vec[..])
|
self.ser_bound.as_ref().map(|vec| &vec[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn de_bound(&self) -> Option<&[ast::WherePredicate]> {
|
pub fn de_bound(&self) -> Option<&[syn::WherePredicate]> {
|
||||||
self.de_bound.as_ref().map(|vec| &vec[..])
|
self.de_bound.as_ref().map(|vec| &vec[..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type SerAndDe<T> = (Option<Spanned<T>>, Option<Spanned<T>>);
|
type SerAndDe<T> = (Option<T>, Option<T>);
|
||||||
|
|
||||||
fn get_ser_and_de<T, F>(
|
fn get_ser_and_de<T, F>(
|
||||||
cx: &ExtCtxt,
|
cx: &Ctxt,
|
||||||
attribute: &'static str,
|
attr_name: &'static str,
|
||||||
items: &[ast::NestedMetaItem],
|
items: &[syn::MetaItem],
|
||||||
f: F
|
f: F
|
||||||
) -> Result<SerAndDe<T>, ()>
|
) -> Result<SerAndDe<T>, ()>
|
||||||
where F: Fn(&ExtCtxt, &str, &ast::Lit) -> Result<T, ()>,
|
where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result<T, ()>,
|
||||||
{
|
{
|
||||||
let mut ser_item = Attr::none(cx, attribute);
|
let mut ser_item = Attr::none(cx, attr_name);
|
||||||
let mut de_item = Attr::none(cx, attribute);
|
let mut de_item = Attr::none(cx, attr_name);
|
||||||
|
|
||||||
for item in items {
|
for item in items {
|
||||||
match item.node {
|
match *item {
|
||||||
ast::NestedMetaItemKind::MetaItem(ref meta_item) => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "serialize" => {
|
||||||
match meta_item.node {
|
if let Ok(v) = f(cx, attr_name, name.as_ref(), lit) {
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize" => {
|
ser_item.set(v);
|
||||||
if let Ok(v) = f(cx, name, lit) {
|
}
|
||||||
ser_item.set(item.span, v);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize" => {
|
syn::MetaItem::NameValue(ref name, ref lit) if name == "deserialize" => {
|
||||||
if let Ok(v) = f(cx, name, lit) {
|
if let Ok(v) = f(cx, attr_name, name.as_ref(), lit) {
|
||||||
de_item.set(item.span, v);
|
de_item.set(v);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
cx.span_err(
|
|
||||||
item.span,
|
|
||||||
&format!("unknown {} attribute `{}`",
|
|
||||||
attribute,
|
|
||||||
meta_item_to_string(meta_item)));
|
|
||||||
|
|
||||||
return Err(());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(
|
cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., deserialize = ...)`",
|
||||||
item.span,
|
attr_name));
|
||||||
&format!("unknown {} attribute `{}`",
|
|
||||||
attribute,
|
|
||||||
meta_list_item_to_string(item)));
|
|
||||||
|
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((ser_item.get_spanned(), de_item.get_spanned()))
|
Ok((ser_item.get(), de_item.get()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_renames(
|
fn get_renames(
|
||||||
cx: &ExtCtxt,
|
cx: &Ctxt,
|
||||||
items: &[ast::NestedMetaItem],
|
items: &[syn::MetaItem],
|
||||||
) -> Result<SerAndDe<InternedString>, ()> {
|
) -> Result<SerAndDe<String>, ()> {
|
||||||
get_ser_and_de(cx, "rename", items, get_str_from_lit)
|
get_ser_and_de(cx, "rename", items, get_string_from_lit)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_where_predicates(
|
fn get_where_predicates(
|
||||||
cx: &ExtCtxt,
|
cx: &Ctxt,
|
||||||
items: &[ast::NestedMetaItem],
|
items: &[syn::MetaItem],
|
||||||
) -> Result<SerAndDe<Vec<ast::WherePredicate>>, ()> {
|
) -> Result<SerAndDe<Vec<syn::WherePredicate>>, ()> {
|
||||||
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
get_ser_and_de(cx, "bound", items, parse_lit_into_where)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_serde_meta_items(attr: &ast::Attribute) -> Option<Vec<ast::MetaItem>> {
|
pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::MetaItem>> {
|
||||||
match attr.node.value.node {
|
match attr.value {
|
||||||
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
syn::MetaItem::List(ref name, ref items) if name == "serde" => {
|
||||||
attr::mark_used(attr);
|
Some(items.iter().cloned().collect())
|
||||||
Some(items.iter().filter_map(|item| {
|
|
||||||
match item.node {
|
|
||||||
ast::NestedMetaItemKind::MetaItem(ref meta_item) => {
|
|
||||||
Some((*meta_item.clone()).clone())
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}).collect())
|
|
||||||
}
|
}
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This syntax folder rewrites tokens to say their spans are coming from a macro context.
|
fn get_string_from_lit(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<String, ()> {
|
||||||
struct Respanner<'a, 'b: 'a> {
|
if let syn::Lit::Str(ref s, _) = *lit {
|
||||||
cx: &'a ExtCtxt<'b>,
|
Ok(s.clone())
|
||||||
}
|
} else {
|
||||||
|
cx.error(format!("expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||||
impl<'a, 'b> Folder for Respanner<'a, 'b> {
|
attr_name, meta_item_name));
|
||||||
fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree {
|
Err(())
|
||||||
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(tokenstream::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(tokenstream::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, ()> {
|
fn parse_lit_into_path(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result<syn::Path, ()> {
|
||||||
match lit.node {
|
let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit));
|
||||||
ast::LitKind::Str(ref s, _) => Ok(s.clone()),
|
syn::parse_path(&string).map_err(|err| cx.error(err))
|
||||||
_ => {
|
|
||||||
cx.span_err(
|
|
||||||
lit.span,
|
|
||||||
&format!("serde annotation `{}` must be a string, not `{}`",
|
|
||||||
name,
|
|
||||||
lit_to_string(lit)));
|
|
||||||
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we just parse a string literal from an attibute, any syntax errors in the
|
fn parse_lit_into_where(cx: &Ctxt, attr_name: &str, meta_item_name: &str, lit: &syn::Lit) -> Result<Vec<syn::WherePredicate>, ()> {
|
||||||
// source will only have spans that point inside the string and not back to the
|
let string = try!(get_string_from_lit(cx, attr_name, meta_item_name, lit));
|
||||||
// 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, ()>
|
|
||||||
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(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::Path, ()> {
|
|
||||||
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>, ()> {
|
|
||||||
let string = try!(get_str_from_lit(cx, name, lit));
|
|
||||||
if string.is_empty() {
|
if string.is_empty() {
|
||||||
return Ok(Vec::new());
|
return Ok(Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
let where_string = format!("where {}", string);
|
let where_string = format!("where {}", string);
|
||||||
|
|
||||||
parse_string_via_tts(cx, name, where_string, |parser| {
|
syn::parse_where_clause(&where_string).map(|wh| wh.predicates).map_err(|err| cx.error(err))
|
||||||
let where_clause = try!(parser.parse_where_clause());
|
|
||||||
Ok(where_clause.predicates)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Ctxt {
|
||||||
|
errors: RefCell<Option<Vec<String>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ctxt {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Ctxt {
|
||||||
|
errors: RefCell::new(Some(Vec::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error<T: Display>(&self, msg: T) {
|
||||||
|
self.errors.borrow_mut().as_mut().unwrap().push(msg.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check(self) -> Result<(), String> {
|
||||||
|
let mut errors = self.errors.borrow_mut().take().unwrap();
|
||||||
|
match errors.len() {
|
||||||
|
0 => Ok(()),
|
||||||
|
1 => Err(errors.pop().unwrap()),
|
||||||
|
n => {
|
||||||
|
let mut msg = format!("{} errors:", n);
|
||||||
|
for err in errors {
|
||||||
|
msg.push_str("\n\t# ");
|
||||||
|
msg.push_str(&err);
|
||||||
|
}
|
||||||
|
Err(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Ctxt {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.errors.borrow().is_some() {
|
||||||
|
panic!("forgot to check for errors");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
use std::error;
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Error {
|
|
||||||
UnexpectedItemKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "expected a struct or enum")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl error::Error for Error {
|
|
||||||
fn description(&self) -> &str {
|
|
||||||
"expected a struct or enum"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,10 @@
|
|||||||
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
||||||
#![cfg_attr(feature = "clippy", feature(plugin))]
|
#![cfg_attr(feature = "clippy", feature(plugin))]
|
||||||
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
|
||||||
|
|
||||||
#[cfg(feature = "with-syntex")]
|
extern crate syn;
|
||||||
#[macro_use]
|
|
||||||
extern crate syntex_syntax as syntax;
|
|
||||||
#[cfg(feature = "with-syntex")]
|
|
||||||
extern crate syntex_errors as errors;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
|
||||||
#[macro_use]
|
|
||||||
extern crate syntax;
|
|
||||||
#[cfg(not(feature = "with-syntex"))]
|
|
||||||
extern crate rustc_errors as errors;
|
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod attr;
|
pub mod attr;
|
||||||
|
|
||||||
mod error;
|
mod ctxt;
|
||||||
pub use error::Error;
|
pub use ctxt::Ctxt;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "0.8.9"
|
version = "0.8.10"
|
||||||
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 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
@@ -14,10 +14,13 @@ include = ["Cargo.toml", "src/**/*.rs"]
|
|||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
rustc-macro = true
|
rustc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies.serde_codegen]
|
||||||
serde_codegen = { version = "=0.8.9", path = "../serde_codegen" }
|
version = "=0.8.10"
|
||||||
|
path = "../serde_codegen"
|
||||||
|
default-features = false
|
||||||
|
features = ["with-syn"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
fnv = "1.0"
|
fnv = "1.0"
|
||||||
serde = { version = "0.8.9", path = "../serde" }
|
serde = { version = "0.8.10", path = "../serde" }
|
||||||
serde_test = { version = "0.8.9", path = "../serde_test" }
|
serde_test = { version = "0.8.10", path = "../serde_test" }
|
||||||
|
|||||||
@@ -9,13 +9,17 @@ use rustc_macro::TokenStream;
|
|||||||
#[rustc_macro_derive(Serialize)]
|
#[rustc_macro_derive(Serialize)]
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let item = format!("#[derive(Serialize)]\n{}", input);
|
let item = format!("#[derive(Serialize)]\n{}", input);
|
||||||
let expanded = serde_codegen::expand_str(&item).unwrap();
|
match serde_codegen::expand_single_item(&item) {
|
||||||
expanded.parse().unwrap()
|
Ok(expanded) => expanded.parse().unwrap(),
|
||||||
|
Err(msg) => panic!(msg),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustc_macro_derive(Deserialize)]
|
#[rustc_macro_derive(Deserialize)]
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
||||||
let item = format!("#[derive(Deserialize)]\n{}", input);
|
let item = format!("#[derive(Deserialize)]\n{}", input);
|
||||||
let expanded = serde_codegen::expand_str(&item).unwrap();
|
match serde_codegen::expand_single_item(&item) {
|
||||||
expanded.parse().unwrap()
|
Ok(expanded) => expanded.parse().unwrap(),
|
||||||
|
Err(msg) => panic!(msg),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
#![feature(custom_attribute, custom_derive, plugin)]
|
#![feature(custom_attribute, custom_derive, plugin)]
|
||||||
#![plugin(serde_macros)]
|
#![plugin(serde_macros)]
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)] //~ ERROR: 6 errors:
|
||||||
struct S {
|
struct S {
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
#[serde(rename(serialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(serialize="y"))] // ERROR: duplicate serde attribute `rename`
|
||||||
a: (),
|
a: (),
|
||||||
|
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
#[serde(rename="y")] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename="y")] // ERROR: duplicate serde attribute `rename`
|
||||||
b: (),
|
b: (),
|
||||||
|
|
||||||
#[serde(rename(serialize="x"))]
|
#[serde(rename(serialize="x"))]
|
||||||
@@ -16,16 +16,16 @@ struct S {
|
|||||||
c: (),
|
c: (),
|
||||||
|
|
||||||
#[serde(rename="x")]
|
#[serde(rename="x")]
|
||||||
#[serde(rename(deserialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(deserialize="y"))] // ERROR: duplicate serde attribute `rename`
|
||||||
d: (),
|
d: (),
|
||||||
|
|
||||||
#[serde(rename(serialize="x", serialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(serialize="x", serialize="y"))] // ERROR: duplicate serde attribute `rename`
|
||||||
e: (),
|
e: (),
|
||||||
|
|
||||||
#[serde(rename="x", serialize="y")] //~ ERROR: unknown serde field attribute `serialize = "y"`
|
#[serde(rename="x", serialize="y")] // ERROR: unknown serde field attribute `serialize`
|
||||||
f: (),
|
f: (),
|
||||||
|
|
||||||
#[serde(rename(serialize="x"), rename(serialize="y"))] //~ ERROR: duplicate serde attribute `rename`
|
#[serde(rename(serialize="x"), rename(serialize="y"))] // ERROR: duplicate serde attribute `rename`
|
||||||
g: (),
|
g: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,27 +3,27 @@
|
|||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)] //~ unknown serde container attribute `abc`
|
||||||
#[serde(abc="xyz")] //~ unknown serde container attribute `abc = "xyz"`
|
#[serde(abc="xyz")]
|
||||||
struct Foo {
|
struct A {
|
||||||
x: u32,
|
x: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)] //~ unknown serde container attribute `abc`
|
||||||
#[serde(abc="xyz")] //~ unknown serde container attribute `abc = "xyz"`
|
#[serde(abc="xyz")]
|
||||||
struct Foo {
|
struct B {
|
||||||
x: u32,
|
x: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)] //~ unknown serde field attribute `abc`
|
||||||
struct Foo {
|
struct C {
|
||||||
#[serde(abc="xyz")] //~ unknown serde field attribute `abc = "xyz"`
|
#[serde(abc="xyz")]
|
||||||
x: u32,
|
x: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)] //~ unknown serde field attribute `abc`
|
||||||
struct Foo {
|
struct D {
|
||||||
#[serde(abc="xyz")] //~ unknown serde field attribute `abc = "xyz"`
|
#[serde(abc="xyz")]
|
||||||
x: u32,
|
x: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#![feature(custom_attribute, custom_derive, plugin)]
|
#![feature(custom_attribute, custom_derive, plugin)]
|
||||||
#![plugin(serde_macros)]
|
#![plugin(serde_macros)]
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)] //~ ERROR: Serde does not support deserializing fields of type &str
|
||||||
struct Test<'a> {
|
struct Test<'a> {
|
||||||
s: &'a str, //~ ERROR: Serde does not support deserializing fields of type &str
|
s: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_test"
|
name = "serde_test"
|
||||||
version = "0.8.9"
|
version = "0.8.10"
|
||||||
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 = "Token De/Serializer for testing De/Serialize implementations"
|
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||||
@@ -12,4 +12,4 @@ keywords = ["serde", "serialization"]
|
|||||||
include = ["Cargo.toml", "src/**/*.rs"]
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "0.8.9", path = "../serde" }
|
serde = { version = "0.8.10", path = "../serde" }
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_testing"
|
name = "serde_testing"
|
||||||
version = "0.8.9"
|
version = "0.8.10"
|
||||||
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"
|
||||||
|
|||||||
@@ -600,3 +600,28 @@ fn test_default_ty_param() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_enum_state_field() {
|
||||||
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
enum SomeEnum {
|
||||||
|
Key { key: char, state: bool },
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_tokens(
|
||||||
|
&SomeEnum::Key { key: 'a', state: true },
|
||||||
|
&[
|
||||||
|
Token::EnumMapStart("SomeEnum", "Key", 2),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("key"),
|
||||||
|
Token::Char('a'),
|
||||||
|
|
||||||
|
Token::EnumMapSep,
|
||||||
|
Token::Str("state"),
|
||||||
|
Token::Bool(true),
|
||||||
|
|
||||||
|
Token::EnumMapEnd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user