mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 01:18:02 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8378267b9b | |||
| 5716e8c508 | |||
| 0e9d45da60 | |||
| 6e7a75c859 | |||
| 0ff91e4451 | |||
| 305fab7c16 | |||
| a959073a81 | |||
| 2f0fc6e6f1 | |||
| 7bd87feb62 | |||
| bff2301ac3 | |||
| fd3c15fb68 | |||
| 808b06940e | |||
| 8cce6ecf15 | |||
| ef97f87b96 | |||
| 93a7568ff6 | |||
| 0439bb9d02 | |||
| 886670134a | |||
| 65e36647f5 | |||
| 51fdb0e4ef | |||
| a4de662adb | |||
| ff02b0c741 | |||
| 6b3958d5fc | |||
| 84b289dd7b | |||
| dbba537b66 | |||
| bc2324fba7 | |||
| 9082b75e75 | |||
| 4b9f751d74 | |||
| 451700d3d2 | |||
| 1c5d83889c | |||
| f659fa8919 | |||
| 87393b61bb | |||
| d9b6feef19 | |||
| fb18a5cc56 | |||
| eaff73a541 | |||
| 19ec8bbdb9 |
+18
-15
@@ -3,30 +3,33 @@ rust:
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
- 1.5.0
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libcurl4-openssl-dev
|
||||
- libelf-dev
|
||||
- libdw-dev
|
||||
- binutils-dev
|
||||
before_script:
|
||||
- |
|
||||
pip install 'travis-cargo<0.2' --user &&
|
||||
export PATH=$HOME/.local/bin:$PATH
|
||||
- pip install 'travis-cargo<0.2' --user
|
||||
- export PATH=$HOME/.local/bin:$PATH
|
||||
script:
|
||||
- |
|
||||
(cd serde && travis-cargo build) &&
|
||||
(cd serde && travis-cargo test) &&
|
||||
(cd serde && travis-cargo --only nightly test -- --features nightly-testing) &&
|
||||
(cd serde_tests && travis-cargo test) &&
|
||||
(cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing) &&
|
||||
(cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing) &&
|
||||
(cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing) &&
|
||||
(cd serde && travis-cargo --only stable doc) &&
|
||||
(cd serde_codegen && travis-cargo --only stable doc)
|
||||
- (cd serde && travis-cargo build)
|
||||
- (cd serde && travis-cargo test)
|
||||
- (cd serde && travis-cargo --only nightly test -- --features nightly-testing)
|
||||
- (cd serde_tests && travis-cargo test)
|
||||
- (cd serde_tests && travis-cargo --only nightly test -- --features nightly-testing)
|
||||
- (cd serde_macros && travis-cargo --only nightly test -- --features nightly-testing)
|
||||
- (cd serde_macros && travis-cargo --only nightly bench -- --features nightly-testing)
|
||||
- (cd serde && travis-cargo --only stable doc)
|
||||
- (cd serde_codegen && travis-cargo --only stable doc)
|
||||
- (cd serde_macros && travis-cargo --only nightly doc)
|
||||
after_success:
|
||||
- "(cd serde && travis-cargo --only stable doc-upload)"
|
||||
- "(cd serde_tests && travis-cargo coveralls --no-sudo)"
|
||||
- (cd serde && travis-cargo --only stable doc-upload)
|
||||
#- (cd serde_codegen && travis-cargo --only stable doc-upload)
|
||||
#- (cd serde_macros && travis-cargo --only nightly doc-upload)
|
||||
- (cd serde_tests && travis-cargo coveralls --no-sudo)
|
||||
env:
|
||||
global:
|
||||
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
|
||||
|
||||
@@ -46,7 +46,6 @@ serde_macros = "*"
|
||||
#![feature(custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@@ -714,7 +713,8 @@ Field Annotations:
|
||||
| `#[serde(default)]` | If the value is not specified, use the `Default::default()` |
|
||||
| `#[serde(default="$path")]` | Call the path to a function `fn() -> T` to build the value |
|
||||
| `#[serde(skip_serializing)]` | Do not serialize this value |
|
||||
| `#[serde(skip_serializing_if="$path")]` | Do not serialize this value if this function `fn(&T) -> bool` returns `false` |
|
||||
| `#[serde(skip_deserializing)]` | Always use `Default::default()` or `#[serde(default="$path")]` instead of deserializing this value |
|
||||
| `#[serde(skip_serializing_if="$path")]` | Do not serialize this value if this function `fn(&T) -> bool` returns `true` |
|
||||
| `#[serde(serialize_with="$path")]` | Call a function `fn<T, S>(&T, &mut S) -> Result<(), S::Error> where S: Serializer` to serialize this value |
|
||||
| `#[serde(deserialize_with="$path")]` | Call a function `fn<T, D>(&mut D) -> Result<T, D::Error> where D: Deserializer` to deserialize this value |
|
||||
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.7.0"
|
||||
version = "0.7.4"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://serde-rs.github.io/serde/serde/serde/index.html"
|
||||
documentation = "https://serde-rs.github.io/serde/serde/"
|
||||
readme = "../README.md"
|
||||
keywords = ["serde", "serialization"]
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
|
||||
// Extracted from https://github.com/rust-num/num.
|
||||
|
||||
// Rust 1.5 is unhappy that this private module is undocumented.
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use std::{usize, u8, u16, u32, u64};
|
||||
use std::{isize, i8, i16, i32, i64};
|
||||
use std::{f32, f64};
|
||||
|
||||
@@ -881,6 +881,15 @@ impl<T: Deserialize> Deserialize for Box<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Box<[T]> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Box<[T]>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let v: Vec<T> = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(v.into_boxed_slice())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Arc<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
[package]
|
||||
name = "serde_codegen"
|
||||
version = "0.7.2"
|
||||
version = "0.7.4"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html"
|
||||
build = "build.rs"
|
||||
documentation = "https://github.com/serde-rs/serde"
|
||||
keywords = ["serde", "serialization"]
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["with-syntex"]
|
||||
@@ -16,13 +16,13 @@ nightly-testing = ["clippy"]
|
||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
||||
|
||||
[build-dependencies]
|
||||
quasi_codegen = { version = "^0.9.0", optional = true }
|
||||
quasi_codegen = { version = "^0.10.0", optional = true }
|
||||
syntex = { version = "^0.31.0", optional = true }
|
||||
|
||||
[dependencies]
|
||||
aster = { version = "^0.15.0", default-features = false }
|
||||
aster = { version = "^0.16.0", default-features = false }
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
quasi = { version = "^0.9.0", default-features = false }
|
||||
quasi_macros = { version = "^0.9.0", optional = true }
|
||||
syntex = { version = "^0.31.0", optional = true }
|
||||
syntex_syntax = { version = "^0.31.0", optional = true }
|
||||
quasi = { version = "^0.10.0", default-features = false }
|
||||
quasi_macros = { version = "^0.10.0", optional = true }
|
||||
syntex = { version = "^0.32.0", optional = true }
|
||||
syntex_syntax = { version = "^0.32.0", optional = true }
|
||||
|
||||
+39
-25
@@ -4,7 +4,7 @@ use syntax::attr;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::fold::Folder;
|
||||
use syntax::parse::parser::PathParsingMode;
|
||||
use syntax::parse::parser::PathStyle;
|
||||
use syntax::parse::token::{self, InternedString};
|
||||
use syntax::parse;
|
||||
use syntax::print::pprust::{lit_to_string, meta_item_to_string};
|
||||
@@ -30,11 +30,6 @@ impl Name {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the string expression of the field ident.
|
||||
pub fn ident_expr(&self) -> P<ast::Expr> {
|
||||
AstBuilder::new().expr().str(self.ident)
|
||||
}
|
||||
|
||||
/// Return the container name for the container when serializing.
|
||||
pub fn serialize_name(&self) -> InternedString {
|
||||
match self.serialize_name {
|
||||
@@ -181,10 +176,11 @@ impl VariantAttrs {
|
||||
pub struct FieldAttrs {
|
||||
name: Name,
|
||||
skip_serializing_field: bool,
|
||||
skip_deserializing_field: bool,
|
||||
skip_serializing_field_if: Option<P<ast::Expr>>,
|
||||
default_expr_if_missing: Option<P<ast::Expr>>,
|
||||
serialize_with: Option<P<ast::Expr>>,
|
||||
deserialize_with: Option<P<ast::Expr>>,
|
||||
deserialize_with: P<ast::Expr>,
|
||||
}
|
||||
|
||||
impl FieldAttrs {
|
||||
@@ -201,13 +197,16 @@ impl FieldAttrs {
|
||||
None => { cx.span_bug(field.span, "struct field has no name?") }
|
||||
};
|
||||
|
||||
let identity = quote_expr!(cx, |x| x);
|
||||
|
||||
let mut field_attrs = FieldAttrs {
|
||||
name: Name::new(field_ident),
|
||||
skip_serializing_field: false,
|
||||
skip_deserializing_field: false,
|
||||
skip_serializing_field_if: None,
|
||||
default_expr_if_missing: None,
|
||||
serialize_with: None,
|
||||
deserialize_with: None,
|
||||
deserialize_with: identity,
|
||||
};
|
||||
|
||||
for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) {
|
||||
@@ -249,6 +248,18 @@ impl FieldAttrs {
|
||||
field_attrs.skip_serializing_field = true;
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_deserializing)]`
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
||||
field_attrs.skip_deserializing_field = true;
|
||||
|
||||
// Initialize field to Default::default() unless a different
|
||||
// default is specified by `#[serde(default="...")]`
|
||||
if field_attrs.default_expr_if_missing.is_none() {
|
||||
let default_expr = builder.expr().default();
|
||||
field_attrs.default_expr_if_missing = Some(default_expr);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse `#[serde(skip_serializing_if="...")]`
|
||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
||||
let expr = wrap_skip_serializing(
|
||||
@@ -283,7 +294,7 @@ impl FieldAttrs {
|
||||
try!(parse_lit_into_path(cx, name, lit)),
|
||||
);
|
||||
|
||||
field_attrs.deserialize_with = Some(expr);
|
||||
field_attrs.deserialize_with = expr;
|
||||
}
|
||||
|
||||
_ => {
|
||||
@@ -310,7 +321,7 @@ impl FieldAttrs {
|
||||
match self.default_expr_if_missing {
|
||||
Some(ref expr) => expr.clone(),
|
||||
None => {
|
||||
let name = self.name.ident_expr();
|
||||
let name = self.name.deserialize_name_expr();
|
||||
AstBuilder::new().expr()
|
||||
.try()
|
||||
.method_call("missing_field").id("visitor")
|
||||
@@ -325,6 +336,10 @@ impl FieldAttrs {
|
||||
self.skip_serializing_field
|
||||
}
|
||||
|
||||
pub fn skip_deserializing_field(&self) -> bool {
|
||||
self.skip_deserializing_field
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field_if(&self) -> Option<&P<ast::Expr>> {
|
||||
self.skip_serializing_field_if.as_ref()
|
||||
}
|
||||
@@ -333,8 +348,8 @@ impl FieldAttrs {
|
||||
self.serialize_with.as_ref()
|
||||
}
|
||||
|
||||
pub fn deserialize_with(&self) -> Option<&P<ast::Expr>> {
|
||||
self.deserialize_with.as_ref()
|
||||
pub fn deserialize_with(&self) -> &P<ast::Expr> {
|
||||
&self.deserialize_with
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,7 +397,7 @@ fn get_renames(cx: &ExtCtxt,
|
||||
Ok((ser_name, de_name))
|
||||
}
|
||||
|
||||
fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
|
||||
pub fn get_serde_meta_items(attr: &ast::Attribute) -> Option<&[P<ast::MetaItem>]> {
|
||||
match attr.node.value.node {
|
||||
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
||||
attr::mark_used(&attr);
|
||||
@@ -473,7 +488,7 @@ fn parse_lit_into_path(cx: &ExtCtxt, name: &str, lit: &ast::Lit) -> Result<ast::
|
||||
|
||||
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts);
|
||||
|
||||
let path = match parser.parse_path(PathParsingMode::LifetimeAndTypesWithoutColons) {
|
||||
let path = match parser.parse_path(PathStyle::Type) {
|
||||
Ok(path) => path,
|
||||
Err(mut e) => {
|
||||
e.emit();
|
||||
@@ -557,14 +572,14 @@ fn wrap_serialize_with(cx: &ExtCtxt,
|
||||
quote_expr!(cx, {
|
||||
trait __SerdeSerializeWith {
|
||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ::serde::ser::Serializer;
|
||||
where S: _serde::ser::Serializer;
|
||||
}
|
||||
|
||||
impl<'a, T> __SerdeSerializeWith for &'a T
|
||||
where T: 'a + __SerdeSerializeWith,
|
||||
{
|
||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ::serde::ser::Serializer
|
||||
where S: _serde::ser::Serializer
|
||||
{
|
||||
(**self).__serde_serialize_with(serializer)
|
||||
}
|
||||
@@ -572,7 +587,7 @@ fn wrap_serialize_with(cx: &ExtCtxt,
|
||||
|
||||
impl $generics __SerdeSerializeWith for $container_ty $where_clause {
|
||||
fn __serde_serialize_with<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ::serde::ser::Serializer
|
||||
where S: _serde::ser::Serializer
|
||||
{
|
||||
$expr
|
||||
}
|
||||
@@ -582,11 +597,11 @@ fn wrap_serialize_with(cx: &ExtCtxt,
|
||||
value: &'a T,
|
||||
}
|
||||
|
||||
impl<'a, T> ::serde::ser::Serialize for __SerdeSerializeWithStruct<'a, T>
|
||||
impl<'a, T> _serde::ser::Serialize for __SerdeSerializeWithStruct<'a, T>
|
||||
where T: 'a + __SerdeSerializeWith
|
||||
{
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ::serde::ser::Serializer
|
||||
where S: _serde::ser::Serializer
|
||||
{
|
||||
self.value.__serde_serialize_with(serializer)
|
||||
}
|
||||
@@ -613,21 +628,20 @@ fn wrap_deserialize_with(cx: &ExtCtxt,
|
||||
|
||||
let where_clause = &generics.where_clause;
|
||||
|
||||
quote_expr!(cx, {
|
||||
quote_expr!(cx, ({
|
||||
struct __SerdeDeserializeWithStruct $generics $where_clause {
|
||||
value: $field_ty,
|
||||
}
|
||||
|
||||
impl $generics ::serde::de::Deserialize for $ty_path $where_clause {
|
||||
impl $generics _serde::de::Deserialize for $ty_path $where_clause {
|
||||
fn deserialize<D>(deserializer: &mut D) -> ::std::result::Result<Self, D::Error>
|
||||
where D: ::serde::de::Deserializer
|
||||
where D: _serde::de::Deserializer
|
||||
{
|
||||
let value = try!($path(deserializer));
|
||||
Ok(__SerdeDeserializeWithStruct { value: value })
|
||||
}
|
||||
}
|
||||
|
||||
let value: $ty_path = try!(visitor.visit_value());
|
||||
Ok(value.value)
|
||||
})
|
||||
|visit: $ty_path| visit.value
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use aster::AstBuilder;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ptr::P;
|
||||
use syntax::visit;
|
||||
|
||||
// Remove the default from every type parameter because in the generated impls
|
||||
// they look like associated types: "error: associated type bindings are not
|
||||
// allowed here".
|
||||
pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
||||
ast::Generics {
|
||||
ty_params: generics.ty_params.iter().map(|ty_param| {
|
||||
ast::TyParam {
|
||||
default: None,
|
||||
.. ty_param.clone()
|
||||
}}).collect(),
|
||||
.. generics.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_bound(
|
||||
cx: &ExtCtxt,
|
||||
builder: &AstBuilder,
|
||||
item: &ast::Item,
|
||||
generics: &ast::Generics,
|
||||
filter: &Fn(&ast::StructField) -> bool,
|
||||
bound: &ast::Path,
|
||||
) -> ast::Generics {
|
||||
builder.from_generics(generics.clone())
|
||||
.with_predicates(
|
||||
all_variants(cx, item).iter()
|
||||
.flat_map(|variant_data| all_struct_fields(variant_data))
|
||||
.filter(|field| filter(field))
|
||||
.map(|field| &field.ty)
|
||||
// TODO this filter can be removed later, see comment on function
|
||||
.filter(|ty| contains_generic(ty, generics))
|
||||
.map(|ty| strip_reference(ty))
|
||||
.map(|ty| builder.where_predicate()
|
||||
// the type that is being bounded e.g. T
|
||||
.bound().build(ty.clone())
|
||||
// the bound e.g. Serialize
|
||||
.bound().trait_(bound.clone()).build()
|
||||
.build()))
|
||||
.build()
|
||||
}
|
||||
|
||||
fn all_variants<'a>(cx: &ExtCtxt, item: &'a ast::Item) -> Vec<&'a ast::VariantData> {
|
||||
match item.node {
|
||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
||||
vec![variant_data]
|
||||
}
|
||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
||||
enum_def.variants.iter()
|
||||
.map(|variant| &variant.node.data)
|
||||
.collect()
|
||||
}
|
||||
_ => {
|
||||
cx.span_bug(item.span, "expected Item to be Struct or Enum");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn all_struct_fields(variant_data: &ast::VariantData) -> &[ast::StructField] {
|
||||
match *variant_data {
|
||||
ast::VariantData::Struct(ref fields, _) |
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
fields
|
||||
}
|
||||
ast::VariantData::Unit(_) => {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rust <1.7 enforces that `where` clauses involve generic type parameters. The
|
||||
// corresponding compiler error is E0193. It is no longer enforced in Rust >=1.7
|
||||
// so this filtering can be removed in the future when we stop supporting <1.7.
|
||||
//
|
||||
// E0193 means we must not generate a `where` clause like `i32: Serialize`
|
||||
// because even though i32 implements Serialize, i32 is not a generic type
|
||||
// parameter. Clauses like `T: Serialize` and `Option<T>: Serialize` are okay.
|
||||
// This function decides whether a given type references any of the generic type
|
||||
// parameters in the input `Generics`.
|
||||
fn contains_generic(ty: &ast::Ty, generics: &ast::Generics) -> bool {
|
||||
struct FindGeneric<'a> {
|
||||
generic_names: &'a HashSet<ast::Name>,
|
||||
found_generic: bool,
|
||||
}
|
||||
impl<'a, 'v> visit::Visitor<'v> for FindGeneric<'a> {
|
||||
fn visit_path(&mut self, path: &'v ast::Path, _id: ast::NodeId) {
|
||||
if !path.global
|
||||
&& path.segments.len() == 1
|
||||
&& self.generic_names.contains(&path.segments[0].identifier.name) {
|
||||
self.found_generic = true;
|
||||
} else {
|
||||
visit::walk_path(self, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let generic_names: HashSet<_> = generics.ty_params.iter()
|
||||
.map(|ty_param| ty_param.ident.name)
|
||||
.collect();
|
||||
|
||||
let mut visitor = FindGeneric {
|
||||
generic_names: &generic_names,
|
||||
found_generic: false,
|
||||
};
|
||||
visit::walk_ty(&mut visitor, ty);
|
||||
visitor.found_generic
|
||||
}
|
||||
|
||||
// This is required to handle types that use both a reference and a value of
|
||||
// the same type, as in:
|
||||
//
|
||||
// enum Test<'a, T> where T: 'a {
|
||||
// Lifetime(&'a T),
|
||||
// NoLifetime(T),
|
||||
// }
|
||||
//
|
||||
// Preserving references, we would generate an impl like:
|
||||
//
|
||||
// impl<'a, T> Serialize for Test<'a, T>
|
||||
// where &'a T: Serialize,
|
||||
// T: Serialize { ... }
|
||||
//
|
||||
// And taking a reference to one of the elements would fail with:
|
||||
//
|
||||
// error: cannot infer an appropriate lifetime for pattern due
|
||||
// to conflicting requirements [E0495]
|
||||
// Test::NoLifetime(ref v) => { ... }
|
||||
// ^~~~~
|
||||
//
|
||||
// Instead, we strip references before adding `T: Serialize` bounds in order to
|
||||
// generate:
|
||||
//
|
||||
// impl<'a, T> Serialize for Test<'a, T>
|
||||
// where T: Serialize { ... }
|
||||
fn strip_reference(ty: &P<ast::Ty>) -> &P<ast::Ty> {
|
||||
match ty.node {
|
||||
ast::TyKind::Rptr(_, ref mut_ty) => &mut_ty.ty,
|
||||
_ => ty
|
||||
}
|
||||
}
|
||||
+223
-128
@@ -14,6 +14,7 @@ use syntax::parse::token::InternedString;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use attr;
|
||||
use bound;
|
||||
use error::Error;
|
||||
|
||||
pub fn expand_derive_deserialize(
|
||||
@@ -46,11 +47,7 @@ pub fn expand_derive_deserialize(
|
||||
}
|
||||
};
|
||||
|
||||
let impl_generics = builder.from_generics(generics.clone())
|
||||
.add_ty_param_bound(
|
||||
builder.path().global().ids(&["serde", "de", "Deserialize"]).build()
|
||||
)
|
||||
.build();
|
||||
let impl_generics = build_impl_generics(cx, &builder, item, generics);
|
||||
|
||||
let ty = builder.ty().path()
|
||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||
@@ -66,19 +63,103 @@ pub fn expand_derive_deserialize(
|
||||
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let dummy_const = builder.id(format!("_IMPL_DESERIALIZE_FOR_{}", item.ident));
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
impl $impl_generics ::serde::de::Deserialize for $ty $where_clause {
|
||||
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
||||
where __D: ::serde::de::Deserializer,
|
||||
{
|
||||
$body
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const $dummy_const: () = {
|
||||
extern crate serde as _serde;
|
||||
#[automatically_derived]
|
||||
impl $impl_generics _serde::de::Deserialize for $ty $where_clause {
|
||||
fn deserialize<__D>(deserializer: &mut __D) -> ::std::result::Result<$ty, __D::Error>
|
||||
where __D: _serde::de::Deserializer,
|
||||
{
|
||||
$body
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
).unwrap();
|
||||
|
||||
push(Annotatable::Item(impl_item))
|
||||
}
|
||||
|
||||
// All the generics in the input, plus a bound `T: Deserialize` for each generic
|
||||
// field type that will be deserialized by us, plus a bound `T: Default` for
|
||||
// each generic field type that will be set to a default value.
|
||||
fn build_impl_generics(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
item: &Item,
|
||||
generics: &ast::Generics,
|
||||
) -> ast::Generics {
|
||||
let generics = bound::without_defaults(generics);
|
||||
let generics = bound::with_bound(cx, builder, item, &generics,
|
||||
&deserialized_by_us,
|
||||
&builder.path().ids(&["_serde", "de", "Deserialize"]).build());
|
||||
let generics = bound::with_bound(cx, builder, item, &generics,
|
||||
&requires_default,
|
||||
&builder.path().global().ids(&["std", "default", "Default"]).build());
|
||||
generics
|
||||
}
|
||||
|
||||
// Fields with a `skip_deserializing` or `deserialize_with` attribute are not
|
||||
// deserialized by us. All other fields may need a `T: Deserialize` bound where
|
||||
// T is the type of the field.
|
||||
fn deserialized_by_us(field: &ast::StructField) -> bool {
|
||||
for meta_items in field.attrs.iter().filter_map(attr::get_serde_meta_items) {
|
||||
for meta_item in meta_items {
|
||||
match meta_item.node {
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
||||
return false
|
||||
}
|
||||
ast::MetaItemKind::NameValue(ref name, _) if name == &"deserialize_with" => {
|
||||
// TODO: For now we require `T: Deserialize` even if the
|
||||
// field has `deserialize_with`. The reason is the signature
|
||||
// of serde::de::MapVisitor::missing_field which looks like:
|
||||
//
|
||||
// fn missing_field<T>(...) -> Result<T, Self::Error> where T: Deserialize
|
||||
//
|
||||
// So in order to use missing_field, the type must have the
|
||||
// `T: Deserialize` bound. Some formats rely on this bound
|
||||
// because they treat missing fields as unit.
|
||||
//
|
||||
// Long-term the fix would be to change the signature of
|
||||
// missing_field so it can, for example, use the
|
||||
// `deserialize_with` function to visit a unit in place of
|
||||
// the missing field.
|
||||
//
|
||||
// See https://github.com/serde-rs/serde/issues/259
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
// Fields with a `default` attribute (not `default=...`), and fields with a
|
||||
// `skip_deserializing` attribute that do not also have `default=...`.
|
||||
fn requires_default(field: &ast::StructField) -> bool {
|
||||
let mut has_skip_deserializing = false;
|
||||
for meta_items in field.attrs.iter().filter_map(attr::get_serde_meta_items) {
|
||||
for meta_item in meta_items {
|
||||
match meta_item.node {
|
||||
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
||||
return true
|
||||
}
|
||||
ast::MetaItemKind::NameValue(ref name, _) if name == &"default" => {
|
||||
return false
|
||||
}
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_deserializing" => {
|
||||
has_skip_deserializing = true
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
has_skip_deserializing
|
||||
}
|
||||
|
||||
fn deserialize_body(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
@@ -249,8 +330,7 @@ fn deserialize_visitor(
|
||||
fn deserializer_ty_param(builder: &aster::AstBuilder) -> ast::TyParam {
|
||||
builder.ty_param("__D")
|
||||
.trait_bound(builder.path()
|
||||
.global()
|
||||
.segment("serde").build()
|
||||
.segment("_serde").build()
|
||||
.segment("de").build()
|
||||
.id("Deserializer")
|
||||
.build())
|
||||
@@ -272,19 +352,19 @@ fn deserialize_unit_struct(
|
||||
Ok(quote_expr!(cx, {
|
||||
struct __Visitor;
|
||||
|
||||
impl ::serde::de::Visitor for __Visitor {
|
||||
impl _serde::de::Visitor for __Visitor {
|
||||
type Value = $type_ident;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> ::std::result::Result<$type_ident, E>
|
||||
where E: ::serde::de::Error,
|
||||
where E: _serde::de::Error,
|
||||
{
|
||||
Ok($type_ident)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> ::std::result::Result<$type_ident, V::Error>
|
||||
where V: ::serde::de::SeqVisitor,
|
||||
where V: _serde::de::SeqVisitor,
|
||||
{
|
||||
try!(visitor.end());
|
||||
self.visit_unit()
|
||||
@@ -324,20 +404,20 @@ fn deserialize_newtype_struct(
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_newtype_struct<D>(&mut self, deserializer: &mut D) -> ::std::result::Result<Self::Value, D::Error>
|
||||
where D: ::serde::de::Deserializer,
|
||||
where D: _serde::de::Deserializer,
|
||||
{
|
||||
let value = try!(::serde::de::Deserialize::deserialize(deserializer));
|
||||
let value = try!(_serde::de::Deserialize::deserialize(deserializer));
|
||||
Ok($type_ident(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
@@ -377,12 +457,12 @@ fn deserialize_tuple_struct(
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
@@ -405,7 +485,7 @@ fn deserialize_seq(
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => { value },
|
||||
None => {
|
||||
return Err(::serde::de::Error::end_of_stream());
|
||||
return Err(_serde::de::Error::end_of_stream());
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
@@ -430,19 +510,28 @@ fn deserialize_struct_as_seq(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
fields: &[ast::StructField],
|
||||
fields: &[(&ast::StructField, attr::FieldAttrs)],
|
||||
) -> Result<P<ast::Expr>, Error> {
|
||||
let let_values: Vec<_> = (0 .. fields.len())
|
||||
.map(|i| {
|
||||
let let_values: Vec<_> = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(_, ref attrs))| {
|
||||
let name = builder.id(format!("__field{}", i));
|
||||
quote_stmt!(cx,
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => { value },
|
||||
None => {
|
||||
return Err(::serde::de::Error::end_of_stream());
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
if attrs.skip_deserializing_field() {
|
||||
let default = attrs.expr_is_missing();
|
||||
quote_stmt!(cx,
|
||||
let $name = $default;
|
||||
).unwrap()
|
||||
} else {
|
||||
let deserialize_with = attrs.deserialize_with();
|
||||
quote_stmt!(cx,
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => { $deserialize_with(value) },
|
||||
None => {
|
||||
return Err(_serde::de::Error::end_of_stream());
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -450,7 +539,7 @@ fn deserialize_struct_as_seq(
|
||||
.with_id_exprs(
|
||||
fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, field)| {
|
||||
.map(|(i, &(field, _))| {
|
||||
(
|
||||
match field.ident {
|
||||
Some(name) => name.clone(),
|
||||
@@ -493,22 +582,21 @@ fn deserialize_struct(
|
||||
|
||||
let type_path = builder.path().id(type_ident).build();
|
||||
|
||||
let fields_with_attrs = try!(fields_with_attrs(cx, impl_generics, &ty, fields, false));
|
||||
|
||||
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
fields,
|
||||
&fields_with_attrs,
|
||||
));
|
||||
|
||||
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
&ty,
|
||||
impl_generics,
|
||||
fields,
|
||||
&fields_with_attrs,
|
||||
container_attrs,
|
||||
false,
|
||||
));
|
||||
|
||||
let type_name = container_attrs.name().deserialize_name_expr();
|
||||
@@ -518,19 +606,19 @@ fn deserialize_struct(
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::MapVisitor,
|
||||
where __V: _serde::de::MapVisitor,
|
||||
{
|
||||
$visit_map_expr
|
||||
}
|
||||
@@ -586,7 +674,7 @@ fn deserialize_item_enum(
|
||||
let ignored_arm = if container_attrs.deny_unknown_fields() {
|
||||
None
|
||||
} else {
|
||||
Some(quote_arm!(cx, __Field::__ignore => { Err(::serde::de::Error::end_of_stream()) }))
|
||||
Some(quote_arm!(cx, __Field::__ignore => { Err(_serde::de::Error::end_of_stream()) }))
|
||||
};
|
||||
|
||||
// Match arms to extract a variant from a string
|
||||
@@ -623,11 +711,11 @@ fn deserialize_item_enum(
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::EnumVisitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::EnumVisitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::VariantVisitor,
|
||||
where __V: _serde::de::VariantVisitor,
|
||||
{
|
||||
match try!(visitor.visit_variant()) {
|
||||
$variant_arms
|
||||
@@ -719,11 +807,11 @@ fn deserialize_tuple_variant(
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
@@ -750,22 +838,21 @@ fn deserialize_struct_variant(
|
||||
.id(variant_ident)
|
||||
.build();
|
||||
|
||||
let fields_with_attrs = try!(fields_with_attrs(cx, generics, &ty, fields, true));
|
||||
|
||||
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
fields,
|
||||
&fields_with_attrs,
|
||||
));
|
||||
|
||||
let (field_visitor, fields_stmt, field_expr) = try!(deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
type_path,
|
||||
&ty,
|
||||
generics,
|
||||
fields,
|
||||
&fields_with_attrs,
|
||||
container_attrs,
|
||||
true,
|
||||
));
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
@@ -780,19 +867,19 @@ fn deserialize_struct_variant(
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
impl $visitor_generics _serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
where __V: _serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::MapVisitor,
|
||||
where __V: _serde::de::MapVisitor,
|
||||
{
|
||||
$field_expr
|
||||
}
|
||||
@@ -851,7 +938,7 @@ fn deserialize_field_visitor(
|
||||
quote_expr!(cx, Ok(__Field::__ignore))
|
||||
} else {
|
||||
quote_expr!(cx, {
|
||||
Err(::serde::de::Error::invalid_value($index_error_msg))
|
||||
Err(_serde::de::Error::invalid_value($index_error_msg))
|
||||
})
|
||||
};
|
||||
|
||||
@@ -877,7 +964,7 @@ fn deserialize_field_visitor(
|
||||
let fallthrough_str_arm_expr = if !is_variant && !container_attrs.deny_unknown_fields() {
|
||||
quote_expr!(cx, Ok(__Field::__ignore))
|
||||
} else {
|
||||
quote_expr!(cx, Err(::serde::de::Error::$unknown_ident(value)))
|
||||
quote_expr!(cx, Err(_serde::de::Error::$unknown_ident(value)))
|
||||
};
|
||||
|
||||
let str_body = quote_expr!(cx,
|
||||
@@ -907,7 +994,7 @@ fn deserialize_field_visitor(
|
||||
} else {
|
||||
quote_expr!(cx, {
|
||||
let value = ::std::string::String::from_utf8_lossy(value);
|
||||
Err(::serde::de::Error::$unknown_ident(&value))
|
||||
Err(_serde::de::Error::$unknown_ident(&value))
|
||||
})
|
||||
};
|
||||
|
||||
@@ -919,10 +1006,10 @@ fn deserialize_field_visitor(
|
||||
);
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
impl ::serde::de::Deserialize for __Field {
|
||||
impl _serde::de::Deserialize for __Field {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> ::std::result::Result<__Field, D::Error>
|
||||
where D: ::serde::de::Deserializer,
|
||||
where D: _serde::de::Deserializer,
|
||||
{
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -930,25 +1017,25 @@ fn deserialize_field_visitor(
|
||||
phantom: PhantomData<D>
|
||||
}
|
||||
|
||||
impl<__D> ::serde::de::Visitor for __FieldVisitor<__D>
|
||||
where __D: ::serde::de::Deserializer
|
||||
impl<__D> _serde::de::Visitor for __FieldVisitor<__D>
|
||||
where __D: _serde::de::Deserializer
|
||||
{
|
||||
type Value = __Field;
|
||||
|
||||
fn visit_usize<E>(&mut self, value: usize) -> ::std::result::Result<__Field, E>
|
||||
where E: ::serde::de::Error,
|
||||
where E: _serde::de::Error,
|
||||
{
|
||||
$index_body
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> ::std::result::Result<__Field, E>
|
||||
where E: ::serde::de::Error,
|
||||
where E: _serde::de::Error,
|
||||
{
|
||||
$str_body
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(&mut self, value: &[u8]) -> ::std::result::Result<__Field, E>
|
||||
where E: ::serde::de::Error,
|
||||
where E: _serde::de::Error,
|
||||
{
|
||||
$bytes_body
|
||||
}
|
||||
@@ -966,29 +1053,17 @@ fn deserialize_struct_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
container_ty: &P<ast::Ty>,
|
||||
generics: &ast::Generics,
|
||||
fields: &[ast::StructField],
|
||||
fields: &[(&ast::StructField, attr::FieldAttrs)],
|
||||
container_attrs: &attr::ContainerAttrs,
|
||||
is_enum: bool,
|
||||
) -> Result<(Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>), Error> {
|
||||
let field_exprs = fields.iter()
|
||||
.map(|field| {
|
||||
let field_attrs = try!(
|
||||
attr::FieldAttrs::from_field(cx,
|
||||
container_ty,
|
||||
generics,
|
||||
field,
|
||||
is_enum)
|
||||
);
|
||||
Ok(field_attrs.name().deserialize_name())
|
||||
})
|
||||
.map(|&(_, ref attrs)| attrs.name().deserialize_name())
|
||||
.collect();
|
||||
|
||||
let field_visitor = deserialize_field_visitor(
|
||||
cx,
|
||||
builder,
|
||||
try!(field_exprs),
|
||||
field_exprs,
|
||||
container_attrs,
|
||||
false,
|
||||
);
|
||||
@@ -997,17 +1072,14 @@ fn deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_path,
|
||||
container_ty,
|
||||
generics,
|
||||
fields,
|
||||
container_attrs,
|
||||
is_enum,
|
||||
));
|
||||
|
||||
let fields_expr = builder.expr().ref_().slice()
|
||||
.with_exprs(
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
.map(|&(field, _)| {
|
||||
match field.ident {
|
||||
Some(name) => builder.expr().str(name),
|
||||
None => {
|
||||
@@ -1029,62 +1101,65 @@ fn deserialize_map(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
container_ty: &P<ast::Ty>,
|
||||
generics: &ast::Generics,
|
||||
fields: &[ast::StructField],
|
||||
fields: &[(&ast::StructField, attr::FieldAttrs)],
|
||||
container_attrs: &attr::ContainerAttrs,
|
||||
is_enum: bool,
|
||||
) -> Result<P<ast::Expr>, Error> {
|
||||
// Create the field names for the fields.
|
||||
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
let fields_attrs_names = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(ref field, ref attrs))|
|
||||
(field, attrs, builder.id(format!("__field{}", i))))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Declare each field that will be deserialized.
|
||||
let let_values: Vec<ast::Stmt> = fields_attrs_names.iter()
|
||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing_field())
|
||||
.map(|&(_, _, name)| quote_stmt!(cx, let mut $name = None;).unwrap())
|
||||
.collect();
|
||||
|
||||
let field_attrs: Vec<_> = try!(
|
||||
fields.iter()
|
||||
.map(|field| attr::FieldAttrs::from_field(cx, container_ty, generics, field, is_enum))
|
||||
.collect()
|
||||
);
|
||||
|
||||
// Declare each field.
|
||||
let let_values: Vec<ast::Stmt> = field_names.iter()
|
||||
.map(|field_name| quote_stmt!(cx, let mut $field_name = None;).unwrap())
|
||||
.collect();
|
||||
// Match arms to extract a value for a field.
|
||||
let value_arms = fields_attrs_names.iter()
|
||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing_field())
|
||||
.map(|&(_, ref attrs, name)| {
|
||||
let deserialize_with = attrs.deserialize_with();
|
||||
quote_arm!(cx,
|
||||
__Field::$name => {
|
||||
$name = Some($deserialize_with(try!(visitor.visit_value())));
|
||||
}
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Match arms to ignore value for fields that have `skip_deserializing`.
|
||||
// Ignored even if `deny_unknown_fields` is set.
|
||||
let skipped_arms = fields_attrs_names.iter()
|
||||
.filter(|&&(_, ref attrs, _)| attrs.skip_deserializing_field())
|
||||
.map(|&(_, _, name)| {
|
||||
quote_arm!(cx,
|
||||
__Field::$name => {
|
||||
try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>());
|
||||
}
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Visit ignored values to consume them
|
||||
let ignored_arm = if container_attrs.deny_unknown_fields() {
|
||||
None
|
||||
} else {
|
||||
Some(quote_arm!(cx,
|
||||
_ => { try!(visitor.visit_value::<::serde::de::impls::IgnoredAny>()); }
|
||||
_ => { try!(visitor.visit_value::<_serde::de::impls::IgnoredAny>()); }
|
||||
))
|
||||
};
|
||||
|
||||
// Match arms to extract a value for a field.
|
||||
let value_arms = field_attrs.iter().zip(field_names.iter())
|
||||
.map(|(field_attr, field_name)| {
|
||||
let expr = match field_attr.deserialize_with() {
|
||||
Some(expr) => expr.clone(),
|
||||
None => quote_expr!(cx, visitor.visit_value()),
|
||||
};
|
||||
|
||||
quote_arm!(cx,
|
||||
__Field::$field_name => {
|
||||
$field_name = Some(try!($expr));
|
||||
}
|
||||
)
|
||||
})
|
||||
.chain(ignored_arm.into_iter())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let extract_values = field_attrs.iter().zip(field_names.iter())
|
||||
.map(|(field_attr, field_name)| {
|
||||
let missing_expr = field_attr.expr_is_missing();
|
||||
let extract_values = fields_attrs_names.iter()
|
||||
.filter(|&&(_, ref attrs, _)| !attrs.skip_deserializing_field())
|
||||
.map(|&(_, ref attrs, name)| {
|
||||
let missing_expr = attrs.expr_is_missing();
|
||||
|
||||
Ok(quote_stmt!(cx,
|
||||
let $field_name = match $field_name {
|
||||
Some($field_name) => $field_name,
|
||||
let $name = match $name {
|
||||
Some($name) => $name,
|
||||
None => $missing_expr
|
||||
};
|
||||
).unwrap())
|
||||
@@ -1095,9 +1170,8 @@ fn deserialize_map(
|
||||
|
||||
let result = builder.expr().struct_path(struct_path)
|
||||
.with_id_exprs(
|
||||
fields.iter()
|
||||
.zip(field_names.iter())
|
||||
.map(|(field, field_name)| {
|
||||
fields_attrs_names.iter()
|
||||
.map(|&(field, attrs, name)| {
|
||||
(
|
||||
match field.ident {
|
||||
Some(name) => name.clone(),
|
||||
@@ -1105,7 +1179,11 @@ fn deserialize_map(
|
||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||
}
|
||||
},
|
||||
builder.expr().id(field_name),
|
||||
if attrs.skip_deserializing_field() {
|
||||
attrs.expr_is_missing()
|
||||
} else {
|
||||
builder.expr().id(name)
|
||||
}
|
||||
)
|
||||
})
|
||||
)
|
||||
@@ -1117,6 +1195,8 @@ fn deserialize_map(
|
||||
while let Some(key) = try!(visitor.visit_key()) {
|
||||
match key {
|
||||
$value_arms
|
||||
$skipped_arms
|
||||
$ignored_arm
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1127,3 +1207,18 @@ fn deserialize_map(
|
||||
Ok($result)
|
||||
}))
|
||||
}
|
||||
|
||||
fn fields_with_attrs<'a>(
|
||||
cx: &ExtCtxt,
|
||||
generics: &ast::Generics,
|
||||
ty: &P<ast::Ty>,
|
||||
fields: &'a [ast::StructField],
|
||||
is_enum: bool
|
||||
) -> Result<Vec<(&'a ast::StructField, attr::FieldAttrs)>, Error> {
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
let attrs = try!(attr::FieldAttrs::from_field(cx, &ty, generics, field, is_enum));
|
||||
Ok((field, attrs))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod attr;
|
||||
mod bound;
|
||||
mod de;
|
||||
mod error;
|
||||
mod ser;
|
||||
|
||||
+56
-18
@@ -8,10 +8,10 @@ use syntax::ast::{
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use attr;
|
||||
use bound;
|
||||
use error::Error;
|
||||
|
||||
pub fn expand_derive_serialize(
|
||||
@@ -60,11 +60,7 @@ fn serialize_item(
|
||||
}
|
||||
};
|
||||
|
||||
let impl_generics = builder.from_generics(generics.clone())
|
||||
.add_ty_param_bound(
|
||||
builder.path().global().ids(&["serde", "ser", "Serialize"]).build()
|
||||
)
|
||||
.build();
|
||||
let impl_generics = build_impl_generics(cx, builder, item, generics);
|
||||
|
||||
let ty = builder.ty().path()
|
||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||
@@ -78,15 +74,57 @@ fn serialize_item(
|
||||
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let dummy_const = builder.id(format!("_IMPL_SERIALIZE_FOR_{}", item.ident));
|
||||
|
||||
Ok(quote_item!(cx,
|
||||
impl $impl_generics ::serde::ser::Serialize for $ty $where_clause {
|
||||
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||
where __S: ::serde::ser::Serializer,
|
||||
{
|
||||
$body
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const $dummy_const: () = {
|
||||
extern crate serde as _serde;
|
||||
#[automatically_derived]
|
||||
impl $impl_generics _serde::ser::Serialize for $ty $where_clause {
|
||||
fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error>
|
||||
where __S: _serde::ser::Serializer,
|
||||
{
|
||||
$body
|
||||
}
|
||||
}
|
||||
};
|
||||
).unwrap())
|
||||
}
|
||||
|
||||
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
||||
// field type that will be serialized by us.
|
||||
fn build_impl_generics(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
item: &Item,
|
||||
generics: &ast::Generics,
|
||||
) -> ast::Generics {
|
||||
let generics = bound::without_defaults(generics);
|
||||
let generics = bound::with_bound(cx, builder, item, &generics,
|
||||
&serialized_by_us,
|
||||
&builder.path().ids(&["_serde", "ser", "Serialize"]).build());
|
||||
generics
|
||||
}
|
||||
|
||||
// Fields with a `skip_serializing` or `serialize_with` attribute are not
|
||||
// serialized by us. All other fields may need a `T: Serialize` bound where T is
|
||||
// the type of the field.
|
||||
fn serialized_by_us(field: &ast::StructField) -> bool {
|
||||
for meta_items in field.attrs.iter().filter_map(attr::get_serde_meta_items) {
|
||||
for meta_item in meta_items {
|
||||
match meta_item.node {
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
||||
return false
|
||||
}
|
||||
ast::MetaItemKind::NameValue(ref name, _) if name == &"serialize_with" => {
|
||||
return false
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
).unwrap())
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn serialize_body(
|
||||
@@ -330,7 +368,7 @@ fn serialize_variant(
|
||||
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::serialize_unit_variant(
|
||||
_serde::ser::Serializer::serialize_unit_variant(
|
||||
_serializer,
|
||||
$type_name,
|
||||
$variant_index,
|
||||
@@ -349,7 +387,7 @@ fn serialize_variant(
|
||||
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::serialize_newtype_variant(
|
||||
_serde::ser::Serializer::serialize_newtype_variant(
|
||||
_serializer,
|
||||
$type_name,
|
||||
$variant_index,
|
||||
@@ -616,12 +654,12 @@ fn serialize_tuple_struct_visitor(
|
||||
).unwrap(),
|
||||
|
||||
quote_item!(cx,
|
||||
impl $visitor_impl_generics ::serde::ser::SeqVisitor
|
||||
impl $visitor_impl_generics _serde::ser::SeqVisitor
|
||||
for Visitor $visitor_generics
|
||||
$where_clause {
|
||||
#[inline]
|
||||
fn visit<S>(&mut self, _serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer
|
||||
where S: _serde::ser::Serializer
|
||||
{
|
||||
match self.state {
|
||||
$arms
|
||||
@@ -721,12 +759,12 @@ fn serialize_struct_visitor(
|
||||
|
||||
quote_item!(cx,
|
||||
impl $visitor_impl_generics
|
||||
::serde::ser::MapVisitor
|
||||
_serde::ser::MapVisitor
|
||||
for Visitor $visitor_generics
|
||||
$where_clause {
|
||||
#[inline]
|
||||
fn visit<S>(&mut self, _serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer,
|
||||
where S: _serde::ser::Serializer,
|
||||
{
|
||||
loop {
|
||||
match self.state {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_macros"
|
||||
version = "0.7.2"
|
||||
version = "0.7.4"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -17,12 +17,12 @@ nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-tes
|
||||
|
||||
[dependencies]
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
serde_codegen = { version = "^0.7.2", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||
serde_codegen = { version = "^0.7.4", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||
|
||||
[dev-dependencies]
|
||||
compiletest_rs = "^0.1.1"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "^0.7.0", path = "../serde" }
|
||||
serde = { version = "^0.7.4", path = "../serde" }
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
|
||||
@@ -14,14 +14,14 @@ build = "build.rs"
|
||||
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
|
||||
|
||||
[build-dependencies]
|
||||
syntex = { version = "^0.31.0" }
|
||||
syntex_syntax = { version = "^0.31.0" }
|
||||
syntex = { version = "^0.32.0" }
|
||||
syntex_syntax = { version = "^0.32.0" }
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
||||
|
||||
[dev-dependencies]
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde" }
|
||||
syntex = "^0.31.0"
|
||||
syntex = "^0.32.0"
|
||||
|
||||
[dependencies]
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
#![cfg_attr(feature = "nightly", feature(plugin))]
|
||||
#![cfg_attr(feature = "nightly", plugin(clippy))]
|
||||
|
||||
extern crate serde;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::default::Default;
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
extern crate serde;
|
||||
use self::serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
use token::{
|
||||
Error,
|
||||
@@ -10,23 +10,33 @@ use token::{
|
||||
assert_de_tokens_error
|
||||
};
|
||||
|
||||
trait Trait: Sized {
|
||||
trait MyDefault: Sized {
|
||||
fn my_default() -> Self;
|
||||
}
|
||||
|
||||
trait ShouldSkip: Sized {
|
||||
fn should_skip(&self) -> bool;
|
||||
}
|
||||
|
||||
trait SerializeWith: Sized {
|
||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer;
|
||||
}
|
||||
|
||||
trait DeserializeWith: Sized {
|
||||
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer;
|
||||
}
|
||||
|
||||
impl Trait for i32 {
|
||||
impl MyDefault for i32 {
|
||||
fn my_default() -> Self { 123 }
|
||||
}
|
||||
|
||||
impl ShouldSkip for i32 {
|
||||
fn should_skip(&self) -> bool { *self == 123 }
|
||||
}
|
||||
|
||||
impl SerializeWith for i32 {
|
||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
@@ -36,7 +46,9 @@ impl Trait for i32 {
|
||||
false.serialize(ser)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DeserializeWith for i32 {
|
||||
fn deserialize_with<D>(de: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
@@ -49,18 +61,25 @@ impl Trait for i32 {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DefaultStruct<A, B: Default, C> where C: Trait {
|
||||
struct DefaultStruct<A, B, C, D, E>
|
||||
where C: MyDefault,
|
||||
E: MyDefault,
|
||||
{
|
||||
a1: A,
|
||||
#[serde(default)]
|
||||
a2: B,
|
||||
#[serde(default="Trait::my_default")]
|
||||
#[serde(default="MyDefault::my_default")]
|
||||
a3: C,
|
||||
#[serde(skip_deserializing)]
|
||||
a4: D,
|
||||
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||
a5: E,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_struct() {
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3 },
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
vec![
|
||||
Token::StructStart("DefaultStruct", Some(3)),
|
||||
|
||||
@@ -76,12 +95,20 @@ fn test_default_struct() {
|
||||
Token::Str("a3"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a4"),
|
||||
Token::I32(4),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a5"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 0, a3: 123 },
|
||||
&DefaultStruct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||
vec![
|
||||
Token::StructStart("DefaultStruct", Some(1)),
|
||||
|
||||
@@ -95,22 +122,29 @@ fn test_default_struct() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
enum DefaultEnum<A, B: Default, C> where C: Trait {
|
||||
enum DefaultEnum<A, B, C, D, E>
|
||||
where C: MyDefault,
|
||||
E: MyDefault
|
||||
{
|
||||
Struct {
|
||||
a1: A,
|
||||
#[serde(default)]
|
||||
a2: B,
|
||||
#[serde(default="Trait::my_default")]
|
||||
#[serde(default="MyDefault::my_default")]
|
||||
a3: C,
|
||||
#[serde(skip_deserializing)]
|
||||
a4: D,
|
||||
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||
a5: E,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_enum() {
|
||||
assert_de_tokens(
|
||||
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3 },
|
||||
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
vec![
|
||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(3)),
|
||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(5)),
|
||||
|
||||
Token::EnumMapSep,
|
||||
Token::Str("a1"),
|
||||
@@ -124,14 +158,22 @@ fn test_default_enum() {
|
||||
Token::Str("a3"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::EnumMapSep,
|
||||
Token::Str("a4"),
|
||||
Token::I32(4),
|
||||
|
||||
Token::EnumMapSep,
|
||||
Token::Str("a5"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::EnumMapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123 },
|
||||
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
|
||||
vec![
|
||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(3)),
|
||||
Token::EnumMapStart("DefaultEnum", "Struct", Some(5)),
|
||||
|
||||
Token::EnumMapSep,
|
||||
Token::Str("a1"),
|
||||
@@ -142,6 +184,97 @@ fn test_default_enum() {
|
||||
);
|
||||
}
|
||||
|
||||
// Does not implement std::default::Default.
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct NoStdDefault(i8);
|
||||
|
||||
impl MyDefault for NoStdDefault {
|
||||
fn my_default() -> Self {
|
||||
NoStdDefault(123)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct ContainsNoStdDefault<A: MyDefault> {
|
||||
#[serde(default="MyDefault::my_default")]
|
||||
a: A,
|
||||
}
|
||||
|
||||
// Tests that a struct field does not need to implement std::default::Default if
|
||||
// it is annotated with `default=...`.
|
||||
#[test]
|
||||
fn test_no_std_default() {
|
||||
assert_de_tokens(
|
||||
&ContainsNoStdDefault { a: NoStdDefault(123) },
|
||||
vec![
|
||||
Token::StructStart("ContainsNoStdDefault", Some(1)),
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&ContainsNoStdDefault { a: NoStdDefault(8) },
|
||||
vec![
|
||||
Token::StructStart("ContainsNoStdDefault", Some(1)),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a"),
|
||||
Token::StructNewType("NoStdDefault"),
|
||||
Token::I8(8),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Does not implement Deserialize.
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct NotDeserializeStruct(i8);
|
||||
|
||||
impl Default for NotDeserializeStruct {
|
||||
fn default() -> Self {
|
||||
NotDeserializeStruct(123)
|
||||
}
|
||||
}
|
||||
|
||||
// Does not implement Deserialize.
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum NotDeserializeEnum { Trouble }
|
||||
|
||||
impl MyDefault for NotDeserializeEnum {
|
||||
fn my_default() -> Self {
|
||||
NotDeserializeEnum::Trouble
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct ContainsNotDeserialize<A, B, C: MyDefault> {
|
||||
#[serde(skip_deserializing)]
|
||||
a: A,
|
||||
#[serde(skip_deserializing, default)]
|
||||
b: B,
|
||||
#[serde(skip_deserializing, default="MyDefault::my_default")]
|
||||
c: C,
|
||||
}
|
||||
|
||||
// Tests that a struct field does not need to implement Deserialize if it is
|
||||
// annotated with skip_deserializing, whether using the std Default or a
|
||||
// custom default.
|
||||
#[test]
|
||||
fn test_elt_not_deserialize() {
|
||||
assert_de_tokens(
|
||||
&ContainsNotDeserialize {
|
||||
a: NotDeserializeStruct(123),
|
||||
b: NotDeserializeStruct(123),
|
||||
c: NotDeserializeEnum::Trouble,
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("ContainsNotDeserialize", Some(3)),
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct DenyUnknown {
|
||||
@@ -152,7 +285,7 @@ struct DenyUnknown {
|
||||
fn test_ignore_unknown() {
|
||||
// 'Default' allows unknown. Basic smoke test of ignore...
|
||||
assert_de_tokens(
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3 },
|
||||
&DefaultStruct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
|
||||
vec![
|
||||
Token::StructStart("DefaultStruct", Some(5)),
|
||||
|
||||
@@ -389,11 +522,11 @@ fn test_rename_enum() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SkipSerializingStruct<'a, B, C> where C: Trait {
|
||||
struct SkipSerializingStruct<'a, B, C> where C: ShouldSkip {
|
||||
a: &'a i8,
|
||||
#[serde(skip_serializing)]
|
||||
b: B,
|
||||
#[serde(skip_serializing_if="Trait::should_skip")]
|
||||
#[serde(skip_serializing_if="ShouldSkip::should_skip")]
|
||||
c: C,
|
||||
}
|
||||
|
||||
@@ -440,12 +573,12 @@ fn test_skip_serializing_struct() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SkipSerializingEnum<'a, B, C> where C: Trait {
|
||||
enum SkipSerializingEnum<'a, B, C> where C: ShouldSkip {
|
||||
Struct {
|
||||
a: &'a i8,
|
||||
#[serde(skip_serializing)]
|
||||
_b: B,
|
||||
#[serde(skip_serializing_if="Trait::should_skip")]
|
||||
#[serde(skip_serializing_if="ShouldSkip::should_skip")]
|
||||
c: C,
|
||||
}
|
||||
}
|
||||
@@ -492,10 +625,62 @@ fn test_skip_serializing_enum() {
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct NotSerializeStruct(i8);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum NotSerializeEnum { Trouble }
|
||||
|
||||
impl SerializeWith for NotSerializeEnum {
|
||||
fn serialize_with<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer
|
||||
{
|
||||
"trouble".serialize(ser)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerializeWithStruct<'a, B> where B: Trait {
|
||||
struct ContainsNotSerialize<'a, B, C, D> where B: 'a, D: SerializeWith {
|
||||
a: &'a Option<i8>,
|
||||
#[serde(skip_serializing)]
|
||||
b: &'a B,
|
||||
#[serde(skip_serializing)]
|
||||
c: Option<C>,
|
||||
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||
d: D,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_elt_not_serialize() {
|
||||
let a = 1;
|
||||
assert_ser_tokens(
|
||||
&ContainsNotSerialize {
|
||||
a: &Some(a),
|
||||
b: &NotSerializeStruct(2),
|
||||
c: Some(NotSerializeEnum::Trouble),
|
||||
d: NotSerializeEnum::Trouble,
|
||||
},
|
||||
&[
|
||||
Token::StructStart("ContainsNotSerialize", Some(2)),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a"),
|
||||
Token::Option(true),
|
||||
Token::I8(1),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("d"),
|
||||
Token::Str("trouble"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerializeWithStruct<'a, B> where B: SerializeWith {
|
||||
a: &'a i8,
|
||||
#[serde(serialize_with="Trait::serialize_with")]
|
||||
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||
b: B,
|
||||
}
|
||||
|
||||
@@ -544,10 +729,10 @@ fn test_serialize_with_struct() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SerializeWithEnum<'a, B> where B: Trait {
|
||||
enum SerializeWithEnum<'a, B> where B: SerializeWith {
|
||||
Struct {
|
||||
a: &'a i8,
|
||||
#[serde(serialize_with="Trait::serialize_with")]
|
||||
#[serde(serialize_with="SerializeWith::serialize_with")]
|
||||
b: B,
|
||||
}
|
||||
}
|
||||
@@ -597,9 +782,9 @@ fn test_serialize_with_enum() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeserializeWithStruct<B> where B: Trait {
|
||||
struct DeserializeWithStruct<B> where B: DeserializeWith {
|
||||
a: i8,
|
||||
#[serde(deserialize_with="Trait::deserialize_with")]
|
||||
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||
b: B,
|
||||
}
|
||||
|
||||
@@ -647,10 +832,10 @@ fn test_deserialize_with_struct() {
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
enum DeserializeWithEnum<B> where B: Trait {
|
||||
enum DeserializeWithEnum<B> where B: DeserializeWith {
|
||||
Struct {
|
||||
a: i8,
|
||||
#[serde(deserialize_with="Trait::deserialize_with")]
|
||||
#[serde(deserialize_with="DeserializeWith::deserialize_with")]
|
||||
b: B,
|
||||
}
|
||||
}
|
||||
@@ -697,3 +882,57 @@ fn test_deserialize_with_enum() {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_renamed_field_struct() {
|
||||
assert_de_tokens_error::<RenameStruct>(
|
||||
vec![
|
||||
Token::StructStart("Superhero", Some(2)),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
Error::MissingFieldError("a3"),
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<RenameStructSerializeDeserialize>(
|
||||
vec![
|
||||
Token::StructStart("SuperheroDe", Some(2)),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructEnd,
|
||||
],
|
||||
Error::MissingFieldError("a5"),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_renamed_field_enum() {
|
||||
assert_de_tokens_error::<RenameEnum>(
|
||||
vec![
|
||||
Token::EnumMapStart("Superhero", "barry_allan", Some(1)),
|
||||
|
||||
Token::EnumMapEnd,
|
||||
],
|
||||
Error::MissingFieldError("b"),
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<RenameEnumSerializeDeserialize<i8>>(
|
||||
vec![
|
||||
Token::EnumMapStart("SuperheroDe", "jason_todd", Some(2)),
|
||||
|
||||
Token::EnumMapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(0),
|
||||
|
||||
Token::EnumMapEnd,
|
||||
],
|
||||
Error::MissingFieldError("d"),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use serde;
|
||||
use std::fmt;
|
||||
use std::error;
|
||||
use serde::Serialize;
|
||||
use serde::bytes::{ByteBuf, Bytes};
|
||||
|
||||
extern crate serde;
|
||||
use self::serde::Serialize;
|
||||
use self::serde::bytes::{ByteBuf, Bytes};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::net;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::de::{Deserializer, Visitor};
|
||||
extern crate serde;
|
||||
use self::serde::de::{Deserializer, Visitor};
|
||||
|
||||
use token::{
|
||||
Error,
|
||||
@@ -24,6 +25,7 @@ struct TupleStruct(i32, i32, i32);
|
||||
struct Struct {
|
||||
a: i32,
|
||||
b: i32,
|
||||
#[serde(skip_deserializing)]
|
||||
c: i32,
|
||||
}
|
||||
|
||||
@@ -60,6 +62,17 @@ macro_rules! declare_tests {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_error_tests {
|
||||
($($name:ident<$target:ident> { $tokens:expr, $expected:expr, })+) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
assert_de_tokens_error::<$target>($tokens, $expected);
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
declare_tests! {
|
||||
@@ -524,7 +537,40 @@ declare_tests! {
|
||||
],
|
||||
}
|
||||
test_struct {
|
||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
||||
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||
Token::MapStart(Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||
Token::StructStart("Struct", Some(3)),
|
||||
Token::StructSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
Token::StructEnd,
|
||||
],
|
||||
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_struct_with_skip {
|
||||
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||
Token::MapStart(Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
@@ -537,9 +583,13 @@ declare_tests! {
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("d"),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
||||
Struct { a: 1, b: 2, c: 0 } => vec![
|
||||
Token::StructStart("Struct", Some(3)),
|
||||
Token::StructSep,
|
||||
Token::Str("a"),
|
||||
@@ -552,6 +602,10 @@ declare_tests! {
|
||||
Token::StructSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("d"),
|
||||
Token::I32(4),
|
||||
Token::StructEnd,
|
||||
],
|
||||
}
|
||||
@@ -611,6 +665,21 @@ declare_tests! {
|
||||
Token::Unit,
|
||||
],
|
||||
}
|
||||
test_box {
|
||||
Box::new(0i32) => vec![Token::I32(0)],
|
||||
}
|
||||
test_boxed_slice {
|
||||
Box::new([0, 1, 2]) => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(0),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_net_ipv4addr {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => vec![Token::Str("1.2.3.4")],
|
||||
}
|
||||
@@ -638,12 +707,22 @@ fn test_net_ipaddr() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_error() {
|
||||
assert_de_tokens_error::<Enum>(
|
||||
declare_error_tests! {
|
||||
test_unknown_variant<Enum> {
|
||||
vec![
|
||||
Token::EnumUnit("Enum", "Foo"),
|
||||
],
|
||||
Error::UnknownVariantError("Foo".to_owned()),
|
||||
)
|
||||
}
|
||||
test_struct_seq_too_long<Struct> {
|
||||
vec![
|
||||
Token::SeqStart(Some(4)),
|
||||
Token::SeqSep, Token::I32(1),
|
||||
Token::SeqSep, Token::I32(2),
|
||||
Token::SeqSep, Token::I32(3),
|
||||
Token::SeqSep, Token::I32(4),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
Error::UnexpectedToken(Token::SeqSep),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::marker::PhantomData;
|
||||
use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens};
|
||||
|
||||
/*
|
||||
@@ -143,6 +144,19 @@ pub enum GenericEnum<T, U> {
|
||||
Map { x: T, y: U },
|
||||
}
|
||||
|
||||
trait AssociatedType {
|
||||
type X;
|
||||
}
|
||||
|
||||
impl AssociatedType for i32 {
|
||||
type X = i32;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DefaultTyParam<T: AssociatedType<X=i32> = i32> {
|
||||
phantom: PhantomData<T>
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_named_unit() {
|
||||
assert_tokens(
|
||||
@@ -601,3 +615,19 @@ fn test_generic_enum_map() {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_ty_param() {
|
||||
assert_tokens(
|
||||
&DefaultTyParam::<i32> { phantom: PhantomData },
|
||||
vec![
|
||||
Token::StructStart("DefaultTyParam", Some(1)),
|
||||
|
||||
Token::StructSep,
|
||||
Token::Str("phantom"),
|
||||
Token::UnitStruct("PhantomData"),
|
||||
|
||||
Token::StructEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -262,6 +262,21 @@ declare_ser_tests! {
|
||||
Token::EnumMapEnd,
|
||||
],
|
||||
}
|
||||
test_box {
|
||||
Box::new(0i32) => &[Token::I32(0)],
|
||||
}
|
||||
test_boxed_slice {
|
||||
Box::new([0, 1, 2]) => &[
|
||||
Token::SeqArrayStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(0),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_net_ipv4addr {
|
||||
"1.2.3.4".parse::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ use std::fmt;
|
||||
use std::iter;
|
||||
use std::error;
|
||||
|
||||
use serde::ser::{self, Serialize};
|
||||
use serde::de;
|
||||
use serde::de::value::{self, ValueDeserializer};
|
||||
extern crate serde;
|
||||
use self::serde::ser::{self, Serialize};
|
||||
use self::serde::de;
|
||||
use self::serde::de::value::{self, ValueDeserializer};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Token<'a> {
|
||||
|
||||
Reference in New Issue
Block a user