mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 08:18:03 +00:00
Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b6965ecde8 | |||
| 3f3cffe317 | |||
| 4e7b0ac089 | |||
| f6a66a5537 | |||
| fd5ab8c5c8 | |||
| 39fa78e2ec | |||
| e18416ac7f | |||
| b63cc13f3a | |||
| 74b230c183 | |||
| 064241f03c | |||
| 32163cd53b | |||
| 3a5aa00262 | |||
| 7a3e3a61f4 | |||
| 517c2f79b7 | |||
| 3cde6fa333 | |||
| da7bb8e109 | |||
| 718822449f | |||
| 43624e8e7f | |||
| 4507eaec5b | |||
| b3212f4c2b | |||
| 6d25fc9dbb | |||
| a5d0703e44 | |||
| 0a32cea26e | |||
| da4e37d3f5 | |||
| 3f9cbc157a | |||
| a51f930101 | |||
| 77edd8e544 | |||
| 8087b7cec6 | |||
| 8df841f048 | |||
| bfa2b69193 | |||
| fbcf905c9f | |||
| 979a4bcd88 | |||
| 342ea25290 | |||
| 8d8f17982a | |||
| dd3233ac85 | |||
| 9b57f60c2a | |||
| 8038832a79 | |||
| 072ff149f5 | |||
| 8f08baf43a | |||
| b3b3b7d565 | |||
| 1a8a11e924 | |||
| f3f098e7f5 | |||
| 09822c99cc | |||
| 966b104d48 | |||
| 59e0d5e081 | |||
| c687ee60ff | |||
| af6fbba9b8 | |||
| a577574cfe | |||
| 7521db7b0b | |||
| 72de877ec3 | |||
| f872b3fb4b | |||
| ddc33ee747 | |||
| 612e384b03 | |||
| 1c88856fc8 | |||
| 534e6d1f4c | |||
| 7ad31a01dd | |||
| 00cd2900e7 | |||
| 05a238fac4 | |||
| 310db21d64 | |||
| 72af0896e8 | |||
| c4392ff256 | |||
| c68ab508c0 | |||
| 76cca814f0 | |||
| 22b69fc5c9 | |||
| a1bd0c1667 | |||
| 48ea75fddc | |||
| 4b49f716b9 | |||
| 55f5f397d7 | |||
| 4be4f60f21 | |||
| 9a8037bbf2 | |||
| c7f1af90b2 | |||
| 902d3a0aa5 | |||
| c14eb28223 | |||
| 2722a04e52 | |||
| 5dbcd7957a |
+4
-2
@@ -22,9 +22,11 @@ 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_macros && travis-cargo --only nightly test) &&
|
||||
(cd serde_macros && travis-cargo --only nightly bench) &&
|
||||
(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)
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ Documentation is available at:
|
||||
* [serde\_json](https://serde-rs.github.io/serde/serde_json/serde_json/index.html)
|
||||
* [serde\_codegen](https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html)
|
||||
|
||||
Using Serde
|
||||
===========
|
||||
Using Serde with Nightly Rust and serde\_macros
|
||||
===============================================
|
||||
|
||||
Here is a simple example that demonstrates how to use Serde by serializing and
|
||||
deserializing to JSON. Serde comes with some powerful code generation libraries
|
||||
@@ -76,6 +76,9 @@ When run, it produces:
|
||||
Point { x: 1, y: 2 }
|
||||
```
|
||||
|
||||
Using Serde with Stable Rust, syntex, and serde\_codegen
|
||||
========================================================
|
||||
|
||||
Stable Rust is a little more complicated because it does not yet support
|
||||
compiler plugins. Instead we need to use the code generation library
|
||||
[syntex](https://github.com/erickt/rust-syntex) for this:
|
||||
@@ -126,6 +129,28 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
`build.rs`
|
||||
|
||||
```rust
|
||||
extern crate syntex;
|
||||
extern crate serde_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
let src = Path::new("src/main.rs.in");
|
||||
let dst = Path::new(&out_dir).join("main.rs");
|
||||
|
||||
let mut registry = syntex::Registry::new();
|
||||
|
||||
serde_codegen::register(&mut registry);
|
||||
registry.expand("", &src, &dst).unwrap();
|
||||
}
|
||||
```
|
||||
|
||||
This also produces:
|
||||
|
||||
```
|
||||
@@ -215,6 +240,20 @@ include!(concat!(env!("OUT_DIR"), "/main.rs"));
|
||||
|
||||
The `src/main.rs.in` is the same as before.
|
||||
|
||||
Then to run with stable:
|
||||
|
||||
```
|
||||
% cargo build
|
||||
...
|
||||
```
|
||||
|
||||
Or with nightly:
|
||||
|
||||
```rust
|
||||
% cargo build --features nightly --no-default-features
|
||||
...
|
||||
```
|
||||
|
||||
Serialization without Macros
|
||||
============================
|
||||
|
||||
@@ -311,6 +350,8 @@ as a named map. Its visitor uses a simple state machine to iterate through all
|
||||
the fields:
|
||||
|
||||
```rust
|
||||
extern crate serde;
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@@ -479,6 +520,13 @@ deserializes an enum variant from a string. So for our `Point` example from
|
||||
before, we need to generate:
|
||||
|
||||
```rust
|
||||
extern crate serde;
|
||||
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
enum PointField {
|
||||
X,
|
||||
Y,
|
||||
@@ -507,11 +555,7 @@ impl serde::Deserialize for PointField {
|
||||
deserializer.visit(PointFieldVisitor)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This is then used in our actual deserializer:
|
||||
|
||||
```rust
|
||||
impl serde::Deserialize for Point {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Point, D::Error>
|
||||
where D: serde::de::Deserializer
|
||||
@@ -557,6 +601,84 @@ impl serde::de::Visitor for PointVisitor {
|
||||
}
|
||||
```
|
||||
|
||||
Design Considerations and tradeoffs for Serializers and Deserializers
|
||||
=====================================================================
|
||||
|
||||
Serde serialization and deserialization implementations are written in such a
|
||||
way that they err on being able to represent more values, and also provide
|
||||
better error messages when they are passed an incorrect type to deserialize
|
||||
from. For example, by default, it is a syntax error to deserialize a `String`
|
||||
into an `Option<String>`. This is implemented such that it is possible to
|
||||
distinguish between the values `None` and `Some(())`, if the serialization
|
||||
format supports option types.
|
||||
|
||||
However, many formats do not have option types, and represents optional values
|
||||
as either a `null`, or some other value. Serde `Serializer`s and
|
||||
`Deserializer`s can opt-in support for this. For serialization, this is pretty
|
||||
easy. Simply implement these methods:
|
||||
|
||||
```rust
|
||||
...
|
||||
|
||||
fn visit_none(&mut self) -> Result<(), Self::Error> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
fn visit_some<T>(&mut self, value: T) -> Result<(), Self::Error> {
|
||||
value.serialize(self)
|
||||
}
|
||||
...
|
||||
```
|
||||
|
||||
For deserialization, this can be implemented by way of the
|
||||
`Deserializer::visit_option` hook, which presumes that there is some ability to peek at what is the
|
||||
next value in the serialized token stream. This following example is from
|
||||
[serde_tests::TokenDeserializer](https://github.com/serde-rs/serde/blob/master/serde_tests/tests/token.rs#L435-L454),
|
||||
where it checks to see if the next value is an `Option`, a `()`, or some other
|
||||
value:
|
||||
|
||||
```rust
|
||||
...
|
||||
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::Option(false)) => {
|
||||
self.tokens.next();
|
||||
visitor.visit_none()
|
||||
}
|
||||
Some(&Token::Option(true)) => {
|
||||
self.tokens.next();
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
Some(&Token::Unit) => {
|
||||
self.tokens.next();
|
||||
visitor.visit_none()
|
||||
}
|
||||
Some(_) => visitor.visit_some(self),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
Annotations
|
||||
===========
|
||||
|
||||
`serde_codegen` and `serde_macros` support annotations that help to customize
|
||||
how types are serialized. Here are the supported annotations:
|
||||
|
||||
| Annotation | Function |
|
||||
| ---------- | -------- |
|
||||
| `#[serde(rename(json="name1", xml="name2"))` | Serialize this field with the given name for the given formats |
|
||||
| `#[serde(default)` | If the value is not specified, use the `Default::default()` |
|
||||
| `#[serde(rename="name")` | Serialize this field with the given name |
|
||||
| `#[serde(skip_serializing)` | Do not serialize this value |
|
||||
| `#[serde(skip_serializing_if_empty)` | Do not serialize this value if `$value.is_empty()` is `true` |
|
||||
| `#[serde(skip_serializing_if_none)` | Do not serialize this value if `$value.is_none()` is `true` |
|
||||
|
||||
Serialization Formats Using Serde
|
||||
=================================
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
target
|
||||
Cargo.lock
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "serde-syntex-example"
|
||||
version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["serde_codegen"]
|
||||
nightly = ["serde_macros"]
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = { version = "^0.6.4", optional = true }
|
||||
syntex = "^0.22.0"
|
||||
|
||||
[dependencies]
|
||||
serde = "^0.6.1"
|
||||
serde_json = "^0.6.0"
|
||||
serde_macros = { version = "^0.6.1", optional = true }
|
||||
@@ -0,0 +1,20 @@
|
||||
This example demonstrates how to use Serde with Syntex. On stable or nightly
|
||||
with Syntex, it can be built with:
|
||||
|
||||
```
|
||||
% multirust run stable cargo run
|
||||
Running `target/debug/serde-syntex-example`
|
||||
{"x":1,"y":2}
|
||||
Point { x: 1, y: 2 }
|
||||
|
||||
% multirust run nightly cargo run
|
||||
Running `target/debug/serde-syntex-example`
|
||||
{"x":1,"y":2}
|
||||
Point { x: 1, y: 2 }
|
||||
```
|
||||
|
||||
On nightly, it can use a plugin with:
|
||||
|
||||
```
|
||||
% multirust run nightly cargo run --features nightly --no-default-features
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
#[cfg(not(feature = "serde_macros"))]
|
||||
mod inner {
|
||||
extern crate syntex;
|
||||
extern crate serde_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
let src = Path::new("src/main.rs.in");
|
||||
let dst = Path::new(&out_dir).join("main.rs");
|
||||
|
||||
let mut registry = syntex::Registry::new();
|
||||
|
||||
serde_codegen::register(&mut registry);
|
||||
registry.expand("", &src, &dst).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde_macros")]
|
||||
mod inner {
|
||||
pub fn main() {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
inner::main();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#![cfg_attr(nightly, feature(custom_derive, plugin))]
|
||||
#![cfg_attr(nightly, plugin(serde_macros))]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
#[cfg(feature = "serde_macros")]
|
||||
include!("main.rs.in");
|
||||
|
||||
#[cfg(not(feature = "serde_macros"))]
|
||||
include!(concat!(env!("OUT_DIR"), "/main.rs"));
|
||||
@@ -0,0 +1,16 @@
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let point = Point { x: 1, y: 2 };
|
||||
let serialized = serde_json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized);
|
||||
|
||||
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
println!("{:?}", deserialized);
|
||||
}
|
||||
+9
-4
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.5.3"
|
||||
version = "0.6.15"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
@@ -9,9 +9,14 @@ documentation = "https://serde-rs.github.io/serde/serde/serde/index.html"
|
||||
readme = "../README.md"
|
||||
keywords = ["serde", "serialization"]
|
||||
|
||||
[dependencies]
|
||||
num = "*"
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
nightly-testing = ["clippy", "nightly"]
|
||||
num-bigint = ["num/bigint"]
|
||||
num-complex = ["num/complex"]
|
||||
num-impls = ["num-bigint", "num-complex", "num-rational"]
|
||||
num-rational = ["num/rational"]
|
||||
|
||||
[dependencies]
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
num = { version = "^0.1.27", default-features = false }
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
use std::cmp;
|
||||
use std::io;
|
||||
use std::slice;
|
||||
|
||||
trait IntoBufRead {
|
||||
type IntoBuf: io::BufRead + BufReadExt;
|
||||
|
||||
fn into_buf_read(self) -> Self::IntoBuf;
|
||||
}
|
||||
|
||||
trait BufReadExt {
|
||||
fn get_buf(&self) -> &[u8];
|
||||
fn read_u8(&mut self) -> io::Result<Option<u8>>;
|
||||
}
|
||||
|
||||
struct SliceReader<'a> {
|
||||
buf: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> io::Read for SliceReader<'a> {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let amt = cmp::min(buf.len(), self.buf.len());
|
||||
let (a, b) = self.buf.split_at(amt);
|
||||
slice::bytes::copy_memory(buf, a);
|
||||
*self.buf = b;
|
||||
Ok(amt)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> io::BufRead for SliceReader<'a> {
|
||||
fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) }
|
||||
fn consume(&mut self, amt: usize) { *self.buf = &self.buf[amt..]; }
|
||||
}
|
||||
|
||||
impl<'a> BufReadExt for SliceReader<'a> {
|
||||
fn get_buf(&self) -> &[u8] { self.buf }
|
||||
fn read_u8(&mut self) -> io::Result<Option<u8>> {
|
||||
let byte = self.buf.get(0);
|
||||
*self.buf = &self.buf[1..];
|
||||
byte
|
||||
}
|
||||
}
|
||||
|
||||
struct BufReader<R> {
|
||||
inner: R,
|
||||
buf: io::Cursor<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<R> BufReader<R> where R: io::Read {
|
||||
fn new(inner: R) -> Self {
|
||||
BufferedReader::with_capacity(io::DEFAULT_BUF_SIZE, inner)
|
||||
}
|
||||
|
||||
fn new(cap: usize, inner: R) -> Self {
|
||||
BufferedReader {
|
||||
inner: inner,
|
||||
buf: io::Cursor::new(Vec::with_capacity(cap)),
|
||||
}
|
||||
}
|
||||
|
||||
fn into_inner(self) -> R {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> Read for BufReader<R> where R: io::Read {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
// If we don't have any buffered data and we're doing a massive read
|
||||
// (larger than our internal buffer), bypass our internal buffer
|
||||
// entirely.
|
||||
if self.buf.get_ref().len() == self.buf.position() as usize &&
|
||||
buf.len() >= self.buf.get_ref().capacity() {
|
||||
return self.inner.read(buf);
|
||||
}
|
||||
try!(self.fill_buf());
|
||||
self.buf.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> BufReadExt for BufReader<R> {
|
||||
fn get_buf(&self) -> &[u8] {
|
||||
self.buf.get_ref()
|
||||
}
|
||||
|
||||
fn read_u8(&mut self) -> io::Result<Option<u8>> {
|
||||
if self.buf.get_ref().len() == self.buf.position() as usize {
|
||||
|
||||
}
|
||||
let byte = self.buf.get(0);
|
||||
*self.buf = &self.buf[1..];
|
||||
byte
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -60,19 +60,21 @@ impl<'a> ser::Serialize for Bytes<'a> {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `ByteBuf` wraps a `Vec<u8>` in order to hook into serialize and from deserialize a byte array.
|
||||
/// `ByteBuf` wraps a `Vec<u8>` and serializes as a byte array.
|
||||
#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
pub struct ByteBuf {
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl ByteBuf {
|
||||
/// Construct a new, empty `ByteBuf`.
|
||||
pub fn new() -> Self {
|
||||
ByteBuf {
|
||||
bytes: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a new, empty `ByteBuf` with the specified capacity.
|
||||
pub fn with_capacity(cap: usize) -> Self {
|
||||
ByteBuf {
|
||||
bytes: Vec::with_capacity(cap)
|
||||
@@ -142,6 +144,7 @@ impl ser::Serialize for ByteBuf {
|
||||
}
|
||||
}
|
||||
|
||||
/// This type implements the `serde::de::Visitor` trait for a `ByteBuf`.
|
||||
pub struct ByteBufVisitor;
|
||||
|
||||
impl de::Visitor for ByteBufVisitor {
|
||||
|
||||
+120
-13
@@ -1,3 +1,5 @@
|
||||
//! This module contains `Deserialize` and `Visitor` implementations.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{
|
||||
BinaryHeap,
|
||||
@@ -39,6 +41,7 @@ use de::{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A visitor that produces a `()`.
|
||||
pub struct UnitVisitor;
|
||||
|
||||
impl Visitor for UnitVisitor {
|
||||
@@ -67,6 +70,7 @@ impl Deserialize for () {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A visitor that produces a `bool`.
|
||||
pub struct BoolVisitor;
|
||||
|
||||
impl Visitor for BoolVisitor {
|
||||
@@ -113,11 +117,13 @@ macro_rules! impl_deserialize_num_method {
|
||||
}
|
||||
}
|
||||
|
||||
/// A visitor that produces a primitive type.
|
||||
pub struct PrimitiveVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> PrimitiveVisitor<T> {
|
||||
/// Construct a new `PrimitiveVisitor`.
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
PrimitiveVisitor {
|
||||
@@ -148,7 +154,9 @@ impl<
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<T, E>
|
||||
where E: Error,
|
||||
{
|
||||
str::FromStr::from_str(v.trim()).or(Err(Error::type_mismatch(Type::Str)))
|
||||
str::FromStr::from_str(v.trim()).or_else(|_| {
|
||||
Err(Error::type_mismatch(Type::Str))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +236,7 @@ impl Visitor for StringVisitor {
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v.to_string())
|
||||
Ok(v.to_owned())
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<String, E>
|
||||
@@ -241,12 +249,12 @@ impl Visitor for StringVisitor {
|
||||
where E: Error,
|
||||
{
|
||||
match str::from_utf8(v) {
|
||||
Ok(s) => Ok(s.to_string()),
|
||||
Ok(s) => Ok(s.to_owned()),
|
||||
Err(_) => Err(Error::type_mismatch(Type::String)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_byte_buf<'a, E>(&mut self, v: Vec<u8>) -> Result<String, E>
|
||||
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
match String::from_utf8(v) {
|
||||
@@ -275,6 +283,13 @@ impl<
|
||||
> Visitor for OptionVisitor<T> {
|
||||
type Value = Option<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<Option<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
|
||||
where E: Error,
|
||||
@@ -300,7 +315,7 @@ impl<T> Deserialize for Option<T> where T: Deserialize {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! set_impl {
|
||||
macro_rules! seq_impl {
|
||||
(
|
||||
$ty:ty,
|
||||
< $($constraints:ident),* >,
|
||||
@@ -310,11 +325,13 @@ macro_rules! set_impl {
|
||||
$with_capacity:expr,
|
||||
$insert:expr
|
||||
) => {
|
||||
/// A visitor that produces a sequence.
|
||||
pub struct $visitor_name<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> $visitor_name<T> {
|
||||
/// Construct a new sequence visitor.
|
||||
pub fn new() -> Self {
|
||||
$visitor_name {
|
||||
marker: PhantomData,
|
||||
@@ -362,7 +379,7 @@ macro_rules! set_impl {
|
||||
}
|
||||
}
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
BinaryHeap<T>,
|
||||
<Deserialize, Ord>,
|
||||
BinaryHeapVisitor,
|
||||
@@ -371,7 +388,7 @@ set_impl!(
|
||||
BinaryHeap::with_capacity(visitor.size_hint().0),
|
||||
BinaryHeap::push);
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
BTreeSet<T>,
|
||||
<Deserialize, Eq, Ord>,
|
||||
BTreeSetVisitor,
|
||||
@@ -381,7 +398,7 @@ set_impl!(
|
||||
BTreeSet::insert);
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
EnumSet<T>,
|
||||
<Deserialize, CLike>,
|
||||
EnumSetVisitor,
|
||||
@@ -390,7 +407,7 @@ set_impl!(
|
||||
EnumSet::new(),
|
||||
EnumSet::insert);
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
LinkedList<T>,
|
||||
<Deserialize>,
|
||||
LinkedListVisitor,
|
||||
@@ -399,7 +416,7 @@ set_impl!(
|
||||
LinkedList::new(),
|
||||
LinkedList::push_back);
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
HashSet<T>,
|
||||
<Deserialize, Eq, Hash>,
|
||||
HashSetVisitor,
|
||||
@@ -408,7 +425,7 @@ set_impl!(
|
||||
HashSet::with_capacity(visitor.size_hint().0),
|
||||
HashSet::insert);
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
Vec<T>,
|
||||
<Deserialize>,
|
||||
VecVisitor,
|
||||
@@ -417,7 +434,7 @@ set_impl!(
|
||||
Vec::with_capacity(visitor.size_hint().0),
|
||||
Vec::push);
|
||||
|
||||
set_impl!(
|
||||
seq_impl!(
|
||||
VecDeque<T>,
|
||||
<Deserialize>,
|
||||
VecDequeVisitor,
|
||||
@@ -433,6 +450,7 @@ struct ArrayVisitor0<T> {
|
||||
}
|
||||
|
||||
impl<T> ArrayVisitor0<T> {
|
||||
/// Construct a `ArrayVisitor0<T>`.
|
||||
pub fn new() -> Self {
|
||||
ArrayVisitor0 {
|
||||
marker: PhantomData,
|
||||
@@ -477,6 +495,7 @@ macro_rules! array_impls {
|
||||
}
|
||||
|
||||
impl<T> $visitor<T> {
|
||||
/// Construct a `ArrayVisitor*<T>`.
|
||||
pub fn new() -> Self {
|
||||
$visitor {
|
||||
marker: PhantomData
|
||||
@@ -566,6 +585,7 @@ macro_rules! tuple_impls {
|
||||
() => {};
|
||||
($($len:expr => $visitor:ident => ($($name:ident),+),)+) => {
|
||||
$(
|
||||
/// Construct a tuple visitor.
|
||||
pub struct $visitor<$($name,)+> {
|
||||
marker: PhantomData<($($name,)+)>,
|
||||
}
|
||||
@@ -573,6 +593,7 @@ macro_rules! tuple_impls {
|
||||
impl<
|
||||
$($name: Deserialize,)+
|
||||
> $visitor<$($name,)+> {
|
||||
/// Construct a `TupleVisitor*<T>`.
|
||||
pub fn new() -> Self {
|
||||
$visitor { marker: PhantomData }
|
||||
}
|
||||
@@ -643,11 +664,13 @@ macro_rules! map_impl {
|
||||
$with_capacity:expr,
|
||||
$insert:expr
|
||||
) => {
|
||||
/// A visitor that produces a map.
|
||||
pub struct $visitor_name<K, V> {
|
||||
marker: PhantomData<$ty>,
|
||||
}
|
||||
|
||||
impl<K, V> $visitor_name<K, V> {
|
||||
/// Construct a `MapVisitor*<T>`.
|
||||
pub fn new() -> Self {
|
||||
$visitor_name {
|
||||
marker: PhantomData,
|
||||
@@ -739,7 +762,7 @@ impl Deserialize for path::PathBuf {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(PathBufVisitor)
|
||||
deserializer.visit_string(PathBufVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -881,3 +904,87 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
||||
deserializer.visit_enum("Result", VARIANTS, Visitor(PhantomData))
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Deserialize for ::num::bigint::BigInt {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
use ::num::Num;
|
||||
use ::num::bigint::BigInt;
|
||||
|
||||
struct BigIntVisitor;
|
||||
|
||||
impl Visitor for BigIntVisitor {
|
||||
type Value = BigInt;
|
||||
|
||||
fn visit_str<E>(&mut self, s: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
match BigInt::from_str_radix(s, 10) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Error::invalid_value(&err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(BigIntVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Deserialize for ::num::bigint::BigUint {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
use ::num::Num;
|
||||
use ::num::bigint::BigUint;
|
||||
|
||||
struct BigUintVisitor;
|
||||
|
||||
impl Visitor for BigUintVisitor {
|
||||
type Value = ::num::bigint::BigUint;
|
||||
|
||||
fn visit_str<E>(&mut self, s: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
match BigUint::from_str_radix(s, 10) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Error::invalid_value(&err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(BigUintVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-complex")]
|
||||
impl<T> Deserialize for ::num::complex::Complex<T>
|
||||
where T: Deserialize + Clone + ::num::Num
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let (re, im) = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(::num::complex::Complex::new(re, im))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-rational")]
|
||||
impl<T> Deserialize for ::num::rational::Ratio<T>
|
||||
where T: Deserialize + Clone + ::num::Integer + PartialOrd
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let (numer, denom) = try!(Deserialize::deserialize(deserializer));
|
||||
if denom == ::num::Zero::zero() {
|
||||
Err(Error::invalid_value("denominator is zero"))
|
||||
} else {
|
||||
Ok(::num::rational::Ratio::new_raw(numer, denom))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+127
-18
@@ -21,6 +21,11 @@ pub trait Error: Sized {
|
||||
Error::syntax("incorrect type")
|
||||
}
|
||||
|
||||
/// Raised when a `Deserialize` was passed an incorrect value.
|
||||
fn invalid_value(msg: &str) -> Self {
|
||||
Error::syntax(msg)
|
||||
}
|
||||
|
||||
/// Raised when a `Deserialize` type unexpectedly hit the end of the stream.
|
||||
fn end_of_stream() -> Self;
|
||||
|
||||
@@ -34,40 +39,100 @@ pub trait Error: Sized {
|
||||
/// `Type` represents all the primitive types that can be deserialized. This is used by
|
||||
/// `Error::kind_mismatch`.
|
||||
pub enum Type {
|
||||
/// Represents a `bool` type.
|
||||
Bool,
|
||||
|
||||
/// Represents a `usize` type.
|
||||
Usize,
|
||||
|
||||
/// Represents a `u8` type.
|
||||
U8,
|
||||
|
||||
/// Represents a `u16` type.
|
||||
U16,
|
||||
|
||||
/// Represents a `u32` type.
|
||||
U32,
|
||||
|
||||
/// Represents a `u64` type.
|
||||
U64,
|
||||
|
||||
/// Represents a `isize` type.
|
||||
Isize,
|
||||
|
||||
/// Represents a `i8` type.
|
||||
I8,
|
||||
|
||||
/// Represents a `i16` type.
|
||||
I16,
|
||||
|
||||
/// Represents a `i32` type.
|
||||
I32,
|
||||
|
||||
/// Represents a `i64` type.
|
||||
I64,
|
||||
|
||||
/// Represents a `f32` type.
|
||||
F32,
|
||||
|
||||
/// Represents a `f64` type.
|
||||
F64,
|
||||
|
||||
/// Represents a `char` type.
|
||||
Char,
|
||||
|
||||
/// Represents a `&str` type.
|
||||
Str,
|
||||
|
||||
/// Represents a `String` type.
|
||||
String,
|
||||
|
||||
/// Represents a `()` type.
|
||||
Unit,
|
||||
|
||||
/// Represents an `Option<T>` type.
|
||||
Option,
|
||||
|
||||
/// Represents a sequence type.
|
||||
Seq,
|
||||
|
||||
/// Represents a map type.
|
||||
Map,
|
||||
|
||||
/// Represents a unit struct type.
|
||||
UnitStruct,
|
||||
|
||||
/// Represents a newtype type.
|
||||
NewtypeStruct,
|
||||
|
||||
/// Represents a tuple struct type.
|
||||
TupleStruct,
|
||||
|
||||
/// Represents a struct type.
|
||||
Struct,
|
||||
|
||||
/// Represents a tuple type.
|
||||
Tuple,
|
||||
|
||||
/// Represents an `enum` type.
|
||||
Enum,
|
||||
|
||||
/// Represents a struct variant.
|
||||
StructVariant,
|
||||
|
||||
/// Represents a tuple variant.
|
||||
TupleVariant,
|
||||
|
||||
/// Represents a unit variant.
|
||||
UnitVariant,
|
||||
|
||||
/// Represents a `&[u8]` type.
|
||||
Bytes,
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `Deserialize` represents a type that can be deserialized.
|
||||
pub trait Deserialize: Sized {
|
||||
/// Deserialize this value given this `Deserializer`.
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
@@ -89,6 +154,7 @@ pub trait Deserialize: Sized {
|
||||
/// supporting the `visit_*` types is that it does not allow for deserializing into a generic
|
||||
/// `json::Value`-esque type.
|
||||
pub trait Deserializer {
|
||||
/// The error type that can be returned if some error occurs during deserialization.
|
||||
type Error: Error;
|
||||
|
||||
/// This method walks a visitor through a value as it is being deserialized.
|
||||
@@ -337,6 +403,16 @@ pub trait Deserializer {
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting some sort of struct key mapping.
|
||||
/// This allows deserializers to choose between &str, usize, or &[u8] to properly deserialize a
|
||||
/// struct key.
|
||||
#[inline]
|
||||
fn visit_struct_field<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// Specify a format string for the deserializer.
|
||||
///
|
||||
/// The deserializer format is used to determine which format
|
||||
@@ -349,87 +425,103 @@ pub trait Deserializer {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// This trait represents a visitor that walks through a deserializer.
|
||||
pub trait Visitor {
|
||||
/// The value produced by this visitor.
|
||||
type Value: Deserialize;
|
||||
|
||||
/// `visit_bool` deserializes a `bool` into a `Value`.
|
||||
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Bool))
|
||||
}
|
||||
|
||||
/// `visit_isize` deserializes a `isize` into a `Value`.
|
||||
fn visit_isize<E>(&mut self, v: isize) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i8` deserializes a `i8` into a `Value`.
|
||||
fn visit_i8<E>(&mut self, v: i8) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i16` deserializes a `i16` into a `Value`.
|
||||
fn visit_i16<E>(&mut self, v: i16) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i32` deserializes a `i32` into a `Value`.
|
||||
fn visit_i32<E>(&mut self, v: i32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i64` deserializes a `i64` into a `Value`.
|
||||
fn visit_i64<E>(&mut self, _v: i64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::I64))
|
||||
}
|
||||
|
||||
/// `visit_usize` deserializes a `usize` into a `Value`.
|
||||
fn visit_usize<E>(&mut self, v: usize) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u8` deserializes a `u8` into a `Value`.
|
||||
fn visit_u8<E>(&mut self, v: u8) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u16` deserializes a `u16` into a `Value`.
|
||||
fn visit_u16<E>(&mut self, v: u16) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u32` deserializes a `u32` into a `Value`.
|
||||
fn visit_u32<E>(&mut self, v: u32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u64` deserializes a `u64` into a `Value`.
|
||||
fn visit_u64<E>(&mut self, _v: u64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::U64))
|
||||
}
|
||||
|
||||
/// `visit_f32` deserializes a `f32` into a `Value`.
|
||||
fn visit_f32<E>(&mut self, v: f32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
/// `visit_f64` deserializes a `f64` into a `Value`.
|
||||
fn visit_f64<E>(&mut self, _v: f64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::F64))
|
||||
}
|
||||
|
||||
/// `visit_char` deserializes a `char` into a `Value`.
|
||||
#[inline]
|
||||
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
@@ -439,12 +531,14 @@ pub trait Visitor {
|
||||
self.visit_string(v.to_string())
|
||||
}
|
||||
|
||||
/// `visit_str` deserializes a `&str` into a `Value`.
|
||||
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Str))
|
||||
}
|
||||
|
||||
/// `visit_string` deserializes a `String` into a `Value`.
|
||||
#[inline]
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
@@ -452,12 +546,14 @@ pub trait Visitor {
|
||||
self.visit_str(&v)
|
||||
}
|
||||
|
||||
/// `visit_unit` deserializes a `()` into a `Value`.
|
||||
fn visit_unit<E>(&mut self) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Unit))
|
||||
}
|
||||
|
||||
/// `visit_unit_struct` deserializes a unit struct into a `Value`.
|
||||
#[inline]
|
||||
fn visit_unit_struct<E>(&mut self, _name: &'static str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
@@ -465,42 +561,49 @@ pub trait Visitor {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
/// `visit_none` deserializes a none value into a `Value`.
|
||||
fn visit_none<E>(&mut self) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Option))
|
||||
}
|
||||
|
||||
/// `visit_some` deserializes a value into a `Value`.
|
||||
fn visit_some<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Option))
|
||||
}
|
||||
|
||||
/// `visit_newtype_struct` deserializes a value into a `Value`.
|
||||
fn visit_newtype_struct<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::NewtypeStruct))
|
||||
}
|
||||
|
||||
/// `visit_bool` deserializes a `SeqVisitor` into a `Value`.
|
||||
fn visit_seq<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Seq))
|
||||
}
|
||||
|
||||
/// `visit_map` deserializes a `MapVisitor` into a `Value`.
|
||||
fn visit_map<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Map))
|
||||
}
|
||||
|
||||
/// `visit_bytes` deserializes a `&[u8]` into a `Value`.
|
||||
fn visit_bytes<E>(&mut self, _v: &[u8]) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::type_mismatch(Type::Bytes))
|
||||
}
|
||||
|
||||
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.
|
||||
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
@@ -510,14 +613,23 @@ pub trait Visitor {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `SeqVisitor` visits each item in a sequence.
|
||||
///
|
||||
/// This is a trait that a `Deserializer` passes to a `Visitor` implementation, which deserializes
|
||||
/// each item in a sequence.
|
||||
pub trait SeqVisitor {
|
||||
/// The error type that can be returned if some error occurs during deserialization.
|
||||
type Error: Error;
|
||||
|
||||
/// This returns a `Ok(Some(value))` for the next value in the sequence, or `Ok(None)` if there
|
||||
/// are no more remaining items.
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Self::Error>
|
||||
where T: Deserialize;
|
||||
|
||||
/// This signals to the `SeqVisitor` that the `Visitor` does not expect any more items.
|
||||
fn end(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
/// Return the lower and upper bound of items remaining in the sequence.
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(0, None)
|
||||
@@ -547,9 +659,15 @@ impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `MapVisitor` visits each item in a sequence.
|
||||
///
|
||||
/// This is a trait that a `Deserializer` passes to a `Visitor` implementation.
|
||||
pub trait MapVisitor {
|
||||
/// The error type that can be returned if some error occurs during deserialization.
|
||||
type Error: Error;
|
||||
|
||||
/// This returns a `Ok(Some((key, value)))` for the next (key-value) pair in the map, or
|
||||
/// `Ok(None)` if there are no more remaining items.
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
|
||||
where K: Deserialize,
|
||||
@@ -564,19 +682,25 @@ pub trait MapVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// This returns a `Ok(Some(key))` for the next key in the map, or `Ok(None)` if there are no
|
||||
/// more remaining items.
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Self::Error>
|
||||
where K: Deserialize;
|
||||
|
||||
/// This returns a `Ok(value)` for the next value in the map.
|
||||
fn visit_value<V>(&mut self) -> Result<V, Self::Error>
|
||||
where V: Deserialize;
|
||||
|
||||
/// This signals to the `MapVisitor` that the `Visitor` does not expect any more items.
|
||||
fn end(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
/// Return the lower and upper bound of items remaining in the sequence.
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(0, None)
|
||||
}
|
||||
|
||||
/// Report that there
|
||||
fn missing_field<V>(&mut self, field: &'static str) -> Result<V, Self::Error>
|
||||
where V: Deserialize,
|
||||
{
|
||||
@@ -625,8 +749,10 @@ impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
||||
/// `EnumVisitor` is a visitor that is created by the `Deserialize` and passed to the
|
||||
/// `Deserializer` in order to deserialize enums.
|
||||
pub trait EnumVisitor {
|
||||
/// The value produced by this visitor.
|
||||
type Value;
|
||||
|
||||
/// Visit the specific variant with the `VariantVisitor`.
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: VariantVisitor;
|
||||
}
|
||||
@@ -636,6 +762,7 @@ pub trait EnumVisitor {
|
||||
/// `VariantVisitor` is a visitor that is created by the `Deserializer` and passed to the
|
||||
/// `Deserialize` in order to deserialize a specific enum variant.
|
||||
pub trait VariantVisitor {
|
||||
/// The error type that can be returned if some error occurs during deserialization.
|
||||
type Error: Error;
|
||||
|
||||
/// `visit_variant` is called to identify which variant to deserialize.
|
||||
@@ -711,21 +838,3 @@ impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
||||
(**self).visit_struct(fields, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait EnumSeqVisitor {
|
||||
type Value;
|
||||
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: SeqVisitor;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait EnumMapVisitor {
|
||||
type Value;
|
||||
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: MapVisitor;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
||||
|
||||
use std::collections::{
|
||||
BTreeMap,
|
||||
BTreeSet,
|
||||
@@ -16,11 +18,19 @@ use bytes;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// This represents all the possible errors that can occur using the `ValueDeserializer`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
/// The value had some syntatic error.
|
||||
SyntaxError,
|
||||
|
||||
/// EOF while deserializing a value.
|
||||
EndOfStreamError,
|
||||
|
||||
/// Unknown field in struct.
|
||||
UnknownFieldError(String),
|
||||
|
||||
/// Struct is missing a field.
|
||||
MissingFieldError(&'static str),
|
||||
}
|
||||
|
||||
@@ -33,9 +43,12 @@ impl de::Error for Error {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// This trait converts primitive types into a deserializer.
|
||||
pub trait ValueDeserializer {
|
||||
/// The actual deserializer type.
|
||||
type Deserializer: de::Deserializer<Error=Error>;
|
||||
|
||||
/// Convert this value into a deserializer.
|
||||
fn into_deserializer(self) -> Self::Deserializer;
|
||||
}
|
||||
|
||||
@@ -72,6 +85,7 @@ impl de::Deserializer for UnitDeserializer {
|
||||
|
||||
macro_rules! primitive_deserializer {
|
||||
($ty:ty, $name:ident, $method:ident) => {
|
||||
/// A helper deserializer that deserializes a number.
|
||||
pub struct $name(Option<$ty>);
|
||||
|
||||
impl ValueDeserializer for $ty {
|
||||
@@ -212,12 +226,14 @@ impl<'a> de::VariantVisitor for StringDeserializer {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A helper deserializer that deserializes a sequence.
|
||||
pub struct SeqDeserializer<I> {
|
||||
iter: I,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<I> SeqDeserializer<I> {
|
||||
/// Construct a new `SeqDeserializer<I>`.
|
||||
pub fn new(iter: I, len: usize) -> Self {
|
||||
SeqDeserializer {
|
||||
iter: iter,
|
||||
@@ -308,6 +324,7 @@ impl<T> ValueDeserializer for HashSet<T>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A helper deserializer that deserializes a map.
|
||||
pub struct MapDeserializer<I, K, V>
|
||||
where I: Iterator<Item=(K, V)>,
|
||||
K: ValueDeserializer,
|
||||
@@ -323,6 +340,7 @@ impl<I, K, V> MapDeserializer<I, K, V>
|
||||
K: ValueDeserializer,
|
||||
V: ValueDeserializer,
|
||||
{
|
||||
/// Construct a new `MapDeserializer<I, K, V>`.
|
||||
pub fn new(iter: I, len: usize) -> Self {
|
||||
MapDeserializer {
|
||||
iter: iter,
|
||||
@@ -429,6 +447,7 @@ impl<'a> ValueDeserializer for bytes::Bytes<'a>
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper deserializer that deserializes a `&[u8]`.
|
||||
pub struct BytesDeserializer<'a> (Option<&'a [u8]>);
|
||||
|
||||
impl<'a> de::Deserializer for BytesDeserializer<'a> {
|
||||
@@ -456,6 +475,7 @@ impl ValueDeserializer for bytes::ByteBuf
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper deserializer that deserializes a `Vec<u8>`.
|
||||
pub struct ByteBufDeserializer(Option<Vec<u8>>);
|
||||
|
||||
impl de::Deserializer for ByteBufDeserializer {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! Module that contains helper iterators.
|
||||
|
||||
use std::io;
|
||||
use std::iter::Peekable;
|
||||
|
||||
/// Iterator over a byte stream that tracks the current position's line and column.
|
||||
pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
||||
iter: Iter,
|
||||
line: usize,
|
||||
@@ -8,6 +11,7 @@ pub struct LineColIterator<Iter: Iterator<Item=io::Result<u8>>> {
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
||||
/// Construct a new `LineColIterator<Iter>`.
|
||||
pub fn new(iter: Iter) -> LineColIterator<Iter> {
|
||||
LineColIterator {
|
||||
iter: iter,
|
||||
|
||||
+10
-1
@@ -5,8 +5,17 @@
|
||||
//! handshake protocol between serializers and serializees can be completely optimized away,
|
||||
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific
|
||||
//! type.
|
||||
//!
|
||||
//! For a detailed tutorial on the different ways to use serde please check out the
|
||||
//! [github repository](https://github.com/serde-rs/serde)
|
||||
|
||||
#![doc(html_root_url="https://serde-rs.github.io/serde/serde")]
|
||||
#![cfg_attr(feature = "nightly", feature(collections, core, enumset, nonzero, step_trait, zero_one))]
|
||||
#![cfg_attr(feature = "nightly", feature(collections, enumset, nonzero, plugin, step_trait,
|
||||
zero_one))]
|
||||
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
||||
#![cfg_attr(feature = "nightly-testing", allow(linkedlist))]
|
||||
|
||||
#![deny(missing_docs)]
|
||||
|
||||
extern crate num;
|
||||
|
||||
|
||||
+85
-2
@@ -1,3 +1,5 @@
|
||||
//! Implementations for all of Rust's builtin types.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{
|
||||
BinaryHeap,
|
||||
@@ -117,6 +119,27 @@ impl<T> SeqVisitor for Option<T> where T: Serialize {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A `serde::Visitor` for sequence iterators.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use serde::{Serialize, Serializer};
|
||||
/// use serde::ser::impls::SeqIteratorVisitor;
|
||||
///
|
||||
/// struct Seq(Vec<u32>);
|
||||
///
|
||||
/// impl Serialize for Seq {
|
||||
/// fn serialize<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||
/// where S: Serializer,
|
||||
/// {
|
||||
/// ser.visit_seq(SeqIteratorVisitor::new(
|
||||
/// self.0.iter(),
|
||||
/// Some(self.0.len()),
|
||||
/// ))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub struct SeqIteratorVisitor<Iter> {
|
||||
iter: Iter,
|
||||
len: Option<usize>,
|
||||
@@ -125,6 +148,7 @@ pub struct SeqIteratorVisitor<Iter> {
|
||||
impl<T, Iter> SeqIteratorVisitor<Iter>
|
||||
where Iter: Iterator<Item=T>
|
||||
{
|
||||
/// Construct a new `SeqIteratorVisitor<Iter>`.
|
||||
#[inline]
|
||||
pub fn new(iter: Iter, len: Option<usize>) -> SeqIteratorVisitor<Iter> {
|
||||
SeqIteratorVisitor {
|
||||
@@ -332,12 +356,14 @@ macro_rules! tuple_impls {
|
||||
}
|
||||
)+) => {
|
||||
$(
|
||||
/// A tuple visitor.
|
||||
pub struct $TupleVisitor<'a, $($T: 'a),+> {
|
||||
tuple: &'a ($($T,)+),
|
||||
state: u8,
|
||||
}
|
||||
|
||||
impl<'a, $($T: 'a),+> $TupleVisitor<'a, $($T),+> {
|
||||
/// Construct a new, empty `TupleVisitor`.
|
||||
pub fn new(tuple: &'a ($($T,)+)) -> $TupleVisitor<'a, $($T),+> {
|
||||
$TupleVisitor {
|
||||
tuple: tuple,
|
||||
@@ -489,6 +515,28 @@ tuple_impls! {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A `serde::Visitor` for (key, value) map iterators.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::HashMap;
|
||||
/// use serde::{Serialize, Serializer};
|
||||
/// use serde::ser::impls::MapIteratorVisitor;
|
||||
///
|
||||
/// struct Map(HashMap<u32, u32>);
|
||||
///
|
||||
/// impl Serialize for Map {
|
||||
/// fn serialize<S>(&self, ser: &mut S) -> Result<(), S::Error>
|
||||
/// where S: Serializer,
|
||||
/// {
|
||||
/// ser.visit_map(MapIteratorVisitor::new(
|
||||
/// self.0.iter(),
|
||||
/// Some(self.0.len()),
|
||||
/// ))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub struct MapIteratorVisitor<Iter> {
|
||||
iter: Iter,
|
||||
len: Option<usize>,
|
||||
@@ -497,6 +545,7 @@ pub struct MapIteratorVisitor<Iter> {
|
||||
impl<K, V, Iter> MapIteratorVisitor<Iter>
|
||||
where Iter: Iterator<Item=(K, V)>
|
||||
{
|
||||
/// Construct a new `MapIteratorVisitor<Iter>`.
|
||||
#[inline]
|
||||
pub fn new(iter: Iter, len: Option<usize>) -> MapIteratorVisitor<Iter> {
|
||||
MapIteratorVisitor {
|
||||
@@ -517,8 +566,8 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
let value = try!(serializer.visit_map_elt(key, value));
|
||||
Ok(Some(value))
|
||||
try!(serializer.visit_map_elt(key, value));
|
||||
Ok(Some(()))
|
||||
}
|
||||
None => Ok(None)
|
||||
}
|
||||
@@ -651,3 +700,37 @@ impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Serialize for ::num::bigint::BigInt {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
self.to_str_radix(10).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-bigint")]
|
||||
impl Serialize for ::num::bigint::BigUint {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
self.to_str_radix(10).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-complex")]
|
||||
impl<T> Serialize for ::num::complex::Complex<T>
|
||||
where T: Serialize + Clone + ::num::Num
|
||||
{
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
(&self.re, &self.im).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "num-rational")]
|
||||
impl<T> Serialize for ::num::rational::Ratio<T>
|
||||
where T: Serialize + Clone + ::num::Integer + PartialOrd
|
||||
{
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
(self.numer(), self.denom()).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,18 @@ pub mod impls;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A trait that describes a type that can be serialized by a `Serializer`.
|
||||
pub trait Serialize {
|
||||
/// Serializes this value into this serializer.
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A trait that describes a type that can serialize a stream of values into the underlying format.
|
||||
pub trait Serializer {
|
||||
/// The error type that can be returned if some error occurs during serialization.
|
||||
type Error;
|
||||
|
||||
/// `visit_bool` serializes a `bool` value.
|
||||
@@ -110,13 +114,20 @@ pub trait Serializer {
|
||||
self.visit_seq(impls::SeqIteratorVisitor::new(value.iter(), Some(value.len())))
|
||||
}
|
||||
|
||||
/// Serializes a `()` value.
|
||||
fn visit_unit(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a unit struct value.
|
||||
///
|
||||
/// By default, unit structs are serialized as a `()`.
|
||||
#[inline]
|
||||
fn visit_unit_struct(&mut self, _name: &'static str) -> Result<(), Self::Error> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
/// Serializes a unit variant, otherwise known as a variant with no arguments.
|
||||
///
|
||||
/// By default, unit variants are serialized as a `()`.
|
||||
#[inline]
|
||||
fn visit_unit_variant(&mut self,
|
||||
_name: &'static str,
|
||||
@@ -155,17 +166,27 @@ pub trait Serializer {
|
||||
Some(value))
|
||||
}
|
||||
|
||||
/// Serializes a `None` value.
|
||||
fn visit_none(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a `Some(...)` value.
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), Self::Error>
|
||||
where V: Serialize;
|
||||
|
||||
/// Serializes a sequence.
|
||||
///
|
||||
/// Callees of this method need to construct a `SeqVisitor`, which iterates through each item
|
||||
/// in the sequence.
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor;
|
||||
|
||||
/// Serializes a sequence element.
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize;
|
||||
|
||||
/// Serializes a tuple.
|
||||
///
|
||||
/// By default this serializes a tuple as a sequence.
|
||||
#[inline]
|
||||
fn visit_tuple<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor,
|
||||
@@ -173,6 +194,9 @@ pub trait Serializer {
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
/// Serializes a tuple element.
|
||||
///
|
||||
/// By default, tuples are serialized as a sequence.
|
||||
#[inline]
|
||||
fn visit_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
@@ -180,6 +204,9 @@ pub trait Serializer {
|
||||
self.visit_seq_elt(value)
|
||||
}
|
||||
|
||||
/// Serializes a tuple struct.
|
||||
///
|
||||
/// By default, tuple structs are serialized as a tuple.
|
||||
#[inline]
|
||||
fn visit_tuple_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
@@ -189,6 +216,9 @@ pub trait Serializer {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
/// Serializes a tuple struct element.
|
||||
///
|
||||
/// By default, tuple struct elements are serialized as a tuple element.
|
||||
#[inline]
|
||||
fn visit_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
@@ -196,6 +226,9 @@ pub trait Serializer {
|
||||
self.visit_tuple_elt(value)
|
||||
}
|
||||
|
||||
/// Serializes a tuple variant.
|
||||
///
|
||||
/// By default, tuple variants are serialized as a tuple struct.
|
||||
#[inline]
|
||||
fn visit_tuple_variant<V>(&mut self,
|
||||
_name: &'static str,
|
||||
@@ -207,6 +240,9 @@ pub trait Serializer {
|
||||
self.visit_tuple_struct(variant, visitor)
|
||||
}
|
||||
|
||||
/// Serializes a tuple element.
|
||||
///
|
||||
/// By default, tuples are serialized as a sequence.
|
||||
#[inline]
|
||||
fn visit_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
@@ -214,13 +250,21 @@ pub trait Serializer {
|
||||
self.visit_tuple_struct_elt(value)
|
||||
}
|
||||
|
||||
/// Serializes a map.
|
||||
///
|
||||
/// Callees of this method need to construct a `MapVisitor`, which iterates through each item
|
||||
/// in the map.
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor;
|
||||
|
||||
/// Serializes a map element (key-value pair).
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize;
|
||||
|
||||
/// Serializes a struct.
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
#[inline]
|
||||
fn visit_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
@@ -230,6 +274,9 @@ pub trait Serializer {
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
/// Serializes an element of a struct.
|
||||
///
|
||||
/// By default, struct elements are serialized as a map element with the field name as the key.
|
||||
#[inline]
|
||||
fn visit_struct_elt<V>(&mut self,
|
||||
key: &'static str,
|
||||
@@ -239,6 +286,9 @@ pub trait Serializer {
|
||||
self.visit_map_elt(key, value)
|
||||
}
|
||||
|
||||
/// Serializes a struct variant.
|
||||
///
|
||||
/// By default, struct variants are serialized as a struct.
|
||||
#[inline]
|
||||
fn visit_struct_variant<V>(&mut self,
|
||||
_name: &'static str,
|
||||
@@ -250,6 +300,9 @@ pub trait Serializer {
|
||||
self.visit_struct(variant, visitor)
|
||||
}
|
||||
|
||||
/// Serializes an element of a struct variant.
|
||||
///
|
||||
/// By default, struct variant elements are serialized as a struct element.
|
||||
#[inline]
|
||||
fn visit_struct_variant_elt<V>(&mut self,
|
||||
key: &'static str,
|
||||
@@ -268,7 +321,13 @@ pub trait Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that is used by a `Serialize` to iterate through a sequence.
|
||||
#[cfg_attr(feature = "nightly-testing", allow(len_without_is_empty))]
|
||||
pub trait SeqVisitor {
|
||||
/// Serializes a sequence item in the serializer.
|
||||
///
|
||||
/// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when
|
||||
/// complete.
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
@@ -279,7 +338,13 @@ pub trait SeqVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that is used by a `Serialize` to iterate through a map.
|
||||
#[cfg_attr(feature = "nightly-testing", allow(len_without_is_empty))]
|
||||
pub trait MapVisitor {
|
||||
/// Serializes a map item in the serializer.
|
||||
///
|
||||
/// This returns `Ok(Some(()))` when there are more items to serialize, or `Ok(None)` when
|
||||
/// complete.
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_codegen"
|
||||
version = "0.5.3"
|
||||
version = "0.6.14"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -12,15 +12,17 @@ keywords = ["serde", "serialization"]
|
||||
[features]
|
||||
default = ["with-syntex"]
|
||||
nightly = ["quasi_macros"]
|
||||
nightly-testing = ["clippy"]
|
||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
||||
|
||||
[build-dependencies]
|
||||
quasi_codegen = { verision = "*", optional = true }
|
||||
syntex = { version = "*", optional = true }
|
||||
quasi_codegen = { version = "^0.7.0", optional = true }
|
||||
syntex = { version = "^0.29.0", optional = true }
|
||||
|
||||
[dependencies]
|
||||
aster = { version = "*", default-features = false }
|
||||
quasi = { verision = "*", default-features = false }
|
||||
quasi_macros = { version = "*", optional = true }
|
||||
syntex = { version = "*", optional = true }
|
||||
syntex_syntax = { version = "*", optional = true }
|
||||
aster = { version = "^0.13.1", default-features = false }
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
quasi = { version = "^0.7.0", default-features = false }
|
||||
quasi_macros = { version = "^0.7.0", optional = true }
|
||||
syntex = { version = "^0.29.0", optional = true }
|
||||
syntex_syntax = { version = "^0.29.0", optional = true }
|
||||
|
||||
+241
-44
@@ -2,10 +2,15 @@ use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::print::pprust::meta_item_to_string;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
/// Represents field name information
|
||||
#[derive(Debug)]
|
||||
pub enum FieldNames {
|
||||
Global(P<ast::Expr>),
|
||||
Format{
|
||||
@@ -15,47 +20,20 @@ pub enum FieldNames {
|
||||
}
|
||||
|
||||
/// Represents field attribute information
|
||||
#[derive(Debug)]
|
||||
pub struct FieldAttrs {
|
||||
skip_serializing_field: bool,
|
||||
skip_serializing_field_if_empty: bool,
|
||||
skip_serializing_field_if_none: bool,
|
||||
names: FieldNames,
|
||||
use_default: bool,
|
||||
}
|
||||
|
||||
impl FieldAttrs {
|
||||
/// Create a FieldAttr with a single default field name
|
||||
pub fn new(
|
||||
skip_serializing_field: bool,
|
||||
default_value: bool,
|
||||
name: P<ast::Expr>,
|
||||
) -> FieldAttrs {
|
||||
FieldAttrs {
|
||||
skip_serializing_field: skip_serializing_field,
|
||||
names: FieldNames::Global(name),
|
||||
use_default: default_value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a FieldAttr with format specific field names
|
||||
pub fn new_with_formats(
|
||||
skip_serializing_field: bool,
|
||||
default_value: bool,
|
||||
default_name: P<ast::Expr>,
|
||||
formats: HashMap<P<ast::Expr>, P<ast::Expr>>,
|
||||
) -> FieldAttrs {
|
||||
FieldAttrs {
|
||||
skip_serializing_field: skip_serializing_field,
|
||||
names: FieldNames::Format {
|
||||
formats: formats,
|
||||
default: default_name,
|
||||
},
|
||||
use_default: default_value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a set of formats that the field has attributes for.
|
||||
pub fn formats(&self) -> HashSet<P<ast::Expr>> {
|
||||
match self.names {
|
||||
FieldNames::Format{ref formats, default: _} => {
|
||||
FieldNames::Format { ref formats, .. } => {
|
||||
let mut set = HashSet::new();
|
||||
for (fmt, _) in formats.iter() {
|
||||
set.insert(fmt.clone());
|
||||
@@ -70,22 +48,21 @@ impl FieldAttrs {
|
||||
///
|
||||
/// The resulting expression assumes that `S` refers to a type
|
||||
/// that implements `Serializer`.
|
||||
pub fn serializer_key_expr(self, cx: &ExtCtxt) -> P<ast::Expr> {
|
||||
pub fn serializer_key_expr(&self, cx: &ExtCtxt) -> P<ast::Expr> {
|
||||
match self.names {
|
||||
FieldNames::Global(x) => x,
|
||||
FieldNames::Format{formats, default} => {
|
||||
FieldNames::Global(ref name) => name.clone(),
|
||||
FieldNames::Format { ref formats, ref default } => {
|
||||
let arms = formats.iter()
|
||||
.map(|(fmt, lit)| {
|
||||
quote_arm!(cx, $fmt => { $lit })
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
quote_expr!(cx,
|
||||
{
|
||||
match S::format() {
|
||||
$arms
|
||||
_ => { $default }
|
||||
}
|
||||
})
|
||||
match S::format() {
|
||||
$arms
|
||||
_ => { $default }
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -94,17 +71,17 @@ impl FieldAttrs {
|
||||
pub fn default_key_expr(&self) -> &P<ast::Expr> {
|
||||
match self.names {
|
||||
FieldNames::Global(ref expr) => expr,
|
||||
FieldNames::Format{formats: _, ref default} => default
|
||||
FieldNames::Format { ref default, .. } => default,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the field name for the field in the specified format.
|
||||
pub fn key_expr(&self, format: &P<ast::Expr>) -> &P<ast::Expr> {
|
||||
match self.names {
|
||||
FieldNames::Global(ref expr) =>
|
||||
expr,
|
||||
FieldNames::Format{ref formats, ref default} =>
|
||||
FieldNames::Global(ref expr) => expr,
|
||||
FieldNames::Format { ref formats, ref default } => {
|
||||
formats.get(format).unwrap_or(default)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,4 +94,224 @@ impl FieldAttrs {
|
||||
pub fn skip_serializing_field(&self) -> bool {
|
||||
self.skip_serializing_field
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field_if_empty(&self) -> bool {
|
||||
self.skip_serializing_field_if_empty
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field_if_none(&self) -> bool {
|
||||
self.skip_serializing_field_if_none
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FieldAttrsBuilder<'a> {
|
||||
cx: &'a ExtCtxt<'a>,
|
||||
builder: &'a aster::AstBuilder,
|
||||
skip_serializing_field: bool,
|
||||
skip_serializing_field_if_empty: bool,
|
||||
skip_serializing_field_if_none: bool,
|
||||
name: Option<P<ast::Expr>>,
|
||||
format_rename: HashMap<P<ast::Expr>, P<ast::Expr>>,
|
||||
use_default: bool,
|
||||
}
|
||||
|
||||
impl<'a> FieldAttrsBuilder<'a> {
|
||||
pub fn new(cx: &'a ExtCtxt<'a>,
|
||||
builder: &'a aster::AstBuilder) -> FieldAttrsBuilder<'a> {
|
||||
FieldAttrsBuilder {
|
||||
cx: cx,
|
||||
builder: builder,
|
||||
skip_serializing_field: false,
|
||||
skip_serializing_field_if_empty: false,
|
||||
skip_serializing_field_if_none: false,
|
||||
name: None,
|
||||
format_rename: HashMap::new(),
|
||||
use_default: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn field(mut self, field: &ast::StructField) -> Result<FieldAttrsBuilder<'a>, ()> {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => {
|
||||
self.name = Some(self.builder.expr().str(name));
|
||||
}
|
||||
ast::UnnamedField(_) => { }
|
||||
};
|
||||
|
||||
self.attrs(&field.node.attrs)
|
||||
}
|
||||
|
||||
pub fn attrs(mut self, attrs: &[ast::Attribute]) -> Result<FieldAttrsBuilder<'a>, ()> {
|
||||
for attr in attrs {
|
||||
self = try!(self.attr(attr));
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn attr(mut self, attr: &ast::Attribute) -> Result<FieldAttrsBuilder<'a>, ()> {
|
||||
match attr.node.value.node {
|
||||
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
||||
attr::mark_used(&attr);
|
||||
for item in items {
|
||||
self = try!(self.meta_item(item));
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
_ => {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn meta_item(mut self, meta_item: &P<ast::MetaItem>) -> Result<FieldAttrsBuilder<'a>, ()> {
|
||||
match meta_item.node {
|
||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"rename" => {
|
||||
let expr = self.builder.expr().build_lit(P(lit.clone()));
|
||||
|
||||
Ok(self.name(expr))
|
||||
}
|
||||
ast::MetaItemKind::List(ref name, ref items) if name == &"rename" => {
|
||||
for item in items {
|
||||
match item.node {
|
||||
ast::MetaItemKind::NameValue(ref name, ref lit) => {
|
||||
let name = self.builder.expr().str(name);
|
||||
let expr = self.builder.expr().build_lit(P(lit.clone()));
|
||||
|
||||
self = self.format_rename(name, expr);
|
||||
}
|
||||
_ => { }
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
ast::MetaItemKind::Word(ref name) if name == &"default" => {
|
||||
Ok(self.default())
|
||||
}
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing" => {
|
||||
Ok(self.skip_serializing_field())
|
||||
}
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing_if_empty" => {
|
||||
Ok(self.skip_serializing_field_if_empty())
|
||||
}
|
||||
ast::MetaItemKind::Word(ref name) if name == &"skip_serializing_if_none" => {
|
||||
Ok(self.skip_serializing_field_if_none())
|
||||
}
|
||||
_ => {
|
||||
self.cx.span_err(
|
||||
meta_item.span,
|
||||
&format!("unknown serde field attribute `{}`",
|
||||
meta_item_to_string(meta_item)));
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field(mut self) -> FieldAttrsBuilder<'a> {
|
||||
self.skip_serializing_field = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field_if_empty(mut self) -> FieldAttrsBuilder<'a> {
|
||||
self.skip_serializing_field_if_empty = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn skip_serializing_field_if_none(mut self) -> FieldAttrsBuilder<'a> {
|
||||
self.skip_serializing_field_if_none = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn name(mut self, name: P<ast::Expr>) -> FieldAttrsBuilder<'a> {
|
||||
self.name = Some(name);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn format_rename(mut self, format: P<ast::Expr>, name: P<ast::Expr>) -> FieldAttrsBuilder<'a> {
|
||||
self.format_rename.insert(format, name);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn default(mut self) -> FieldAttrsBuilder<'a> {
|
||||
self.use_default = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> FieldAttrs {
|
||||
let name = self.name.expect("here");
|
||||
let names = if self.format_rename.is_empty() {
|
||||
FieldNames::Global(name)
|
||||
} else {
|
||||
FieldNames::Format {
|
||||
formats: self.format_rename,
|
||||
default: name,
|
||||
}
|
||||
};
|
||||
|
||||
FieldAttrs {
|
||||
skip_serializing_field: self.skip_serializing_field,
|
||||
skip_serializing_field_if_empty: self.skip_serializing_field_if_empty,
|
||||
skip_serializing_field_if_none: self.skip_serializing_field_if_none,
|
||||
names: names,
|
||||
use_default: self.use_default,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents container (e.g. struct) attribute information
|
||||
#[derive(Debug)]
|
||||
pub struct ContainerAttrs;
|
||||
|
||||
pub struct ContainerAttrsBuilder<'a> {
|
||||
cx: &'a ExtCtxt<'a>,
|
||||
}
|
||||
|
||||
impl<'a> ContainerAttrsBuilder<'a> {
|
||||
pub fn new(cx: &'a ExtCtxt) -> Self {
|
||||
ContainerAttrsBuilder {
|
||||
cx: cx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn attrs(mut self, attrs: &[ast::Attribute]) -> Result<Self, ()> {
|
||||
for attr in attrs {
|
||||
self = try!(self.attr(attr));
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn attr(mut self, attr: &ast::Attribute) -> Result<Self, ()> {
|
||||
match attr.node.value.node {
|
||||
ast::MetaItemKind::List(ref name, ref items) if name == &"serde" => {
|
||||
attr::mark_used(&attr);
|
||||
for item in items {
|
||||
self = try!(self.meta_item(item));
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
_ => {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn meta_item(self, meta_item: &P<ast::MetaItem>) -> Result<Self, ()> {
|
||||
match meta_item.node {
|
||||
_ => {
|
||||
self.cx.span_err(
|
||||
meta_item.span,
|
||||
&format!("unknown serde container attribute `{}`",
|
||||
meta_item_to_string(meta_item)));
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> ContainerAttrs {
|
||||
ContainerAttrs
|
||||
}
|
||||
}
|
||||
|
||||
+199
-190
@@ -4,17 +4,14 @@ use aster;
|
||||
|
||||
use syntax::ast::{
|
||||
self,
|
||||
Ident,
|
||||
MetaItem,
|
||||
Item,
|
||||
Expr,
|
||||
StructDef,
|
||||
EnumDef,
|
||||
Ident,
|
||||
Item,
|
||||
MetaItem,
|
||||
};
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::owned_slice::OwnedSlice;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use attr;
|
||||
@@ -32,7 +29,7 @@ pub fn expand_derive_deserialize(
|
||||
_ => {
|
||||
cx.span_err(
|
||||
meta_item.span,
|
||||
"`derive` may only be applied to structs and enums");
|
||||
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -40,9 +37,14 @@ pub fn expand_derive_deserialize(
|
||||
let builder = aster::AstBuilder::new().span(span);
|
||||
|
||||
let generics = match item.node {
|
||||
ast::ItemStruct(_, ref generics) => generics,
|
||||
ast::ItemEnum(_, ref generics) => generics,
|
||||
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
||||
ast::ItemKind::Struct(_, ref generics) => generics,
|
||||
ast::ItemKind::Enum(_, ref generics) => generics,
|
||||
_ => {
|
||||
cx.span_err(
|
||||
meta_item.span,
|
||||
"`#[derive(Deserialize)]` may only be applied to structs and enums");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let impl_generics = builder.from_generics(generics.clone())
|
||||
@@ -55,18 +57,17 @@ pub fn expand_derive_deserialize(
|
||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||
.build();
|
||||
|
||||
let body = deserialize_body(
|
||||
cx,
|
||||
&builder,
|
||||
&item,
|
||||
&impl_generics,
|
||||
ty.clone(),
|
||||
);
|
||||
let body = match deserialize_body(cx, &builder, &item, &impl_generics, ty.clone()) {
|
||||
Ok(body) => body,
|
||||
Err(()) => {
|
||||
// An error occured, but it should have been reported already.
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
#[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,
|
||||
@@ -85,19 +86,24 @@ fn deserialize_body(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
// Note: While we don't have any container attributes, we still want to try to
|
||||
// parse them so we can report a proper error if we get passed an unknown attribute.
|
||||
let _ = try!(field::container_attrs(cx, item));
|
||||
|
||||
match item.node {
|
||||
ast::ItemStruct(ref struct_def, _) => {
|
||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
||||
deserialize_item_struct(
|
||||
cx,
|
||||
builder,
|
||||
item,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
item.span,
|
||||
variant_data,
|
||||
)
|
||||
}
|
||||
ast::ItemEnum(ref enum_def, _) => {
|
||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
||||
deserialize_item_enum(
|
||||
cx,
|
||||
builder,
|
||||
@@ -107,7 +113,10 @@ fn deserialize_body(
|
||||
enum_def,
|
||||
)
|
||||
}
|
||||
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
||||
_ => {
|
||||
cx.span_bug(item.span,
|
||||
"expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,27 +126,18 @@ fn deserialize_item_struct(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
let mut named_fields = vec![];
|
||||
let mut unnamed_fields = 0;
|
||||
|
||||
for field in struct_def.fields.iter() {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => { named_fields.push(name); }
|
||||
ast::UnnamedField(_) => { unnamed_fields += 1; }
|
||||
}
|
||||
}
|
||||
|
||||
match (named_fields.is_empty(), unnamed_fields) {
|
||||
(true, 0) => {
|
||||
span: Span,
|
||||
variant_data: &ast::VariantData,
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
match *variant_data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
deserialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, 1) => {
|
||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||
deserialize_newtype_struct(
|
||||
cx,
|
||||
&builder,
|
||||
@@ -146,29 +146,34 @@ fn deserialize_item_struct(
|
||||
ty,
|
||||
)
|
||||
}
|
||||
(true, _) => {
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
||||
cx.span_bug(span, "tuple struct has named fields")
|
||||
}
|
||||
|
||||
deserialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
unnamed_fields,
|
||||
fields.len(),
|
||||
)
|
||||
}
|
||||
(false, 0) => {
|
||||
ast::VariantData::Struct(ref fields, _) => {
|
||||
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
||||
cx.span_bug(span, "struct has unnamed fields")
|
||||
}
|
||||
|
||||
deserialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
fields,
|
||||
)
|
||||
}
|
||||
(false, _) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,14 +184,14 @@ fn deserialize_visitor(
|
||||
trait_generics: &ast::Generics,
|
||||
forward_ty_params: Vec<ast::TyParam>,
|
||||
forward_tys: Vec<P<ast::Ty>>
|
||||
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics) {
|
||||
) -> Result<(P<ast::Item>, P<ast::Ty>, P<ast::Expr>, ast::Generics), ()> {
|
||||
if trait_generics.ty_params.is_empty() && forward_tys.is_empty() {
|
||||
(
|
||||
Ok((
|
||||
builder.item().tuple_struct("__Visitor").build(),
|
||||
builder.ty().id("__Visitor"),
|
||||
builder.expr().id("__Visitor"),
|
||||
trait_generics.clone(),
|
||||
)
|
||||
))
|
||||
} else {
|
||||
let placeholders : Vec<_> = trait_generics.ty_params.iter()
|
||||
.map(|t| builder.ty().id(t.ident))
|
||||
@@ -194,9 +199,9 @@ fn deserialize_visitor(
|
||||
let mut trait_generics = trait_generics.clone();
|
||||
let mut ty_params = forward_ty_params.clone();
|
||||
ty_params.extend(trait_generics.ty_params.into_vec());
|
||||
trait_generics.ty_params = OwnedSlice::from_vec(ty_params);
|
||||
trait_generics.ty_params = P::from_vec(ty_params);
|
||||
|
||||
(
|
||||
Ok((
|
||||
builder.item().tuple_struct("__Visitor")
|
||||
.generics().with(trait_generics.clone()).build()
|
||||
.with_tys({
|
||||
@@ -234,7 +239,7 @@ fn deserialize_visitor(
|
||||
})
|
||||
.build(),
|
||||
trait_generics,
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,10 +263,10 @@ fn deserialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
struct __Visitor;
|
||||
|
||||
impl ::serde::de::Visitor for __Visitor {
|
||||
@@ -284,7 +289,7 @@ fn deserialize_unit_struct(
|
||||
}
|
||||
|
||||
deserializer.visit_unit_struct($type_name, __Visitor)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct(
|
||||
@@ -293,16 +298,15 @@ fn deserialize_newtype_struct(
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||
deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
));
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
cx,
|
||||
@@ -313,7 +317,7 @@ fn deserialize_newtype_struct(
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
@@ -336,7 +340,7 @@ fn deserialize_newtype_struct(
|
||||
}
|
||||
|
||||
deserializer.visit_newtype_struct($type_name, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct(
|
||||
@@ -346,16 +350,15 @@ fn deserialize_tuple_struct(
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||
deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
));
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
cx,
|
||||
@@ -366,7 +369,7 @@ fn deserialize_tuple_struct(
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
@@ -381,7 +384,7 @@ fn deserialize_tuple_struct(
|
||||
}
|
||||
|
||||
deserializer.visit_tuple_struct($type_name, $fields, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_seq(
|
||||
@@ -390,7 +393,7 @@ fn deserialize_seq(
|
||||
struct_path: ast::Path,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
let let_values: Vec<P<ast::Stmt>> = (0 .. fields)
|
||||
let let_values: Vec<ast::Stmt> = (0 .. fields)
|
||||
.map(|i| {
|
||||
let name = builder.id(format!("__field{}", i));
|
||||
quote_stmt!(cx,
|
||||
@@ -422,9 +425,9 @@ fn deserialize_struct_as_seq(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
struct_def: &StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
let let_values: Vec<P<ast::Stmt>> = (0 .. struct_def.fields.len())
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let let_values: Vec<ast::Stmt> = (0 .. fields.len())
|
||||
.map(|i| {
|
||||
let name = builder.id(format!("__field{}", i));
|
||||
quote_stmt!(cx,
|
||||
@@ -440,13 +443,15 @@ fn deserialize_struct_as_seq(
|
||||
|
||||
let result = builder.expr().struct_path(struct_path)
|
||||
.with_id_exprs(
|
||||
struct_def.fields.iter()
|
||||
fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, field)| {
|
||||
(
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => name.clone(),
|
||||
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
||||
ast::UnnamedField(_) => {
|
||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||
}
|
||||
},
|
||||
builder.expr().id(format!("__field{}", i)),
|
||||
)
|
||||
@@ -454,13 +459,13 @@ fn deserialize_struct_as_seq(
|
||||
)
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$let_values
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_struct(
|
||||
@@ -469,36 +474,36 @@ fn deserialize_struct(
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = deserialize_visitor(
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
&impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
));
|
||||
|
||||
let type_path = builder.path().id(type_ident).build();
|
||||
|
||||
let visit_seq_expr = deserialize_struct_as_seq(
|
||||
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
struct_def
|
||||
);
|
||||
fields,
|
||||
));
|
||||
|
||||
let (field_visitor, fields_stmt, visit_map_expr) = deserialize_struct_visitor(
|
||||
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_def,
|
||||
type_path.clone()
|
||||
);
|
||||
type_path.clone(),
|
||||
fields,
|
||||
));
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$field_visitor
|
||||
|
||||
$visitor_item
|
||||
@@ -524,7 +529,7 @@ fn deserialize_struct(
|
||||
$fields_stmt
|
||||
|
||||
deserializer.visit_struct($type_name, FIELDS, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_item_enum(
|
||||
@@ -534,7 +539,7 @@ fn deserialize_item_enum(
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
enum_def: &EnumDef,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
@@ -543,15 +548,17 @@ fn deserialize_item_enum(
|
||||
cx,
|
||||
builder,
|
||||
enum_def.variants.iter()
|
||||
.map(|variant|
|
||||
attr::FieldAttrs::new(
|
||||
false,
|
||||
true,
|
||||
builder.expr().str(variant.node.name)))
|
||||
.map(|variant| {
|
||||
let expr = builder.expr().str(variant.node.name);
|
||||
attr::FieldAttrsBuilder::new(cx, builder)
|
||||
.name(expr)
|
||||
.default()
|
||||
.build()
|
||||
})
|
||||
.collect()
|
||||
);
|
||||
|
||||
let variants_expr = builder.expr().addr_of().slice()
|
||||
let variants_expr = builder.expr().ref_().slice()
|
||||
.with_exprs(
|
||||
enum_def.variants.iter()
|
||||
.map(|variant| {
|
||||
@@ -565,35 +572,33 @@ fn deserialize_item_enum(
|
||||
).unwrap();
|
||||
|
||||
// Match arms to extract a variant from a string
|
||||
let variant_arms: Vec<_> = enum_def.variants.iter()
|
||||
.enumerate()
|
||||
.map(|(i, variant)| {
|
||||
let variant_name = builder.pat().enum_()
|
||||
.id("__Field").id(format!("__field{}", i)).build()
|
||||
.build();
|
||||
let mut variant_arms = vec![];
|
||||
for (i, variant) in enum_def.variants.iter().enumerate() {
|
||||
let variant_name = builder.pat().path()
|
||||
.id("__Field").id(format!("__field{}", i))
|
||||
.build();
|
||||
|
||||
let expr = deserialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
impl_generics,
|
||||
ty.clone(),
|
||||
variant,
|
||||
);
|
||||
|
||||
quote_arm!(cx, $variant_name => { $expr })
|
||||
})
|
||||
.collect();
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||
deserialize_visitor(
|
||||
let expr = try!(deserialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
ty.clone(),
|
||||
variant,
|
||||
));
|
||||
|
||||
quote_expr!(cx, {
|
||||
let arm = quote_arm!(cx, $variant_name => { $expr });
|
||||
variant_arms.push(arm);
|
||||
}
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
));
|
||||
|
||||
Ok(quote_expr!(cx, {
|
||||
$variant_visitor
|
||||
|
||||
$visitor_item
|
||||
@@ -613,7 +618,7 @@ fn deserialize_item_enum(
|
||||
$variants_stmt
|
||||
|
||||
deserializer.visit_enum($type_name, VARIANTS, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_variant(
|
||||
@@ -623,23 +628,23 @@ fn deserialize_variant(
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
variant: &ast::Variant,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let variant_ident = variant.node.name;
|
||||
|
||||
match variant.node.kind {
|
||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
||||
quote_expr!(cx, {
|
||||
match variant.node.data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
Ok(quote_expr!(cx, {
|
||||
try!(visitor.visit_unit());
|
||||
Ok($type_ident::$variant_ident)
|
||||
})
|
||||
}))
|
||||
}
|
||||
ast::TupleVariantKind(ref args) if args.len() == 1 => {
|
||||
quote_expr!(cx, {
|
||||
ast::VariantData::Tuple(ref args, _) if args.len() == 1 => {
|
||||
Ok(quote_expr!(cx, {
|
||||
let val = try!(visitor.visit_newtype());
|
||||
Ok($type_ident::$variant_ident(val))
|
||||
})
|
||||
}))
|
||||
}
|
||||
ast::TupleVariantKind(ref args) => {
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
deserialize_tuple_variant(
|
||||
cx,
|
||||
builder,
|
||||
@@ -647,10 +652,10 @@ fn deserialize_variant(
|
||||
variant_ident,
|
||||
generics,
|
||||
ty,
|
||||
args.len(),
|
||||
fields.len(),
|
||||
)
|
||||
}
|
||||
ast::StructVariantKind(ref struct_def) => {
|
||||
ast::VariantData::Struct(ref fields, _) => {
|
||||
deserialize_struct_variant(
|
||||
cx,
|
||||
builder,
|
||||
@@ -658,7 +663,7 @@ fn deserialize_variant(
|
||||
variant_ident,
|
||||
generics,
|
||||
ty,
|
||||
struct_def,
|
||||
fields,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -672,16 +677,15 @@ fn deserialize_tuple_variant(
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||
deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
));
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
cx,
|
||||
@@ -690,7 +694,7 @@ fn deserialize_tuple_variant(
|
||||
fields,
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $visitor_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
@@ -704,7 +708,7 @@ fn deserialize_tuple_variant(
|
||||
}
|
||||
|
||||
visitor.visit_tuple($fields, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_struct_variant(
|
||||
@@ -714,8 +718,8 @@ fn deserialize_struct_variant(
|
||||
variant_ident: ast::Ident,
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let where_clause = &generics.where_clause;
|
||||
|
||||
let type_path = builder.path()
|
||||
@@ -723,29 +727,28 @@ fn deserialize_struct_variant(
|
||||
.id(variant_ident)
|
||||
.build();
|
||||
|
||||
let visit_seq_expr = deserialize_struct_as_seq(
|
||||
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
||||
cx,
|
||||
builder,
|
||||
type_path.clone(),
|
||||
struct_def
|
||||
);
|
||||
fields,
|
||||
));
|
||||
|
||||
let (field_visitor, fields_stmt, field_expr) = deserialize_struct_visitor(
|
||||
let (field_visitor, fields_stmt, field_expr) = try!(deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_def,
|
||||
type_path
|
||||
);
|
||||
type_path,
|
||||
fields,
|
||||
));
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) =
|
||||
deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
);
|
||||
let (visitor_item, visitor_ty, visitor_expr, visitor_generics) = try!(deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
vec![deserializer_ty_param(builder)],
|
||||
vec![deserializer_ty_arg(builder)],
|
||||
));
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$field_visitor
|
||||
|
||||
$visitor_item
|
||||
@@ -771,7 +774,7 @@ fn deserialize_struct_variant(
|
||||
$fields_stmt
|
||||
|
||||
visitor.visit_struct(FIELDS, $visitor_expr)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn deserialize_field_visitor(
|
||||
@@ -789,7 +792,7 @@ fn deserialize_field_visitor(
|
||||
.enum_("__Field")
|
||||
.with_variants(
|
||||
field_idents.iter().map(|field_ident| {
|
||||
builder.variant(field_ident).tuple().build()
|
||||
builder.variant(field_ident).unit()
|
||||
})
|
||||
)
|
||||
.build();
|
||||
@@ -914,7 +917,7 @@ fn deserialize_field_visitor(
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(__FieldVisitor::<D>{ phantom: PhantomData })
|
||||
deserializer.visit_struct_field(__FieldVisitor::<D>{ phantom: PhantomData })
|
||||
}
|
||||
}
|
||||
).unwrap();
|
||||
@@ -925,29 +928,31 @@ fn deserialize_field_visitor(
|
||||
fn deserialize_struct_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_def: &ast::StructDef,
|
||||
struct_path: ast::Path,
|
||||
) -> (Vec<P<ast::Item>>, P<ast::Stmt>, P<ast::Expr>) {
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<(Vec<P<ast::Item>>, ast::Stmt, P<ast::Expr>), ()> {
|
||||
let field_visitor = deserialize_field_visitor(
|
||||
cx,
|
||||
builder,
|
||||
field::struct_field_attrs(cx, builder, struct_def),
|
||||
try!(field::struct_field_attrs(cx, builder, fields)),
|
||||
);
|
||||
|
||||
let visit_map_expr = deserialize_map(
|
||||
let visit_map_expr = try!(deserialize_map(
|
||||
cx,
|
||||
builder,
|
||||
struct_path,
|
||||
struct_def,
|
||||
);
|
||||
fields,
|
||||
));
|
||||
|
||||
let fields_expr = builder.expr().addr_of().slice()
|
||||
let fields_expr = builder.expr().ref_().slice()
|
||||
.with_exprs(
|
||||
struct_def.fields.iter()
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => builder.expr().str(name),
|
||||
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
||||
ast::UnnamedField(_) => {
|
||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
@@ -957,22 +962,22 @@ fn deserialize_struct_visitor(
|
||||
const FIELDS: &'static [&'static str] = $fields_expr;
|
||||
).unwrap();
|
||||
|
||||
(field_visitor, fields_stmt, visit_map_expr)
|
||||
Ok((field_visitor, fields_stmt, visit_map_expr))
|
||||
}
|
||||
|
||||
fn deserialize_map(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
struct_def: &StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
// Create the field names for the fields.
|
||||
let field_names: Vec<ast::Ident> = (0 .. struct_def.fields.len())
|
||||
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
// Declare each field.
|
||||
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||
let let_values: Vec<ast::Stmt> = field_names.iter()
|
||||
.map(|field_name| quote_stmt!(cx, let mut $field_name = None;).unwrap())
|
||||
.collect();
|
||||
|
||||
@@ -987,8 +992,10 @@ fn deserialize_map(
|
||||
})
|
||||
.collect();
|
||||
|
||||
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||
.zip(field::struct_field_attrs(cx, builder, struct_def).iter())
|
||||
let field_attrs = try!(field::struct_field_attrs(cx, builder, fields));
|
||||
|
||||
let extract_values: Vec<ast::Stmt> = field_names.iter()
|
||||
.zip(field_attrs.iter())
|
||||
.map(|(field_name, field_attr)| {
|
||||
let missing_expr = if field_attr.use_default() {
|
||||
quote_expr!(cx, ::std::default::Default::default())
|
||||
@@ -1025,13 +1032,15 @@ fn deserialize_map(
|
||||
|
||||
let result = builder.expr().struct_path(struct_path)
|
||||
.with_id_exprs(
|
||||
struct_def.fields.iter()
|
||||
fields.iter()
|
||||
.zip(field_names.iter())
|
||||
.map(|(field, field_name)| {
|
||||
(
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => name.clone(),
|
||||
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
||||
ast::UnnamedField(_) => {
|
||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||
}
|
||||
},
|
||||
builder.expr().id(field_name),
|
||||
)
|
||||
@@ -1039,7 +1048,7 @@ fn deserialize_map(
|
||||
)
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$let_values
|
||||
|
||||
while let Some(key) = try!(visitor.visit_key()) {
|
||||
@@ -1053,5 +1062,5 @@ fn deserialize_map(
|
||||
try!(visitor.end());
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
+20
-137
@@ -1,147 +1,30 @@
|
||||
use std::collections::HashMap;
|
||||
use syntax::ast;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
|
||||
use aster;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use attr::FieldAttrs;
|
||||
|
||||
enum Rename<'a> {
|
||||
None,
|
||||
Global(&'a ast::Lit),
|
||||
Format(HashMap<P<ast::Expr>, &'a ast::Lit>)
|
||||
}
|
||||
|
||||
fn rename<'a>(
|
||||
builder: &aster::AstBuilder,
|
||||
mi: &'a ast::MetaItem,
|
||||
) -> Option<Rename<'a>>
|
||||
{
|
||||
match mi.node {
|
||||
ast::MetaNameValue(ref n, ref lit) => {
|
||||
if n == &"rename" {
|
||||
Some(Rename::Global(lit))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
ast::MetaList(ref n, ref items) => {
|
||||
if n == &"rename" {
|
||||
let mut m = HashMap::new();
|
||||
m.extend(
|
||||
items.iter()
|
||||
.filter_map(
|
||||
|item|
|
||||
match item.node {
|
||||
ast::MetaNameValue(ref n, ref lit) =>
|
||||
Some((builder.expr().str(n),
|
||||
lit)),
|
||||
_ => None
|
||||
}));
|
||||
Some(Rename::Format(m))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn default_value(mi: &ast::MetaItem) -> bool {
|
||||
if let ast::MetaItem_::MetaWord(ref n) = mi.node {
|
||||
n == &"default"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn skip_serializing_field(mi: &ast::MetaItem) -> bool {
|
||||
if let ast::MetaItem_::MetaWord(ref n) = mi.node {
|
||||
n == &"skip_serializing"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn field_attrs<'a>(
|
||||
builder: &aster::AstBuilder,
|
||||
field: &'a ast::StructField,
|
||||
) -> (Rename<'a>, bool, bool) {
|
||||
field.node.attrs.iter()
|
||||
.find(|sa| {
|
||||
if let ast::MetaList(ref n, _) = sa.node.value.node {
|
||||
n == &"serde"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.and_then(|sa| {
|
||||
if let ast::MetaList(_, ref vals) = sa.node.value.node {
|
||||
attr::mark_used(&sa);
|
||||
Some((
|
||||
vals.iter()
|
||||
.fold(None, |v, mi| v.or(rename(builder, mi)))
|
||||
.unwrap_or(Rename::None),
|
||||
vals.iter().any(|mi| default_value(mi)),
|
||||
vals.iter().any(|mi| skip_serializing_field(mi)),
|
||||
))
|
||||
} else {
|
||||
Some((Rename::None, false, false))
|
||||
}
|
||||
})
|
||||
.unwrap_or((Rename::None, false, false))
|
||||
}
|
||||
use attr;
|
||||
|
||||
pub fn struct_field_attrs(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> Vec<FieldAttrs> {
|
||||
struct_def.fields.iter()
|
||||
.map(|field| {
|
||||
match field_attrs(builder, field) {
|
||||
(Rename::Global(rename), default_value, skip_serializing_field) =>
|
||||
FieldAttrs::new(
|
||||
skip_serializing_field,
|
||||
default_value,
|
||||
builder.expr().build_lit(P(rename.clone()))),
|
||||
(Rename::Format(renames), default_value, skip_serializing_field) => {
|
||||
let mut res = HashMap::new();
|
||||
res.extend(
|
||||
renames.into_iter()
|
||||
.map(|(k,v)|
|
||||
(k, builder.expr().build_lit(P(v.clone())))));
|
||||
FieldAttrs::new_with_formats(
|
||||
skip_serializing_field,
|
||||
default_value,
|
||||
default_field_name(cx, builder, field.node.kind),
|
||||
res)
|
||||
},
|
||||
(Rename::None, default_value, skip_serializing_field) => {
|
||||
FieldAttrs::new(
|
||||
skip_serializing_field,
|
||||
default_value,
|
||||
default_field_name(cx, builder, field.node.kind))
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<Vec<attr::FieldAttrs>, ()> {
|
||||
let mut attrs = vec![];
|
||||
for field in fields {
|
||||
let builder = attr::FieldAttrsBuilder::new(cx, builder);
|
||||
let builder = try!(builder.field(field));
|
||||
let attr = builder.build();
|
||||
attrs.push(attr);
|
||||
}
|
||||
|
||||
Ok(attrs)
|
||||
}
|
||||
|
||||
fn default_field_name(
|
||||
pub fn container_attrs(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
kind: ast::StructFieldKind,
|
||||
) -> P<ast::Expr> {
|
||||
match kind {
|
||||
ast::NamedField(name, _) => {
|
||||
builder.expr().str(name)
|
||||
}
|
||||
ast::UnnamedField(_) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
container: &ast::Item,
|
||||
) -> Result<attr::ContainerAttrs, ()> {
|
||||
let builder = attr::ContainerAttrsBuilder::new(cx);
|
||||
let builder = try!(builder.attrs(container.attrs()));
|
||||
Ok(builder.build())
|
||||
}
|
||||
|
||||
+19
-11
@@ -1,3 +1,6 @@
|
||||
#![cfg_attr(feature = "nightly-testing", plugin(clippy))]
|
||||
#![cfg_attr(feature = "nightly-testing", feature(plugin))]
|
||||
#![cfg_attr(feature = "nightly-testing", allow(used_underscore_binding))]
|
||||
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
||||
#![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))]
|
||||
|
||||
@@ -14,7 +17,10 @@ extern crate syntex_syntax as syntax;
|
||||
extern crate syntax;
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
extern crate rustc;
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
use syntax::feature_gate::AttributeType;
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
||||
@@ -26,14 +32,6 @@ include!("lib.rs.in");
|
||||
pub fn register(reg: &mut syntex::Registry) {
|
||||
use syntax::{ast, fold};
|
||||
|
||||
reg.add_attr("feature(custom_derive)");
|
||||
reg.add_attr("feature(custom_attribute)");
|
||||
|
||||
reg.add_decorator("derive_Serialize", ser::expand_derive_serialize);
|
||||
reg.add_decorator("derive_Deserialize", de::expand_derive_deserialize);
|
||||
|
||||
reg.add_post_expansion_pass(strip_attributes);
|
||||
|
||||
/// Strip the serde attributes from the crate.
|
||||
#[cfg(feature = "with-syntex")]
|
||||
fn strip_attributes(krate: ast::Crate) -> ast::Crate {
|
||||
@@ -43,7 +41,7 @@ pub fn register(reg: &mut syntex::Registry) {
|
||||
impl fold::Folder for StripAttributeFolder {
|
||||
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
|
||||
match attr.node.value.node {
|
||||
ast::MetaList(ref n, _) if n == &"serde" => { return None; }
|
||||
ast::MetaItemKind::List(ref n, _) if n == &"serde" => { return None; }
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -57,10 +55,18 @@ pub fn register(reg: &mut syntex::Registry) {
|
||||
|
||||
fold::Folder::fold_crate(&mut StripAttributeFolder, krate)
|
||||
}
|
||||
|
||||
reg.add_attr("feature(custom_derive)");
|
||||
reg.add_attr("feature(custom_attribute)");
|
||||
|
||||
reg.add_decorator("derive_Serialize", ser::expand_derive_serialize);
|
||||
reg.add_decorator("derive_Deserialize", de::expand_derive_deserialize);
|
||||
|
||||
reg.add_post_expansion_pass(strip_attributes);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
pub fn register(reg: &mut rustc::plugin::Registry) {
|
||||
pub fn register(reg: &mut rustc_plugin::Registry) {
|
||||
reg.register_syntax_extension(
|
||||
syntax::parse::token::intern("derive_Serialize"),
|
||||
syntax::ext::base::MultiDecorator(
|
||||
@@ -70,4 +76,6 @@ pub fn register(reg: &mut rustc::plugin::Registry) {
|
||||
syntax::parse::token::intern("derive_Deserialize"),
|
||||
syntax::ext::base::MultiDecorator(
|
||||
Box::new(de::expand_derive_deserialize)));
|
||||
|
||||
reg.register_attribute("serde".to_owned(), AttributeType::Normal);
|
||||
}
|
||||
|
||||
+181
-134
@@ -4,8 +4,6 @@ use syntax::ast::{
|
||||
Ident,
|
||||
MetaItem,
|
||||
Item,
|
||||
Expr,
|
||||
StructDef,
|
||||
};
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
@@ -13,7 +11,7 @@ use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use field::struct_field_attrs;
|
||||
use field;
|
||||
|
||||
pub fn expand_derive_serialize(
|
||||
cx: &mut ExtCtxt,
|
||||
@@ -27,7 +25,7 @@ pub fn expand_derive_serialize(
|
||||
_ => {
|
||||
cx.span_err(
|
||||
meta_item.span,
|
||||
"`derive` may only be applied to structs and enums");
|
||||
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -35,9 +33,14 @@ pub fn expand_derive_serialize(
|
||||
let builder = aster::AstBuilder::new().span(span);
|
||||
|
||||
let generics = match item.node {
|
||||
ast::ItemStruct(_, ref generics) => generics,
|
||||
ast::ItemEnum(_, ref generics) => generics,
|
||||
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive(Serialize)]")
|
||||
ast::ItemKind::Struct(_, ref generics) => generics,
|
||||
ast::ItemKind::Enum(_, ref generics) => generics,
|
||||
_ => {
|
||||
cx.span_err(
|
||||
meta_item.span,
|
||||
"`#[derive(Serialize)]` may only be applied to structs and enums");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let impl_generics = builder.from_generics(generics.clone())
|
||||
@@ -50,18 +53,17 @@ pub fn expand_derive_serialize(
|
||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||
.build();
|
||||
|
||||
let body = serialize_body(
|
||||
cx,
|
||||
&builder,
|
||||
&item,
|
||||
&impl_generics,
|
||||
ty.clone(),
|
||||
);
|
||||
let body = match serialize_body(cx, &builder, &item, &impl_generics, ty.clone()) {
|
||||
Ok(body) => body,
|
||||
Err(()) => {
|
||||
// An error occured, but it should have been reported already.
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let impl_item = quote_item!(cx,
|
||||
#[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,
|
||||
@@ -80,19 +82,24 @@ fn serialize_body(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
// Note: While we don't have any container attributes, we still want to try to
|
||||
// parse them so we can report a proper error if we get passed an unknown attribute.
|
||||
let _ = try!(field::container_attrs(cx, item));
|
||||
|
||||
match item.node {
|
||||
ast::ItemStruct(ref struct_def, _) => {
|
||||
ast::ItemKind::Struct(ref variant_data, _) => {
|
||||
serialize_item_struct(
|
||||
cx,
|
||||
builder,
|
||||
item,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
item.span,
|
||||
variant_data,
|
||||
)
|
||||
}
|
||||
ast::ItemEnum(ref enum_def, _) => {
|
||||
ast::ItemKind::Enum(ref enum_def, _) => {
|
||||
serialize_item_enum(
|
||||
cx,
|
||||
builder,
|
||||
@@ -102,7 +109,10 @@ fn serialize_body(
|
||||
enum_def,
|
||||
)
|
||||
}
|
||||
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive(Serialize)]")
|
||||
_ => {
|
||||
cx.span_bug(item.span,
|
||||
"expected ItemStruct or ItemEnum in #[derive(Serialize)]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,57 +122,52 @@ fn serialize_item_struct(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
let mut named_fields = vec![];
|
||||
let mut unnamed_fields = 0;
|
||||
|
||||
for field in struct_def.fields.iter() {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => { named_fields.push(name); }
|
||||
ast::UnnamedField(_) => { unnamed_fields += 1; }
|
||||
}
|
||||
}
|
||||
|
||||
match (named_fields.is_empty(), unnamed_fields) {
|
||||
(true, 0) => {
|
||||
span: Span,
|
||||
variant_data: &ast::VariantData,
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
match *variant_data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
serialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, 1) => {
|
||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||
serialize_newtype_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, _) => {
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
||||
cx.span_bug(span, "tuple struct has named fields")
|
||||
}
|
||||
|
||||
serialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
unnamed_fields,
|
||||
fields.len(),
|
||||
)
|
||||
}
|
||||
(false, 0) => {
|
||||
ast::VariantData::Struct(ref fields, _) => {
|
||||
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
||||
cx.span_bug(span, "struct has unnamed fields")
|
||||
}
|
||||
|
||||
serialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
named_fields,
|
||||
fields,
|
||||
)
|
||||
}
|
||||
(false, _) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,20 +175,24 @@ fn serialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, serializer.visit_unit_struct($type_name))
|
||||
Ok(quote_expr!(cx,
|
||||
serializer.visit_unit_struct($type_name)
|
||||
))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, serializer.visit_newtype_struct($type_name, &self.0))
|
||||
Ok(quote_expr!(cx,
|
||||
serializer.visit_newtype_struct($type_name, &self.0)
|
||||
))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
@@ -193,7 +202,7 @@ fn serialize_tuple_struct(
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
@@ -208,7 +217,7 @@ fn serialize_tuple_struct(
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_tuple_struct($type_name, Visitor {
|
||||
@@ -216,7 +225,7 @@ fn serialize_tuple_struct(
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||
})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn serialize_struct(
|
||||
@@ -225,10 +234,14 @@ fn serialize_struct(
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
fields: Vec<Ident>,
|
||||
) -> P<ast::Expr> {
|
||||
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||
fields: &[ast::StructField],
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let value_exprs = fields.iter().map(|field| {
|
||||
let name = field.node.ident().expect("struct has unnamed field");
|
||||
quote_expr!(cx, &self.value.$name)
|
||||
});
|
||||
|
||||
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
ty.clone(),
|
||||
@@ -236,14 +249,14 @@ fn serialize_struct(
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(ty.clone()),
|
||||
struct_def,
|
||||
fields,
|
||||
impl_generics,
|
||||
fields.iter().map(|field| quote_expr!(cx, &self.value.$field)),
|
||||
);
|
||||
value_exprs,
|
||||
));
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_struct($type_name, Visitor {
|
||||
@@ -251,7 +264,7 @@ fn serialize_struct(
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||
})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn serialize_item_enum(
|
||||
@@ -261,27 +274,28 @@ fn serialize_item_enum(
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
enum_def: &ast::EnumDef,
|
||||
) -> P<ast::Expr> {
|
||||
let arms: Vec<ast::Arm> = enum_def.variants.iter()
|
||||
.enumerate()
|
||||
.map(|(variant_index, variant)| {
|
||||
serialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
impl_generics,
|
||||
ty.clone(),
|
||||
variant,
|
||||
variant_index,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let mut arms = vec![];
|
||||
|
||||
quote_expr!(cx,
|
||||
for (variant_index, variant) in enum_def.variants.iter().enumerate() {
|
||||
let arm = try!(serialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
impl_generics,
|
||||
ty.clone(),
|
||||
variant,
|
||||
variant_index,
|
||||
));
|
||||
|
||||
arms.push(arm);
|
||||
}
|
||||
|
||||
Ok(quote_expr!(cx,
|
||||
match *self {
|
||||
$arms
|
||||
}
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fn serialize_variant(
|
||||
@@ -292,18 +306,18 @@ fn serialize_variant(
|
||||
ty: P<ast::Ty>,
|
||||
variant: &ast::Variant,
|
||||
variant_index: usize,
|
||||
) -> ast::Arm {
|
||||
) -> Result<ast::Arm, ()> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
let variant_ident = variant.node.name;
|
||||
let variant_name = builder.expr().str(variant_ident);
|
||||
|
||||
match variant.node.kind {
|
||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
||||
let pat = builder.pat().enum_()
|
||||
.id(type_ident).id(variant_ident).build()
|
||||
match variant.node.data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
let pat = builder.pat().path()
|
||||
.id(type_ident).id(variant_ident)
|
||||
.build();
|
||||
|
||||
quote_arm!(cx,
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::visit_unit_variant(
|
||||
serializer,
|
||||
@@ -312,16 +326,17 @@ fn serialize_variant(
|
||||
$variant_name,
|
||||
)
|
||||
}
|
||||
)
|
||||
))
|
||||
},
|
||||
ast::TupleVariantKind(ref args) if args.len() == 1 => {
|
||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||
let field = builder.id("__simple_value");
|
||||
let field = builder.pat().ref_id(field);
|
||||
let pat = builder.pat().enum_()
|
||||
.id(type_ident).id(variant_ident).build()
|
||||
.with_pats(Some(field).into_iter())
|
||||
.build();
|
||||
quote_arm!(cx,
|
||||
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::visit_newtype_variant(
|
||||
serializer,
|
||||
@@ -331,16 +346,19 @@ fn serialize_variant(
|
||||
__simple_value,
|
||||
)
|
||||
}
|
||||
)
|
||||
))
|
||||
},
|
||||
ast::TupleVariantKind(ref args) => {
|
||||
let fields: Vec<ast::Ident> = (0 .. args.len())
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
let field_names: Vec<ast::Ident> = (0 .. fields.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
let pat = builder.pat().enum_()
|
||||
.id(type_ident).id(variant_ident).build()
|
||||
.with_pats(fields.iter().map(|field| builder.pat().ref_id(field)))
|
||||
.with_pats(
|
||||
field_names.iter()
|
||||
.map(|field| builder.pat().ref_id(field))
|
||||
)
|
||||
.build();
|
||||
|
||||
let expr = serialize_tuple_variant(
|
||||
@@ -351,27 +369,29 @@ fn serialize_variant(
|
||||
variant_name,
|
||||
generics,
|
||||
ty,
|
||||
args,
|
||||
fields,
|
||||
field_names,
|
||||
);
|
||||
|
||||
quote_arm!(cx, $pat => { $expr })
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => { $expr }
|
||||
))
|
||||
}
|
||||
ast::StructVariantKind(ref struct_def) => {
|
||||
let fields: Vec<_> = (0 .. struct_def.fields.len())
|
||||
ast::VariantData::Struct(ref fields, _) => {
|
||||
let field_names: Vec<_> = (0 .. fields.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
let pat = builder.pat().struct_()
|
||||
.id(type_ident).id(variant_ident).build()
|
||||
.with_pats(
|
||||
fields.iter()
|
||||
.zip(struct_def.fields.iter())
|
||||
field_names.iter()
|
||||
.zip(fields.iter())
|
||||
.map(|(id, field)| {
|
||||
let name = match field.node.kind {
|
||||
ast::NamedField(name, _) => name,
|
||||
ast::UnnamedField(_) => {
|
||||
cx.bug("struct variant has unnamed fields")
|
||||
cx.span_bug(field.span, "struct variant has unnamed fields")
|
||||
}
|
||||
};
|
||||
|
||||
@@ -380,7 +400,7 @@ fn serialize_variant(
|
||||
)
|
||||
.build();
|
||||
|
||||
let expr = serialize_struct_variant(
|
||||
let expr = try!(serialize_struct_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_name,
|
||||
@@ -388,11 +408,13 @@ fn serialize_variant(
|
||||
variant_name,
|
||||
generics,
|
||||
ty,
|
||||
struct_def,
|
||||
fields,
|
||||
);
|
||||
field_names,
|
||||
));
|
||||
|
||||
quote_arm!(cx, $pat => { $expr })
|
||||
Ok(quote_arm!(cx,
|
||||
$pat => { $expr }
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -405,16 +427,16 @@ fn serialize_tuple_variant(
|
||||
variant_name: P<ast::Expr>,
|
||||
generics: &ast::Generics,
|
||||
structure_ty: P<ast::Ty>,
|
||||
args: &[ast::VariantArg],
|
||||
fields: Vec<Ident>,
|
||||
fields: &[ast::StructField],
|
||||
field_names: Vec<Ident>,
|
||||
) -> P<ast::Expr> {
|
||||
let variant_ty = builder.ty().tuple()
|
||||
.with_tys(
|
||||
args.iter().map(|arg| {
|
||||
fields.iter().map(|field| {
|
||||
builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(arg.ty.clone())
|
||||
.build_ty(field.node.ty.clone())
|
||||
})
|
||||
)
|
||||
.build();
|
||||
@@ -424,13 +446,13 @@ fn serialize_tuple_variant(
|
||||
builder,
|
||||
structure_ty.clone(),
|
||||
variant_ty,
|
||||
args.len(),
|
||||
fields.len(),
|
||||
generics,
|
||||
);
|
||||
|
||||
let value_expr = builder.expr().tuple()
|
||||
.with_exprs(
|
||||
fields.iter().map(|field| {
|
||||
field_names.iter().map(|field| {
|
||||
builder.expr().id(field)
|
||||
})
|
||||
)
|
||||
@@ -455,12 +477,12 @@ fn serialize_struct_variant(
|
||||
variant_name: P<ast::Expr>,
|
||||
generics: &ast::Generics,
|
||||
structure_ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
fields: Vec<Ident>,
|
||||
) -> P<ast::Expr> {
|
||||
fields: &[ast::StructField],
|
||||
field_names: Vec<Ident>,
|
||||
) -> Result<P<ast::Expr>, ()> {
|
||||
let value_ty = builder.ty().tuple()
|
||||
.with_tys(
|
||||
struct_def.fields.iter().map(|field| {
|
||||
fields.iter().map(|field| {
|
||||
builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
@@ -471,27 +493,27 @@ fn serialize_struct_variant(
|
||||
|
||||
let value_expr = builder.expr().tuple()
|
||||
.with_exprs(
|
||||
fields.iter().map(|field| {
|
||||
field_names.iter().map(|field| {
|
||||
builder.expr().id(field)
|
||||
})
|
||||
)
|
||||
.build();
|
||||
|
||||
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||
let (visitor_struct, visitor_impl) = try!(serialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
structure_ty.clone(),
|
||||
value_ty,
|
||||
struct_def,
|
||||
fields,
|
||||
generics,
|
||||
(0 .. fields.len()).map(|i| {
|
||||
(0 .. field_names.len()).map(|i| {
|
||||
builder.expr()
|
||||
.tup_field(i)
|
||||
.field("value").self_()
|
||||
})
|
||||
);
|
||||
));
|
||||
|
||||
quote_expr!(cx, {
|
||||
Ok(quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_struct_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||
@@ -499,7 +521,7 @@ fn serialize_struct_variant(
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||
})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_visitor(
|
||||
@@ -574,29 +596,37 @@ fn serialize_struct_visitor<I>(
|
||||
builder: &aster::AstBuilder,
|
||||
structure_ty: P<ast::Ty>,
|
||||
variant_ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
fields: &[ast::StructField],
|
||||
generics: &ast::Generics,
|
||||
value_exprs: I,
|
||||
) -> (P<ast::Item>, P<ast::Item>)
|
||||
) -> Result<(P<ast::Item>, P<ast::Item>), ()>
|
||||
where I: Iterator<Item=P<ast::Expr>>,
|
||||
{
|
||||
let field_attrs = struct_field_attrs(cx, builder, struct_def);
|
||||
let value_exprs = value_exprs.collect::<Vec<_>>();
|
||||
|
||||
let len = struct_def.fields.len() - field_attrs.iter()
|
||||
.fold(0, |sum, field| {
|
||||
sum + if field.skip_serializing_field() { 1 } else { 0 }
|
||||
});
|
||||
let field_attrs = try!(field::struct_field_attrs(cx, builder, fields));
|
||||
|
||||
let arms: Vec<ast::Arm> = field_attrs.into_iter()
|
||||
.zip(value_exprs)
|
||||
let arms: Vec<ast::Arm> = field_attrs.iter()
|
||||
.zip(value_exprs.iter())
|
||||
.filter(|&(ref field, _)| !field.skip_serializing_field())
|
||||
.enumerate()
|
||||
.map(|(i, (field, value_expr))| {
|
||||
.map(|(i, (ref field, value_expr))| {
|
||||
let key_expr = field.serializer_key_expr(cx);
|
||||
|
||||
let stmt = if field.skip_serializing_field_if_empty() {
|
||||
quote_stmt!(cx, if ($value_expr).is_empty() { continue; })
|
||||
} else if field.skip_serializing_field_if_none() {
|
||||
quote_stmt!(cx, if ($value_expr).is_none() { continue; })
|
||||
} else {
|
||||
quote_stmt!(cx, {})
|
||||
};
|
||||
|
||||
quote_arm!(cx,
|
||||
$i => {
|
||||
self.state += 1;
|
||||
Ok(
|
||||
$stmt
|
||||
|
||||
return Ok(
|
||||
Some(
|
||||
try!(
|
||||
serializer.visit_struct_elt(
|
||||
@@ -605,7 +635,7 @@ fn serialize_struct_visitor<I>(
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -622,7 +652,22 @@ fn serialize_struct_visitor<I>(
|
||||
.strip_bounds()
|
||||
.build();
|
||||
|
||||
(
|
||||
let len = field_attrs.iter()
|
||||
.zip(value_exprs.iter())
|
||||
.map(|(field, value_expr)| {
|
||||
if field.skip_serializing_field() {
|
||||
quote_expr!(cx, 0)
|
||||
} else if field.skip_serializing_field_if_empty() {
|
||||
quote_expr!(cx, if ($value_expr).is_empty() { 0 } else { 1 })
|
||||
} else if field.skip_serializing_field_if_none() {
|
||||
quote_expr!(cx, if ($value_expr).is_none() { 0 } else { 1 })
|
||||
} else {
|
||||
quote_expr!(cx, 1)
|
||||
}
|
||||
})
|
||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||
|
||||
Ok((
|
||||
quote_item!(cx,
|
||||
struct Visitor $visitor_impl_generics $where_clause {
|
||||
state: usize,
|
||||
@@ -640,9 +685,11 @@ fn serialize_struct_visitor<I>(
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer,
|
||||
{
|
||||
match self.state {
|
||||
$arms
|
||||
_ => Ok(None)
|
||||
loop {
|
||||
match self.state {
|
||||
$arms
|
||||
_ => { return Ok(None); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -652,5 +699,5 @@ fn serialize_struct_visitor<I>(
|
||||
}
|
||||
}
|
||||
).unwrap(),
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
+18
-5
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_macros"
|
||||
version = "0.5.3"
|
||||
version = "0.6.14"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
@@ -12,10 +12,23 @@ keywords = ["serde", "serialization"]
|
||||
name = "serde_macros"
|
||||
plugin = true
|
||||
|
||||
[features]
|
||||
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
|
||||
|
||||
[dependencies]
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
serde_codegen = { version = "^0.6.14", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||
|
||||
[dev-dependencies]
|
||||
num = "*"
|
||||
rustc-serialize = "*"
|
||||
serde = { version = "*", path = "../serde", features = ["nightly"] }
|
||||
compiletest_rs = "^0.0.11"
|
||||
num = "^0.1.27"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "^0.6.14", path = "../serde", features = ["num-impls"] }
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
path = "tests/test.rs"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
path = "benches/bench.rs"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![feature(custom_attribute, custom_derive, plugin, test)]
|
||||
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate num;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#![feature(plugin_registrar, rustc_private)]
|
||||
#![cfg_attr(feature = "clippy", feature(plugin))]
|
||||
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
||||
|
||||
extern crate serde_codegen;
|
||||
extern crate rustc;
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[plugin_registrar]
|
||||
#[doc(hidden)]
|
||||
pub fn plugin_registrar(reg: &mut rustc::plugin::Registry) {
|
||||
pub fn plugin_registrar(reg: &mut rustc_plugin::Registry) {
|
||||
serde_codegen::register(reg);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#![feature(custom_attribute, custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(abc="xyz")] //~ unknown serde container attribute `abc = "xyz"`
|
||||
struct Foo {
|
||||
x: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(abc="xyz")] //~ unknown serde container attribute `abc = "xyz"`
|
||||
struct Foo {
|
||||
x: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Foo {
|
||||
#[serde(abc="xyz")] //~ unknown serde field attribute `abc = "xyz"`
|
||||
x: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Foo {
|
||||
#[serde(abc="xyz")] //~ unknown serde field attribute `abc = "xyz"`
|
||||
x: u32,
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
@@ -0,0 +1,25 @@
|
||||
extern crate compiletest_rs as compiletest;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::env::var;
|
||||
|
||||
fn run_mode(mode: &'static str) {
|
||||
let mut config = compiletest::default_config();
|
||||
|
||||
let cfg_mode = mode.parse().ok().expect("Invalid mode");
|
||||
|
||||
config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_owned());
|
||||
if let Ok(name) = var::<&str>("TESTNAME") {
|
||||
let s : String = name.to_owned();
|
||||
config.filter = Some(s)
|
||||
}
|
||||
config.mode = cfg_mode;
|
||||
config.src_base = PathBuf::from(format!("tests/{}", mode));
|
||||
|
||||
compiletest::run_tests(&config);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_test() {
|
||||
run_mode("compile-fail");
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
#![feature(test, custom_attribute, custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
extern crate test;
|
||||
|
||||
include!("../../serde_tests/tests/test.rs.in");
|
||||
|
||||
mod compile_tests;
|
||||
|
||||
+14
-8
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_tests"
|
||||
version = "0.5.0"
|
||||
version = "0.6.3"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
@@ -10,16 +10,22 @@ readme = "README.md"
|
||||
keywords = ["serialization"]
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"]
|
||||
|
||||
[build-dependencies]
|
||||
syntex = { version = "*" }
|
||||
syntex_syntax = { version = "*" }
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
||||
syntex = { version = "^0.29.0" }
|
||||
syntex_syntax = { version = "^0.29.0" }
|
||||
serde_codegen = { version = "^0.6.14", path = "../serde_codegen", features = ["with-syntex"] }
|
||||
|
||||
[dev-dependencies]
|
||||
num = "*"
|
||||
rustc-serialize = "*"
|
||||
serde = { version = "*", path = "../serde" }
|
||||
syntex = "*"
|
||||
num = "^0.1.26"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde", features = ["num-impls"] }
|
||||
syntex = "^0.29.0"
|
||||
|
||||
[dependencies]
|
||||
clippy = { version = "^0.*", optional = true }
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#![feature(test)]
|
||||
#![cfg_attr(feature = "nightly", feature(plugin))]
|
||||
#![cfg_attr(feature = "nightly", plugin(clippy))]
|
||||
|
||||
extern crate num;
|
||||
extern crate rustc_serialize;
|
||||
|
||||
@@ -415,7 +415,7 @@ fn bench_decoder_dog(b: &mut Bencher) {
|
||||
#[bench]
|
||||
fn bench_decoder_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Animal::Frog("Henry".to_string(), 349);
|
||||
let animal = Animal::Frog("Henry".to_owned(), 349);
|
||||
|
||||
let mut d = decoder::AnimalDecoder::new(animal.clone());
|
||||
let value: Animal = Decodable::decode(&mut d).unwrap();
|
||||
@@ -439,7 +439,7 @@ fn bench_deserializer_dog(b: &mut Bencher) {
|
||||
#[bench]
|
||||
fn bench_deserializer_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Animal::Frog("Henry".to_string(), 349);
|
||||
let animal = Animal::Frog("Henry".to_owned(), 349);
|
||||
|
||||
let mut d = deserializer::AnimalDeserializer::new(animal.clone());
|
||||
let value: Animal = Deserialize::deserialize(&mut d).unwrap();
|
||||
|
||||
@@ -399,7 +399,7 @@ fn bench_decoder_000(b: &mut Bencher) {
|
||||
fn bench_decoder_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, isize> = HashMap::new();
|
||||
for i in (0 .. 3) {
|
||||
for i in 0 .. 3 {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_decoder(decoder::IsizeDecoder::new(m.clone()), m)
|
||||
@@ -410,7 +410,7 @@ fn bench_decoder_003(b: &mut Bencher) {
|
||||
fn bench_decoder_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, isize> = HashMap::new();
|
||||
for i in (0 .. 100) {
|
||||
for i in 0 .. 100 {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_decoder(decoder::IsizeDecoder::new(m.clone()), m)
|
||||
@@ -439,7 +439,7 @@ fn bench_deserializer_000(b: &mut Bencher) {
|
||||
fn bench_deserializer_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, isize> = HashMap::new();
|
||||
for i in (0 .. 3) {
|
||||
for i in 0 .. 3 {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_deserializer(deserializer::IsizeDeserializer::new(m.clone()), m)
|
||||
@@ -450,7 +450,7 @@ fn bench_deserializer_003(b: &mut Bencher) {
|
||||
fn bench_deserializer_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, isize> = HashMap::new();
|
||||
for i in (0 .. 100) {
|
||||
for i in 0 .. 100 {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_deserializer(deserializer::IsizeDeserializer::new(m.clone()), m)
|
||||
|
||||
@@ -614,7 +614,7 @@ mod deserializer {
|
||||
fn bench_decoder_0_0(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("abc".to_string(), Some('c'));
|
||||
map.insert("abc".to_owned(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(),
|
||||
@@ -653,11 +653,11 @@ fn bench_decoder_1_0(b: &mut Bencher) {
|
||||
fn bench_decoder_1_5(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("1".to_string(), Some('a'));
|
||||
map.insert("2".to_string(), None);
|
||||
map.insert("3".to_string(), Some('b'));
|
||||
map.insert("4".to_string(), None);
|
||||
map.insert("5".to_string(), Some('c'));
|
||||
map.insert("1".to_owned(), Some('a'));
|
||||
map.insert("2".to_owned(), None);
|
||||
map.insert("3".to_owned(), Some('b'));
|
||||
map.insert("4".to_owned(), None);
|
||||
map.insert("5".to_owned(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
@@ -716,11 +716,11 @@ fn bench_deserializer_1_0(b: &mut Bencher) {
|
||||
fn bench_deserializer_1_5(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("1".to_string(), Some('a'));
|
||||
map.insert("2".to_string(), None);
|
||||
map.insert("3".to_string(), Some('b'));
|
||||
map.insert("4".to_string(), None);
|
||||
map.insert("5".to_string(), Some('c'));
|
||||
map.insert("1".to_owned(), Some('a'));
|
||||
map.insert("2".to_owned(), None);
|
||||
map.insert("3".to_owned(), Some('b'));
|
||||
map.insert("4".to_owned(), None);
|
||||
map.insert("5".to_owned(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
#![cfg_attr(feature = "nightly", feature(plugin))]
|
||||
#![cfg_attr(feature = "nightly", plugin(clippy))]
|
||||
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||
|
||||
@@ -39,6 +39,20 @@ struct SkipSerializingFields<A: default::Default> {
|
||||
b: A,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SkipSerializingIfEmptyFields<A: default::Default> {
|
||||
a: i8,
|
||||
#[serde(skip_serializing_if_empty, default)]
|
||||
b: Vec<A>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SkipSerializingIfNoneFields<A: default::Default> {
|
||||
a: i8,
|
||||
#[serde(skip_serializing_if_none, default)]
|
||||
b: Option<A>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default() {
|
||||
assert_de_tokens(
|
||||
@@ -169,3 +183,161 @@ fn test_skip_serializing_fields() {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_serializing_fields_if_empty() {
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingIfEmptyFields::<i32> {
|
||||
a: 1,
|
||||
b: vec![],
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipSerializingIfEmptyFields::<i32> {
|
||||
a: 1,
|
||||
b: vec![],
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingIfEmptyFields {
|
||||
a: 1,
|
||||
b: vec![2],
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipSerializingIfEmptyFields {
|
||||
a: 1,
|
||||
b: vec![2],
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_serializing_fields_if_none() {
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingIfNoneFields::<i32> {
|
||||
a: 1,
|
||||
b: None,
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipSerializingIfNoneFields::<i32> {
|
||||
a: 1,
|
||||
b: None,
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingIfNoneFields {
|
||||
a: 1,
|
||||
b: Some(2),
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::Option(true),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipSerializingIfNoneFields {
|
||||
a: 1,
|
||||
b: Some(2),
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::Option(true),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use serde::de::{Deserializer, Visitor};
|
||||
|
||||
@@ -90,12 +96,12 @@ declare_tests! {
|
||||
test_char {
|
||||
'a' => vec![Token::Char('a')],
|
||||
'a' => vec![Token::Str("a")],
|
||||
'a' => vec![Token::String("a".to_string())],
|
||||
'a' => vec![Token::String("a".to_owned())],
|
||||
}
|
||||
test_string {
|
||||
"abc".to_string() => vec![Token::Str("abc")],
|
||||
"abc".to_string() => vec![Token::String("abc".to_string())],
|
||||
"a".to_string() => vec![Token::Char('a')],
|
||||
"abc".to_owned() => vec![Token::Str("abc")],
|
||||
"abc".to_owned() => vec![Token::String("abc".to_owned())],
|
||||
"a".to_owned() => vec![Token::Char('a')],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => vec![Token::Unit],
|
||||
@@ -555,4 +561,38 @@ declare_tests! {
|
||||
Token::Unit,
|
||||
],
|
||||
}
|
||||
test_num_bigint {
|
||||
BigInt::from_i64(123).unwrap() => vec![Token::Str("123")],
|
||||
BigInt::from_i64(-123).unwrap() => vec![Token::Str("-123")],
|
||||
}
|
||||
test_num_biguint {
|
||||
BigUint::from_i64(123).unwrap() => vec![Token::Str("123")],
|
||||
}
|
||||
test_num_complex {
|
||||
Complex::new(1, 2) => vec![
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_num_ratio {
|
||||
Ratio::new(1, 2) => vec![
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_path_buf {
|
||||
PathBuf::from("/usr/local/lib") => vec![
|
||||
Token::String("/usr/local/lib".to_owned()),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use token::Token;
|
||||
|
||||
@@ -58,7 +64,7 @@ declare_ser_tests! {
|
||||
}
|
||||
test_str {
|
||||
"abc" => &[Token::Str("abc")],
|
||||
"abc".to_string() => &[Token::Str("abc")],
|
||||
"abc".to_owned() => &[Token::Str("abc")],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => &[Token::Option(false)],
|
||||
@@ -259,4 +265,43 @@ declare_ser_tests! {
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_num_bigint {
|
||||
BigInt::from_i64(123).unwrap() => &[Token::Str("123")],
|
||||
BigInt::from_i64(-123).unwrap() => &[Token::Str("-123")],
|
||||
}
|
||||
test_num_biguint {
|
||||
BigUint::from_i64(123).unwrap() => &[Token::Str("123")],
|
||||
}
|
||||
test_num_complex {
|
||||
Complex::new(1, 2) => &[
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_num_ratio {
|
||||
Ratio::new(1, 2) => &[
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_path {
|
||||
Path::new("/usr/local/lib") => &[
|
||||
Token::Str("/usr/local/lib"),
|
||||
],
|
||||
}
|
||||
test_path_buf {
|
||||
PathBuf::from("/usr/local/lib") => &[
|
||||
Token::Str("/usr/local/lib"),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ impl de::Error for Error {
|
||||
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field(field: &str) -> Error {
|
||||
Error::UnknownFieldError(field.to_string())
|
||||
Error::UnknownFieldError(field.to_owned())
|
||||
}
|
||||
|
||||
fn missing_field(field: &'static str) -> Error {
|
||||
@@ -379,7 +379,6 @@ impl<I> de::Deserializer for Deserializer<I>
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
println!("visit {:?}", self.tokens.peek());
|
||||
match self.tokens.next() {
|
||||
Some(Token::Bool(v)) => visitor.visit_bool(v),
|
||||
Some(Token::Isize(v)) => visitor.visit_isize(v),
|
||||
|
||||
Reference in New Issue
Block a user