mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-22 22:58:02 +00:00
Compare commits
225 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 | |||
| 7511eeae7b | |||
| 5f4a7e54bc | |||
| b7e3058078 | |||
| f3358cc377 | |||
| fa3460e0a7 | |||
| 42069ca669 | |||
| acc7141b64 | |||
| 3e1b950b14 | |||
| b50b702d1c | |||
| caddd11c15 | |||
| 5711e3d860 | |||
| 8b1058b8ec | |||
| 092ba6dae2 | |||
| cab653e7c7 | |||
| 8d999a8edd | |||
| 7b773ac088 | |||
| 4f6b27eec1 | |||
| fe20852b2c | |||
| fc58ea7487 | |||
| c61cea0eb1 | |||
| 764b25bd07 | |||
| a66cd25787 | |||
| 0e8d94750b | |||
| 3c915189f4 | |||
| c5541cddeb | |||
| 63561609a6 | |||
| 5dc356ddb0 | |||
| dc36fd38d6 | |||
| 26873bf3d5 | |||
| ff53323790 | |||
| fd3869d380 | |||
| 1d538bc59d | |||
| 784cfcd49e | |||
| a142d319e7 | |||
| da3bf3c20a | |||
| 1672306fa1 | |||
| 7fc780ba1b | |||
| 7fb2bd50bf | |||
| 2aeb51ad51 | |||
| 0482b756e8 | |||
| 1d9c707a76 | |||
| 199ed417bd | |||
| 199a02cb68 | |||
| b6371f045f | |||
| fd84aec485 | |||
| ed6777e59f | |||
| 22024a2b71 | |||
| 8eff38b6f6 | |||
| fa562d449d | |||
| c9d55362d6 | |||
| 23031d057f | |||
| 882d394352 | |||
| fbdede68a4 | |||
| e88ef4715c | |||
| 94f3dd25d8 | |||
| 2c58a9c11d | |||
| 97528b59cf | |||
| 6715fb6652 | |||
| fefc010deb | |||
| 6cbb72decf | |||
| 7e25ed863c | |||
| 5a56394814 | |||
| de1059f648 | |||
| 97f08086dd | |||
| 0348a3914d | |||
| 5dc245d2ce | |||
| 71cc95248c | |||
| 2cb7d66767 | |||
| 49fa208242 | |||
| d2fef27721 | |||
| 351b7039a8 | |||
| 7585ce9ed4 | |||
| 578a0178b5 | |||
| 6c9cebdcc3 | |||
| 35e2022e9a | |||
| 0058e3a8d4 | |||
| abf28ee167 | |||
| 5f1cb9b96c | |||
| 8f8fc6f3ff | |||
| 24787195a1 | |||
| 5885111863 | |||
| b1cb5379de | |||
| 447d08bd91 | |||
| b0512a4479 | |||
| 8663435a05 | |||
| 327990bc5f | |||
| 57753c9044 | |||
| e35603eb85 | |||
| 8fa40fe7e1 | |||
| d4c20829f6 | |||
| dbe2beacb0 | |||
| b9a938a01c | |||
| 4dd7345568 | |||
| b3cf9375d4 | |||
| 1751155a3a | |||
| 5dae700aec | |||
| affa9382be | |||
| 10f23dddfe | |||
| d30cf07254 | |||
| 31491b822f | |||
| 4c19cfead5 | |||
| b2754c2c3b | |||
| f56976db1d | |||
| 77b8a8baa0 | |||
| 0b9190cce3 | |||
| 2a40c5dd24 | |||
| 60ab494226 | |||
| ac758ed3c8 | |||
| 236d40d73e | |||
| 92029a05c6 | |||
| c7b9997dd1 | |||
| 48309bfdd0 | |||
| f76d1ab10d | |||
| 45f552f006 | |||
| 4d42cfea53 | |||
| 7109e2d379 | |||
| ff214643ce | |||
| 04918a52eb | |||
| 5dd71600f7 | |||
| ea9ed22e3e | |||
| 1611daf802 | |||
| e24f52d3ae | |||
| bcc9e15b05 | |||
| af835a2699 | |||
| 5f2d306f9b | |||
| 36fb49b522 | |||
| ed5b4d7319 | |||
| b09c0fc653 | |||
| ab3e40ca45 | |||
| 8f67e9c048 | |||
| 7dc1a64f03 | |||
| ac3a3e922f | |||
| 4e50c56542 | |||
| 26b1ed79c0 | |||
| 2e8ef0f768 | |||
| c993414b92 | |||
| ed6ef4e149 | |||
| 859cdcc815 | |||
| 745a95b607 | |||
| 81d617bd93 | |||
| 3396388222 | |||
| ff8c3b3d51 | |||
| 3d0efd123f | |||
| 8ca1b9ac3c | |||
| 2c24be90d2 | |||
| 482f92af61 | |||
| 24ac61f9f2 | |||
| 426394cd7b | |||
| 7c3e95a7c7 | |||
| 9dd5f9dc7a | |||
| e6776ffc37 | |||
| adae2bd3c5 | |||
| 14ca260c26 | |||
| 64588f0cb6 | |||
| 50cac7f985 | |||
| 5fe85128c2 | |||
| bc236bde34 | |||
| 15794a5ed6 | |||
| 62058962de | |||
| 7d52366403 | |||
| ee45eb8340 | |||
| 801f37b305 | |||
| bdec0b3e63 | |||
| 5c631f3e58 | |||
| 5fd9daa865 | |||
| 0485702a68 | |||
| 875610044f | |||
| ec483fc07d | |||
| 53e6e29571 | |||
| c5eed99c6a | |||
| fe5176113b | |||
| 0f7c67efa7 | |||
| cd0ee64892 | |||
| ec3af2cb6a | |||
| 960b68937d | |||
| a1e101b513 | |||
| c30311153c | |||
| a935ebe8b9 | |||
| 83ee86122b | |||
| af752ddcb5 | |||
| 9550063275 | |||
| c117a680cf |
+2
-2
@@ -1,2 +1,2 @@
|
||||
/target/
|
||||
/Cargo.lock
|
||||
target
|
||||
Cargo.lock
|
||||
|
||||
+37
-12
@@ -1,16 +1,41 @@
|
||||
language: rust
|
||||
sudo: false
|
||||
|
||||
rust:
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libcurl4-openssl-dev
|
||||
- libelf-dev
|
||||
- libdw-dev
|
||||
|
||||
before_script:
|
||||
- |
|
||||
pip install 'travis-cargo<0.2' --user &&
|
||||
export PATH=$HOME/.local/bin:$PATH
|
||||
|
||||
script:
|
||||
- cargo test
|
||||
- cargo bench
|
||||
- cargo doc
|
||||
after_success: |
|
||||
[ $TRAVIS_BRANCH = "master" ] &&
|
||||
[ $TRAVIS_PULL_REQUEST = false ] &&
|
||||
cargo doc &&
|
||||
echo "<meta http-equiv=refresh content=0;url=`echo $TRAVIS_REPO_SLUG | cut -d '/' -f 2`/index.html>" > target/doc/index.html &&
|
||||
sudo pip install ghp-import &&
|
||||
ghp-import -n target/doc &&
|
||||
git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
|
||||
- |
|
||||
(cd serde && travis-cargo build) &&
|
||||
(cd serde && travis-cargo test) &&
|
||||
(cd serde_tests && travis-cargo test) &&
|
||||
(cd serde_macros && travis-cargo --only nightly test) &&
|
||||
(cd serde_macros && travis-cargo --only nightly bench) &&
|
||||
(cd serde && travis-cargo --only stable doc) &&
|
||||
(cd serde_codegen && travis-cargo --only stable doc)
|
||||
|
||||
after_success:
|
||||
- (cd serde && travis-cargo --only stable doc-upload)
|
||||
- (cd serde_tests && travis-cargo coveralls --no-sudo)
|
||||
|
||||
env:
|
||||
global:
|
||||
secure: HO41LMpMXkF2In9+1sxWVu7fgolL+y9+4Q5PI6wZX2L5pDwpPJCjxaQarQXCEnoIxED1PlP03JuF7ULNz0zw1ylYhAOfOSdkxFZRnE2wMZqq6qvXBHwyMiDrAociIzoPKSGv7JVrKPsjsnd+96K6xxueIodQZrmAdyq7N/M82Mc=
|
||||
# override the default `--features unstable` used for the nightly branch (optional)
|
||||
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
|
||||
|
||||
# encrypted github token for doc upload (see `GH_TOKEN` link above)
|
||||
- secure: HO41LMpMXkF2In9+1sxWVu7fgolL+y9+4Q5PI6wZX2L5pDwpPJCjxaQarQXCEnoIxED1PlP03JuF7ULNz0zw1ylYhAOfOSdkxFZRnE2wMZqq6qvXBHwyMiDrAociIzoPKSGv7JVrKPsjsnd+96K6xxueIodQZrmAdyq7N/M82Mc=
|
||||
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.3.3"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "http://serde-rs.github.io/serde/serde"
|
||||
readme = "README.md"
|
||||
keywords = ["serialization"]
|
||||
|
||||
[dependencies]
|
||||
num = "*"
|
||||
|
||||
[dev-dependencies]
|
||||
rustc-serialize = "*"
|
||||
serde_macros = { version = "*", path = "serde_macros" }
|
||||
@@ -2,6 +2,7 @@ Serde Rust Serialization Framework
|
||||
==================================
|
||||
|
||||
[](https://travis-ci.org/serde-rs/serde)
|
||||
[](https://coveralls.io/github/serde-rs/serde?branch=master)
|
||||
[](https://crates.io/crates/serde)
|
||||
|
||||
Serde is a powerful framework that enables serialization libraries to
|
||||
@@ -10,76 +11,225 @@ information. In many situations, the 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.
|
||||
|
||||
Documentation is available at http://serde-rs.github.io/serde/serde
|
||||
Documentation is available at:
|
||||
|
||||
Making a Type Serializable
|
||||
==========================
|
||||
* [serde](https://serde-rs.github.io/serde/serde/serde/index.html)
|
||||
* [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)
|
||||
|
||||
The simplest way to make a type serializable is to use the `serde_macros`
|
||||
syntax extension, which comes with a `#[derive(Serialize, Deserialize)]`
|
||||
annotation, which automatically generates implementations of
|
||||
[Serialize](http://serde-rs.github.io/serde/serde/ser/trait.Serialize.html)
|
||||
and
|
||||
[Deserialize](http://serde-rs.github.io/serde/serde/de/trait.Deserialize.html)
|
||||
for the annotated type:
|
||||
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
|
||||
that work with Stable and Nightly Rust that eliminate much of the complexity of
|
||||
hand rolling serialization and deserialization for a given type. First lets see
|
||||
how we would use Nightly Rust, which is currently a bit simpler than Stable
|
||||
Rust:
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[package]
|
||||
name = "serde_example_nightly"
|
||||
version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
serde_macros = "*"
|
||||
```
|
||||
|
||||
`src/main.rs`
|
||||
|
||||
```rust
|
||||
#[feature(custom_derive, plugin)]
|
||||
#[plugin(serde_macros)]
|
||||
#![feature(custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
...
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[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);
|
||||
}
|
||||
```
|
||||
|
||||
Serde bundles a high performance JSON serializer and deserializer,
|
||||
[serde::json](http://serde-rs.github.io/serde/serde/json/index.html),
|
||||
which comes with the helper functions
|
||||
[to_string](http://serde-rs.github.io/serde/serde/json/ser/fn.to_string.html)
|
||||
and
|
||||
[from_str](http://serde-rs.github.io/serde/serde/json/de/fn.from_str.html)
|
||||
that make it easy to go to and from JSON:
|
||||
When run, it produces:
|
||||
|
||||
```
|
||||
% cargo run
|
||||
{"x":1,"y":2}
|
||||
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:
|
||||
|
||||
```toml
|
||||
[package]
|
||||
name = "serde_example"
|
||||
version = "0.1.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
serde_codegen = "*"
|
||||
syntex = "*"
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
```
|
||||
|
||||
`src/main.rs`:
|
||||
|
||||
```rust
|
||||
use serde::json;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/main.rs"));
|
||||
```
|
||||
|
||||
`src/main.rs.in`:
|
||||
|
||||
```rust
|
||||
#[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);
|
||||
}
|
||||
```
|
||||
|
||||
This also produces:
|
||||
|
||||
```
|
||||
% cargo run
|
||||
{"x":1,"y":2}
|
||||
Point { x: 1, y: 2 }
|
||||
```
|
||||
|
||||
While this works well with Stable Rust, be aware that the error locations
|
||||
currently are reported in the generated file instead of in the source file. You
|
||||
may find it easier to develop with Nightly Rust and `serde\_macros`, then
|
||||
deploy with Stable Rust and `serde_codegen`. It's possible to combine both
|
||||
approaches in one setup:
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[package]
|
||||
name = "serde_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 = "*", optional = true }
|
||||
syntex = "*"
|
||||
|
||||
[dependencies]
|
||||
serde = "*"
|
||||
serde_json = "*"
|
||||
serde_macros = { version = "*", optional = true }
|
||||
```
|
||||
|
||||
`build.rs`:
|
||||
|
||||
```rust
|
||||
#[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();
|
||||
}
|
||||
```
|
||||
|
||||
`src/main.rs`:
|
||||
|
||||
```rust
|
||||
#![cfg_attr(feature = "serde_macros", feature(custom_derive, plugin))]
|
||||
#![cfg_attr(feature = "serde_macros", 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"));
|
||||
```
|
||||
|
||||
The `src/main.rs.in` is the same as before.
|
||||
|
||||
Then to run with stable:
|
||||
|
||||
```
|
||||
% cargo build
|
||||
...
|
||||
|
||||
let point = Point { x: 1, y: 2 };
|
||||
let serialized_point = json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point); // prints: {"x":1,"y":2}
|
||||
|
||||
let deserialize_point: Point = json::from_str(&serialized_point).unwrap();
|
||||
```
|
||||
|
||||
[serde::json](http://serde-rs.github.io/serde/serde/json/index.html) also
|
||||
supports a generic
|
||||
[Value](http://serde-rs.github.io/serde/serde/json/value/enum.Value.html)
|
||||
type, which can represent any JSON value. Also, any
|
||||
[Serialize](http://serde-rs.github.io/serde/serde/ser/trait.Serialize.html)
|
||||
and
|
||||
[Deserialize](http://serde-rs.github.io/serde/serde/de/trait.Deserialize.html)
|
||||
can be converted into a
|
||||
[Value](http://serde-rs.github.io/serde/serde/json/value/enum.Value.html)
|
||||
with the methods
|
||||
[to_value](http://serde-rs.github.io/serde/serde/json/value/fn.to_value.html)
|
||||
and
|
||||
[from_value](http://serde-rs.github.io/serde/serde/json/value/fn.from_value.html):
|
||||
Or with nightly:
|
||||
|
||||
```rust
|
||||
let point = Point { x: 1, y: 2 };
|
||||
let point_value = json::to_value(&point).unwrap();
|
||||
|
||||
println!("{}", point_value.find("x")); // prints: Some(1)
|
||||
|
||||
let deserialize_point: Point = json::from_value(point_value).unwrap();
|
||||
% cargo build --features nightly --no-default-features
|
||||
...
|
||||
```
|
||||
|
||||
Serialization without Macros
|
||||
@@ -174,10 +324,12 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
||||
```
|
||||
|
||||
Serializing structs follow this same pattern. In fact, structs are represented
|
||||
as a named map. It's visitor uses a simple state machine to iterate through all
|
||||
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,
|
||||
@@ -187,7 +339,7 @@ impl serde::Serialize for Point {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: serde::Serializer
|
||||
{
|
||||
serializer.visit_named_map("Point", PointMapVisitor {
|
||||
serializer.visit_struct("Point", PointMapVisitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
})
|
||||
@@ -206,11 +358,11 @@ impl<'a> serde::ser::MapVisitor for PointMapVisitor<'a> {
|
||||
match self.state {
|
||||
0 => {
|
||||
self.state += 1;
|
||||
Ok(Some(try!(serializer.visit_map_elt("x", &self.value.x))))
|
||||
Ok(Some(try!(serializer.visit_struct_elt("x", &self.value.x))))
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
Ok(Some(try!(serializer.visit_map_elt("y", &self.value.y))))
|
||||
Ok(Some(try!(serializer.visit_struct_elt("y", &self.value.y))))
|
||||
}
|
||||
_ => {
|
||||
Ok(None)
|
||||
@@ -275,7 +427,7 @@ to generate an error for a few common error conditions. Here's how it could be u
|
||||
fn visit_string<E>(&mut self, _: String) -> Result<i32, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(serde::de::Error::syntax_error())
|
||||
Err(serde::de::Error::syntax("expect a string"))
|
||||
}
|
||||
|
||||
...
|
||||
@@ -346,6 +498,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,
|
||||
@@ -355,35 +514,32 @@ impl serde::Deserialize for PointField {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<PointField, D::Error>
|
||||
where D: serde::de::Deserializer
|
||||
{
|
||||
struct FieldVisitor;
|
||||
struct PointFieldVisitor;
|
||||
|
||||
impl serde::de::Visitor for FieldVisitor {
|
||||
type Value = Field;
|
||||
impl serde::de::Visitor for PointFieldVisitor {
|
||||
type Value = PointField;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<PointField, E>
|
||||
where E: serde::de::Error
|
||||
{
|
||||
match value {
|
||||
"x" => Ok(Field::X),
|
||||
"y" => Ok(Field::Y),
|
||||
_ => Err(serde::de::Error::syntax_error()),
|
||||
"x" => Ok(PointField::X),
|
||||
"y" => Ok(PointField::Y),
|
||||
_ => Err(serde::de::Error::syntax("expected x or y")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(FieldVisitor)
|
||||
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
|
||||
{
|
||||
deserializer.visit_named_map("Point", PointVisitor)
|
||||
static FIELDS: &'static [&'static str] = &["x", "y"];
|
||||
deserializer.visit_struct("Point", FIELDS, PointVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,8 +556,8 @@ impl serde::de::Visitor for PointVisitor {
|
||||
|
||||
loop {
|
||||
match try!(visitor.visit_key()) {
|
||||
Some(Field::X) => { x = Some(try!(visitor.visit_value())); }
|
||||
Some(Field::Y) => { y = Some(try!(visitor.visit_value())); }
|
||||
Some(PointField::X) => { x = Some(try!(visitor.visit_value())); }
|
||||
Some(PointField::Y) => { y = Some(try!(visitor.visit_value())); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
@@ -422,3 +578,29 @@ impl serde::de::Visitor for PointVisitor {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
=================================
|
||||
|
||||
| Format | Name |
|
||||
| ------ | ---- |
|
||||
| Bincode | [bincode](https://crates.io/crates/bincode) |
|
||||
| JSON | [serde\_json](https://crates.io/crates/serde_json) |
|
||||
| MessagePack | [rmp](https://crates.io/crates/rmp) |
|
||||
| XML | [serde\_xml](https://github.com/serde-rs/xml) |
|
||||
| YAML | [serde\_yaml](https://github.com/serde-rs/yaml/) |
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,64 +0,0 @@
|
||||
#![feature(custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use serde::json;
|
||||
|
||||
// Creating serializable types with serde is quite simple with `serde_macros`. It implements a
|
||||
// syntax extension that automatically generates the necessary serde trait implementations.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let point = Point { x: 5, y: 6 };
|
||||
|
||||
// Serializing to JSON is pretty simple by using the `to_string` method:
|
||||
let serialized_point = json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
//
|
||||
// {"x":5,"y":6}
|
||||
|
||||
// There is also support for pretty printing using `to_string_pretty`:
|
||||
let serialized_point = json::to_string_pretty(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
//
|
||||
// {
|
||||
// "x":5,
|
||||
// "y":6
|
||||
// }
|
||||
|
||||
// Values can also be deserialized with the same style using `from_str`:
|
||||
let deserialized_point: Point = json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_point);
|
||||
// prints:
|
||||
//
|
||||
// Point { x: 5, y: 6 }
|
||||
|
||||
// `Point`s aren't the only type that can be serialized to. Because `Point` members have the
|
||||
// same type, they can be also serialized into a map. Also,
|
||||
let deserialized_map: BTreeMap<String, i64> = json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_map);
|
||||
// prints:
|
||||
//
|
||||
// {"x": 5, "y": 6}
|
||||
|
||||
// If you need to accept arbitrary data, you can also deserialize into `json::Value`, which
|
||||
// can represent all JSON values.
|
||||
let deserialized_value: json::Value = json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_value);
|
||||
// prints:
|
||||
//
|
||||
// {"x":5,"y":6}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "0.6.7"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://serde-rs.github.io/serde/serde/serde/index.html"
|
||||
readme = "../README.md"
|
||||
keywords = ["serde", "serialization"]
|
||||
|
||||
[dependencies]
|
||||
num = "^0.1.27"
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
num-impls = ["num-bigint", "num-complex", "num-rational"]
|
||||
num-bigint = ["num/bigint"]
|
||||
num-complex = ["num/complex"]
|
||||
num-rational = ["num/rational"]
|
||||
@@ -1,6 +1,8 @@
|
||||
//! Helper module to enable serializing bytes more efficiently
|
||||
|
||||
use std::ops;
|
||||
use std::fmt;
|
||||
use std::ascii;
|
||||
|
||||
use ser;
|
||||
use de;
|
||||
@@ -8,10 +10,17 @@ use de;
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `Bytes` wraps a `&[u8]` in order to serialize into a byte array.
|
||||
#[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Bytes<'a> {
|
||||
bytes: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> fmt::Debug for Bytes<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "b\"{}\"", escape_bytestring(self.bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u8]> for Bytes<'a> {
|
||||
fn from(bytes: &'a [u8]) -> Self {
|
||||
Bytes {
|
||||
@@ -28,6 +37,12 @@ impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Into<&'a [u8]> for Bytes<'a> {
|
||||
fn into(self) -> &'a [u8] {
|
||||
self.bytes
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ops::Deref for Bytes<'a> {
|
||||
type Target = [u8];
|
||||
|
||||
@@ -45,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.
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
/// `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)
|
||||
@@ -65,10 +82,22 @@ impl ByteBuf {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for ByteBuf where T: Into<Vec<u8>> {
|
||||
fn from(bytes: T) -> Self {
|
||||
impl fmt::Debug for ByteBuf {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "b\"{}\"", escape_bytestring(self.bytes.as_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Vec<u8>> for ByteBuf {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.bytes
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for ByteBuf {
|
||||
fn from(bytes: Vec<u8>) -> Self {
|
||||
ByteBuf {
|
||||
bytes: bytes.into(),
|
||||
bytes: bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,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 {
|
||||
@@ -172,3 +202,15 @@ impl de::Deserialize for ByteBuf {
|
||||
deserializer.visit_bytes(ByteBufVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn escape_bytestring(bytes: &[u8]) -> String {
|
||||
let mut result = String::new();
|
||||
for &b in bytes {
|
||||
for esc in ascii::escape_default(b) {
|
||||
result.push(esc as char);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,981 @@
|
||||
//! This module contains `Deserialize` and `Visitor` implementations.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{
|
||||
BinaryHeap,
|
||||
BTreeMap,
|
||||
BTreeSet,
|
||||
LinkedList,
|
||||
HashMap,
|
||||
HashSet,
|
||||
VecDeque,
|
||||
};
|
||||
#[cfg(feature = "nightly")]
|
||||
use collections::enum_set::{CLike, EnumSet};
|
||||
use std::hash::Hash;
|
||||
use std::marker::PhantomData;
|
||||
use std::path;
|
||||
use std::rc::Rc;
|
||||
use std::str;
|
||||
use std::sync::Arc;
|
||||
|
||||
use num::FromPrimitive;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
use core::nonzero::{NonZero, Zeroable};
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::num::Zero;
|
||||
|
||||
use de::{
|
||||
Deserialize,
|
||||
Deserializer,
|
||||
EnumVisitor,
|
||||
Error,
|
||||
MapVisitor,
|
||||
SeqVisitor,
|
||||
Type,
|
||||
VariantVisitor,
|
||||
Visitor,
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A visitor that produces a `()`.
|
||||
pub struct UnitVisitor;
|
||||
|
||||
impl Visitor for UnitVisitor {
|
||||
type Value = ();
|
||||
|
||||
fn visit_unit<E>(&mut self) -> Result<(), E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
visitor.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for () {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<(), D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_unit(UnitVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A visitor that produces a `bool`.
|
||||
pub struct BoolVisitor;
|
||||
|
||||
impl Visitor for BoolVisitor {
|
||||
type Value = bool;
|
||||
|
||||
fn visit_bool<E>(&mut self, v: bool) -> Result<bool, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, s: &str) -> Result<bool, E>
|
||||
where E: Error,
|
||||
{
|
||||
match s.trim() {
|
||||
"true" => Ok(true),
|
||||
"false" => Ok(false),
|
||||
_ => Err(Error::type_mismatch(Type::Bool)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for bool {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<bool, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_bool(BoolVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! impl_deserialize_num_method {
|
||||
($src_ty:ty, $method:ident, $from_method:ident, $ty:expr) => {
|
||||
#[inline]
|
||||
fn $method<E>(&mut self, v: $src_ty) -> Result<T, E>
|
||||
where E: Error,
|
||||
{
|
||||
match FromPrimitive::$from_method(v) {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(Error::type_mismatch($ty)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Deserialize + FromPrimitive + str::FromStr
|
||||
> Visitor for PrimitiveVisitor<T> {
|
||||
type Value = T;
|
||||
|
||||
impl_deserialize_num_method!(isize, visit_isize, from_isize, Type::Isize);
|
||||
impl_deserialize_num_method!(i8, visit_i8, from_i8, Type::I8);
|
||||
impl_deserialize_num_method!(i16, visit_i16, from_i16, Type::I16);
|
||||
impl_deserialize_num_method!(i32, visit_i32, from_i32, Type::I32);
|
||||
impl_deserialize_num_method!(i64, visit_i64, from_i64, Type::I64);
|
||||
impl_deserialize_num_method!(usize, visit_usize, from_usize, Type::Usize);
|
||||
impl_deserialize_num_method!(u8, visit_u8, from_u8, Type::U8);
|
||||
impl_deserialize_num_method!(u16, visit_u16, from_u16, Type::U16);
|
||||
impl_deserialize_num_method!(u32, visit_u32, from_u32, Type::U32);
|
||||
impl_deserialize_num_method!(u64, visit_u64, from_u64, Type::U64);
|
||||
impl_deserialize_num_method!(f32, visit_f32, from_f32, Type::F32);
|
||||
impl_deserialize_num_method!(f64, visit_f64, from_f64, Type::F64);
|
||||
|
||||
#[inline]
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_deserialize_num {
|
||||
($ty:ty, $method:ident) => {
|
||||
impl Deserialize for $ty {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.$method(PrimitiveVisitor::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_deserialize_num!(isize, visit_isize);
|
||||
impl_deserialize_num!(i8, visit_i8);
|
||||
impl_deserialize_num!(i16, visit_i16);
|
||||
impl_deserialize_num!(i32, visit_i32);
|
||||
impl_deserialize_num!(i64, visit_i64);
|
||||
impl_deserialize_num!(usize, visit_usize);
|
||||
impl_deserialize_num!(u8, visit_u8);
|
||||
impl_deserialize_num!(u16, visit_u16);
|
||||
impl_deserialize_num!(u32, visit_u32);
|
||||
impl_deserialize_num!(u64, visit_u64);
|
||||
impl_deserialize_num!(f32, visit_f32);
|
||||
impl_deserialize_num!(f64, visit_f64);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct CharVisitor;
|
||||
|
||||
impl Visitor for CharVisitor {
|
||||
type Value = char;
|
||||
|
||||
#[inline]
|
||||
fn visit_char<E>(&mut self, v: char) -> Result<char, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<char, E>
|
||||
where E: Error,
|
||||
{
|
||||
let mut iter = v.chars();
|
||||
if let Some(v) = iter.next() {
|
||||
if iter.next().is_some() {
|
||||
Err(Error::type_mismatch(Type::Char))
|
||||
} else {
|
||||
Ok(v)
|
||||
}
|
||||
} else {
|
||||
Err(Error::end_of_stream())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for char {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<char, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_char(CharVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct StringVisitor;
|
||||
|
||||
impl Visitor for StringVisitor {
|
||||
type Value = String;
|
||||
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v.to_string())
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
match str::from_utf8(v) {
|
||||
Ok(s) => Ok(s.to_string()),
|
||||
Err(_) => Err(Error::type_mismatch(Type::String)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_byte_buf<'a, E>(&mut self, v: Vec<u8>) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
match String::from_utf8(v) {
|
||||
Ok(s) => Ok(s),
|
||||
Err(_) => Err(Error::type_mismatch(Type::String)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for String {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_string(StringVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct OptionVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Deserialize,
|
||||
> Visitor for OptionVisitor<T> {
|
||||
type Value = Option<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
Ok(Some(try!(Deserialize::deserialize(deserializer))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for Option<T> where T: Deserialize {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_option(OptionVisitor { marker: PhantomData })
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! seq_impl {
|
||||
(
|
||||
$ty:ty,
|
||||
< $($constraints:ident),* >,
|
||||
$visitor_name:ident,
|
||||
$visitor:ident,
|
||||
$ctor:expr,
|
||||
$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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for $visitor_name<T>
|
||||
where T: $($constraints +)*,
|
||||
{
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<$ty, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok($ctor)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut $visitor: V) -> Result<$ty, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
let mut values = $with_capacity;
|
||||
|
||||
while let Some(value) = try!($visitor.visit()) {
|
||||
$insert(&mut values, value);
|
||||
}
|
||||
|
||||
try!($visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for $ty
|
||||
where T: $($constraints +)*,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_seq($visitor_name::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seq_impl!(
|
||||
BinaryHeap<T>,
|
||||
<Deserialize, Ord>,
|
||||
BinaryHeapVisitor,
|
||||
visitor,
|
||||
BinaryHeap::new(),
|
||||
BinaryHeap::with_capacity(visitor.size_hint().0),
|
||||
BinaryHeap::push);
|
||||
|
||||
seq_impl!(
|
||||
BTreeSet<T>,
|
||||
<Deserialize, Eq, Ord>,
|
||||
BTreeSetVisitor,
|
||||
visitor,
|
||||
BTreeSet::new(),
|
||||
BTreeSet::new(),
|
||||
BTreeSet::insert);
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
seq_impl!(
|
||||
EnumSet<T>,
|
||||
<Deserialize, CLike>,
|
||||
EnumSetVisitor,
|
||||
visitor,
|
||||
EnumSet::new(),
|
||||
EnumSet::new(),
|
||||
EnumSet::insert);
|
||||
|
||||
seq_impl!(
|
||||
LinkedList<T>,
|
||||
<Deserialize>,
|
||||
LinkedListVisitor,
|
||||
visitor,
|
||||
LinkedList::new(),
|
||||
LinkedList::new(),
|
||||
LinkedList::push_back);
|
||||
|
||||
seq_impl!(
|
||||
HashSet<T>,
|
||||
<Deserialize, Eq, Hash>,
|
||||
HashSetVisitor,
|
||||
visitor,
|
||||
HashSet::new(),
|
||||
HashSet::with_capacity(visitor.size_hint().0),
|
||||
HashSet::insert);
|
||||
|
||||
seq_impl!(
|
||||
Vec<T>,
|
||||
<Deserialize>,
|
||||
VecVisitor,
|
||||
visitor,
|
||||
Vec::new(),
|
||||
Vec::with_capacity(visitor.size_hint().0),
|
||||
Vec::push);
|
||||
|
||||
seq_impl!(
|
||||
VecDeque<T>,
|
||||
<Deserialize>,
|
||||
VecDequeVisitor,
|
||||
visitor,
|
||||
VecDeque::new(),
|
||||
VecDeque::with_capacity(visitor.size_hint().0),
|
||||
VecDeque::push_back);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct ArrayVisitor0<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> ArrayVisitor0<T> {
|
||||
/// Construct a `ArrayVisitor0<T>`.
|
||||
pub fn new() -> Self {
|
||||
ArrayVisitor0 {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for ArrayVisitor0<T> where T: Deserialize + Default {
|
||||
type Value = [T; 0];
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<[T; 0], E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok([T::default(); 0])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<[T; 0], V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
try!(visitor.end());
|
||||
Ok([T::default(); 0])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for [T; 0]
|
||||
where T: Deserialize + Default
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_seq(ArrayVisitor0::new())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! array_impls {
|
||||
($($visitor:ident, $len:expr => ($($name:ident),+),)+) => {
|
||||
$(
|
||||
struct $visitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> $visitor<T> {
|
||||
/// Construct a `ArrayVisitor*<T>`.
|
||||
pub fn new() -> Self {
|
||||
$visitor {
|
||||
marker: PhantomData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for $visitor<T> where T: Deserialize {
|
||||
type Value = [T; $len];
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<[T; $len], V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
$(
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(val) => val,
|
||||
None => { return Err(Error::end_of_stream()); }
|
||||
};
|
||||
)+;
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok([$($name,)+])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for [T; $len]
|
||||
where T: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_seq($visitor::new())
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
array_impls! {
|
||||
ArrayVisitor1, 1 => (a),
|
||||
ArrayVisitor2, 2 => (a, b),
|
||||
ArrayVisitor3, 3 => (a, b, c),
|
||||
ArrayVisitor4, 4 => (a, b, c, d),
|
||||
ArrayVisitor5, 5 => (a, b, c, d, e),
|
||||
ArrayVisitor6, 6 => (a, b, c, d, e, f),
|
||||
ArrayVisitor7, 7 => (a, b, c, d, e, f, g),
|
||||
ArrayVisitor8, 8 => (a, b, c, d, e, f, g, h),
|
||||
ArrayVisitor9, 9 => (a, b, c, d, e, f, g, h, i),
|
||||
ArrayVisitor10, 10 => (a, b, c, d, e, f, g, h, i, j),
|
||||
ArrayVisitor11, 11 => (a, b, c, d, e, f, g, h, i, j, k),
|
||||
ArrayVisitor12, 12 => (a, b, c, d, e, f, g, h, i, j, k, l),
|
||||
ArrayVisitor13, 13 => (a, b, c, d, e, f, g, h, i, j, k, l, m),
|
||||
ArrayVisitor14, 14 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n),
|
||||
ArrayVisitor15, 15 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o),
|
||||
ArrayVisitor16, 16 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p),
|
||||
ArrayVisitor17, 17 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q),
|
||||
ArrayVisitor18, 18 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r),
|
||||
ArrayVisitor19, 19 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s),
|
||||
ArrayVisitor20, 20 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s ,t),
|
||||
ArrayVisitor21, 21 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u),
|
||||
ArrayVisitor22, 22 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v),
|
||||
ArrayVisitor23, 23 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w),
|
||||
ArrayVisitor24, 24 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x),
|
||||
ArrayVisitor25, 25 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y),
|
||||
ArrayVisitor26, 26 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z),
|
||||
ArrayVisitor27, 27 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa),
|
||||
ArrayVisitor28, 28 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa, ab),
|
||||
ArrayVisitor29, 29 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa, ab, ac),
|
||||
ArrayVisitor30, 30 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa, ab, ac, ad),
|
||||
ArrayVisitor31, 31 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa, ab, ac, ad, ae),
|
||||
ArrayVisitor32, 32 => (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
|
||||
y, z, aa, ab, ac, ad, ae, af),
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! tuple_impls {
|
||||
() => {};
|
||||
($($len:expr => $visitor:ident => ($($name:ident),+),)+) => {
|
||||
$(
|
||||
/// Construct a tuple visitor.
|
||||
pub struct $visitor<$($name,)+> {
|
||||
marker: PhantomData<($($name,)+)>,
|
||||
}
|
||||
|
||||
impl<
|
||||
$($name: Deserialize,)+
|
||||
> $visitor<$($name,)+> {
|
||||
/// Construct a `TupleVisitor*<T>`.
|
||||
pub fn new() -> Self {
|
||||
$visitor { marker: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<
|
||||
$($name: Deserialize,)+
|
||||
> Visitor for $visitor<$($name,)+> {
|
||||
type Value = ($($name,)+);
|
||||
|
||||
#[inline]
|
||||
#[allow(non_snake_case)]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<($($name,)+), V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
$(
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
None => { return Err(Error::end_of_stream()); }
|
||||
};
|
||||
)+;
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(($($name,)+))
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
$($name: Deserialize),+
|
||||
> Deserialize for ($($name,)+) {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_tuple($len, $visitor::new())
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
tuple_impls! {
|
||||
1 => TupleVisitor1 => (T0),
|
||||
2 => TupleVisitor2 => (T0, T1),
|
||||
3 => TupleVisitor3 => (T0, T1, T2),
|
||||
4 => TupleVisitor4 => (T0, T1, T2, T3),
|
||||
5 => TupleVisitor5 => (T0, T1, T2, T3, T4),
|
||||
6 => TupleVisitor6 => (T0, T1, T2, T3, T4, T5),
|
||||
7 => TupleVisitor7 => (T0, T1, T2, T3, T4, T5, T6),
|
||||
8 => TupleVisitor8 => (T0, T1, T2, T3, T4, T5, T6, T7),
|
||||
9 => TupleVisitor9 => (T0, T1, T2, T3, T4, T5, T6, T7, T8),
|
||||
10 => TupleVisitor10 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
|
||||
11 => TupleVisitor11 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
|
||||
12 => TupleVisitor12 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! map_impl {
|
||||
(
|
||||
$ty:ty,
|
||||
< $($constraints:ident),* >,
|
||||
$visitor_name:ident,
|
||||
$visitor:ident,
|
||||
$ctor:expr,
|
||||
$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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Visitor for $visitor_name<K, V>
|
||||
where K: $($constraints +)*,
|
||||
V: Deserialize,
|
||||
{
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<$ty, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok($ctor)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<Visitor>(&mut self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error>
|
||||
where Visitor: MapVisitor,
|
||||
{
|
||||
let mut values = $with_capacity;
|
||||
|
||||
while let Some((key, value)) = try!($visitor.visit()) {
|
||||
$insert(&mut values, key, value);
|
||||
}
|
||||
|
||||
try!($visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Deserialize for $ty
|
||||
where K: $($constraints +)*,
|
||||
V: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_map($visitor_name::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
map_impl!(
|
||||
BTreeMap<K, V>,
|
||||
<Deserialize, Eq, Ord>,
|
||||
BTreeMapVisitor,
|
||||
visitor,
|
||||
BTreeMap::new(),
|
||||
BTreeMap::new(),
|
||||
BTreeMap::insert);
|
||||
|
||||
map_impl!(
|
||||
HashMap<K, V>,
|
||||
<Deserialize, Eq, Hash>,
|
||||
HashMapVisitor,
|
||||
visitor,
|
||||
HashMap::new(),
|
||||
HashMap::with_capacity(visitor.size_hint().0),
|
||||
HashMap::insert);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct PathBufVisitor;
|
||||
|
||||
impl Visitor for PathBufVisitor {
|
||||
type Value = path::PathBuf;
|
||||
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<path::PathBuf, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(From::from(v))
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<path::PathBuf, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_str(&v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for path::PathBuf {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(PathBufVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T: Deserialize> Deserialize for Box<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Box<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Box::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Arc<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Arc::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Rc<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Rc<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Rc::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Cow<'a, T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Cow::Owned(val))
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
|
||||
let value = try!(Deserialize::deserialize(deserializer));
|
||||
if value == Zero::zero() {
|
||||
return Err(Error::syntax("expected a non-zero value"))
|
||||
}
|
||||
unsafe {
|
||||
Ok(NonZero::new(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Result<T, E>, D::Error>
|
||||
where D: Deserializer {
|
||||
enum Field {
|
||||
Ok,
|
||||
Err,
|
||||
}
|
||||
|
||||
impl Deserialize for Field {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
|
||||
where D: Deserializer
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl ::de::Visitor for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn visit_usize<E>(&mut self, value: usize) -> Result<Field, E> where E: Error {
|
||||
match value {
|
||||
0 => Ok(Field::Ok),
|
||||
1 => Ok(Field::Err),
|
||||
_ => Err(Error::unknown_field(&value.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E> where E: Error {
|
||||
match value {
|
||||
"Ok" => Ok(Field::Ok),
|
||||
"Err" => Ok(Field::Err),
|
||||
_ => Err(Error::unknown_field(value)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(&mut self, value: &[u8]) -> Result<Field, E> where E: Error {
|
||||
match value {
|
||||
b"Ok" => Ok(Field::Ok),
|
||||
b"Err" => Ok(Field::Err),
|
||||
_ => {
|
||||
match str::from_utf8(value) {
|
||||
Ok(value) => Err(Error::unknown_field(value)),
|
||||
Err(_) => Err(Error::type_mismatch(Type::String)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(FieldVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct Visitor<T, E>(PhantomData<Result<T, E>>);
|
||||
|
||||
impl<T, E> EnumVisitor for Visitor<T, E>
|
||||
where T: Deserialize,
|
||||
E: Deserialize
|
||||
{
|
||||
type Value = Result<T, E>;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<Result<T, E>, V::Error>
|
||||
where V: VariantVisitor
|
||||
{
|
||||
match try!(visitor.visit_variant()) {
|
||||
Field::Ok => {
|
||||
let value = try!(visitor.visit_newtype());
|
||||
Ok(Ok(value))
|
||||
}
|
||||
Field::Err => {
|
||||
let value = try!(visitor.visit_newtype());
|
||||
Ok(Err(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const VARIANTS: &'static [&'static str] = &["Ok", "Err"];
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,830 @@
|
||||
//! Generic deserialization framework.
|
||||
|
||||
pub mod impls;
|
||||
pub mod value;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `Error` is a trait that allows a `Deserialize` to generically create a
|
||||
/// `Deserializer` error.
|
||||
pub trait Error: Sized {
|
||||
/// Raised when there is general error when deserializing a type.
|
||||
fn syntax(msg: &str) -> Self;
|
||||
|
||||
/// Raised when a fixed sized sequence or map was passed in the wrong amount of arguments.
|
||||
fn length_mismatch(_len: usize) -> Self {
|
||||
Error::syntax("incorrect length")
|
||||
}
|
||||
|
||||
/// Raised when a `Deserialize` was passed an incorrect type.
|
||||
fn type_mismatch(_type: Type) -> Self {
|
||||
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;
|
||||
|
||||
/// Raised when a `Deserialize` struct type received an unexpected struct field.
|
||||
fn unknown_field(field: &str) -> Self;
|
||||
|
||||
/// Raised when a `Deserialize` struct type did not receive a field.
|
||||
fn missing_field(field: &'static str) -> Self;
|
||||
}
|
||||
|
||||
/// `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>
|
||||
where D: Deserializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `Deserializer` is a trait that can deserialize values by threading a `Visitor` trait through a
|
||||
/// value. It supports two entry point styles which enables different kinds of deserialization.
|
||||
///
|
||||
/// 1) The `visit` method. File formats like JSON embed the type of it's construct in it's file
|
||||
/// format. This allows the `Deserializer` to deserialize into a generic type like
|
||||
/// `json::Value`, which can represent all JSON types.
|
||||
///
|
||||
/// 2) The `visit_*` methods. File formats like bincode do not embed in it's format how to decode
|
||||
/// it's values. It relies instead on the `Deserialize` type to hint to the `Deserializer` with
|
||||
/// the `visit_*` methods how it should parse the next value. One downside though to only
|
||||
/// 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.
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor;
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `bool` value.
|
||||
#[inline]
|
||||
fn visit_bool<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `usize` value.
|
||||
#[inline]
|
||||
fn visit_usize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_u64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `u8` value.
|
||||
#[inline]
|
||||
fn visit_u8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_u64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `u16` value.
|
||||
#[inline]
|
||||
fn visit_u16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_u64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `u32` value.
|
||||
#[inline]
|
||||
fn visit_u32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_u64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `u64` value.
|
||||
#[inline]
|
||||
fn visit_u64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `isize` value.
|
||||
#[inline]
|
||||
fn visit_isize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_i64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `i8` value.
|
||||
#[inline]
|
||||
fn visit_i8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_i64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `i16` value.
|
||||
#[inline]
|
||||
fn visit_i16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_i64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `i32` value.
|
||||
#[inline]
|
||||
fn visit_i32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_i64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `i64` value.
|
||||
#[inline]
|
||||
fn visit_i64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `f32` value.
|
||||
#[inline]
|
||||
fn visit_f32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_f64(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `f64` value.
|
||||
#[inline]
|
||||
fn visit_f64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `char` value.
|
||||
#[inline]
|
||||
fn visit_char<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `&str` value.
|
||||
#[inline]
|
||||
fn visit_str<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `String` value.
|
||||
#[inline]
|
||||
fn visit_string<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_str(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `unit` value.
|
||||
#[inline]
|
||||
fn visit_unit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an `Option` value. This allows
|
||||
/// deserializers that encode an optional value as a nullable value to convert the null value
|
||||
/// into a `None`, and a regular value as `Some(value)`.
|
||||
#[inline]
|
||||
fn visit_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a sequence value. This allows
|
||||
/// deserializers to parse sequences that aren't tagged as sequences.
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a map of values. This allows
|
||||
/// deserializers to parse sequences that aren't tagged as maps.
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a unit struct. This allows
|
||||
/// deserializers to a unit struct that aren't tagged as a unit struct.
|
||||
#[inline]
|
||||
fn visit_unit_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_unit(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a newtype struct. This allows
|
||||
/// deserializers to a newtype struct that aren't tagged as a newtype struct.
|
||||
#[inline]
|
||||
fn visit_newtype_struct<V>(&mut self,
|
||||
name: &'static str,
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_tuple_struct(name, 1, visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a tuple struct. This allows
|
||||
/// deserializers to parse sequences that aren't tagged as sequences.
|
||||
#[inline]
|
||||
fn visit_tuple_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_tuple(len, visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a struct. This allows
|
||||
/// deserializers to parse sequences that aren't tagged as maps.
|
||||
#[inline]
|
||||
fn visit_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
_fields: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a tuple value. This allows
|
||||
/// deserializers that provide a custom tuple serialization to properly deserialize the type.
|
||||
#[inline]
|
||||
fn visit_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting an enum value. This allows
|
||||
/// deserializers that provide a custom enumeration serialization to properly deserialize the
|
||||
/// type.
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self,
|
||||
_enum: &'static str,
|
||||
_variants: &'static [&'static str],
|
||||
_visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: EnumVisitor,
|
||||
{
|
||||
Err(Error::syntax("expected an enum"))
|
||||
}
|
||||
|
||||
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows
|
||||
/// deserializers that provide a custom byte vector serialization to properly deserialize the
|
||||
/// type.
|
||||
#[inline]
|
||||
fn visit_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
/// Specify a format string for the deserializer.
|
||||
///
|
||||
/// The deserializer format is used to determine which format
|
||||
/// specific field attributes should be used with the
|
||||
/// deserializer.
|
||||
fn format() -> &'static str {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// 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,
|
||||
{
|
||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||
// doesn't support encoding a `char` into a stack buffer.
|
||||
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,
|
||||
{
|
||||
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,
|
||||
{
|
||||
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,
|
||||
{
|
||||
self.visit_bytes(&v)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
|
||||
type Error = V::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, V::Error>
|
||||
where T: Deserialize
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(&mut self) -> Result<(), V::Error> {
|
||||
(**self).end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(**self).size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `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,
|
||||
V: Deserialize,
|
||||
{
|
||||
match try!(self.visit_key()) {
|
||||
Some(key) => {
|
||||
let value = try!(self.visit_value());
|
||||
Ok(Some((key, value)))
|
||||
}
|
||||
None => Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
{
|
||||
Err(Error::missing_field(field))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
||||
type Error = V_::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
|
||||
where K: Deserialize,
|
||||
V: Deserialize,
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, V_::Error>
|
||||
where K: Deserialize
|
||||
{
|
||||
(**self).visit_key()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_value<V>(&mut self) -> Result<V, V_::Error>
|
||||
where V: Deserialize
|
||||
{
|
||||
(**self).visit_value()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(&mut self) -> Result<(), V_::Error> {
|
||||
(**self).end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(**self).size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `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;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `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.
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Self::Error>
|
||||
where V: Deserialize;
|
||||
|
||||
/// `visit_unit` is called when deserializing a variant with no values.
|
||||
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
||||
Err(Error::type_mismatch(Type::UnitVariant))
|
||||
}
|
||||
|
||||
/// `visit_newtype` is called when deserializing a variant with a single value. By default this
|
||||
/// uses the `visit_tuple` method to deserialize the value.
|
||||
#[inline]
|
||||
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
||||
where T: Deserialize,
|
||||
{
|
||||
let (value,) = try!(self.visit_tuple(1, impls::TupleVisitor1::new()));
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// `visit_tuple` is called when deserializing a tuple-like variant.
|
||||
fn visit_tuple<V>(&mut self,
|
||||
_len: usize,
|
||||
_visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor
|
||||
{
|
||||
Err(Error::type_mismatch(Type::TupleVariant))
|
||||
}
|
||||
|
||||
/// `visit_struct` is called when deserializing a struct-like variant.
|
||||
fn visit_struct<V>(&mut self,
|
||||
_fields: &'static [&'static str],
|
||||
_visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor
|
||||
{
|
||||
Err(Error::type_mismatch(Type::StructVariant))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
||||
type Error = T::Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, T::Error>
|
||||
where V: Deserialize
|
||||
{
|
||||
(**self).visit_variant()
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), T::Error> {
|
||||
(**self).visit_unit()
|
||||
}
|
||||
|
||||
fn visit_newtype<D>(&mut self) -> Result<D, T::Error>
|
||||
where D: Deserialize,
|
||||
{
|
||||
(**self).visit_newtype()
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(&mut self,
|
||||
len: usize,
|
||||
visitor: V) -> Result<V::Value, T::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
(**self).visit_tuple(len, visitor)
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, T::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
(**self).visit_struct(fields, visitor)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
//! This module supports deserializing from primitives with the `ValueDeserializer` trait.
|
||||
|
||||
use std::collections::{
|
||||
BTreeMap,
|
||||
BTreeSet,
|
||||
@@ -12,28 +14,41 @@ use std::hash::Hash;
|
||||
use std::vec;
|
||||
|
||||
use de;
|
||||
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),
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax_error() -> Self { Error::SyntaxError }
|
||||
fn end_of_stream_error() -> Self { Error::EndOfStreamError }
|
||||
fn unknown_field_error(field: &str) -> Self { Error::UnknownFieldError(field.to_string()) }
|
||||
fn missing_field_error(field: &'static str) -> Self { Error::MissingFieldError(field) }
|
||||
fn syntax(_: &str) -> Self { Error::SyntaxError }
|
||||
fn end_of_stream() -> Self { Error::EndOfStreamError }
|
||||
fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(String::from(field)) }
|
||||
fn missing_field(field: &'static str) -> Self { Error::MissingFieldError(field) }
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// 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;
|
||||
}
|
||||
|
||||
@@ -70,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 {
|
||||
@@ -88,7 +104,7 @@ macro_rules! primitive_deserializer {
|
||||
{
|
||||
match self.0.take() {
|
||||
Some(v) => visitor.$method(v),
|
||||
None => Err(de::Error::end_of_stream_error()),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,11 +147,14 @@ impl<'a> de::Deserializer for StrDeserializer<'a> {
|
||||
{
|
||||
match self.0.take() {
|
||||
Some(v) => visitor.visit_str(v),
|
||||
None => Err(de::Error::end_of_stream_error()),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_enum<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
fn visit_enum<V>(&mut self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
visitor.visit(self)
|
||||
@@ -177,11 +196,14 @@ impl de::Deserializer for StringDeserializer {
|
||||
{
|
||||
match self.0.take() {
|
||||
Some(string) => visitor.visit_string(string),
|
||||
None => Err(de::Error::end_of_stream_error()),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_enum<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
fn visit_enum<V>(&mut self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
visitor.visit(self)
|
||||
@@ -204,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,
|
||||
@@ -254,7 +278,7 @@ impl<I, T> de::SeqVisitor for SeqDeserializer<I>
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::end_of_stream_error())
|
||||
Err(de::Error::length_mismatch(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,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,
|
||||
@@ -315,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,
|
||||
@@ -367,7 +393,7 @@ impl<I, K, V> de::MapVisitor for MapDeserializer<I, K, V>
|
||||
let mut de = value.into_deserializer();
|
||||
de::Deserialize::deserialize(&mut de)
|
||||
}
|
||||
None => Err(de::Error::syntax_error())
|
||||
None => Err(de::Error::syntax("expected a map value"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,7 +401,7 @@ impl<I, K, V> de::MapVisitor for MapDeserializer<I, K, V>
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::end_of_stream_error())
|
||||
Err(de::Error::length_mismatch(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,3 +435,58 @@ impl<K, V> ValueDeserializer for HashMap<K, V>
|
||||
MapDeserializer::new(self.into_iter(), len)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'a> ValueDeserializer for bytes::Bytes<'a>
|
||||
{
|
||||
type Deserializer = BytesDeserializer<'a>;
|
||||
|
||||
fn into_deserializer(self) -> BytesDeserializer<'a> {
|
||||
BytesDeserializer(Some(self.into()))
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper deserializer that deserializes a `&[u8]`.
|
||||
pub struct BytesDeserializer<'a> (Option<&'a [u8]>);
|
||||
|
||||
impl<'a> de::Deserializer for BytesDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.0.take() {
|
||||
Some(bytes) => visitor.visit_bytes(bytes),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl ValueDeserializer for bytes::ByteBuf
|
||||
{
|
||||
type Deserializer = ByteBufDeserializer;
|
||||
|
||||
fn into_deserializer(self) -> Self::Deserializer {
|
||||
ByteBufDeserializer(Some(self.into()))
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper deserializer that deserializes a `Vec<u8>`.
|
||||
pub struct ByteBufDeserializer(Option<Vec<u8>>);
|
||||
|
||||
impl de::Deserializer for ByteBufDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.0.take() {
|
||||
Some(bytes) => visitor.visit_byte_buf(bytes),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
use std::io;
|
||||
//! 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,
|
||||
@@ -7,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,
|
||||
@@ -25,12 +30,17 @@ impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Iter> {
|
||||
pub fn get_ref(&self) -> &Iter { &self.iter }
|
||||
|
||||
/// Gets a mutable reference to the underlying iterator.
|
||||
pub fn get_mut(&self) -> &Iter { &self.iter }
|
||||
pub fn get_mut(&mut self) -> &mut Iter { &mut self.iter }
|
||||
|
||||
/// Unwraps this `LineColIterator`, returning the underlying iterator.
|
||||
pub fn into_inner(self) -> Iter { self.iter }
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item=io::Result<u8>>> LineColIterator<Peekable<Iter>> {
|
||||
/// peeks at the next value
|
||||
pub fn peek(&mut self) -> Option<&io::Result<u8>> { self.iter.peek() }
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item=io::Result<u8>>> Iterator for LineColIterator<Iter> {
|
||||
type Item = io::Result<u8>;
|
||||
fn next(&mut self) -> Option<io::Result<u8>> {
|
||||
@@ -5,15 +5,27 @@
|
||||
//! 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.
|
||||
#![doc(html_root_url="http://erickt.github.io/rust-serde")]
|
||||
//!
|
||||
//! 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, enumset, nonzero, step_trait, zero_one))]
|
||||
|
||||
#![deny(missing_docs)]
|
||||
|
||||
extern crate num;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate collections;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
extern crate core;
|
||||
|
||||
pub use ser::{Serialize, Serializer};
|
||||
pub use de::{Deserialize, Deserializer, Error};
|
||||
|
||||
pub mod bytes;
|
||||
pub mod de;
|
||||
pub mod iter;
|
||||
pub mod json;
|
||||
pub mod ser;
|
||||
@@ -1,9 +1,31 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
//! Implementations for all of Rust's builtin types.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{
|
||||
BinaryHeap,
|
||||
BTreeMap,
|
||||
BTreeSet,
|
||||
LinkedList,
|
||||
HashMap,
|
||||
HashSet,
|
||||
VecDeque,
|
||||
};
|
||||
#[cfg(feature = "nightly")]
|
||||
use collections::enum_set::{CLike, EnumSet};
|
||||
use std::hash::Hash;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::iter;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::num;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::ops;
|
||||
use std::path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
use core::nonzero::{NonZero, Zeroable};
|
||||
|
||||
use super::{
|
||||
Serialize,
|
||||
Serializer,
|
||||
@@ -75,8 +97,49 @@ impl<T> Serialize for Option<T> where T: Serialize {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> SeqVisitor for Option<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
match self.take() {
|
||||
Some(value) => {
|
||||
try!(serializer.visit_seq_elt(value));
|
||||
Ok(Some(()))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
Some(if self.is_some() { 1 } else { 0 })
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// 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>,
|
||||
@@ -85,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 {
|
||||
@@ -104,8 +168,8 @@ impl<T, Iter> SeqVisitor for SeqIteratorVisitor<Iter>
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
let value = try!(serializer.visit_seq_elt(value));
|
||||
Ok(Some(value))
|
||||
try!(serializer.visit_seq_elt(value));
|
||||
Ok(Some(()))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
@@ -119,7 +183,7 @@ impl<T, Iter> SeqVisitor for SeqIteratorVisitor<Iter>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'a, T> Serialize for &'a [T]
|
||||
impl<T> Serialize for [T]
|
||||
where T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
@@ -130,12 +194,63 @@ impl<'a, T> Serialize for &'a [T]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Vec<T> where T: Serialize {
|
||||
macro_rules! array_impls {
|
||||
($len:expr) => {
|
||||
impl<T> Serialize for [T; $len] where T: Serialize {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some($len)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
array_impls!(0);
|
||||
array_impls!(1);
|
||||
array_impls!(2);
|
||||
array_impls!(3);
|
||||
array_impls!(4);
|
||||
array_impls!(5);
|
||||
array_impls!(6);
|
||||
array_impls!(7);
|
||||
array_impls!(8);
|
||||
array_impls!(9);
|
||||
array_impls!(10);
|
||||
array_impls!(11);
|
||||
array_impls!(12);
|
||||
array_impls!(13);
|
||||
array_impls!(14);
|
||||
array_impls!(15);
|
||||
array_impls!(16);
|
||||
array_impls!(17);
|
||||
array_impls!(18);
|
||||
array_impls!(19);
|
||||
array_impls!(20);
|
||||
array_impls!(21);
|
||||
array_impls!(22);
|
||||
array_impls!(23);
|
||||
array_impls!(24);
|
||||
array_impls!(25);
|
||||
array_impls!(26);
|
||||
array_impls!(27);
|
||||
array_impls!(28);
|
||||
array_impls!(29);
|
||||
array_impls!(30);
|
||||
array_impls!(31);
|
||||
array_impls!(32);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T> Serialize for BinaryHeap<T>
|
||||
where T: Serialize + Ord
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(&self[..]).serialize(serializer)
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +265,18 @@ impl<T> Serialize for BTreeSet<T>
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<T> Serialize for EnumSet<T>
|
||||
where T: Serialize + CLike
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for HashSet<T>
|
||||
where T: Serialize + Eq + Hash,
|
||||
{
|
||||
@@ -161,6 +288,49 @@ impl<T> Serialize for HashSet<T>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for LinkedList<T>
|
||||
where T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<A> Serialize for ops::Range<A>
|
||||
where A: Serialize + Clone + iter::Step + num::One,
|
||||
for<'a> &'a A: ops::Add<&'a A, Output = A>,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let len = iter::Step::steps_between(&self.start, &self.end, &A::one());
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.clone(), len))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Vec<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(&self[..]).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for VecDeque<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl Serialize for () {
|
||||
@@ -186,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,
|
||||
@@ -210,7 +382,7 @@ macro_rules! tuple_impls {
|
||||
$(
|
||||
$state => {
|
||||
self.state += 1;
|
||||
Ok(Some(try!(serializer.visit_seq_elt(&e!(self.tuple.$idx)))))
|
||||
Ok(Some(try!(serializer.visit_tuple_elt(&e!(self.tuple.$idx)))))
|
||||
}
|
||||
)+
|
||||
_ => {
|
||||
@@ -229,7 +401,7 @@ macro_rules! tuple_impls {
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
|
||||
serializer.visit_seq($TupleVisitor::new(self))
|
||||
serializer.visit_tuple($TupleVisitor::new(self))
|
||||
}
|
||||
}
|
||||
)+
|
||||
@@ -343,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>,
|
||||
@@ -351,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 {
|
||||
@@ -410,20 +605,6 @@ impl<K, V> Serialize for HashMap<K, V>
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: `VecMap` is unstable.
|
||||
/*
|
||||
impl<V> Serialize for VecMap<V>
|
||||
where V: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize {
|
||||
@@ -471,6 +652,30 @@ impl<T> Serialize for Arc<T> where T: Serialize, {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
match *self {
|
||||
Result::Ok(ref value) => {
|
||||
serializer.visit_newtype_variant("Result", 0, "Ok", value)
|
||||
}
|
||||
Result::Err(ref value) => {
|
||||
serializer.visit_newtype_variant("Result", 1, "Err", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl Serialize for path::Path {
|
||||
@@ -488,3 +693,44 @@ impl Serialize for path::PathBuf {
|
||||
self.to_str().unwrap().serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
|
||||
(**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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,354 @@
|
||||
//! Generic serialization framework.
|
||||
|
||||
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.
|
||||
fn visit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_isize` serializes a `isize` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_isize(&mut self, v: isize) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i8` serializes a `i8` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, v: i8) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i16` serializes a `i16` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, v: i16) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i32` serializes a `i32` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, v: i32) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i64` serializes a `i64` value.
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_usize` serializes a `usize` value. By default it casts the value to a `u64` and
|
||||
/// passes it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_usize(&mut self, v: usize) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u8` serializes a `u8` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, v: u8) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u32` serializes a `u32` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, v: u16) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u32` serializes a `u32` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, v: u32) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u64` serializes a `u64` value.
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_f32` serializes a `f32` value. By default it casts the value to a `f64` and passes
|
||||
/// it to the `visit_f64` method.
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, v: f32) -> Result<(), Self::Error> {
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
/// `visit_f64` serializes a `f64` value.
|
||||
fn visit_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_char` serializes a character. By default it serializes it as a `&str` containing a
|
||||
/// single character.
|
||||
#[inline]
|
||||
fn visit_char(&mut self, v: char) -> Result<(), Self::Error> {
|
||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||
// doesn't support encoding a `char` into a stack buffer.
|
||||
self.visit_str(&v.to_string())
|
||||
}
|
||||
|
||||
/// `visit_str` serializes a `&str`.
|
||||
fn visit_str(&mut self, value: &str) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_bytes` is a hook that enables those serialization formats that support serializing
|
||||
/// byte slices separately from generic arrays. By default it serializes as a regular array.
|
||||
#[inline]
|
||||
fn visit_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
|
||||
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,
|
||||
_variant_index: usize,
|
||||
_variant: &'static str) -> Result<(), Self::Error> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
/// The `visit_newtype_struct` allows a tuple struct with a single element, also known as a
|
||||
/// newtyped value, to be more efficiently serialized than a tuple struct with multiple items.
|
||||
/// By default it just serializes the value as a tuple struct sequence.
|
||||
#[inline]
|
||||
fn visit_newtype_struct<T>(&mut self,
|
||||
name: &'static str,
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
self.visit_tuple_struct(name, Some(value))
|
||||
}
|
||||
|
||||
/// The `visit_newtype_variant` allows a variant with a single item to be more efficiently
|
||||
/// serialized than a variant with multiple items. By default it just serializes the value as a
|
||||
/// tuple variant sequence.
|
||||
#[inline]
|
||||
fn visit_newtype_variant<T>(&mut self,
|
||||
name: &'static str,
|
||||
variant_index: usize,
|
||||
variant: &'static str,
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
self.visit_tuple_variant(
|
||||
name,
|
||||
variant_index,
|
||||
variant,
|
||||
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,
|
||||
{
|
||||
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
|
||||
{
|
||||
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,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
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
|
||||
{
|
||||
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,
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
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
|
||||
{
|
||||
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,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
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,
|
||||
value: V) -> Result<(), Self::Error>
|
||||
where V: Serialize,
|
||||
{
|
||||
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,
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
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,
|
||||
value: V) -> Result<(), Self::Error>
|
||||
where V: Serialize,
|
||||
{
|
||||
self.visit_struct_elt(key, value)
|
||||
}
|
||||
|
||||
/// Specify a format string for the serializer.
|
||||
///
|
||||
/// The serializer format is used to determine which format
|
||||
/// specific field attributes should be used with the serializer.
|
||||
fn format() -> &'static str {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that is used by a `Serialize` to iterate through a sequence.
|
||||
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;
|
||||
|
||||
/// Return the length of the sequence if known.
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that is used by a `Serialize` to iterate through a map.
|
||||
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;
|
||||
|
||||
/// Return the length of the map if known.
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "serde_codegen"
|
||||
version = "0.6.9"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html"
|
||||
build = "build.rs"
|
||||
keywords = ["serde", "serialization"]
|
||||
|
||||
[features]
|
||||
default = ["with-syntex"]
|
||||
nightly = ["quasi_macros"]
|
||||
with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"]
|
||||
|
||||
[build-dependencies]
|
||||
quasi_codegen = { version = "^0.4.0", optional = true }
|
||||
syntex = { version = "^0.26.0", optional = true }
|
||||
|
||||
[dependencies]
|
||||
aster = { version = "^0.10.0", default-features = false }
|
||||
quasi = { version = "^0.4.0", default-features = false }
|
||||
quasi_macros = { version = "^0.4.0", optional = true }
|
||||
syntex = { version = "^0.26.0", optional = true }
|
||||
syntex_syntax = { version = "^0.26.0", optional = true }
|
||||
@@ -0,0 +1,28 @@
|
||||
#[cfg(feature = "with-syntex")]
|
||||
mod inner {
|
||||
extern crate syntex;
|
||||
extern crate quasi_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
let mut registry = syntex::Registry::new();
|
||||
quasi_codegen::register(&mut registry);
|
||||
|
||||
let src = Path::new("src/lib.rs.in");
|
||||
let dst = Path::new(&out_dir).join("lib.rs");
|
||||
|
||||
registry.expand("", &src, &dst).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
mod inner {
|
||||
pub fn main() {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
inner::main();
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
/// Represents field name information
|
||||
#[derive(Debug)]
|
||||
pub enum FieldNames {
|
||||
Global(P<ast::Expr>),
|
||||
Format{
|
||||
formats: HashMap<P<ast::Expr>, P<ast::Expr>>,
|
||||
default: P<ast::Expr>,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
/// 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: _} => {
|
||||
let mut set = HashSet::new();
|
||||
for (fmt, _) in formats.iter() {
|
||||
set.insert(fmt.clone());
|
||||
};
|
||||
set
|
||||
},
|
||||
_ => HashSet::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Return an expression for the field key name for serialisation.
|
||||
///
|
||||
/// The resulting expression assumes that `S` refers to a type
|
||||
/// that implements `Serializer`.
|
||||
pub fn serializer_key_expr(&self, cx: &ExtCtxt) -> P<ast::Expr> {
|
||||
match self.names {
|
||||
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 }
|
||||
}
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the default field name for the field.
|
||||
pub fn default_key_expr(&self) -> &P<ast::Expr> {
|
||||
match self.names {
|
||||
FieldNames::Global(ref expr) => expr,
|
||||
FieldNames::Format{formats: _, 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 } => {
|
||||
formats.get(format).unwrap_or(default)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Predicate for using a field's default value
|
||||
pub fn use_default(&self) -> bool {
|
||||
self.use_default
|
||||
}
|
||||
|
||||
/// Predicate for ignoring a field when serializing a value
|
||||
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> {
|
||||
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(builder: &'a aster::AstBuilder) -> FieldAttrsBuilder<'a> {
|
||||
FieldAttrsBuilder {
|
||||
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) -> 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(self, attrs: &[ast::Attribute]) -> FieldAttrsBuilder<'a> {
|
||||
attrs.iter().fold(self, FieldAttrsBuilder::attr)
|
||||
}
|
||||
|
||||
pub fn attr(self, attr: &ast::Attribute) -> FieldAttrsBuilder<'a> {
|
||||
match attr.node.value.node {
|
||||
ast::MetaList(ref name, ref items) if name == &"serde" => {
|
||||
attr::mark_used(&attr);
|
||||
items.iter().fold(self, FieldAttrsBuilder::meta_item)
|
||||
}
|
||||
_ => {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn meta_item(mut self, meta_item: &P<ast::MetaItem>) -> FieldAttrsBuilder<'a> {
|
||||
match meta_item.node {
|
||||
ast::MetaNameValue(ref name, ref lit) if name == &"rename" => {
|
||||
let expr = self.builder.expr().build_lit(P(lit.clone()));
|
||||
|
||||
self.name(expr)
|
||||
}
|
||||
ast::MetaList(ref name, ref items) if name == &"rename" => {
|
||||
for item in items {
|
||||
match item.node {
|
||||
ast::MetaNameValue(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);
|
||||
}
|
||||
_ => { }
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
ast::MetaWord(ref name) if name == &"default" => {
|
||||
self.default()
|
||||
}
|
||||
ast::MetaWord(ref name) if name == &"skip_serializing" => {
|
||||
self.skip_serializing_field()
|
||||
}
|
||||
ast::MetaWord(ref name) if name == &"skip_serializing_if_empty" => {
|
||||
self.skip_serializing_field_if_empty()
|
||||
}
|
||||
ast::MetaWord(ref name) if name == &"skip_serializing_if_none" => {
|
||||
self.skip_serializing_field_if_none()
|
||||
}
|
||||
_ => {
|
||||
// Ignore unknown meta variables for now.
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
use syntax::ast;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
|
||||
use aster;
|
||||
use attr::{FieldAttrs, FieldAttrsBuilder};
|
||||
|
||||
pub fn struct_field_attrs(
|
||||
_cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
fields: &[ast::StructField],
|
||||
) -> Vec<FieldAttrs> {
|
||||
fields.iter()
|
||||
.map(|field| {
|
||||
FieldAttrsBuilder::new(builder).field(field).build()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))]
|
||||
#![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))]
|
||||
|
||||
extern crate aster;
|
||||
extern crate quasi;
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
extern crate syntex;
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
extern crate syntex_syntax as syntax;
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
extern crate syntax;
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
include!("lib.rs.in");
|
||||
|
||||
#[cfg(feature = "with-syntex")]
|
||||
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 {
|
||||
/// Helper folder that strips the serde attributes after the extensions have been expanded.
|
||||
struct StripAttributeFolder;
|
||||
|
||||
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; }
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Some(attr)
|
||||
}
|
||||
|
||||
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
||||
fold::noop_fold_mac(mac, self)
|
||||
}
|
||||
}
|
||||
|
||||
fold::Folder::fold_crate(&mut StripAttributeFolder, krate)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "with-syntex"))]
|
||||
pub fn register(reg: &mut rustc_plugin::Registry) {
|
||||
reg.register_syntax_extension(
|
||||
syntax::parse::token::intern("derive_Serialize"),
|
||||
syntax::ext::base::MultiDecorator(
|
||||
Box::new(ser::expand_derive_serialize)));
|
||||
|
||||
reg.register_syntax_extension(
|
||||
syntax::parse::token::intern("derive_Deserialize"),
|
||||
syntax::ext::base::MultiDecorator(
|
||||
Box::new(de::expand_derive_deserialize)));
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
mod attr;
|
||||
mod de;
|
||||
mod field;
|
||||
mod ser;
|
||||
@@ -1,27 +1,35 @@
|
||||
use aster;
|
||||
|
||||
use syntax::ast::{
|
||||
Ident,
|
||||
MetaItem,
|
||||
Item,
|
||||
Expr,
|
||||
StructDef,
|
||||
};
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
use field::{Direction, struct_field_strs};
|
||||
use field::struct_field_attrs;
|
||||
|
||||
pub fn expand_derive_serialize(
|
||||
cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
_mitem: &MetaItem,
|
||||
item: &Item,
|
||||
push: &mut FnMut(P<ast::Item>)
|
||||
meta_item: &MetaItem,
|
||||
annotatable: &Annotatable,
|
||||
push: &mut FnMut(Annotatable)
|
||||
) {
|
||||
let item = match *annotatable {
|
||||
Annotatable::Item(ref item) => item,
|
||||
_ => {
|
||||
cx.span_err(
|
||||
meta_item.span,
|
||||
"`derive` may only be applied to structs and enums");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let builder = aster::AstBuilder::new().span(span);
|
||||
|
||||
let generics = match item.node {
|
||||
@@ -43,7 +51,7 @@ pub fn expand_derive_serialize(
|
||||
let body = serialize_body(
|
||||
cx,
|
||||
&builder,
|
||||
item,
|
||||
&item,
|
||||
&impl_generics,
|
||||
ty.clone(),
|
||||
);
|
||||
@@ -51,7 +59,6 @@ pub fn expand_derive_serialize(
|
||||
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,
|
||||
@@ -61,7 +68,7 @@ pub fn expand_derive_serialize(
|
||||
}
|
||||
).unwrap();
|
||||
|
||||
push(impl_item)
|
||||
push(Annotatable::Item(impl_item))
|
||||
}
|
||||
|
||||
fn serialize_body(
|
||||
@@ -72,14 +79,14 @@ fn serialize_body(
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
match item.node {
|
||||
ast::ItemStruct(ref struct_def, _) => {
|
||||
ast::ItemStruct(ref variant_data, _) => {
|
||||
serialize_item_struct(
|
||||
cx,
|
||||
builder,
|
||||
item,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
variant_data,
|
||||
)
|
||||
}
|
||||
ast::ItemEnum(ref enum_def, _) => {
|
||||
@@ -88,6 +95,7 @@ fn serialize_body(
|
||||
builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
enum_def,
|
||||
)
|
||||
}
|
||||
@@ -101,50 +109,51 @@ fn serialize_item_struct(
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
variant_data: &ast::VariantData,
|
||||
) -> 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 == 0) {
|
||||
(true, true) => {
|
||||
match *variant_data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
serialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, false) => {
|
||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||
serialize_newtype_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
ast::VariantData::Tuple(ref fields, _) => {
|
||||
if fields.iter().any(|field| !field.node.kind.is_unnamed()) {
|
||||
cx.bug("tuple struct has named fields")
|
||||
}
|
||||
|
||||
serialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
unnamed_fields,
|
||||
fields.len(),
|
||||
)
|
||||
}
|
||||
(false, true) => {
|
||||
ast::VariantData::Struct(ref fields, _) => {
|
||||
if fields.iter().any(|field| field.node.kind.is_unnamed()) {
|
||||
cx.bug("struct has unnamed fields")
|
||||
}
|
||||
|
||||
serialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
named_fields,
|
||||
fields,
|
||||
)
|
||||
}
|
||||
(false, false) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +164,17 @@ fn serialize_unit_struct(
|
||||
) -> P<ast::Expr> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, serializer.visit_named_unit($type_name))
|
||||
quote_expr!(cx, serializer.visit_unit_struct($type_name))
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident
|
||||
) -> P<ast::Expr> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, serializer.visit_newtype_struct($type_name, &self.0))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
@@ -166,15 +185,14 @@ fn serialize_tuple_struct(
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
let value_ty = builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(ty);
|
||||
|
||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
value_ty,
|
||||
ty.clone(),
|
||||
builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(ty.clone()),
|
||||
fields,
|
||||
impl_generics,
|
||||
);
|
||||
@@ -184,9 +202,10 @@ fn serialize_tuple_struct(
|
||||
quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_named_seq($type_name, Visitor {
|
||||
serializer.visit_tuple_struct($type_name, Visitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -197,21 +216,22 @@ fn serialize_struct(
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
fields: Vec<Ident>,
|
||||
fields: &[ast::StructField],
|
||||
) -> P<ast::Expr> {
|
||||
let value_ty = builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(ty.clone());
|
||||
|
||||
let (visitor_struct, visitor_impl) = serialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
value_ty,
|
||||
struct_def,
|
||||
ty.clone(),
|
||||
builder.ty()
|
||||
.ref_()
|
||||
.lifetime("'__a")
|
||||
.build_ty(ty.clone()),
|
||||
fields,
|
||||
impl_generics,
|
||||
fields.iter().map(|field| quote_expr!(cx, &self.value.$field)),
|
||||
fields.iter().map(|field| {
|
||||
let name = field.node.ident().expect("struct has unnamed field");
|
||||
quote_expr!(cx, &self.value.$name)
|
||||
})
|
||||
);
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
@@ -219,9 +239,10 @@ fn serialize_struct(
|
||||
quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_named_map($type_name, Visitor {
|
||||
serializer.visit_struct($type_name, Visitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$ty>,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -231,16 +252,20 @@ fn serialize_item_enum(
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
enum_def: &ast::EnumDef,
|
||||
) -> P<ast::Expr> {
|
||||
let arms: Vec<ast::Arm> = enum_def.variants.iter()
|
||||
.map(|variant| {
|
||||
.enumerate()
|
||||
.map(|(variant_index, variant)| {
|
||||
serialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
impl_generics,
|
||||
ty.clone(),
|
||||
variant,
|
||||
variant_index,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
@@ -257,60 +282,87 @@ fn serialize_variant(
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
variant: &ast::Variant,
|
||||
variant_index: usize,
|
||||
) -> 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() => {
|
||||
match variant.node.data {
|
||||
ast::VariantData::Unit(_) => {
|
||||
let pat = builder.pat().enum_()
|
||||
.id(type_ident).id(variant_ident).build()
|
||||
.build();
|
||||
|
||||
quote_arm!(cx,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::visit_enum_unit(
|
||||
::serde::ser::Serializer::visit_unit_variant(
|
||||
serializer,
|
||||
$type_name,
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
)
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
ast::TupleVariantKind(ref args) => {
|
||||
let fields: Vec<ast::Ident> = (0 .. args.len())
|
||||
},
|
||||
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,
|
||||
$pat => {
|
||||
::serde::ser::Serializer::visit_newtype_variant(
|
||||
serializer,
|
||||
$type_name,
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
__simple_value,
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
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(
|
||||
cx,
|
||||
builder,
|
||||
type_name,
|
||||
variant_index,
|
||||
variant_name,
|
||||
generics,
|
||||
args,
|
||||
ty,
|
||||
fields,
|
||||
field_names,
|
||||
);
|
||||
|
||||
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,
|
||||
@@ -328,10 +380,12 @@ fn serialize_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_name,
|
||||
variant_index,
|
||||
variant_name,
|
||||
generics,
|
||||
struct_def,
|
||||
ty,
|
||||
fields,
|
||||
field_names,
|
||||
);
|
||||
|
||||
quote_arm!(cx, $pat => { $expr })
|
||||
@@ -343,28 +397,20 @@ fn serialize_tuple_variant(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_name: P<ast::Expr>,
|
||||
variant_index: usize,
|
||||
variant_name: P<ast::Expr>,
|
||||
generics: &ast::Generics,
|
||||
args: &[ast::VariantArg],
|
||||
fields: Vec<Ident>,
|
||||
structure_ty: P<ast::Ty>,
|
||||
fields: &[ast::StructField],
|
||||
field_names: Vec<Ident>,
|
||||
) -> P<ast::Expr> {
|
||||
let value_ty = builder.ty().tuple()
|
||||
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();
|
||||
|
||||
let value_expr = builder.expr().tuple()
|
||||
.with_exprs(
|
||||
fields.iter().map(|field| {
|
||||
builder.expr()
|
||||
.addr_of()
|
||||
.id(field)
|
||||
.build_ty(field.node.ty.clone())
|
||||
})
|
||||
)
|
||||
.build();
|
||||
@@ -372,17 +418,27 @@ fn serialize_tuple_variant(
|
||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
value_ty,
|
||||
args.len(),
|
||||
structure_ty.clone(),
|
||||
variant_ty,
|
||||
fields.len(),
|
||||
generics,
|
||||
);
|
||||
|
||||
let value_expr = builder.expr().tuple()
|
||||
.with_exprs(
|
||||
field_names.iter().map(|field| {
|
||||
builder.expr().id(field)
|
||||
})
|
||||
)
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_enum_seq($type_name, $variant_name, Visitor {
|
||||
serializer.visit_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||
value: $value_expr,
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -391,14 +447,16 @@ fn serialize_struct_variant(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_name: P<ast::Expr>,
|
||||
variant_index: usize,
|
||||
variant_name: P<ast::Expr>,
|
||||
generics: &ast::Generics,
|
||||
struct_def: &ast::StructDef,
|
||||
fields: Vec<Ident>,
|
||||
structure_ty: P<ast::Ty>,
|
||||
fields: &[ast::StructField],
|
||||
field_names: Vec<Ident>,
|
||||
) -> 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")
|
||||
@@ -409,10 +467,8 @@ fn serialize_struct_variant(
|
||||
|
||||
let value_expr = builder.expr().tuple()
|
||||
.with_exprs(
|
||||
fields.iter().map(|field| {
|
||||
builder.expr()
|
||||
.addr_of()
|
||||
.id(field)
|
||||
field_names.iter().map(|field| {
|
||||
builder.expr().id(field)
|
||||
})
|
||||
)
|
||||
.build();
|
||||
@@ -420,10 +476,11 @@ fn serialize_struct_variant(
|
||||
let (visitor_struct, visitor_impl) = 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_()
|
||||
@@ -433,9 +490,10 @@ fn serialize_struct_variant(
|
||||
quote_expr!(cx, {
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
serializer.visit_enum_map($type_name, $variant_name, Visitor {
|
||||
serializer.visit_struct_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||
value: $value_expr,
|
||||
state: 0,
|
||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -443,7 +501,8 @@ fn serialize_struct_variant(
|
||||
fn serialize_tuple_struct_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
value_ty: P<ast::Ty>,
|
||||
structure_ty: P<ast::Ty>,
|
||||
variant_ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
generics: &ast::Generics
|
||||
) -> (P<ast::Item>, P<ast::Item>) {
|
||||
@@ -456,7 +515,7 @@ fn serialize_tuple_struct_visitor(
|
||||
quote_arm!(cx,
|
||||
$i => {
|
||||
self.state += 1;
|
||||
let v = try!(serializer.visit_seq_elt(&$expr));
|
||||
let v = try!(serializer.visit_tuple_struct_elt(&$expr));
|
||||
Ok(Some(v))
|
||||
}
|
||||
)
|
||||
@@ -478,7 +537,8 @@ fn serialize_tuple_struct_visitor(
|
||||
quote_item!(cx,
|
||||
struct Visitor $visitor_impl_generics $where_clause {
|
||||
state: usize,
|
||||
value: $value_ty,
|
||||
value: $variant_ty,
|
||||
_structure_ty: ::std::marker::PhantomData<&'__a $structure_ty>,
|
||||
}
|
||||
).unwrap(),
|
||||
|
||||
@@ -488,11 +548,11 @@ fn serialize_tuple_struct_visitor(
|
||||
$where_clause {
|
||||
#[inline]
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> ::std::result::Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer,
|
||||
where S: ::serde::ser::Serializer
|
||||
{
|
||||
match self.state {
|
||||
$arms
|
||||
_ => Ok(None),
|
||||
_ => Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -508,34 +568,48 @@ fn serialize_tuple_struct_visitor(
|
||||
fn serialize_struct_visitor<I>(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
value_ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
structure_ty: P<ast::Ty>,
|
||||
variant_ty: P<ast::Ty>,
|
||||
fields: &[ast::StructField],
|
||||
generics: &ast::Generics,
|
||||
value_exprs: I,
|
||||
) -> (P<ast::Item>, P<ast::Item>)
|
||||
where I: Iterator<Item=P<ast::Expr>>,
|
||||
{
|
||||
let len = struct_def.fields.len();
|
||||
let value_exprs = value_exprs.collect::<Vec<_>>();
|
||||
|
||||
let key_exprs = struct_field_strs(cx, builder, struct_def, Direction::Serialize);
|
||||
let field_attrs = struct_field_attrs(cx, builder, fields);
|
||||
|
||||
let arms: Vec<ast::Arm> = key_exprs.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, (key_expr, 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_map_elt(
|
||||
serializer.visit_struct_elt(
|
||||
$key_expr,
|
||||
$value_expr,
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -552,11 +626,27 @@ 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));
|
||||
|
||||
(
|
||||
quote_item!(cx,
|
||||
struct Visitor $visitor_impl_generics $where_clause {
|
||||
state: usize,
|
||||
value: $value_ty,
|
||||
value: $variant_ty,
|
||||
_structure_ty: ::std::marker::PhantomData<&'__a $structure_ty>,
|
||||
}
|
||||
).unwrap(),
|
||||
|
||||
@@ -569,9 +659,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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-5
@@ -1,16 +1,21 @@
|
||||
[package]
|
||||
name = "serde_macros"
|
||||
version = "0.3.3"
|
||||
version = "0.6.6"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Macros to auto-generate implementations for the serde framework"
|
||||
repository = "https://github.com/erickt/rust-serde"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://github.com/serde-rs/serde"
|
||||
keywords = ["serde", "serialization"]
|
||||
|
||||
[lib]
|
||||
name = "serde_macros"
|
||||
plugin = true
|
||||
|
||||
[dependencies]
|
||||
aster = "*"
|
||||
quasi = "*"
|
||||
quasi_macros = "*"
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||
|
||||
[dev-dependencies]
|
||||
num = "^0.1.27"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde", features = ["nightly", "num-impls"] }
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#![feature(custom_attribute, custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate num;
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate test;
|
||||
|
||||
include!("../../serde_tests/benches/bench.rs.in");
|
||||
@@ -1,695 +0,0 @@
|
||||
use syntax::ast::{
|
||||
Ident,
|
||||
MetaItem,
|
||||
Item,
|
||||
Expr,
|
||||
StructDef,
|
||||
EnumDef,
|
||||
};
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
use field;
|
||||
|
||||
pub fn expand_derive_deserialize(
|
||||
cx: &mut ExtCtxt,
|
||||
span: Span,
|
||||
_mitem: &MetaItem,
|
||||
item: &Item,
|
||||
push: &mut FnMut(P<ast::Item>)
|
||||
) {
|
||||
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)]")
|
||||
};
|
||||
|
||||
let impl_generics = builder.from_generics(generics.clone())
|
||||
.add_ty_param_bound(
|
||||
builder.path().global().ids(&["serde", "de", "Deserialize"]).build()
|
||||
)
|
||||
.build();
|
||||
|
||||
let ty = builder.ty().path()
|
||||
.segment(item.ident).with_generics(impl_generics.clone()).build()
|
||||
.build();
|
||||
|
||||
let body = deserialize_body(
|
||||
cx,
|
||||
&builder,
|
||||
item,
|
||||
&impl_generics,
|
||||
ty.clone(),
|
||||
);
|
||||
|
||||
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,
|
||||
{
|
||||
$body
|
||||
}
|
||||
}
|
||||
).unwrap();
|
||||
|
||||
push(impl_item)
|
||||
}
|
||||
|
||||
fn deserialize_body(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
item: &Item,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
) -> P<ast::Expr> {
|
||||
match item.node {
|
||||
ast::ItemStruct(ref struct_def, _) => {
|
||||
deserialize_item_struct(
|
||||
cx,
|
||||
builder,
|
||||
item,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
)
|
||||
}
|
||||
ast::ItemEnum(ref enum_def, _) => {
|
||||
deserialize_item_enum(
|
||||
cx,
|
||||
builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
enum_def,
|
||||
)
|
||||
}
|
||||
_ => cx.bug("expected ItemStruct or ItemEnum in #[derive(Deserialize)]")
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_item_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
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 == 0) {
|
||||
(true, true) => {
|
||||
deserialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
)
|
||||
}
|
||||
(true, false) => {
|
||||
deserialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
unnamed_fields,
|
||||
)
|
||||
}
|
||||
(false, true) => {
|
||||
deserialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
item.ident,
|
||||
impl_generics,
|
||||
ty,
|
||||
struct_def,
|
||||
)
|
||||
}
|
||||
(false, false) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Build `__Visitor<A, B, ...>(PhantomData<A>, PhantomData<B>, ...)`
|
||||
fn deserialize_visitor(
|
||||
builder: &aster::AstBuilder,
|
||||
trait_generics: &ast::Generics,
|
||||
) -> (P<ast::Item>, P<ast::Ty>, P<ast::Expr>) {
|
||||
if trait_generics.ty_params.is_empty() {
|
||||
(
|
||||
builder.item().tuple_struct("__Visitor").build(),
|
||||
builder.ty().id("__Visitor"),
|
||||
builder.expr().id("__Visitor"),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
builder.item().tuple_struct("__Visitor")
|
||||
.generics().with(trait_generics.clone()).build()
|
||||
.with_tys(
|
||||
trait_generics.ty_params.iter().map(|ty_param| {
|
||||
builder.ty().phantom_data().id(ty_param.ident)
|
||||
})
|
||||
)
|
||||
.build(),
|
||||
builder.ty().path()
|
||||
.segment("__Visitor").with_generics(trait_generics.clone()).build()
|
||||
.build(),
|
||||
builder.expr().call().id("__Visitor")
|
||||
.with_args(
|
||||
trait_generics.ty_params.iter().map(|_| {
|
||||
builder.expr().phantom_data()
|
||||
})
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
) -> P<ast::Expr> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
struct __Visitor;
|
||||
|
||||
impl ::serde::de::Visitor for __Visitor {
|
||||
type Value = $type_ident;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> ::std::result::Result<$type_ident, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
Ok($type_ident)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> ::std::result::Result<$type_ident, V::Error>
|
||||
where V: ::serde::de::SeqVisitor,
|
||||
{
|
||||
try!(visitor.end());
|
||||
self.visit_unit()
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit_named_unit($type_name, __Visitor)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
);
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
cx,
|
||||
builder,
|
||||
builder.path().id(type_ident).build(),
|
||||
fields,
|
||||
);
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $impl_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit_named_seq($type_name, $visitor_expr)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_seq(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
let let_values: Vec<P<ast::Stmt>> = (0 .. fields)
|
||||
.map(|i| {
|
||||
let name = builder.id(format!("__field{}", i));
|
||||
quote_stmt!(cx,
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
return Err(::serde::de::Error::end_of_stream_error());
|
||||
}
|
||||
};
|
||||
).unwrap()
|
||||
})
|
||||
.collect();
|
||||
|
||||
let result = builder.expr().call()
|
||||
.build_path(struct_path)
|
||||
.with_args((0 .. fields).map(|i| builder.expr().id(format!("__field{}", i))))
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
$let_values
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
);
|
||||
|
||||
let (field_visitor, visit_map_expr) = deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_def,
|
||||
builder.path().id(type_ident).build(),
|
||||
);
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$field_visitor
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $impl_generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
#[inline]
|
||||
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::MapVisitor,
|
||||
{
|
||||
$visit_map_expr
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit_named_map($type_name, $visitor_expr)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_item_enum(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
impl_generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
enum_def: &EnumDef,
|
||||
) -> P<ast::Expr> {
|
||||
let where_clause = &impl_generics.where_clause;
|
||||
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
let variant_visitor = deserialize_field_visitor(
|
||||
cx,
|
||||
builder,
|
||||
enum_def.variants.iter()
|
||||
.map(|variant| builder.expr().str(variant.node.name))
|
||||
.collect()
|
||||
);
|
||||
|
||||
// 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 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) = deserialize_visitor(
|
||||
builder,
|
||||
impl_generics,
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$variant_visitor
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $impl_generics ::serde::de::EnumVisitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::VariantVisitor,
|
||||
{
|
||||
match try!(visitor.visit_variant()) {
|
||||
$variant_arms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit_enum($type_name, $visitor_expr)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_variant(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: Ident,
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
variant: &ast::Variant,
|
||||
) -> P<ast::Expr> {
|
||||
let variant_ident = variant.node.name;
|
||||
|
||||
match variant.node.kind {
|
||||
ast::TupleVariantKind(ref args) if args.is_empty() => {
|
||||
quote_expr!(cx, {
|
||||
try!(visitor.visit_unit());
|
||||
Ok($type_ident::$variant_ident)
|
||||
})
|
||||
}
|
||||
ast::TupleVariantKind(ref args) => {
|
||||
deserialize_tuple_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
variant_ident,
|
||||
generics,
|
||||
ty,
|
||||
args.len(),
|
||||
)
|
||||
}
|
||||
ast::StructVariantKind(ref struct_def) => {
|
||||
deserialize_struct_variant(
|
||||
cx,
|
||||
builder,
|
||||
type_ident,
|
||||
variant_ident,
|
||||
generics,
|
||||
ty,
|
||||
struct_def,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple_variant(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: ast::Ident,
|
||||
variant_ident: ast::Ident,
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
fields: usize,
|
||||
) -> P<ast::Expr> {
|
||||
let where_clause = &generics.where_clause;
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
);
|
||||
|
||||
let visit_seq_expr = deserialize_seq(
|
||||
cx,
|
||||
builder,
|
||||
builder.path().id(type_ident).id(variant_ident).build(),
|
||||
fields,
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$visitor_item
|
||||
|
||||
impl $generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit_seq<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::SeqVisitor,
|
||||
{
|
||||
$visit_seq_expr
|
||||
}
|
||||
}
|
||||
|
||||
visitor.visit_seq($visitor_expr)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_struct_variant(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
type_ident: ast::Ident,
|
||||
variant_ident: ast::Ident,
|
||||
generics: &ast::Generics,
|
||||
ty: P<ast::Ty>,
|
||||
struct_def: &ast::StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
let where_clause = &generics.where_clause;
|
||||
|
||||
let (field_visitor, field_expr) = deserialize_struct_visitor(
|
||||
cx,
|
||||
builder,
|
||||
struct_def,
|
||||
builder.path().id(type_ident).id(variant_ident).build(),
|
||||
);
|
||||
|
||||
let (visitor_item, visitor_ty, visitor_expr) = deserialize_visitor(
|
||||
builder,
|
||||
generics,
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$field_visitor
|
||||
|
||||
$visitor_item
|
||||
|
||||
impl $generics ::serde::de::Visitor for $visitor_ty $where_clause {
|
||||
type Value = $ty;
|
||||
|
||||
fn visit_map<__V>(&mut self, mut visitor: __V) -> ::std::result::Result<$ty, __V::Error>
|
||||
where __V: ::serde::de::MapVisitor,
|
||||
{
|
||||
$field_expr
|
||||
}
|
||||
}
|
||||
|
||||
visitor.visit_map($visitor_expr)
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_field_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
field_exprs: Vec<P<ast::Expr>>,
|
||||
) -> Vec<P<ast::Item>> {
|
||||
// Create the field names for the fields.
|
||||
let field_idents: Vec<ast::Ident> = (0 .. field_exprs.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
let field_enum = builder.item()
|
||||
.attr().allow(&["non_camel_case_types"])
|
||||
.enum_("__Field")
|
||||
.with_variants(
|
||||
field_idents.iter().map(|field_ident| {
|
||||
builder.variant(field_ident).tuple().build()
|
||||
})
|
||||
)
|
||||
.build();
|
||||
|
||||
// Match arms to extract a field from a string
|
||||
let field_arms: Vec<_> = field_idents.iter()
|
||||
.zip(field_exprs.into_iter())
|
||||
.map(|(field_ident, field_expr)| {
|
||||
quote_arm!(cx, $field_expr => { Ok(__Field::$field_ident) })
|
||||
})
|
||||
.collect();
|
||||
|
||||
vec![
|
||||
field_enum,
|
||||
|
||||
quote_item!(cx,
|
||||
impl ::serde::de::Deserialize for __Field {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> ::std::result::Result<__Field, D::Error>
|
||||
where D: ::serde::de::Deserializer,
|
||||
{
|
||||
struct __FieldVisitor;
|
||||
|
||||
impl ::serde::de::Visitor for __FieldVisitor {
|
||||
type Value = __Field;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> ::std::result::Result<__Field, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
match value {
|
||||
$field_arms
|
||||
_ => Err(::serde::de::Error::unknown_field_error(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(__FieldVisitor)
|
||||
}
|
||||
}
|
||||
).unwrap(),
|
||||
]
|
||||
}
|
||||
|
||||
fn deserialize_struct_visitor(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_def: &ast::StructDef,
|
||||
struct_path: ast::Path,
|
||||
) -> (Vec<P<ast::Item>>, P<ast::Expr>) {
|
||||
let field_visitor = deserialize_field_visitor(
|
||||
cx,
|
||||
builder,
|
||||
field::struct_field_strs(cx, builder, struct_def, field::Direction::Deserialize),
|
||||
);
|
||||
|
||||
let visit_map_expr = deserialize_map(
|
||||
cx,
|
||||
builder,
|
||||
struct_path,
|
||||
struct_def,
|
||||
);
|
||||
|
||||
(field_visitor, visit_map_expr)
|
||||
}
|
||||
|
||||
fn deserialize_map(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_path: ast::Path,
|
||||
struct_def: &StructDef,
|
||||
) -> P<ast::Expr> {
|
||||
// Create the field names for the fields.
|
||||
let field_names: Vec<ast::Ident> = (0 .. struct_def.fields.len())
|
||||
.map(|i| builder.id(format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
// Declare each field.
|
||||
let let_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||
.map(|field_name| quote_stmt!(cx, let mut $field_name = None;).unwrap())
|
||||
.collect();
|
||||
|
||||
// Match arms to extract a value for a field.
|
||||
let value_arms: Vec<ast::Arm> = field_names.iter()
|
||||
.map(|field_name| {
|
||||
quote_arm!(cx,
|
||||
__Field::$field_name => {
|
||||
$field_name = Some(try!(visitor.visit_value()));
|
||||
}
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||
.zip(struct_def.fields.iter())
|
||||
.map(|(field_name, field)| {
|
||||
let rename = field::field_rename(field, &field::Direction::Deserialize);
|
||||
let name_str = match (rename, field.node.kind) {
|
||||
(Some(rename), _) => builder.expr().build_lit(P(rename.clone())),
|
||||
(None, ast::NamedField(name, _)) => builder.expr().str(name),
|
||||
(None, ast::UnnamedField(_)) => panic!("struct contains unnamed fields"),
|
||||
};
|
||||
|
||||
let missing_expr = if field::default_value(field) {
|
||||
quote_expr!(cx, ::std::default::Default::default())
|
||||
} else {
|
||||
quote_expr!(cx, try!(visitor.missing_field($name_str)))
|
||||
};
|
||||
|
||||
quote_stmt!(cx,
|
||||
let $field_name = match $field_name {
|
||||
Some($field_name) => $field_name,
|
||||
None => $missing_expr,
|
||||
};
|
||||
).unwrap()
|
||||
})
|
||||
.collect();
|
||||
|
||||
let result = builder.expr().struct_path(struct_path)
|
||||
.with_id_exprs(
|
||||
struct_def.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"),
|
||||
},
|
||||
builder.expr().id(field_name),
|
||||
)
|
||||
})
|
||||
)
|
||||
.build();
|
||||
|
||||
quote_expr!(cx, {
|
||||
$let_values
|
||||
|
||||
while let Some(key) = try!(visitor.visit_key()) {
|
||||
match key {
|
||||
$value_arms
|
||||
}
|
||||
}
|
||||
|
||||
$extract_values
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok($result)
|
||||
})
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
use syntax::ext::base::ExtCtxt;
|
||||
use syntax::ptr::P;
|
||||
|
||||
use aster;
|
||||
|
||||
pub enum Direction {
|
||||
Serialize,
|
||||
Deserialize,
|
||||
}
|
||||
|
||||
pub fn field_rename<'a>(
|
||||
field: &'a ast::StructField,
|
||||
direction: &Direction,
|
||||
) -> Option<&'a ast::Lit> {
|
||||
let dir_attr = match *direction {
|
||||
Direction::Serialize => "rename_serialize",
|
||||
Direction::Deserialize => "rename_deserialize",
|
||||
};
|
||||
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);
|
||||
vals.iter().fold(None, |v, mi| {
|
||||
if let ast::MetaNameValue(ref n, ref lit) = mi.node {
|
||||
if n == &"rename" || n == &dir_attr {
|
||||
Some(lit)
|
||||
} else {
|
||||
v
|
||||
}
|
||||
} else {
|
||||
v
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn struct_field_strs(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
struct_def: &ast::StructDef,
|
||||
direction: Direction,
|
||||
) -> Vec<P<ast::Expr>> {
|
||||
struct_def.fields.iter()
|
||||
.map(|field| {
|
||||
match field_rename(field, &direction) {
|
||||
Some(rename) => builder.expr().build_lit(P(rename.clone())),
|
||||
None => {
|
||||
match field.node.kind {
|
||||
ast::NamedField(name, _) => {
|
||||
builder.expr().str(name)
|
||||
}
|
||||
ast::UnnamedField(_) => {
|
||||
cx.bug("struct has named and unnamed fields")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn default_value(field: &ast::StructField) -> bool {
|
||||
field.node.attrs.iter()
|
||||
.any(|sa| {
|
||||
if let ast::MetaItem_::MetaList(ref n, ref vals) = sa.node.value.node {
|
||||
if n == &"serde" {
|
||||
attr::mark_used(&sa);
|
||||
vals.iter()
|
||||
.map(|mi|
|
||||
if let ast::MetaItem_::MetaWord(ref n) = mi.node {
|
||||
n == &"default"
|
||||
} else {
|
||||
false
|
||||
})
|
||||
.any(|x| x)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
+5
-22
@@ -1,27 +1,10 @@
|
||||
#![feature(custom_derive, plugin, plugin_registrar, rustc_private, unboxed_closures)]
|
||||
#![plugin(quasi_macros)]
|
||||
#![feature(plugin_registrar, rustc_private)]
|
||||
|
||||
extern crate aster;
|
||||
extern crate quasi;
|
||||
extern crate rustc;
|
||||
extern crate syntax;
|
||||
|
||||
use syntax::ext::base::Decorator;
|
||||
use syntax::parse::token;
|
||||
use rustc::plugin::Registry;
|
||||
|
||||
mod ser;
|
||||
mod de;
|
||||
mod field;
|
||||
extern crate serde_codegen;
|
||||
extern crate rustc_plugin;
|
||||
|
||||
#[plugin_registrar]
|
||||
#[doc(hidden)]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
reg.register_syntax_extension(
|
||||
token::intern("derive_Serialize"),
|
||||
Decorator(Box::new(ser::expand_derive_serialize)));
|
||||
|
||||
reg.register_syntax_extension(
|
||||
token::intern("derive_Deserialize"),
|
||||
Decorator(Box::new(de::expand_derive_deserialize)));
|
||||
pub fn plugin_registrar(reg: &mut rustc_plugin::Registry) {
|
||||
serde_codegen::register(reg);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#![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");
|
||||
@@ -0,0 +1,30 @@
|
||||
[package]
|
||||
name = "serde_tests"
|
||||
version = "0.6.2"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A generic serialization/deserialization framework"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "http://serde-rs.github.io/serde/serde"
|
||||
readme = "README.md"
|
||||
keywords = ["serialization"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
syntex = { version = "^0.26.0" }
|
||||
syntex_syntax = { version = "^0.26.0" }
|
||||
serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] }
|
||||
|
||||
[dev-dependencies]
|
||||
num = "^0.1.27"
|
||||
rustc-serialize = "^0.3.16"
|
||||
serde = { version = "*", path = "../serde", features = ["num-impls"] }
|
||||
syntex = "^0.26.0"
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
path = "tests/test.rs"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
path = "benches/bench.rs"
|
||||
@@ -0,0 +1,8 @@
|
||||
#![feature(test)]
|
||||
|
||||
extern crate num;
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate test;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bench.rs"));
|
||||
@@ -0,0 +1,4 @@
|
||||
mod bench_enum;
|
||||
mod bench_map;
|
||||
mod bench_struct;
|
||||
mod bench_vec;
|
||||
@@ -1,18 +1,8 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate rustc_serialize;
|
||||
extern crate test;
|
||||
|
||||
use test::Bencher;
|
||||
|
||||
use rustc_serialize::{Decoder, Decodable};
|
||||
|
||||
use serde;
|
||||
use serde::de::{Deserializer, Deserialize};
|
||||
|
||||
use Animal::{Dog, Frog};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Clone, PartialEq, Debug, RustcDecodable, Deserialize)]
|
||||
@@ -30,13 +20,13 @@ pub enum Error {
|
||||
}
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn syntax_error() -> Error { Error::SyntaxError }
|
||||
fn syntax(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
fn unknown_field(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error { Error::SyntaxError }
|
||||
fn missing_field(_: &'static str) -> Error { Error::SyntaxError }
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -298,7 +288,10 @@ mod deserializer {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
fn visit_enum<V>(&mut self,
|
||||
_name: &str,
|
||||
_variants: &[&str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
match self.stack.pop() {
|
||||
@@ -360,7 +353,9 @@ mod deserializer {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
fn visit_tuple<V>(&mut self,
|
||||
_len: usize,
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_seq(self)
|
||||
@@ -408,7 +403,7 @@ mod deserializer {
|
||||
#[bench]
|
||||
fn bench_decoder_dog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Dog;
|
||||
let animal = Animal::Dog;
|
||||
|
||||
let mut d = decoder::AnimalDecoder::new(animal.clone());
|
||||
let value: Animal = Decodable::decode(&mut d).unwrap();
|
||||
@@ -420,7 +415,7 @@ fn bench_decoder_dog(b: &mut Bencher) {
|
||||
#[bench]
|
||||
fn bench_decoder_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Frog("Henry".to_string(), 349);
|
||||
let animal = Animal::Frog("Henry".to_string(), 349);
|
||||
|
||||
let mut d = decoder::AnimalDecoder::new(animal.clone());
|
||||
let value: Animal = Decodable::decode(&mut d).unwrap();
|
||||
@@ -432,7 +427,7 @@ fn bench_decoder_frog(b: &mut Bencher) {
|
||||
#[bench]
|
||||
fn bench_deserializer_dog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Dog;
|
||||
let animal = Animal::Dog;
|
||||
|
||||
let mut d = deserializer::AnimalDeserializer::new(animal.clone());
|
||||
let value: Animal = Deserialize::deserialize(&mut d).unwrap();
|
||||
@@ -444,7 +439,7 @@ fn bench_deserializer_dog(b: &mut Bencher) {
|
||||
#[bench]
|
||||
fn bench_deserializer_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Frog("Henry".to_string(), 349);
|
||||
let animal = Animal::Frog("Henry".to_string(), 349);
|
||||
|
||||
let mut d = deserializer::AnimalDeserializer::new(animal.clone());
|
||||
let value: Animal = Deserialize::deserialize(&mut d).unwrap();
|
||||
@@ -1,16 +1,10 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate rustc_serialize;
|
||||
extern crate test;
|
||||
|
||||
use std::fmt::Debug;
|
||||
use std::collections::HashMap;
|
||||
use test::Bencher;
|
||||
|
||||
use rustc_serialize::{Decoder, Decodable};
|
||||
|
||||
use serde;
|
||||
use serde::de::{Deserializer, Deserialize};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -23,13 +17,13 @@ pub enum Error {
|
||||
}
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn syntax_error() -> Error { Error::SyntaxError }
|
||||
fn syntax(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStream }
|
||||
fn end_of_stream() -> Error { Error::EndOfStream }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
fn unknown_field(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error {
|
||||
fn missing_field(_: &'static str) -> Error {
|
||||
Error::MissingField
|
||||
}
|
||||
}
|
||||
@@ -353,17 +347,17 @@ mod deserializer {
|
||||
|
||||
impl de::Deserializer<Error> for IsizeDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error(&mut self) -> Error {
|
||||
fn end_of_stream(&mut self) -> Error {
|
||||
EndOfStream
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error(&mut self, _token: de::Token, _expected: &[de::TokenKind]) -> Error {
|
||||
fn syntax(&mut self, _token: de::Token, _expected: &[de::TokenKind]) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
fn unexpected_name(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
@@ -423,14 +417,14 @@ fn bench_decoder_100(b: &mut Bencher) {
|
||||
})
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
D: Deserializer<Error=E>,
|
||||
E: Debug,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserialize::deserialize(&mut d).unwrap();
|
||||
fn run_deserializer<D, T>(mut d: D, value: T)
|
||||
where D: Deserializer,
|
||||
D::Error: Debug + PartialEq,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
{
|
||||
let v = T::deserialize(&mut d);
|
||||
|
||||
assert_eq!(value, v);
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
@@ -1,15 +1,9 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate rustc_serialize;
|
||||
extern crate test;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use test::Bencher;
|
||||
|
||||
use rustc_serialize::{Decoder, Decodable};
|
||||
|
||||
use serde;
|
||||
use serde::de::{Deserializer, Deserialize};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -34,20 +28,18 @@ pub struct Outer {
|
||||
pub enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
UnexpectedName,
|
||||
ConversionError,
|
||||
MissingField,
|
||||
OtherError,
|
||||
}
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn syntax_error() -> Error { Error::SyntaxError }
|
||||
fn syntax(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStream }
|
||||
fn end_of_stream() -> Error { Error::EndOfStream }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
fn unknown_field(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error {
|
||||
fn missing_field(_: &'static str) -> Error {
|
||||
Error::MissingField
|
||||
}
|
||||
}
|
||||
@@ -406,7 +398,10 @@ mod deserializer {
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_named_map<V>(&mut self, name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
fn visit_struct<V>(&mut self,
|
||||
name: &str,
|
||||
_fields: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.stack.pop() {
|
||||
@@ -1,15 +1,9 @@
|
||||
#![feature(plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate rustc_serialize;
|
||||
extern crate test;
|
||||
|
||||
use std::fmt::Debug;
|
||||
use test::Bencher;
|
||||
|
||||
use rustc_serialize::{Decoder, Decodable};
|
||||
|
||||
use serde;
|
||||
use serde::de::{Deserializer, Deserialize};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -21,13 +15,13 @@ pub enum Error {
|
||||
}
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn syntax_error() -> Error { Error::SyntaxError }
|
||||
fn syntax(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
fn unknown_field(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error { Error::SyntaxError }
|
||||
fn missing_field(_: &'static str) -> Error { Error::SyntaxError }
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -510,14 +504,14 @@ fn run_decoder<
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
D: Deserializer<Error=E>,
|
||||
E: Debug,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserialize::deserialize(&mut d).unwrap();
|
||||
fn run_deserializer<D, T>(mut d: D, value: T)
|
||||
where D: Deserializer,
|
||||
D::Error: Debug + PartialEq,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
{
|
||||
let v = T::deserialize(&mut d);
|
||||
|
||||
assert_eq!(value, v);
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
@@ -0,0 +1,22 @@
|
||||
extern crate syntex;
|
||||
extern crate serde_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
for &(src, dst) in &[
|
||||
("tests/test.rs.in", "test.rs"),
|
||||
("benches/bench.rs.in", "bench.rs"),
|
||||
] {
|
||||
let src = Path::new(src);
|
||||
let dst = Path::new(&out_dir).join(dst);
|
||||
|
||||
let mut registry = syntex::Registry::new();
|
||||
|
||||
serde_codegen::register(&mut registry);
|
||||
registry.expand("", &src, &dst).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#[macro_export]
|
||||
macro_rules! declare_ser_tests {
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
::token::assert_ser_tokens(&$value, $tokens);
|
||||
)+
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! btreemap {
|
||||
() => {
|
||||
BTreeMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = BTreeMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! btreeset {
|
||||
() => {
|
||||
BTreeSet::new()
|
||||
};
|
||||
($($value:expr),+) => {
|
||||
{
|
||||
let mut set = BTreeSet::new();
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! btreemap {
|
||||
() => {
|
||||
BTreeMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = BTreeMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! hashset {
|
||||
() => {
|
||||
HashSet::new()
|
||||
};
|
||||
($($value:expr),+) => {
|
||||
{
|
||||
let mut set = HashSet::new();
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! hashmap {
|
||||
() => {
|
||||
HashMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = HashMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||
@@ -0,0 +1,10 @@
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
mod token;
|
||||
|
||||
mod test_annotations;
|
||||
mod test_bytes;
|
||||
mod test_de;
|
||||
mod test_macros;
|
||||
mod test_ser;
|
||||
@@ -0,0 +1,343 @@
|
||||
use std::default;
|
||||
|
||||
use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Default {
|
||||
a1: i32,
|
||||
#[serde(default)]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Rename {
|
||||
a1: i32,
|
||||
#[serde(rename="a3")]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct FormatRename {
|
||||
a1: i32,
|
||||
#[serde(rename(xml= "a4", token="a5"))]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
||||
enum SerEnum<A> {
|
||||
Map {
|
||||
a: i8,
|
||||
#[serde(rename(xml= "c", token="d"))]
|
||||
b: A,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SkipSerializingFields<A: default::Default> {
|
||||
a: i8,
|
||||
#[serde(skip_serializing, 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(
|
||||
&Default { a1: 1, a2: 2 },
|
||||
vec![
|
||||
Token::StructStart("Default", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a2"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&Default { a1: 1, a2: 0 },
|
||||
vec![
|
||||
Token::StructStart("Default", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename() {
|
||||
assert_tokens(
|
||||
&Rename { a1: 1, a2: 2 },
|
||||
vec![
|
||||
Token::StructStart("Rename", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a3"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_rename() {
|
||||
assert_tokens(
|
||||
&FormatRename { a1: 1, a2: 2 },
|
||||
vec![
|
||||
Token::StructStart("FormatRename", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a1"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a5"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_format_rename() {
|
||||
assert_tokens(
|
||||
&SerEnum::Map {
|
||||
a: 0,
|
||||
b: String::new(),
|
||||
},
|
||||
vec![
|
||||
Token::EnumMapStart("SerEnum", "Map", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(0),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("d"),
|
||||
Token::Str(""),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_serializing_fields() {
|
||||
assert_ser_tokens(
|
||||
&SkipSerializingFields {
|
||||
a: 1,
|
||||
b: 2,
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SkipSerializingFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&SkipSerializingFields {
|
||||
a: 1,
|
||||
b: 0,
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("SkipSerializingFields", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[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,12 +1,6 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use serde;
|
||||
use serde::Serialize;
|
||||
use serde::bytes::{ByteBuf, Bytes};
|
||||
use serde::json;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -14,13 +8,13 @@ use serde::json;
|
||||
struct Error;
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn syntax_error() -> Error { Error }
|
||||
fn syntax(_: &str) -> Error { Error }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error }
|
||||
fn end_of_stream() -> Error { Error }
|
||||
|
||||
fn unknown_field_error(_field: &str) -> Error { Error }
|
||||
fn unknown_field(_field: &str) -> Error { Error }
|
||||
|
||||
fn missing_field_error(_field: &'static str) -> Error { Error }
|
||||
fn missing_field(_field: &'static str) -> Error { Error }
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -145,17 +139,6 @@ impl serde::Deserializer for BytesDeserializer {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[test]
|
||||
fn test_bytes_ser_json() {
|
||||
let buf = vec![];
|
||||
let bytes = Bytes::from(&buf);
|
||||
assert_eq!(json::to_string(&bytes).unwrap(), "[]".to_string());
|
||||
|
||||
let buf = vec![1, 2, 3];
|
||||
let bytes = Bytes::from(&buf);
|
||||
assert_eq!(json::to_string(&bytes).unwrap(), "[1,2,3]".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_ser_bytes() {
|
||||
let buf = vec![];
|
||||
@@ -169,39 +152,8 @@ fn test_bytes_ser_bytes() {
|
||||
bytes.serialize(&mut ser).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_ser_json() {
|
||||
let bytes = ByteBuf::new();
|
||||
assert_eq!(json::to_string(&bytes).unwrap(), "[]".to_string());
|
||||
|
||||
let bytes = ByteBuf::from(vec![1, 2, 3]);
|
||||
assert_eq!(json::to_string(&bytes).unwrap(), "[1,2,3]".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_ser_bytes() {
|
||||
let bytes = ByteBuf::new();
|
||||
let mut ser = BytesSerializer::new(vec![]);
|
||||
bytes.serialize(&mut ser).unwrap();
|
||||
|
||||
let bytes = ByteBuf::from(vec![1, 2, 3]);
|
||||
let mut ser = BytesSerializer::new(vec![1, 2, 3]);
|
||||
bytes.serialize(&mut ser).unwrap();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_de_json() {
|
||||
let bytes = ByteBuf::new();
|
||||
let v: ByteBuf = json::from_str("[]").unwrap();
|
||||
assert_eq!(v, bytes);
|
||||
|
||||
let bytes = ByteBuf::from(vec![1, 2, 3]);
|
||||
let v: ByteBuf = json::from_str("[1, 2, 3]").unwrap();
|
||||
assert_eq!(v, bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_de_bytes() {
|
||||
let mut de = BytesDeserializer::new(vec![]);
|
||||
@@ -0,0 +1,592 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use serde::de::{Deserializer, Visitor};
|
||||
|
||||
use token::{Token, assert_de_tokens};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
|
||||
struct UnitStruct;
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
struct TupleStruct(i32, i32, i32);
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
struct Struct {
|
||||
a: i32,
|
||||
b: i32,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
enum Enum {
|
||||
Unit,
|
||||
Simple(i32),
|
||||
Seq(i32, i32, i32),
|
||||
Map { a: i32, b: i32, c: i32 }
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! declare_test {
|
||||
($name:ident { $($value:expr => $tokens:expr,)+ }) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
assert_de_tokens(&$value, $tokens);
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_tests {
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
declare_test!($name { $($value => $tokens,)+ });
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
declare_tests! {
|
||||
test_bool {
|
||||
true => vec![Token::Bool(true)],
|
||||
false => vec![Token::Bool(false)],
|
||||
}
|
||||
test_isize {
|
||||
0isize => vec![Token::Isize(0)],
|
||||
0isize => vec![Token::I8(0)],
|
||||
0isize => vec![Token::I16(0)],
|
||||
0isize => vec![Token::I32(0)],
|
||||
0isize => vec![Token::I64(0)],
|
||||
0isize => vec![Token::Usize(0)],
|
||||
0isize => vec![Token::U8(0)],
|
||||
0isize => vec![Token::U16(0)],
|
||||
0isize => vec![Token::U32(0)],
|
||||
0isize => vec![Token::U64(0)],
|
||||
0isize => vec![Token::F32(0.)],
|
||||
0isize => vec![Token::F64(0.)],
|
||||
}
|
||||
test_ints {
|
||||
0isize => vec![Token::Isize(0)],
|
||||
0i8 => vec![Token::I8(0)],
|
||||
0i16 => vec![Token::I16(0)],
|
||||
0i32 => vec![Token::I32(0)],
|
||||
0i64 => vec![Token::I64(0)],
|
||||
}
|
||||
test_uints {
|
||||
0usize => vec![Token::Usize(0)],
|
||||
0u8 => vec![Token::U8(0)],
|
||||
0u16 => vec![Token::U16(0)],
|
||||
0u32 => vec![Token::U32(0)],
|
||||
0u64 => vec![Token::U64(0)],
|
||||
}
|
||||
test_floats {
|
||||
0f32 => vec![Token::F32(0.)],
|
||||
0f64 => vec![Token::F64(0.)],
|
||||
}
|
||||
test_char {
|
||||
'a' => vec![Token::Char('a')],
|
||||
'a' => vec![Token::Str("a")],
|
||||
'a' => vec![Token::String("a".to_string())],
|
||||
}
|
||||
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')],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => vec![Token::Unit],
|
||||
None::<i32> => vec![Token::Option(false)],
|
||||
Some(1) => vec![Token::I32(1)],
|
||||
Some(1) => vec![
|
||||
Token::Option(true),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_result {
|
||||
Ok::<i32, i32>(0) => vec![
|
||||
Token::EnumStart("Result"),
|
||||
Token::Str("Ok"),
|
||||
Token::I32(0),
|
||||
],
|
||||
Err::<i32, i32>(1) => vec![
|
||||
Token::EnumStart("Result"),
|
||||
Token::Str("Err"),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_unit {
|
||||
() => vec![Token::Unit],
|
||||
() => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
() => vec![
|
||||
Token::SeqStart(None),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
() => vec![
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_unit_struct {
|
||||
UnitStruct => vec![Token::Unit],
|
||||
UnitStruct => vec![
|
||||
Token::UnitStruct("UnitStruct"),
|
||||
],
|
||||
UnitStruct => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
UnitStruct => vec![
|
||||
Token::SeqStart(None),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple_struct {
|
||||
TupleStruct(1, 2, 3) => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
TupleStruct(1, 2, 3) => vec![
|
||||
Token::SeqStart(None),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
TupleStruct(1, 2, 3) => vec![
|
||||
Token::TupleStructStart("TupleStruct", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
TupleStruct(1, 2, 3) => vec![
|
||||
Token::TupleStructStart("TupleStruct", None),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreeset {
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_hashset {
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
hashset![1, 2, 3] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
vec![vec![], vec![1], vec![2, 3]] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_array {
|
||||
[0; 0] => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
[0; 0] => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
([0; 0], [1], [2, 3]) => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
[0; 0] => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
[0; 0] => vec![
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple {
|
||||
(1,) => vec![
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
(1, 2, 3) => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreemap {
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2] => vec![
|
||||
Token::MapStart(Some(1)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2, 3 => 4] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::StructStart("Anything", Some(0)),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_hashmap {
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => 2] => vec![
|
||||
Token::MapStart(Some(1)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => 2, 3 => 4] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::UnitStruct("Anything"),
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::StructStart("Anything", Some(0)),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_struct {
|
||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::MapStart(Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
Struct { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::StructStart("Struct", Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_enum_unit {
|
||||
Enum::Unit => vec![
|
||||
Token::EnumUnit("Enum", "Unit"),
|
||||
],
|
||||
}
|
||||
test_enum_simple {
|
||||
Enum::Simple(1) => vec![
|
||||
Token::EnumNewtype("Enum", "Simple"),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_enum_seq {
|
||||
Enum::Seq(1, 2, 3) => vec![
|
||||
Token::EnumSeqStart("Enum", "Seq", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_enum_map {
|
||||
Enum::Map { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::EnumMapStart("Enum", "Map", Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_enum_unit_usize {
|
||||
Enum::Unit => vec![
|
||||
Token::EnumStart("Enum"),
|
||||
Token::Usize(0),
|
||||
Token::Unit,
|
||||
],
|
||||
}
|
||||
test_enum_unit_bytes {
|
||||
Enum::Unit => vec![
|
||||
Token::EnumStart("Enum"),
|
||||
Token::Bytes(b"Unit"),
|
||||
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,
|
||||
],
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,579 @@
|
||||
use token::{Token, assert_tokens, assert_ser_tokens, assert_de_tokens};
|
||||
|
||||
/*
|
||||
trait Trait {
|
||||
type Type;
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct NamedUnit;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerNamedTuple<'a, 'b, A: 'a, B: 'b, C>(&'a A, &'b mut B, C);
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeNamedTuple<A, B, C>(A, B, C);
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerNamedMap<'a, 'b, A: 'a, B: 'b, C> {
|
||||
a: &'a A,
|
||||
b: &'b mut B,
|
||||
c: C,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeNamedMap<A, B, C> {
|
||||
a: A,
|
||||
b: B,
|
||||
c: C,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SerEnum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
//C::Type,
|
||||
&'a mut D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
//d: C::Type,
|
||||
e: &'a mut D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
//C::Type,
|
||||
&'a mut D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
//d: C::Type,
|
||||
e: &'a mut D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
enum DeEnum<B, C: /* Trait */, D> /* where D: Trait */ {
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
//C::Type,
|
||||
D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
//d: C::Type,
|
||||
e: D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
//C::Type,
|
||||
D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
//d: C::Type,
|
||||
e: D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
enum Lifetimes<'a> {
|
||||
LifetimeSeq(&'a i32),
|
||||
NoLifetimeSeq(i32),
|
||||
LifetimeMap { a: &'a i32 },
|
||||
NoLifetimeMap { a: i32 },
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GenericStruct<T> {
|
||||
x: T,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GenericNewtypeStruct<T>(T);
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GenericTupleStruct<T, U>(T, U);
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum GenericEnum<T, U> {
|
||||
Unit,
|
||||
Newtype(T),
|
||||
Seq(T, U),
|
||||
Map { x: T, y: U },
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_named_unit() {
|
||||
assert_tokens(
|
||||
&NamedUnit,
|
||||
vec![Token::UnitStruct("NamedUnit")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_named_tuple() {
|
||||
let a = 5;
|
||||
let mut b = 6;
|
||||
let c = 7;
|
||||
assert_ser_tokens(
|
||||
&SerNamedTuple(&a, &mut b, c),
|
||||
&[
|
||||
Token::TupleStructStart("SerNamedTuple", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(5),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(6),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(7),
|
||||
|
||||
Token::SeqEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_named_tuple() {
|
||||
assert_de_tokens(
|
||||
&DeNamedTuple(5, 6, 7),
|
||||
vec![
|
||||
Token::TupleStructStart("DeNamedTuple", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(5),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(6),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(7),
|
||||
|
||||
Token::SeqEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_named_map() {
|
||||
let a = 5;
|
||||
let mut b = 6;
|
||||
let c = 7;
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerNamedMap {
|
||||
a: &a,
|
||||
b: &mut b,
|
||||
c: c,
|
||||
},
|
||||
&[
|
||||
Token::StructStart("SerNamedMap", Some(3)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(6),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(7),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_named_map() {
|
||||
assert_de_tokens(
|
||||
&DeNamedMap {
|
||||
a: 5,
|
||||
b: 6,
|
||||
c: 7,
|
||||
},
|
||||
vec![
|
||||
Token::StructStart("DeNamedMap", Some(3)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(6),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(7),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_unit() {
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Unit::<u32, u32, u32>,
|
||||
&[
|
||||
Token::EnumUnit("SerEnum", "Unit"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_seq() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let mut e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
&c,
|
||||
//d,
|
||||
&mut e,
|
||||
//f,
|
||||
),
|
||||
&[
|
||||
Token::EnumSeqStart("SerEnum", "Seq", Some(4)),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I8(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(5),
|
||||
|
||||
Token::SeqEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_map() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let mut e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_ser_tokens(
|
||||
&SerEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: &c,
|
||||
//d: d,
|
||||
e: &mut e,
|
||||
//f: f,
|
||||
},
|
||||
&[
|
||||
Token::EnumMapStart("SerEnum", "Map", Some(4)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("e"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_unit() {
|
||||
assert_tokens(
|
||||
&DeEnum::Unit::<u32, u32, u32>,
|
||||
vec![
|
||||
Token::EnumUnit("DeEnum", "Unit"),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_seq() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_tokens(
|
||||
&DeEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
//d,
|
||||
e,
|
||||
//f,
|
||||
),
|
||||
vec![
|
||||
Token::EnumSeqStart("DeEnum", "Seq", Some(4)),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I8(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(5),
|
||||
|
||||
Token::SeqEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_map() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_tokens(
|
||||
&DeEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: c,
|
||||
//d: d,
|
||||
e: e,
|
||||
//f: f,
|
||||
},
|
||||
vec![
|
||||
Token::EnumMapStart("DeEnum", "Map", Some(4)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I8(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("e"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lifetimes() {
|
||||
let value = 5;
|
||||
|
||||
assert_ser_tokens(
|
||||
&Lifetimes::LifetimeSeq(&value),
|
||||
&[
|
||||
Token::EnumNewtype("Lifetimes", "LifetimeSeq"),
|
||||
Token::I32(5),
|
||||
]
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&Lifetimes::NoLifetimeSeq(5),
|
||||
&[
|
||||
Token::EnumNewtype("Lifetimes", "NoLifetimeSeq"),
|
||||
Token::I32(5),
|
||||
]
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&Lifetimes::LifetimeMap { a: &value },
|
||||
&[
|
||||
Token::EnumMapStart("Lifetimes", "LifetimeMap", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
|
||||
assert_ser_tokens(
|
||||
&Lifetimes::NoLifetimeMap { a: 5 },
|
||||
&[
|
||||
Token::EnumMapStart("Lifetimes", "NoLifetimeMap", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(5),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_struct() {
|
||||
assert_tokens(
|
||||
&GenericStruct { x: 5u32 },
|
||||
vec![
|
||||
Token::StructStart("GenericStruct", Some(1)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("x"),
|
||||
Token::U32(5),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_newtype_struct() {
|
||||
assert_tokens(
|
||||
&GenericNewtypeStruct(5u32),
|
||||
vec![
|
||||
Token::StructNewtype("GenericNewtypeStruct"),
|
||||
Token::U32(5),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_tuple_struct() {
|
||||
assert_tokens(
|
||||
&GenericTupleStruct(5u32, 6u32),
|
||||
vec![
|
||||
Token::TupleStructStart("GenericTupleStruct", Some(2)),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::U32(5),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::U32(6),
|
||||
|
||||
Token::SeqEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_enum_unit() {
|
||||
assert_tokens(
|
||||
&GenericEnum::Unit::<u32, u32>,
|
||||
vec![
|
||||
Token::EnumUnit("GenericEnum", "Unit"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_enum_newtype() {
|
||||
assert_tokens(
|
||||
&GenericEnum::Newtype::<u32, u32>(5),
|
||||
vec![
|
||||
Token::EnumNewtype("GenericEnum", "Newtype"),
|
||||
Token::U32(5),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_enum_seq() {
|
||||
assert_tokens(
|
||||
&GenericEnum::Seq::<u32, u32>(5, 6),
|
||||
vec![
|
||||
Token::EnumSeqStart("GenericEnum", "Seq", Some(2)),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::U32(5),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::U32(6),
|
||||
|
||||
Token::SeqEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generic_enum_map() {
|
||||
assert_tokens(
|
||||
&GenericEnum::Map::<u32, u32> { x: 5, y: 6 },
|
||||
vec![
|
||||
Token::EnumMapStart("GenericEnum", "Map", Some(2)),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("x"),
|
||||
Token::U32(5),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("y"),
|
||||
Token::U32(6),
|
||||
|
||||
Token::MapEnd,
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use num::FromPrimitive;
|
||||
use num::bigint::{BigInt, BigUint};
|
||||
use num::complex::Complex;
|
||||
use num::rational::Ratio;
|
||||
|
||||
use token::Token;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct UnitStruct;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TupleStruct(i32, i32, i32);
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Struct {
|
||||
a: i32,
|
||||
b: i32,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
enum Enum {
|
||||
Unit,
|
||||
One(i32),
|
||||
Seq(i32, i32),
|
||||
Map { a: i32, b: i32 },
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
declare_ser_tests! {
|
||||
test_unit {
|
||||
() => &[Token::Unit],
|
||||
}
|
||||
test_bool {
|
||||
true => &[Token::Bool(true)],
|
||||
false => &[Token::Bool(false)],
|
||||
}
|
||||
test_isizes {
|
||||
0isize => &[Token::Isize(0)],
|
||||
0i8 => &[Token::I8(0)],
|
||||
0i16 => &[Token::I16(0)],
|
||||
0i32 => &[Token::I32(0)],
|
||||
0i64 => &[Token::I64(0)],
|
||||
}
|
||||
test_usizes {
|
||||
0usize => &[Token::Usize(0)],
|
||||
0u8 => &[Token::U8(0)],
|
||||
0u16 => &[Token::U16(0)],
|
||||
0u32 => &[Token::U32(0)],
|
||||
0u64 => &[Token::U64(0)],
|
||||
}
|
||||
test_floats {
|
||||
0f32 => &[Token::F32(0.)],
|
||||
0f64 => &[Token::F64(0.)],
|
||||
}
|
||||
test_char {
|
||||
'a' => &[Token::Char('a')],
|
||||
}
|
||||
test_str {
|
||||
"abc" => &[Token::Str("abc")],
|
||||
"abc".to_string() => &[Token::Str("abc")],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => &[Token::Option(false)],
|
||||
Some(1) => &[
|
||||
Token::Option(true),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_result {
|
||||
Ok::<i32, i32>(0) => &[
|
||||
Token::EnumNewtype("Result", "Ok"),
|
||||
Token::I32(0),
|
||||
],
|
||||
Err::<i32, i32>(1) => &[
|
||||
Token::EnumNewtype("Result", "Err"),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_slice {
|
||||
&[0][..0] => &[
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
&[1, 2, 3][..] => &[
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_array {
|
||||
[0; 0] => &[
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
[1, 2, 3] => &[
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
Vec::<isize>::new() => &[
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
vec![vec![], vec![1], vec![2, 3]] => &[
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple {
|
||||
(1,) => &[
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
(1, 2, 3) => &[
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreemap {
|
||||
btreemap![1 => 2] => &[
|
||||
Token::MapStart(Some(1)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2, 3 => 4] => &[
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => &[
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_unit_struct {
|
||||
UnitStruct => &[Token::UnitStruct("UnitStruct")],
|
||||
}
|
||||
test_tuple_struct {
|
||||
TupleStruct(1, 2, 3) => &[
|
||||
Token::TupleStructStart("TupleStruct", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_struct {
|
||||
Struct { a: 1, b: 2, c: 3 } => &[
|
||||
Token::StructStart("Struct", Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_enum {
|
||||
Enum::Unit => &[Token::EnumUnit("Enum", "Unit")],
|
||||
Enum::One(42) => &[Token::EnumNewtype("Enum", "One"), Token::I32(42)],
|
||||
Enum::Seq(1, 2) => &[
|
||||
Token::EnumSeqStart("Enum", "Seq", Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
Enum::Map { a: 1, b: 2 } => &[
|
||||
Token::EnumMapStart("Enum", "Map", Some(2)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
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,
|
||||
],
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,789 @@
|
||||
use std::fmt;
|
||||
use std::iter;
|
||||
|
||||
use serde::{ser, de};
|
||||
use serde::de::value::{self, ValueDeserializer};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Token<'a> {
|
||||
Bool(bool),
|
||||
Isize(isize),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Usize(usize),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'a str),
|
||||
String(String),
|
||||
Bytes(&'a [u8]),
|
||||
|
||||
Option(bool),
|
||||
|
||||
Unit,
|
||||
UnitStruct(&'a str),
|
||||
|
||||
StructNewtype(&'a str),
|
||||
|
||||
EnumStart(&'a str),
|
||||
EnumUnit(&'a str, &'a str),
|
||||
EnumNewtype(&'a str, &'a str),
|
||||
EnumSeqStart(&'a str, &'a str, Option<usize>),
|
||||
EnumMapStart(&'a str, &'a str, Option<usize>),
|
||||
|
||||
SeqStart(Option<usize>),
|
||||
TupleStructStart(&'a str, Option<usize>),
|
||||
SeqSep,
|
||||
SeqEnd,
|
||||
|
||||
MapStart(Option<usize>),
|
||||
StructStart(&'a str, Option<usize>),
|
||||
MapSep,
|
||||
MapEnd,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct Serializer<I> {
|
||||
tokens: I,
|
||||
}
|
||||
|
||||
impl<'a, I> Serializer<I>
|
||||
where I: Iterator<Item=&'a Token<'a>>
|
||||
{
|
||||
pub fn new(tokens: I) -> Serializer<I> {
|
||||
Serializer {
|
||||
tokens: tokens,
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_sequence<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor
|
||||
{
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_mapping<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor
|
||||
{
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I> ser::Serializer for Serializer<I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_newtype_variant<T>(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewtype(name, variant)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_unit_struct(&mut self, name: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_unit_variant(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_bool(&mut self, v: bool) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_isize(&mut self, v: isize) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Isize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i8(&mut self, v: i8) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i16(&mut self, v: i16) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i32(&mut self, v: i32) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i64(&mut self, v: i64) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_usize(&mut self, v: usize) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Usize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u8(&mut self, v: u8) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u16(&mut self, v: u16) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u32(&mut self, v: u32) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u64(&mut self, v: u64) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_f32(&mut self, v: f32) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, v: f64) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_char(&mut self, v: char) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_str(&mut self, v: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_none(&mut self) -> Result<(), ()> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), ()>
|
||||
where V: ser::Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_newtype_struct<T>(&mut self,
|
||||
name: &'static str,
|
||||
value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructNewtype(name)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_tuple_struct<V>(&mut self, name: &str, visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_tuple_variant<V>(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self, name: &str, visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_struct_variant<V>(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), ()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||
|
||||
try!(key.serialize(self));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn format() -> &'static str {
|
||||
"token"
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
enum Error {
|
||||
SyntaxError,
|
||||
EndOfStreamError,
|
||||
UnknownFieldError(String),
|
||||
MissingFieldError(&'static str),
|
||||
InvalidName(&'static str),
|
||||
UnexpectedToken(Token<'static>),
|
||||
ValueError(value::Error),
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field(field: &str) -> Error {
|
||||
Error::UnknownFieldError(field.to_string())
|
||||
}
|
||||
|
||||
fn missing_field(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<value::Error> for Error {
|
||||
fn from(error: value::Error) -> Error {
|
||||
Error::ValueError(error)
|
||||
}
|
||||
}
|
||||
|
||||
struct Deserializer<I> where I: Iterator<Item=Token<'static>> {
|
||||
tokens: iter::Peekable<I>,
|
||||
}
|
||||
|
||||
impl<I> Deserializer<I>
|
||||
where I: Iterator<Item=Token<'static>>
|
||||
{
|
||||
fn new(tokens: I) -> Deserializer<I> {
|
||||
Deserializer {
|
||||
tokens: tokens.peekable(),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_seq(DeserializerSeqVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_map(DeserializerMapVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> de::Deserializer for Deserializer<I>
|
||||
where I: Iterator<Item=Token<'static>>
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
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),
|
||||
Some(Token::I8(v)) => visitor.visit_i8(v),
|
||||
Some(Token::I16(v)) => visitor.visit_i16(v),
|
||||
Some(Token::I32(v)) => visitor.visit_i32(v),
|
||||
Some(Token::I64(v)) => visitor.visit_i64(v),
|
||||
Some(Token::Usize(v)) => visitor.visit_usize(v),
|
||||
Some(Token::U8(v)) => visitor.visit_u8(v),
|
||||
Some(Token::U16(v)) => visitor.visit_u16(v),
|
||||
Some(Token::U32(v)) => visitor.visit_u32(v),
|
||||
Some(Token::U64(v)) => visitor.visit_u64(v),
|
||||
Some(Token::F32(v)) => visitor.visit_f32(v),
|
||||
Some(Token::F64(v)) => visitor.visit_f64(v),
|
||||
Some(Token::Char(v)) => visitor.visit_char(v),
|
||||
Some(Token::Str(v)) => visitor.visit_str(v),
|
||||
Some(Token::String(v)) => visitor.visit_string(v),
|
||||
Some(Token::Bytes(v)) => visitor.visit_bytes(v),
|
||||
Some(Token::Option(false)) => visitor.visit_none(),
|
||||
Some(Token::Option(true)) => visitor.visit_some(self),
|
||||
Some(Token::Unit) => visitor.visit_unit(),
|
||||
Some(Token::UnitStruct(name)) => visitor.visit_unit_struct(name),
|
||||
Some(Token::SeqStart(len)) | Some(Token::TupleStructStart(_, len)) => {
|
||||
self.visit_seq(len, visitor)
|
||||
}
|
||||
Some(Token::MapStart(len)) | Some(Token::StructStart(_, len)) => {
|
||||
self.visit_map(len, visitor)
|
||||
}
|
||||
//Some(Token::Name(_)) => self.visit(visitor),
|
||||
Some(token) => Err(Error::UnexpectedToken(token)),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
||||
/// `None`, or a regular value as `Some(value)`.
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_enum<V>(&mut self,
|
||||
name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::EnumStart(n)) if name == n => {
|
||||
self.tokens.next();
|
||||
|
||||
visitor.visit(DeserializerVariantVisitor {
|
||||
de: self,
|
||||
})
|
||||
}
|
||||
Some(&Token::EnumUnit(n, _))
|
||||
| Some(&Token::EnumNewtype(n, _))
|
||||
| Some(&Token::EnumSeqStart(n, _, _))
|
||||
| Some(&Token::EnumMapStart(n, _, _)) if name == n => {
|
||||
visitor.visit(DeserializerVariantVisitor {
|
||||
de: self,
|
||||
})
|
||||
}
|
||||
Some(_) => {
|
||||
let token = self.tokens.next().unwrap();
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
None => { return Err(Error::EndOfStreamError); }
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_unit_struct<V>(&mut self, name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::UnitStruct(n)) => {
|
||||
self.tokens.next();
|
||||
if name == n {
|
||||
visitor.visit_unit()
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(_) => self.visit(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_newtype_struct<V>(&mut self,
|
||||
name: &str,
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::StructNewtype(n)) => {
|
||||
self.tokens.next();
|
||||
if name == n {
|
||||
visitor.visit_newtype_struct(self)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(_) => self.visit(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_tuple_struct<V>(&mut self,
|
||||
name: &str,
|
||||
len: usize,
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::UnitStruct(n)) => {
|
||||
self.tokens.next();
|
||||
if name == n {
|
||||
visitor.visit_unit()
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(&Token::TupleStructStart(n, _)) => {
|
||||
self.tokens.next();
|
||||
if name == n {
|
||||
self.visit_seq(Some(len), visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(&Token::SeqStart(_)) => {
|
||||
self.tokens.next();
|
||||
self.visit_seq(Some(len), visitor)
|
||||
}
|
||||
Some(_) => self.visit(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self,
|
||||
name: &str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::StructStart(n, _)) => {
|
||||
self.tokens.next();
|
||||
if name == n {
|
||||
self.visit_map(Some(fields.len()), visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(&Token::MapStart(_)) => {
|
||||
self.tokens.next();
|
||||
self.visit_map(Some(fields.len()), visitor)
|
||||
}
|
||||
Some(_) => self.visit(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn format() -> &'static str {
|
||||
"token"
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerSeqVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
||||
de: &'a mut Deserializer<I>,
|
||||
len: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a, I> de::SeqVisitor for DeserializerSeqVisitor<'a, I>
|
||||
where I: Iterator<Item=Token<'static>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::SeqSep) => {
|
||||
self.de.tokens.next();
|
||||
self.len = self.len.map(|len| len - 1);
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
Some(&Token::SeqEnd) => Ok(None),
|
||||
Some(_) => {
|
||||
let token = self.de.tokens.next().unwrap();
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
//assert_eq!(self.len.unwrap_or(0), 0);
|
||||
match self.de.tokens.next() {
|
||||
Some(Token::SeqEnd) => Ok(()),
|
||||
Some(token) => Err(Error::UnexpectedToken(token)),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let len = self.len.unwrap_or(0);
|
||||
(len, self.len)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerMapVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
||||
de: &'a mut Deserializer<I>,
|
||||
len: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a, I> de::MapVisitor for DeserializerMapVisitor<'a, I>
|
||||
where I: Iterator<Item=Token<'static>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Error>
|
||||
where K: de::Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::MapSep) => {
|
||||
self.de.tokens.next();
|
||||
self.len = self.len.map(|len| len - 1);
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
Some(&Token::MapEnd) => Ok(None),
|
||||
Some(_) => {
|
||||
let token = self.de.tokens.next().unwrap();
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
Ok(try!(de::Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
//assert_eq!(self.len.unwrap_or(0), 0);
|
||||
match self.de.tokens.next() {
|
||||
Some(Token::MapEnd) => Ok(()),
|
||||
Some(token) => Err(Error::UnexpectedToken(token)),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let len = self.len.unwrap_or(0);
|
||||
(len, self.len)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerVariantVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
|
||||
de: &'a mut Deserializer<I>,
|
||||
}
|
||||
|
||||
impl<'a, I> de::VariantVisitor for DeserializerVariantVisitor<'a, I>
|
||||
where I: Iterator<Item=Token<'static>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::EnumUnit(_, v))
|
||||
| Some(&Token::EnumNewtype(_, v))
|
||||
| Some(&Token::EnumSeqStart(_, v, _))
|
||||
| Some(&Token::EnumMapStart(_, v, _)) => {
|
||||
let value = try!(de::Deserialize::deserialize(&mut v.into_deserializer()));
|
||||
Ok(value)
|
||||
}
|
||||
Some(_) => {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::EnumUnit(_, _)) => {
|
||||
self.de.tokens.next();
|
||||
Ok(())
|
||||
}
|
||||
Some(_) => {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_newtype<T>(&mut self) -> Result<T, Self::Error>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::EnumNewtype(_, _)) => {
|
||||
self.de.tokens.next();
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
Some(_) => {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(&mut self,
|
||||
len: usize,
|
||||
visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::EnumSeqStart(_, _, Some(enum_len))) => {
|
||||
let token = self.de.tokens.next().unwrap();
|
||||
|
||||
if len == enum_len {
|
||||
self.de.visit_seq(Some(len), visitor)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
}
|
||||
Some(_) => {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::EnumMapStart(_, _, Some(enum_len))) => {
|
||||
let token = self.de.tokens.next().unwrap();
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de.visit_map(Some(fields.len()), visitor)
|
||||
} else {
|
||||
Err(Error::UnexpectedToken(token))
|
||||
}
|
||||
}
|
||||
Some(_) => {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(tokens.iter());
|
||||
assert_eq!(ser::Serialize::serialize(value, &mut ser), Ok(()));
|
||||
assert_eq!(ser.tokens.next(), None);
|
||||
}
|
||||
|
||||
pub fn assert_de_tokens<T>(value: &T, tokens: Vec<Token<'static>>)
|
||||
where T: de::Deserialize + PartialEq + fmt::Debug,
|
||||
{
|
||||
let mut de = Deserializer::new(tokens.into_iter());
|
||||
let v: Result<T, Error> = de::Deserialize::deserialize(&mut de);
|
||||
assert_eq!(v.as_ref(), Ok(value));
|
||||
assert_eq!(de.tokens.next(), None);
|
||||
}
|
||||
|
||||
pub fn assert_tokens<T>(value: &T, tokens: Vec<Token<'static>>)
|
||||
where T: ser::Serialize + de::Deserialize + PartialEq + fmt::Debug,
|
||||
{
|
||||
assert_ser_tokens(value, &tokens[..]);
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
-686
@@ -1,686 +0,0 @@
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::hash::Hash;
|
||||
use std::marker::PhantomData;
|
||||
use std::path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use num::FromPrimitive;
|
||||
|
||||
use de::{
|
||||
Deserialize,
|
||||
Deserializer,
|
||||
Error,
|
||||
MapVisitor,
|
||||
SeqVisitor,
|
||||
Visitor,
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct UnitVisitor;
|
||||
|
||||
impl Visitor for UnitVisitor {
|
||||
type Value = ();
|
||||
|
||||
fn visit_unit<E>(&mut self) -> Result<(), E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
visitor.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for () {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<(), D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(UnitVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct BoolVisitor;
|
||||
|
||||
impl Visitor for BoolVisitor {
|
||||
type Value = bool;
|
||||
|
||||
fn visit_bool<E>(&mut self, v: bool) -> Result<bool, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for bool {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<bool, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(BoolVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! impl_deserialize_num_method {
|
||||
($src_ty:ty, $method:ident, $from_method:ident) => {
|
||||
#[inline]
|
||||
fn $method<E>(&mut self, v: $src_ty) -> Result<T, E>
|
||||
where E: Error,
|
||||
{
|
||||
match FromPrimitive::$from_method(v) {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(Error::syntax_error()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrimitiveVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> PrimitiveVisitor<T> {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
PrimitiveVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Deserialize + FromPrimitive
|
||||
> Visitor for PrimitiveVisitor<T> {
|
||||
type Value = T;
|
||||
|
||||
impl_deserialize_num_method!(isize, visit_isize, from_isize);
|
||||
impl_deserialize_num_method!(i8, visit_i8, from_i8);
|
||||
impl_deserialize_num_method!(i16, visit_i16, from_i16);
|
||||
impl_deserialize_num_method!(i32, visit_i32, from_i32);
|
||||
impl_deserialize_num_method!(i64, visit_i64, from_i64);
|
||||
impl_deserialize_num_method!(usize, visit_usize, from_usize);
|
||||
impl_deserialize_num_method!(u8, visit_u8, from_u8);
|
||||
impl_deserialize_num_method!(u16, visit_u16, from_u16);
|
||||
impl_deserialize_num_method!(u32, visit_u32, from_u32);
|
||||
impl_deserialize_num_method!(u64, visit_u64, from_u64);
|
||||
impl_deserialize_num_method!(f32, visit_f32, from_f32);
|
||||
impl_deserialize_num_method!(f64, visit_f64, from_f64);
|
||||
}
|
||||
|
||||
macro_rules! impl_deserialize_num {
|
||||
($ty:ty) => {
|
||||
impl Deserialize for $ty {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(PrimitiveVisitor::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_deserialize_num!(isize);
|
||||
impl_deserialize_num!(i8);
|
||||
impl_deserialize_num!(i16);
|
||||
impl_deserialize_num!(i32);
|
||||
impl_deserialize_num!(i64);
|
||||
impl_deserialize_num!(usize);
|
||||
impl_deserialize_num!(u8);
|
||||
impl_deserialize_num!(u16);
|
||||
impl_deserialize_num!(u32);
|
||||
impl_deserialize_num!(u64);
|
||||
impl_deserialize_num!(f32);
|
||||
impl_deserialize_num!(f64);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct CharVisitor;
|
||||
|
||||
impl Visitor for CharVisitor {
|
||||
type Value = char;
|
||||
|
||||
#[inline]
|
||||
fn visit_char<E>(&mut self, v: char) -> Result<char, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<char, E>
|
||||
where E: Error,
|
||||
{
|
||||
let mut iter = v.chars();
|
||||
if let Some(v) = iter.next() {
|
||||
if iter.next().is_some() {
|
||||
Err(Error::syntax_error())
|
||||
} else {
|
||||
Ok(v)
|
||||
}
|
||||
} else {
|
||||
Err(Error::end_of_stream_error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for char {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<char, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(CharVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct StringVisitor;
|
||||
|
||||
impl Visitor for StringVisitor {
|
||||
type Value = String;
|
||||
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v.to_string())
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<String, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for String {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(StringVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct OptionVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Deserialize,
|
||||
> Visitor for OptionVisitor<T> {
|
||||
type Value = Option<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
Ok(Some(try!(Deserialize::deserialize(deserializer))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for Option<T> where T: Deserialize {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Option<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit_option(OptionVisitor { marker: PhantomData })
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct BTreeSetVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> BTreeSetVisitor<T> {
|
||||
pub fn new() -> Self {
|
||||
BTreeSetVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for BTreeSetVisitor<T>
|
||||
where T: Deserialize + Eq + Ord,
|
||||
{
|
||||
type Value = BTreeSet<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<BTreeSet<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(BTreeSet::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<BTreeSet<T>, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
let mut values = BTreeSet::new();
|
||||
|
||||
while let Some(value) = try!(visitor.visit()) {
|
||||
values.insert(value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for BTreeSet<T>
|
||||
where T: Deserialize + Eq + Ord,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<BTreeSet<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(BTreeSetVisitor::new())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct HashSetVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> HashSetVisitor<T> {
|
||||
pub fn new() -> Self {
|
||||
HashSetVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for HashSetVisitor<T>
|
||||
where T: Deserialize + Eq + Hash,
|
||||
{
|
||||
type Value = HashSet<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<HashSet<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(HashSet::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<HashSet<T>, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut values = HashSet::with_capacity(len);
|
||||
|
||||
while let Some(value) = try!(visitor.visit()) {
|
||||
values.insert(value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for HashSet<T>
|
||||
where T: Deserialize + Eq + Hash,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<HashSet<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(HashSetVisitor::new())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct VecVisitor<T> {
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> VecVisitor<T> {
|
||||
pub fn new() -> Self {
|
||||
VecVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for VecVisitor<T> where T: Deserialize {
|
||||
type Value = Vec<T>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<Vec<T>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Vec<T>, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut values = Vec::with_capacity(len);
|
||||
|
||||
while let Some(value) = try!(visitor.visit()) {
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for Vec<T>
|
||||
where T: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Vec<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(VecVisitor::new())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! tuple_impls {
|
||||
() => {};
|
||||
($($visitor:ident => ($($name:ident),+),)+) => {
|
||||
$(
|
||||
struct $visitor<$($name,)+> {
|
||||
marker: PhantomData<($($name,)+)>,
|
||||
}
|
||||
|
||||
impl<
|
||||
$($name: Deserialize,)+
|
||||
> Visitor for $visitor<$($name,)+> {
|
||||
type Value = ($($name,)+);
|
||||
|
||||
#[inline]
|
||||
#[allow(non_snake_case)]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<($($name,)+), V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
$(
|
||||
let $name = match try!(visitor.visit()) {
|
||||
Some(value) => value,
|
||||
None => { return Err(Error::end_of_stream_error()); }
|
||||
};
|
||||
)+;
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(($($name,)+))
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
$($name: Deserialize),+
|
||||
> Deserialize for ($($name,)+) {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit($visitor { marker: PhantomData })
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
tuple_impls! {
|
||||
TupleVisitor1 => (T0),
|
||||
TupleVisitor2 => (T0, T1),
|
||||
TupleVisitor3 => (T0, T1, T2),
|
||||
TupleVisitor4 => (T0, T1, T2, T3),
|
||||
TupleVisitor5 => (T0, T1, T2, T3, T4),
|
||||
TupleVisitor6 => (T0, T1, T2, T3, T4, T5),
|
||||
TupleVisitor7 => (T0, T1, T2, T3, T4, T5, T6),
|
||||
TupleVisitor8 => (T0, T1, T2, T3, T4, T5, T6, T7),
|
||||
TupleVisitor9 => (T0, T1, T2, T3, T4, T5, T6, T7, T8),
|
||||
TupleVisitor10 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9),
|
||||
TupleVisitor11 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
|
||||
TupleVisitor12 => (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct BTreeMapVisitor<K, V> {
|
||||
marker: PhantomData<BTreeMap<K, V>>,
|
||||
}
|
||||
|
||||
impl<K, V> BTreeMapVisitor<K, V> {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
BTreeMapVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Visitor for BTreeMapVisitor<K, V>
|
||||
where K: Deserialize + Ord,
|
||||
V: Deserialize
|
||||
{
|
||||
type Value = BTreeMap<K, V>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<BTreeMap<K, V>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(BTreeMap::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<Visitor>(&mut self, mut visitor: Visitor) -> Result<BTreeMap<K, V>, Visitor::Error>
|
||||
where Visitor: MapVisitor,
|
||||
{
|
||||
let mut values = BTreeMap::new();
|
||||
|
||||
while let Some((key, value)) = try!(visitor.visit()) {
|
||||
values.insert(key, value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Deserialize for BTreeMap<K, V>
|
||||
where K: Deserialize + Eq + Ord,
|
||||
V: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<BTreeMap<K, V>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(BTreeMapVisitor::new())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct HashMapVisitor<K, V> {
|
||||
marker: PhantomData<HashMap<K, V>>,
|
||||
}
|
||||
|
||||
impl<K, V> HashMapVisitor<K, V> {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
HashMapVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Visitor for HashMapVisitor<K, V>
|
||||
where K: Deserialize + Eq + Hash,
|
||||
V: Deserialize,
|
||||
{
|
||||
type Value = HashMap<K, V>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<HashMap<K, V>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(HashMap::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V_>(&mut self, mut visitor: V_) -> Result<HashMap<K, V>, V_::Error>
|
||||
where V_: MapVisitor,
|
||||
{
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut values = HashMap::with_capacity(len);
|
||||
|
||||
while let Some((key, value)) = try!(visitor.visit()) {
|
||||
values.insert(key, value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> Deserialize for HashMap<K, V>
|
||||
where K: Deserialize + Eq + Hash,
|
||||
V: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<HashMap<K, V>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(HashMapVisitor::new())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// FIXME: `VecMap` is unstable.
|
||||
/*
|
||||
pub struct VecMapVisitor<V> {
|
||||
marker: PhantomData<VecMap<V>>,
|
||||
}
|
||||
|
||||
impl<V> VecMapVisitor<V> {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
VecMapVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> Visitor for VecMapVisitor<V>
|
||||
where V: Deserialize,
|
||||
{
|
||||
type Value = VecMap<V>;
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<VecMap<V>, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(VecMap::new())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V_>(&mut self, mut visitor: V_) -> Result<VecMap<V>, V_::Error>
|
||||
where V_: MapVisitor,
|
||||
{
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut values = VecMap::with_capacity(len);
|
||||
|
||||
while let Some((key, value)) = try!(visitor.visit()) {
|
||||
values.insert(key, value);
|
||||
}
|
||||
|
||||
try!(visitor.end());
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> Deserialize for VecMap<V>
|
||||
where V: Deserialize,
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<VecMap<V>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(VecMapVisitor::new())
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct PathBufVisitor;
|
||||
|
||||
impl Visitor for PathBufVisitor {
|
||||
type Value = path::PathBuf;
|
||||
|
||||
fn visit_str<E>(&mut self, v: &str) -> Result<path::PathBuf, E>
|
||||
where E: Error,
|
||||
{
|
||||
Ok(From::from(v))
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<path::PathBuf, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_str(&v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for path::PathBuf {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<path::PathBuf, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.visit(PathBufVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T: Deserialize> Deserialize for Box<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Box<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Box::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Arc<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Arc<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Arc::new(val))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserialize> Deserialize for Rc<T> {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Rc<T>, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
let val = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(Rc::new(val))
|
||||
}
|
||||
}
|
||||
-469
@@ -1,469 +0,0 @@
|
||||
//! Generic deserialization framework.
|
||||
|
||||
pub mod impls;
|
||||
pub mod value;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Error {
|
||||
fn syntax_error() -> Self;
|
||||
|
||||
fn end_of_stream_error() -> Self;
|
||||
|
||||
fn unknown_field_error(field: &str) -> Self;
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Self;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Deserialize {
|
||||
/// Deserialize this value given this `Deserializer`.
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
||||
where D: Deserializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `Deserializer` is an abstract trait that can deserialize values into a `Visitor`.
|
||||
pub trait Deserializer {
|
||||
type Error: Error;
|
||||
|
||||
/// The `visit` method walks a visitor through a value as it is being deserialized.
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor;
|
||||
|
||||
/// The `visit_option` method allows a `Deserialize` type to inform the `Deserializer` that
|
||||
/// it's expecting an optional value. This allows deserializers that encode an optional value
|
||||
/// as a nullable value to convert the null value into a `None`, and a regular value as
|
||||
/// `Some(value)`.
|
||||
#[inline]
|
||||
fn visit_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_seq` method allows a `Deserialize` type to inform the `Deserializer` that it's
|
||||
/// expecting a sequence of values. This allows deserializers to parse sequences that aren't
|
||||
/// tagged as sequences.
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_map` method allows a `Deserialize` type to inform the `Deserializer` that it's
|
||||
/// expecting a map of values. This allows deserializers to parse sequences that aren't tagged
|
||||
/// as maps.
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_named_unit` method allows a `Deserialize` type to inform the `Deserializer` that
|
||||
/// it's expecting a named unit. This allows deserializers to a named unit that aren't tagged
|
||||
/// as a named unit.
|
||||
#[inline]
|
||||
fn visit_named_unit<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_named_seq` method allows a `Deserialize` type to inform the `Deserializer` that
|
||||
/// it's expecting a named sequence of values. This allows deserializers to parse sequences
|
||||
/// that aren't tagged as sequences.
|
||||
#[inline]
|
||||
fn visit_named_seq<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_named_map` method allows a `Deserialize` type to inform the `Deserializer` that
|
||||
/// it's expecting a map of values. This allows deserializers to parse sequences that aren't
|
||||
/// tagged as maps.
|
||||
#[inline]
|
||||
fn visit_named_map<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
/// The `visit_enum` method allows a `Deserialize` type to inform the `Deserializer` that it's
|
||||
/// expecting an enum value. This allows deserializers that provide a custom enumeration
|
||||
/// serialization to properly deserialize the type.
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self, _enum: &str, _visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: EnumVisitor,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
/// The `visit_bytes` method allows a `Deserialize` type to inform the `Deserializer` that it's
|
||||
/// expecting a `Vec<u8>`. This allows deserializers that provide a custom byte vector
|
||||
/// serialization to properly deserialize the type.
|
||||
#[inline]
|
||||
fn visit_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
self.visit(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Visitor {
|
||||
type Value;
|
||||
|
||||
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_isize<E>(&mut self, v: isize) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
fn visit_i8<E>(&mut self, v: i8) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
fn visit_i16<E>(&mut self, v: i16) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
fn visit_i32<E>(&mut self, v: i32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
fn visit_i64<E>(&mut self, _v: i64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_usize<E>(&mut self, v: usize) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
fn visit_u8<E>(&mut self, v: u8) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
fn visit_u16<E>(&mut self, v: u16) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
fn visit_u32<E>(&mut self, v: u32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
fn visit_u64<E>(&mut self, _v: u64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_f32<E>(&mut self, v: f32) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
fn visit_f64<E>(&mut self, _v: f64) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||
// doesn't support encoding a `char` into a stack buffer.
|
||||
self.visit_string(v.to_string())
|
||||
}
|
||||
|
||||
fn visit_str<E>(&mut self, _v: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_string<E>(&mut self, v: String) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_str(&v)
|
||||
}
|
||||
|
||||
fn visit_unit<E>(&mut self) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_named_unit<E>(&mut self, _name: &str) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
fn visit_none<E>(&mut self) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_some<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(&mut self, _v: &[u8]) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
fn visit_byte_buf<E>(&mut self, _v: Vec<u8>) -> Result<Self::Value, E>
|
||||
where E: Error,
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait SeqVisitor {
|
||||
type Error: Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Self::Error>
|
||||
where T: Deserialize;
|
||||
|
||||
fn end(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, V> SeqVisitor for &'a mut V where V: SeqVisitor {
|
||||
type Error = V::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, V::Error>
|
||||
where T: Deserialize
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(&mut self) -> Result<(), V::Error> {
|
||||
(**self).end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(**self).size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait MapVisitor {
|
||||
type Error: Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
|
||||
where K: Deserialize,
|
||||
V: Deserialize,
|
||||
{
|
||||
match try!(self.visit_key()) {
|
||||
Some(key) => {
|
||||
let value = try!(self.visit_value());
|
||||
Ok(Some((key, value)))
|
||||
}
|
||||
None => Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Self::Error>
|
||||
where K: Deserialize;
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V, Self::Error>
|
||||
where V: Deserialize;
|
||||
|
||||
fn end(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(0, None)
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, field: &'static str) -> Result<V, Self::Error>
|
||||
where V: Deserialize,
|
||||
{
|
||||
Err(Error::missing_field_error(field))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, V_> MapVisitor for &'a mut V_ where V_: MapVisitor {
|
||||
type Error = V_::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<K, V>(&mut self) -> Result<Option<(K, V)>, V_::Error>
|
||||
where K: Deserialize,
|
||||
V: Deserialize,
|
||||
{
|
||||
(**self).visit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, V_::Error>
|
||||
where K: Deserialize
|
||||
{
|
||||
(**self).visit_key()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_value<V>(&mut self) -> Result<V, V_::Error>
|
||||
where V: Deserialize
|
||||
{
|
||||
(**self).visit_value()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(&mut self) -> Result<(), V_::Error> {
|
||||
(**self).end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(**self).size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `EnumVisitor` is a visitor that is created by the `Deserialize` and passed to the
|
||||
/// `Deserializer` in order to deserialize enums.
|
||||
pub trait EnumVisitor {
|
||||
type Value;
|
||||
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
|
||||
where V: VariantVisitor;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// `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 {
|
||||
type Error: Error;
|
||||
|
||||
/// `visit_variant` is called to identify which variant to deserialize.
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Self::Error>
|
||||
where V: Deserialize;
|
||||
|
||||
/// `visit_unit` is called when deserializing a variant with no values.
|
||||
fn visit_unit(&mut self) -> Result<(), Self::Error> {
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
/// `visit_seq` is called when deserializing a tuple-like variant.
|
||||
fn visit_seq<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
|
||||
/// `visit_map` is called when deserializing a struct-like variant.
|
||||
fn visit_map<V>(&mut self, _visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: Visitor
|
||||
{
|
||||
Err(Error::syntax_error())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> VariantVisitor for &'a mut T where T: VariantVisitor {
|
||||
type Error = T::Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, T::Error>
|
||||
where V: Deserialize
|
||||
{
|
||||
(**self).visit_variant()
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), T::Error> {
|
||||
(**self).visit_unit()
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, T::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
(**self).visit_seq(visitor)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, T::Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
(**self).visit_map(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,84 +0,0 @@
|
||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use ser::{self, Serialize};
|
||||
use json::value::{self, Value};
|
||||
|
||||
pub struct ArrayBuilder {
|
||||
array: Vec<Value>,
|
||||
}
|
||||
|
||||
impl ArrayBuilder {
|
||||
pub fn new() -> ArrayBuilder {
|
||||
ArrayBuilder { array: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
Value::Array(self.array)
|
||||
}
|
||||
|
||||
pub fn push<T: ser::Serialize>(mut self, v: T) -> ArrayBuilder {
|
||||
self.array.push(value::to_value(&v));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push_array<F>(mut self, f: F) -> ArrayBuilder where
|
||||
F: FnOnce(ArrayBuilder) -> ArrayBuilder
|
||||
{
|
||||
let builder = ArrayBuilder::new();
|
||||
self.array.push(f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push_object<F>(mut self, f: F) -> ArrayBuilder where
|
||||
F: FnOnce(ObjectBuilder) -> ObjectBuilder
|
||||
{
|
||||
let builder = ObjectBuilder::new();
|
||||
self.array.push(f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ObjectBuilder {
|
||||
object: BTreeMap<String, Value>,
|
||||
}
|
||||
|
||||
impl ObjectBuilder {
|
||||
pub fn new() -> ObjectBuilder {
|
||||
ObjectBuilder { object: BTreeMap::new() }
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
Value::Object(self.object)
|
||||
}
|
||||
|
||||
pub fn insert<V: ser::Serialize>(mut self, k: String, v: V) -> ObjectBuilder {
|
||||
self.object.insert(k, value::to_value(&v));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_array<F>(mut self, key: String, f: F) -> ObjectBuilder where
|
||||
F: FnOnce(ArrayBuilder) -> ArrayBuilder
|
||||
{
|
||||
let builder = ArrayBuilder::new();
|
||||
self.object.insert(key, f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_object<F>(mut self, key: String, f: F) -> ObjectBuilder where
|
||||
F: FnOnce(ObjectBuilder) -> ObjectBuilder
|
||||
{
|
||||
let builder = ObjectBuilder::new();
|
||||
self.object.insert(key, f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
}
|
||||
-675
@@ -1,675 +0,0 @@
|
||||
use std::char;
|
||||
use std::io;
|
||||
use std::str;
|
||||
|
||||
use de;
|
||||
use iter::LineColIterator;
|
||||
|
||||
use super::error::{Error, ErrorCode};
|
||||
|
||||
pub struct Deserializer<Iter: Iterator<Item=io::Result<u8>>> {
|
||||
rdr: LineColIterator<Iter>,
|
||||
ch: Option<u8>,
|
||||
str_buf: Vec<u8>,
|
||||
}
|
||||
|
||||
impl<Iter> Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
/// Creates the JSON parser from an `std::iter::Iterator`.
|
||||
#[inline]
|
||||
pub fn new(rdr: Iter) -> Result<Deserializer<Iter>, Error> {
|
||||
let mut deserializer = Deserializer {
|
||||
rdr: LineColIterator::new(rdr),
|
||||
ch: None,
|
||||
str_buf: Vec::with_capacity(128),
|
||||
};
|
||||
|
||||
try!(deserializer.bump());
|
||||
|
||||
Ok(deserializer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn end(&mut self) -> Result<(), Error> {
|
||||
try!(self.parse_whitespace());
|
||||
if self.eof() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
}
|
||||
|
||||
fn eof(&self) -> bool { self.ch.is_none() }
|
||||
|
||||
fn ch_or_null(&self) -> u8 { self.ch.unwrap_or(b'\x00') }
|
||||
|
||||
fn bump(&mut self) -> Result<(), Error> {
|
||||
self.ch = match self.rdr.next() {
|
||||
Some(Err(err)) => { return Err(Error::IoError(err)); }
|
||||
Some(Ok(ch)) => Some(ch),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn next_char(&mut self) -> Result<Option<u8>, Error> {
|
||||
try!(self.bump());
|
||||
Ok(self.ch)
|
||||
}
|
||||
|
||||
fn ch_is(&self, c: u8) -> bool {
|
||||
self.ch == Some(c)
|
||||
}
|
||||
|
||||
fn error(&mut self, reason: ErrorCode) -> Error {
|
||||
Error::SyntaxError(reason, self.rdr.line(), self.rdr.col())
|
||||
}
|
||||
|
||||
fn parse_whitespace(&mut self) -> Result<(), Error> {
|
||||
while self.ch_is(b' ') ||
|
||||
self.ch_is(b'\n') ||
|
||||
self.ch_is(b'\t') ||
|
||||
self.ch_is(b'\r') { try!(self.bump()); }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_value<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if self.eof() {
|
||||
return Err(self.error(ErrorCode::EOFWhileParsingValue));
|
||||
}
|
||||
|
||||
let value = match self.ch_or_null() {
|
||||
b'n' => {
|
||||
try!(self.parse_ident(b"ull"));
|
||||
visitor.visit_unit()
|
||||
}
|
||||
b't' => {
|
||||
try!(self.parse_ident(b"rue"));
|
||||
visitor.visit_bool(true)
|
||||
}
|
||||
b'f' => {
|
||||
try!(self.parse_ident(b"alse"));
|
||||
visitor.visit_bool(false)
|
||||
}
|
||||
b'0' ... b'9' | b'-' => self.parse_number(visitor),
|
||||
b'"' => {
|
||||
try!(self.parse_string());
|
||||
let s = str::from_utf8(&self.str_buf).unwrap();
|
||||
visitor.visit_str(s)
|
||||
}
|
||||
b'[' => {
|
||||
try!(self.bump());
|
||||
visitor.visit_seq(SeqVisitor::new(self))
|
||||
}
|
||||
b'{' => {
|
||||
try!(self.bump());
|
||||
visitor.visit_map(MapVisitor::new(self))
|
||||
}
|
||||
_ => {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
};
|
||||
|
||||
match value {
|
||||
Ok(value) => Ok(value),
|
||||
Err(Error::SyntaxError(code, _, _)) => Err(self.error(code)),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ident(&mut self, ident: &[u8]) -> Result<(), Error> {
|
||||
for c in ident {
|
||||
if Some(*c) != try!(self.next_char()) {
|
||||
return Err(self.error(ErrorCode::ExpectedSomeIdent));
|
||||
}
|
||||
}
|
||||
|
||||
try!(self.bump());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_number<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
let mut neg = false;
|
||||
|
||||
if self.ch_is(b'-') {
|
||||
try!(self.bump());
|
||||
neg = true;
|
||||
}
|
||||
|
||||
let res = try!(self.parse_integer());
|
||||
|
||||
if self.ch_is(b'.') || self.ch_is(b'e') || self.ch_is(b'E') {
|
||||
let mut res = res as f64;
|
||||
|
||||
if self.ch_is(b'.') {
|
||||
res = try!(self.parse_decimal(res));
|
||||
}
|
||||
|
||||
if self.ch_is(b'e') || self.ch_is(b'E') {
|
||||
res = try!(self.parse_exponent(res));
|
||||
}
|
||||
|
||||
if neg {
|
||||
visitor.visit_f64(-res)
|
||||
} else {
|
||||
visitor.visit_f64(res)
|
||||
}
|
||||
} else {
|
||||
if neg {
|
||||
let res = -(res as i64);
|
||||
|
||||
// Make sure we didn't underflow.
|
||||
if res > 0 {
|
||||
Err(self.error(ErrorCode::InvalidNumber))
|
||||
} else {
|
||||
visitor.visit_i64(res)
|
||||
}
|
||||
} else {
|
||||
visitor.visit_u64(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_integer(&mut self) -> Result<u64, Error> {
|
||||
let mut res = 0;
|
||||
|
||||
match self.ch_or_null() {
|
||||
b'0' => {
|
||||
try!(self.bump());
|
||||
|
||||
// There can be only one leading '0'.
|
||||
match self.ch_or_null() {
|
||||
b'0' ... b'9' => {
|
||||
return Err(self.error(ErrorCode::InvalidNumber));
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
},
|
||||
b'1' ... b'9' => {
|
||||
while !self.eof() {
|
||||
match self.ch_or_null() {
|
||||
c @ b'0' ... b'9' => {
|
||||
res *= 10;
|
||||
res += (c as u64) - (b'0' as u64);
|
||||
try!(self.bump());
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => { return Err(self.error(ErrorCode::InvalidNumber)); }
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn parse_decimal(&mut self, res: f64) -> Result<f64, Error> {
|
||||
try!(self.bump());
|
||||
|
||||
// Make sure a digit follows the decimal place.
|
||||
match self.ch_or_null() {
|
||||
b'0' ... b'9' => (),
|
||||
_ => { return Err(self.error(ErrorCode::InvalidNumber)); }
|
||||
}
|
||||
|
||||
let mut res = res;
|
||||
let mut dec = 1.0;
|
||||
while !self.eof() {
|
||||
match self.ch_or_null() {
|
||||
c @ b'0' ... b'9' => {
|
||||
dec /= 10.0;
|
||||
res += (((c as u64) - (b'0' as u64)) as f64) * dec;
|
||||
try!(self.bump());
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn parse_exponent(&mut self, mut res: f64) -> Result<f64, Error> {
|
||||
try!(self.bump());
|
||||
|
||||
let mut exp = 0;
|
||||
let mut neg_exp = false;
|
||||
|
||||
if self.ch_is(b'+') {
|
||||
try!(self.bump());
|
||||
} else if self.ch_is(b'-') {
|
||||
try!(self.bump());
|
||||
neg_exp = true;
|
||||
}
|
||||
|
||||
// Make sure a digit follows the exponent place.
|
||||
match self.ch_or_null() {
|
||||
b'0' ... b'9' => (),
|
||||
_ => { return Err(self.error(ErrorCode::InvalidNumber)); }
|
||||
}
|
||||
while !self.eof() {
|
||||
match self.ch_or_null() {
|
||||
c @ b'0' ... b'9' => {
|
||||
exp *= 10;
|
||||
exp += (c as i32) - (b'0' as i32);
|
||||
|
||||
try!(self.bump());
|
||||
}
|
||||
_ => break
|
||||
}
|
||||
}
|
||||
|
||||
let exp: f64 = 10_f64.powi(exp);
|
||||
if neg_exp {
|
||||
res /= exp;
|
||||
} else {
|
||||
res *= exp;
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn decode_hex_escape(&mut self) -> Result<u16, Error> {
|
||||
let mut i = 0;
|
||||
let mut n = 0u16;
|
||||
while i < 4 && !self.eof() {
|
||||
try!(self.bump());
|
||||
n = match self.ch_or_null() {
|
||||
c @ b'0' ... b'9' => n * 16_u16 + ((c as u16) - (b'0' as u16)),
|
||||
b'a' | b'A' => n * 16_u16 + 10_u16,
|
||||
b'b' | b'B' => n * 16_u16 + 11_u16,
|
||||
b'c' | b'C' => n * 16_u16 + 12_u16,
|
||||
b'd' | b'D' => n * 16_u16 + 13_u16,
|
||||
b'e' | b'E' => n * 16_u16 + 14_u16,
|
||||
b'f' | b'F' => n * 16_u16 + 15_u16,
|
||||
_ => { return Err(self.error(ErrorCode::InvalidEscape)); }
|
||||
};
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Error out if we didn't parse 4 digits.
|
||||
if i != 4 {
|
||||
return Err(self.error(ErrorCode::InvalidEscape));
|
||||
}
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn parse_string(&mut self) -> Result<(), Error> {
|
||||
self.str_buf.clear();
|
||||
|
||||
let mut escape = false;
|
||||
|
||||
loop {
|
||||
let ch = match try!(self.next_char()) {
|
||||
Some(ch) => ch,
|
||||
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
||||
};
|
||||
|
||||
if escape {
|
||||
match ch {
|
||||
b'"' => self.str_buf.push(b'"'),
|
||||
b'\\' => self.str_buf.push(b'\\'),
|
||||
b'/' => self.str_buf.push(b'/'),
|
||||
b'b' => self.str_buf.push(b'\x08'),
|
||||
b'f' => self.str_buf.push(b'\x0c'),
|
||||
b'n' => self.str_buf.push(b'\n'),
|
||||
b'r' => self.str_buf.push(b'\r'),
|
||||
b't' => self.str_buf.push(b'\t'),
|
||||
b'u' => {
|
||||
let c = match try!(self.decode_hex_escape()) {
|
||||
0xDC00 ... 0xDFFF => {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
}
|
||||
|
||||
// Non-BMP characters are encoded as a sequence of
|
||||
// two hex escapes, representing UTF-16 surrogates.
|
||||
n1 @ 0xD800 ... 0xDBFF => {
|
||||
match (try!(self.next_char()), try!(self.next_char())) {
|
||||
(Some(b'\\'), Some(b'u')) => (),
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
||||
}
|
||||
}
|
||||
|
||||
let n2 = try!(self.decode_hex_escape());
|
||||
|
||||
if n2 < 0xDC00 || n2 > 0xDFFF {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
}
|
||||
|
||||
let n = (((n1 - 0xD800) as u32) << 10 |
|
||||
(n2 - 0xDC00) as u32) + 0x1_0000;
|
||||
|
||||
match char::from_u32(n as u32) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n => {
|
||||
match char::from_u32(n as u32) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// FIXME: this allocation is required in order to be compatible with stable
|
||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||
let buf = c.to_string();
|
||||
self.str_buf.extend(buf.bytes());
|
||||
}
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::InvalidEscape));
|
||||
}
|
||||
}
|
||||
escape = false;
|
||||
} else {
|
||||
match ch {
|
||||
b'"' => {
|
||||
try!(self.bump());
|
||||
return Ok(());
|
||||
}
|
||||
b'\\' => {
|
||||
escape = true;
|
||||
}
|
||||
ch => {
|
||||
self.str_buf.push(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_object_colon(&mut self) -> Result<(), Error> {
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if self.ch_is(b':') {
|
||||
try!(self.bump());
|
||||
Ok(())
|
||||
} else if self.eof() {
|
||||
Err(self.error(ErrorCode::EOFWhileParsingObject))
|
||||
} else {
|
||||
Err(self.error(ErrorCode::ExpectedColon))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::Deserializer for Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
self.parse_value(visitor)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if self.eof() {
|
||||
return Err(self.error(ErrorCode::EOFWhileParsingValue));
|
||||
}
|
||||
|
||||
if self.ch_is(b'n') {
|
||||
try!(self.parse_ident(b"ull"));
|
||||
visitor.visit_none()
|
||||
} else {
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if self.ch_is(b'{') {
|
||||
try!(self.bump());
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
let value = {
|
||||
try!(visitor.visit(&mut *self))
|
||||
};
|
||||
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if self.ch_is(b'}') {
|
||||
try!(self.bump());
|
||||
Ok(value)
|
||||
} else {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
} else {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqVisitor<'a, Iter: 'a + Iterator<Item=io::Result<u8>>> {
|
||||
de: &'a mut Deserializer<Iter>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Item=io::Result<u8>>> SeqVisitor<'a, Iter> {
|
||||
fn new(de: &'a mut Deserializer<Iter>) -> Self {
|
||||
SeqVisitor {
|
||||
de: de,
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::SeqVisitor for SeqVisitor<'a, Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
if self.de.ch_is(b']') {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
if self.de.ch_is(b',') {
|
||||
try!(self.de.bump());
|
||||
} else if self.de.eof() {
|
||||
return Err(self.de.error(ErrorCode::EOFWhileParsingList));
|
||||
} else {
|
||||
return Err(self.de.error(ErrorCode::ExpectedListCommaOrEnd));
|
||||
}
|
||||
}
|
||||
|
||||
let value = try!(de::Deserialize::deserialize(self.de));
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
if self.de.ch_is(b']') {
|
||||
self.de.bump()
|
||||
} else if self.de.eof() {
|
||||
Err(self.de.error(ErrorCode::EOFWhileParsingList))
|
||||
} else {
|
||||
Err(self.de.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MapVisitor<'a, Iter: 'a + Iterator<Item=io::Result<u8>>> {
|
||||
de: &'a mut Deserializer<Iter>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Item=io::Result<u8>>> MapVisitor<'a, Iter> {
|
||||
fn new(de: &'a mut Deserializer<Iter>) -> Self {
|
||||
MapVisitor {
|
||||
de: de,
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Error>
|
||||
where K: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
if self.de.ch_is(b'}') {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
if self.de.ch_is(b',') {
|
||||
try!(self.de.bump());
|
||||
try!(self.de.parse_whitespace());
|
||||
} else if self.de.eof() {
|
||||
return Err(self.de.error(ErrorCode::EOFWhileParsingObject));
|
||||
} else {
|
||||
return Err(self.de.error(ErrorCode::ExpectedObjectCommaOrEnd));
|
||||
}
|
||||
}
|
||||
|
||||
if self.de.eof() {
|
||||
return Err(self.de.error(ErrorCode::EOFWhileParsingValue));
|
||||
}
|
||||
|
||||
if !self.de.ch_is(b'"') {
|
||||
return Err(self.de.error(ErrorCode::KeyMustBeAString));
|
||||
}
|
||||
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_object_colon());
|
||||
|
||||
Ok(try!(de::Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
if self.de.ch_is(b'}') {
|
||||
try!(self.de.bump());
|
||||
Ok(())
|
||||
} else if self.de.eof() {
|
||||
Err(self.de.error(ErrorCode::EOFWhileParsingObject))
|
||||
} else {
|
||||
Err(self.de.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
let mut de = de::value::ValueDeserializer::into_deserializer(());
|
||||
Ok(try!(de::Deserialize::deserialize(&mut de)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::VariantVisitor for Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize
|
||||
{
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||
try!(self.parse_object_colon());
|
||||
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_object_colon());
|
||||
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_object_colon());
|
||||
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `std::io::Read`.
|
||||
pub fn from_iter<I, T>(iter: I) -> Result<T, Error>
|
||||
where I: Iterator<Item=io::Result<u8>>,
|
||||
T: de::Deserialize,
|
||||
{
|
||||
let mut de = try!(Deserializer::new(iter));
|
||||
let value = try!(de::Deserialize::deserialize(&mut de));
|
||||
|
||||
// Make sure the whole stream has been consumed.
|
||||
try!(de.end());
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `std::io::Read`.
|
||||
pub fn from_reader<R, T>(rdr: R) -> Result<T, Error>
|
||||
where R: io::Read,
|
||||
T: de::Deserialize,
|
||||
{
|
||||
from_iter(rdr.bytes())
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `&str`.
|
||||
pub fn from_slice<T>(v: &[u8]) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
from_iter(v.iter().map(|byte| Ok(*byte)))
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `&str`.
|
||||
pub fn from_str<T>(s: &str) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
from_slice(s.as_bytes())
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
||||
use de;
|
||||
|
||||
/// The errors that can arise while parsing a JSON stream.
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum ErrorCode {
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
EOFWhileParsingString,
|
||||
EOFWhileParsingValue,
|
||||
ExpectedColon,
|
||||
ExpectedConversion,
|
||||
ExpectedEnumEnd,
|
||||
ExpectedEnumEndToken,
|
||||
ExpectedEnumMapStart,
|
||||
ExpectedEnumToken,
|
||||
ExpectedEnumVariantString,
|
||||
ExpectedListCommaOrEnd,
|
||||
ExpectedName,
|
||||
ExpectedObjectCommaOrEnd,
|
||||
ExpectedSomeIdent,
|
||||
ExpectedSomeValue,
|
||||
InvalidEscape,
|
||||
InvalidNumber,
|
||||
InvalidUnicodeCodePoint,
|
||||
KeyMustBeAString,
|
||||
LoneLeadingSurrogateInHexEscape,
|
||||
UnknownField(String),
|
||||
MissingField(&'static str),
|
||||
NotFourDigit,
|
||||
NotUtf8,
|
||||
TrailingCharacters,
|
||||
UnexpectedEndOfHexEscape,
|
||||
UnknownVariant,
|
||||
UnrecognizedHex,
|
||||
}
|
||||
|
||||
impl fmt::Debug for ErrorCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use std::fmt::Debug;
|
||||
|
||||
match *self {
|
||||
//ErrorCode::ConversionError(ref token) => write!(f, "failed to convert {}", token),
|
||||
ErrorCode::EOFWhileParsingList => "EOF While parsing list".fmt(f),
|
||||
ErrorCode::EOFWhileParsingObject => "EOF While parsing object".fmt(f),
|
||||
ErrorCode::EOFWhileParsingString => "EOF While parsing string".fmt(f),
|
||||
ErrorCode::EOFWhileParsingValue => "EOF While parsing value".fmt(f),
|
||||
ErrorCode::ExpectedColon => "expected `:`".fmt(f),
|
||||
ErrorCode::ExpectedConversion => "expected conversion".fmt(f),
|
||||
ErrorCode::ExpectedEnumEnd => "expected enum end".fmt(f),
|
||||
ErrorCode::ExpectedEnumEndToken => "expected enum map end".fmt(f),
|
||||
ErrorCode::ExpectedEnumMapStart => "expected enum map start".fmt(f),
|
||||
ErrorCode::ExpectedEnumToken => "expected enum token".fmt(f),
|
||||
ErrorCode::ExpectedEnumVariantString => "expected variant".fmt(f),
|
||||
ErrorCode::ExpectedListCommaOrEnd => "expected `,` or `]`".fmt(f),
|
||||
ErrorCode::ExpectedName => "expected name".fmt(f),
|
||||
ErrorCode::ExpectedObjectCommaOrEnd => "expected `,` or `}`".fmt(f),
|
||||
ErrorCode::ExpectedSomeIdent => "expected ident".fmt(f),
|
||||
ErrorCode::ExpectedSomeValue => "expected value".fmt(f),
|
||||
//ErrorCode::ExpectedTokens(ref token, tokens) => write!(f, "expected {}, found {}", tokens, token),
|
||||
ErrorCode::InvalidEscape => "invalid escape".fmt(f),
|
||||
ErrorCode::InvalidNumber => "invalid number".fmt(f),
|
||||
ErrorCode::InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f),
|
||||
ErrorCode::KeyMustBeAString => "key must be a string".fmt(f),
|
||||
ErrorCode::LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f),
|
||||
ErrorCode::UnknownField(ref field) => write!(f, "unknown field \"{}\"", field),
|
||||
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
|
||||
ErrorCode::NotFourDigit => "invalid \\u escape (not four digits)".fmt(f),
|
||||
ErrorCode::NotUtf8 => "contents not utf-8".fmt(f),
|
||||
ErrorCode::TrailingCharacters => "trailing characters".fmt(f),
|
||||
ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f),
|
||||
//ErrorCode::UnexpectedName(ref name) => write!(f, "unexpected name {}", name),
|
||||
ErrorCode::UnknownVariant => "unknown variant".fmt(f),
|
||||
ErrorCode::UnrecognizedHex => "invalid \\u escape (unrecognized hex)".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// msg, line, col
|
||||
SyntaxError(ErrorCode, usize, usize),
|
||||
IoError(io::Error),
|
||||
/*
|
||||
ExpectedError(String, String),
|
||||
*/
|
||||
MissingFieldError(&'static str),
|
||||
/*
|
||||
UnknownVariantError(String),
|
||||
*/
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::SyntaxError(..) => "syntax error",
|
||||
Error::IoError(ref error) => error::Error::description(error),
|
||||
/*
|
||||
Error::ExpectedError(ref expected, _) => &expected,
|
||||
*/
|
||||
Error::MissingFieldError(_) => "missing field",
|
||||
/*
|
||||
Error::UnknownVariantError(_) => "unknown variant",
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
Error::IoError(ref error) => Some(error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::SyntaxError(ref code, line, col) => {
|
||||
write!(fmt, "{:?} at line {} column {}", code, line, col)
|
||||
}
|
||||
Error::IoError(ref error) => fmt::Display::fmt(error, fmt),
|
||||
/*
|
||||
Error::ExpectedError(ref expected, ref found) => {
|
||||
Some(format!("expected {}, found {}", expected, found))
|
||||
}
|
||||
*/
|
||||
Error::MissingFieldError(ref field) => {
|
||||
write!(fmt, "missing field {}", field)
|
||||
}
|
||||
/*
|
||||
Error::UnknownVariantError(ref variant) => {
|
||||
Some(format!("unknown variant {}", variant))
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(error: io::Error) -> Error {
|
||||
Error::IoError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<de::value::Error> for Error {
|
||||
fn from(error: de::value::Error) -> Error {
|
||||
match error {
|
||||
de::value::Error::SyntaxError => {
|
||||
de::Error::syntax_error()
|
||||
}
|
||||
de::value::Error::EndOfStreamError => {
|
||||
de::Error::end_of_stream_error()
|
||||
}
|
||||
de::value::Error::UnknownFieldError(field) => {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field), 0, 0)
|
||||
}
|
||||
de::value::Error::MissingFieldError(field) => {
|
||||
de::Error::missing_field_error(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax_error() -> Error {
|
||||
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
|
||||
}
|
||||
|
||||
fn end_of_stream_error() -> Error {
|
||||
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0)
|
||||
}
|
||||
|
||||
fn unknown_field_error(field: &str) -> Error {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field.to_string()), 0, 0)
|
||||
}
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
}
|
||||
-113
@@ -1,113 +0,0 @@
|
||||
//! JSON and serialization
|
||||
//!
|
||||
//! # What is JSON?
|
||||
//!
|
||||
//! JSON (JavaScript Object Notation) is a way to write data in JavaScript. Like XML, it allows to
|
||||
//! encode structured data in a text format that can be easily read by humans. Its simple syntax
|
||||
//! and native compatibility with JavaScript have made it a widely used format.
|
||||
//!
|
||||
//! Data types that can be encoded are JavaScript types (see the `serde::json:Value` enum for more
|
||||
//! details):
|
||||
//!
|
||||
//! * `Boolean`: equivalent to rust's `bool`
|
||||
//! * `I64`: equivalent to rust's `i64`
|
||||
//! * `U64`: equivalent to rust's `u64`
|
||||
//! * `F64`: equivalent to rust's `i64`
|
||||
//! * `String`: equivalent to rust's `String`
|
||||
//! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the
|
||||
//! same array
|
||||
//! * `Object`: equivalent to rust's `BTreeMap<String, serde::json::Value>`
|
||||
//! * `Null`
|
||||
//!
|
||||
//! An object is a series of string keys mapping to values, in `"key": value` format. Arrays are
|
||||
//! enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }). A simple JSON
|
||||
//! document encoding a person, his/her age, address and phone numbers could look like
|
||||
//!
|
||||
//! ```ignore
|
||||
//! {
|
||||
//! "FirstName": "John",
|
||||
//! "LastName": "Doe",
|
||||
//! "Age": 43,
|
||||
//! "Address": {
|
||||
//! "Street": "Downing Street 10",
|
||||
//! "City": "London",
|
||||
//! "Country": "Great Britain"
|
||||
//! },
|
||||
//! "PhoneNumbers": [
|
||||
//! "+44 1234567",
|
||||
//! "+44 2345678"
|
||||
//! ]
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Type-based Serialization and Deserialization
|
||||
//!
|
||||
//! Serde provides a mechanism for low boilerplate serialization & deserialization of values to and
|
||||
//! from JSON via the serialization API. To be able to serialize a piece of data, it must implement
|
||||
//! the `serde::Serialize` trait. To be able to deserialize a piece of data, it must implement the
|
||||
//! `serde::Deserialize` trait. Serde provides provides an annotation to automatically generate
|
||||
//! the code for these traits: `#[derive(Serialize, Deserialize)]`.
|
||||
//!
|
||||
//! The JSON API also provides an enum `serde::json::Value` and a method `to_value` to serialize
|
||||
//! objects. A `serde::json::Value` value can be serialized as a string or buffer using the
|
||||
//! functions described above. You can also use the `json::Serializer` object, which implements the
|
||||
//! `Serializer` trait.
|
||||
//!
|
||||
//! # Examples of use
|
||||
//!
|
||||
//! ## Parsing a `str` to `Value` and reading the result
|
||||
//!
|
||||
//! ```rust
|
||||
//! //#![feature(custom_derive, plugin)]
|
||||
//! //#![plugin(serde_macros)]
|
||||
//!
|
||||
//! extern crate serde;
|
||||
//!
|
||||
//! use serde::json::{self, Value};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let data: Value = json::from_str("{\"foo\": 13, \"bar\": \"baz\"}").unwrap();
|
||||
//! println!("data: {:?}", data);
|
||||
//! // data: {"bar":"baz","foo":13}
|
||||
//! println!("object? {}", data.is_object());
|
||||
//! // object? true
|
||||
//!
|
||||
//! let obj = data.as_object().unwrap();
|
||||
//! let foo = obj.get("foo").unwrap();
|
||||
//!
|
||||
//! println!("array? {:?}", foo.as_array());
|
||||
//! // array? None
|
||||
//! println!("u64? {:?}", foo.as_u64());
|
||||
//! // u64? Some(13u64)
|
||||
//!
|
||||
//! for (key, value) in obj.iter() {
|
||||
//! println!("{}: {}", key, match *value {
|
||||
//! Value::U64(v) => format!("{} (u64)", v),
|
||||
//! Value::String(ref v) => format!("{} (string)", v),
|
||||
//! _ => format!("other")
|
||||
//! });
|
||||
//! }
|
||||
//! // bar: baz (string)
|
||||
//! // foo: 13 (u64)
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
pub use self::de::{Deserializer, from_str};
|
||||
pub use self::error::{Error, ErrorCode};
|
||||
pub use self::ser::{
|
||||
Serializer,
|
||||
to_writer,
|
||||
to_writer_pretty,
|
||||
to_vec,
|
||||
to_vec_pretty,
|
||||
to_string,
|
||||
to_string_pretty,
|
||||
escape_str,
|
||||
};
|
||||
pub use self::value::{Value, to_value, from_value};
|
||||
|
||||
pub mod builder;
|
||||
pub mod de;
|
||||
pub mod error;
|
||||
pub mod ser;
|
||||
pub mod value;
|
||||
-519
@@ -1,519 +0,0 @@
|
||||
use std::io;
|
||||
use std::num::FpCategory;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use ser;
|
||||
|
||||
/// A structure for implementing serialization to JSON.
|
||||
pub struct Serializer<W, F=CompactFormatter> {
|
||||
writer: W,
|
||||
formatter: F,
|
||||
|
||||
/// `first` is used to signify if we should print a comma when we are walking through a
|
||||
/// sequence.
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<W> Serializer<W>
|
||||
where W: io::Write,
|
||||
{
|
||||
/// Creates a new JSON serializer.
|
||||
#[inline]
|
||||
pub fn new(writer: W) -> Self {
|
||||
Serializer::with_formatter(writer, CompactFormatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W> Serializer<W, PrettyFormatter<'a>>
|
||||
where W: io::Write,
|
||||
{
|
||||
/// Creates a new JSON pretty print serializer.
|
||||
#[inline]
|
||||
pub fn pretty(writer: W) -> Self {
|
||||
Serializer::with_formatter(writer, PrettyFormatter::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, F> Serializer<W, F>
|
||||
where W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
/// Creates a new JSON visitor whose output will be written to the writer
|
||||
/// specified.
|
||||
#[inline]
|
||||
pub fn with_formatter(writer: W, formatter: F) -> Self {
|
||||
Serializer {
|
||||
writer: writer,
|
||||
formatter: formatter,
|
||||
first: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the `Writer` from the `Serializer`.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, F> ser::Serializer for Serializer<W, F>
|
||||
where W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Error = io::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> io::Result<()> {
|
||||
if value {
|
||||
self.writer.write_all(b"true")
|
||||
} else {
|
||||
self.writer.write_all(b"false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_isize(&mut self, value: isize) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, value: i8) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, value: i16) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, value: i32) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_usize(&mut self, value: usize) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, value: u8) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, value: u16) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, value: u32) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> io::Result<()> {
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, value: f32) -> io::Result<()> {
|
||||
fmt_f32_or_null(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> io::Result<()> {
|
||||
fmt_f64_or_null(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, value: char) -> io::Result<()> {
|
||||
escape_char(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> io::Result<()> {
|
||||
escape_str(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none(&mut self) -> io::Result<()> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<V>(&mut self, value: V) -> io::Result<()>
|
||||
where V: ser::Serialize
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit(&mut self) -> io::Result<()> {
|
||||
self.writer.write_all(b"null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_unit(&mut self, _name: &str, variant: &str) -> io::Result<()> {
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.writer.write_all(b"[]"));
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> io::Result<()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
match visitor.len() {
|
||||
Some(len) if len == 0 => {
|
||||
self.writer.write_all(b"[]")
|
||||
}
|
||||
_ => {
|
||||
try!(self.formatter.open(&mut self.writer, b'['));
|
||||
|
||||
self.first = true;
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
self.formatter.close(&mut self.writer, b']')
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_seq<V>(&mut self, _name: &str, variant: &str, visitor: V) -> io::Result<()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.visit_seq(visitor));
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> io::Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(self.formatter.comma(&mut self.writer, self.first));
|
||||
self.first = false;
|
||||
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> io::Result<()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
match visitor.len() {
|
||||
Some(len) if len == 0 => {
|
||||
self.writer.write_all(b"{}")
|
||||
}
|
||||
_ => {
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
|
||||
self.first = true;
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_map<V>(&mut self, _name: &str, variant: &str, visitor: V) -> io::Result<()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.visit_map(visitor));
|
||||
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> io::Result<()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
try!(self.formatter.comma(&mut self.writer, self.first));
|
||||
self.first = false;
|
||||
|
||||
try!(key.serialize(self));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
value.serialize(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Formatter {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> io::Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write;
|
||||
}
|
||||
|
||||
pub struct CompactFormatter;
|
||||
|
||||
impl Formatter for CompactFormatter {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(&[ch])
|
||||
}
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
if first {
|
||||
Ok(())
|
||||
} else {
|
||||
writer.write_all(b",")
|
||||
}
|
||||
}
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(b":")
|
||||
}
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(&[ch])
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrettyFormatter<'a> {
|
||||
current_indent: usize,
|
||||
indent: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> PrettyFormatter<'a> {
|
||||
fn new() -> Self {
|
||||
PrettyFormatter::with_indent(b" ")
|
||||
}
|
||||
|
||||
fn with_indent(indent: &'a [u8]) -> Self {
|
||||
PrettyFormatter {
|
||||
current_indent: 0,
|
||||
indent: indent,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Formatter for PrettyFormatter<'a> {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
self.current_indent += 1;
|
||||
writer.write_all(&[ch])
|
||||
}
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
if first {
|
||||
try!(writer.write_all(b"\n"));
|
||||
} else {
|
||||
try!(writer.write_all(b",\n"));
|
||||
}
|
||||
|
||||
indent(writer, self.current_indent, self.indent)
|
||||
}
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(b": ")
|
||||
}
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
self.current_indent -= 1;
|
||||
try!(writer.write(b"\n"));
|
||||
try!(indent(writer, self.current_indent, self.indent));
|
||||
|
||||
writer.write_all(&[ch])
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn escape_bytes<W>(wr: &mut W, bytes: &[u8]) -> io::Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
try!(wr.write_all(b"\""));
|
||||
|
||||
let mut start = 0;
|
||||
|
||||
for (i, byte) in bytes.iter().enumerate() {
|
||||
let escaped = match *byte {
|
||||
b'"' => b"\\\"",
|
||||
b'\\' => b"\\\\",
|
||||
b'\x08' => b"\\b",
|
||||
b'\x0c' => b"\\f",
|
||||
b'\n' => b"\\n",
|
||||
b'\r' => b"\\r",
|
||||
b'\t' => b"\\t",
|
||||
_ => { continue; }
|
||||
};
|
||||
|
||||
if start < i {
|
||||
try!(wr.write_all(&bytes[start..i]));
|
||||
}
|
||||
|
||||
try!(wr.write_all(escaped));
|
||||
|
||||
start = i + 1;
|
||||
}
|
||||
|
||||
if start != bytes.len() {
|
||||
try!(wr.write_all(&bytes[start..]));
|
||||
}
|
||||
|
||||
try!(wr.write_all(b"\""));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn escape_str<W>(wr: &mut W, value: &str) -> io::Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
escape_bytes(wr, value.as_bytes())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn escape_char<W>(wr: &mut W, value: char) -> io::Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
// FIXME: this allocation is required in order to be compatible with stable
|
||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||
escape_bytes(wr, value.to_string().as_bytes())
|
||||
}
|
||||
|
||||
fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> io::Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
match value.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_all(b"null"),
|
||||
_ => {
|
||||
let s = format!("{:?}", value);
|
||||
try!(wr.write_all(s.as_bytes()));
|
||||
if !s.contains('.') {
|
||||
try!(wr.write_all(b".0"))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_f64_or_null<W>(wr: &mut W, value: f64) -> io::Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
match value.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_all(b"null"),
|
||||
_ => {
|
||||
let s = format!("{:?}", value);
|
||||
try!(wr.write_all(s.as_bytes()));
|
||||
if !s.contains('.') {
|
||||
try!(wr.write_all(b".0"))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_writer<W, T>(writer: &mut W, value: &T) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(writer);
|
||||
try!(value.serialize(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_writer_pretty<W, T>(writer: &mut W, value: &T) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::pretty(writer);
|
||||
try!(value.serialize(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
#[inline]
|
||||
pub fn to_vec<T>(value: &T) -> Vec<u8>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
let mut writer = Vec::with_capacity(128);
|
||||
to_writer(&mut writer, value).unwrap();
|
||||
writer
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
#[inline]
|
||||
pub fn to_vec_pretty<T>(value: &T) -> Vec<u8>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
let mut writer = Vec::with_capacity(128);
|
||||
to_writer_pretty(&mut writer, value).unwrap();
|
||||
writer
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
#[inline]
|
||||
pub fn to_string<T>(value: &T) -> Result<String, FromUtf8Error>
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let vec = to_vec(value);
|
||||
String::from_utf8(vec)
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
#[inline]
|
||||
pub fn to_string_pretty<T>(value: &T) -> Result<String, FromUtf8Error>
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let vec = to_vec_pretty(value);
|
||||
String::from_utf8(vec)
|
||||
}
|
||||
|
||||
fn indent<W>(wr: &mut W, n: usize, s: &[u8]) -> io::Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
for _ in 0 .. n {
|
||||
try!(wr.write_all(s));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,883 +0,0 @@
|
||||
use std::collections::{BTreeMap, btree_map};
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::str;
|
||||
use std::vec;
|
||||
|
||||
use num::NumCast;
|
||||
|
||||
use de;
|
||||
use ser;
|
||||
use super::error::Error;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Null,
|
||||
Bool(bool),
|
||||
I64(i64),
|
||||
U64(u64),
|
||||
F64(f64),
|
||||
String(String),
|
||||
Array(Vec<Value>),
|
||||
Object(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// If the `Value` is an Object, returns the value associated with the provided key.
|
||||
/// Otherwise, returns None.
|
||||
pub fn find<'a>(&'a self, key: &str) -> Option<&'a Value>{
|
||||
match self {
|
||||
&Value::Object(ref map) => map.get(key),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to get a nested Value Object for each key in `keys`.
|
||||
/// If any key is found not to exist, find_path will return None.
|
||||
/// Otherwise, it will return the `Value` associated with the final key.
|
||||
pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Value>{
|
||||
let mut target = self;
|
||||
for key in keys.iter() {
|
||||
match target.find(*key) {
|
||||
Some(t) => { target = t; },
|
||||
None => return None
|
||||
}
|
||||
}
|
||||
Some(target)
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, performs a depth-first search until
|
||||
/// a value associated with the provided key is found. If no value is found
|
||||
/// or the `Value` is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Value> {
|
||||
match self {
|
||||
&Value::Object(ref map) => {
|
||||
match map.get(key) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
for (_, v) in map.iter() {
|
||||
match v.search(key) {
|
||||
x if x.is_some() => return x,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is an Object. Returns false otherwise.
|
||||
pub fn is_object<'a>(&'a self) -> bool {
|
||||
self.as_object().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, returns the associated BTreeMap.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_object<'a>(&'a self) -> Option<&'a BTreeMap<String, Value>> {
|
||||
match self {
|
||||
&Value::Object(ref map) => Some(map),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, returns the associated mutable BTreeMap.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_object_mut<'a>(&'a mut self) -> Option<&'a mut BTreeMap<String, Value>> {
|
||||
match self {
|
||||
&mut Value::Object(ref mut map) => Some(map),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is an Array. Returns false otherwise.
|
||||
pub fn is_array<'a>(&'a self) -> bool {
|
||||
self.as_array().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is an Array, returns the associated vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array<'a>(&'a self) -> Option<&'a Vec<Value>> {
|
||||
match self {
|
||||
&Value::Array(ref array) => Some(&*array),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is an Array, returns the associated mutable vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array_mut<'a>(&'a mut self) -> Option<&'a mut Vec<Value>> {
|
||||
match self {
|
||||
&mut Value::Array(ref mut list) => Some(list),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a String. Returns false otherwise.
|
||||
pub fn is_string<'a>(&'a self) -> bool {
|
||||
self.as_string().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a String, returns the associated str.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_string<'a>(&'a self) -> Option<&'a str> {
|
||||
match *self {
|
||||
Value::String(ref s) => Some(&s),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Number. Returns false otherwise.
|
||||
pub fn is_number(&self) -> bool {
|
||||
match *self {
|
||||
Value::I64(_) | Value::U64(_) | Value::F64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a i64. Returns false otherwise.
|
||||
pub fn is_i64(&self) -> bool {
|
||||
match *self {
|
||||
Value::I64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a u64. Returns false otherwise.
|
||||
pub fn is_u64(&self) -> bool {
|
||||
match *self {
|
||||
Value::U64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a f64. Returns false otherwise.
|
||||
pub fn is_f64(&self) -> bool {
|
||||
match *self {
|
||||
Value::F64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a i64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_i64(&self) -> Option<i64> {
|
||||
match *self {
|
||||
Value::I64(n) => Some(n),
|
||||
Value::U64(n) => NumCast::from(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a u64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_u64(&self) -> Option<u64> {
|
||||
match *self {
|
||||
Value::I64(n) => NumCast::from(n),
|
||||
Value::U64(n) => Some(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a f64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_f64(&self) -> Option<f64> {
|
||||
match *self {
|
||||
Value::I64(n) => NumCast::from(n),
|
||||
Value::U64(n) => NumCast::from(n),
|
||||
Value::F64(n) => Some(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Boolean. Returns false otherwise.
|
||||
pub fn is_boolean(&self) -> bool {
|
||||
self.as_boolean().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a Boolean, returns the associated bool.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_boolean(&self) -> Option<bool> {
|
||||
match self {
|
||||
&Value::Bool(b) => Some(b),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Null. Returns false otherwise.
|
||||
pub fn is_null(&self) -> bool {
|
||||
self.as_null().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a Null, returns ().
|
||||
/// Returns None otherwise.
|
||||
pub fn as_null(&self) -> Option<()> {
|
||||
match self {
|
||||
&Value::Null => Some(()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Serialize for Value {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
match *self {
|
||||
Value::Null => serializer.visit_unit(),
|
||||
Value::Bool(v) => serializer.visit_bool(v),
|
||||
Value::I64(v) => serializer.visit_i64(v),
|
||||
Value::U64(v) => serializer.visit_u64(v),
|
||||
Value::F64(v) => serializer.visit_f64(v),
|
||||
Value::String(ref v) => serializer.visit_str(&v),
|
||||
Value::Array(ref v) => v.serialize(serializer),
|
||||
Value::Object(ref v) => v.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserialize for Value {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Value, D::Error>
|
||||
where D: de::Deserializer,
|
||||
{
|
||||
struct ValueVisitor;
|
||||
|
||||
impl de::Visitor for ValueVisitor {
|
||||
type Value = Value;
|
||||
|
||||
#[inline]
|
||||
fn visit_bool<E>(&mut self, value: bool) -> Result<Value, E> {
|
||||
Ok(Value::Bool(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64<E>(&mut self, value: i64) -> Result<Value, E> {
|
||||
if value < 0 {
|
||||
Ok(Value::I64(value))
|
||||
} else {
|
||||
Ok(Value::U64(value as u64))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64<E>(&mut self, value: u64) -> Result<Value, E> {
|
||||
Ok(Value::U64(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64<E>(&mut self, value: f64) -> Result<Value, E> {
|
||||
Ok(Value::F64(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Value, E>
|
||||
where E: de::Error,
|
||||
{
|
||||
self.visit_string(value.to_string())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Value, E> {
|
||||
Ok(Value::String(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(&mut self) -> Result<Value, E> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Value, D::Error>
|
||||
where D: de::Deserializer,
|
||||
{
|
||||
de::Deserialize::deserialize(deserializer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<Value, E> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
||||
where V: de::SeqVisitor,
|
||||
{
|
||||
let values = try!(de::impls::VecVisitor::new().visit_seq(visitor));
|
||||
Ok(Value::Array(values))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
||||
where V: de::MapVisitor,
|
||||
{
|
||||
let values = try!(de::impls::BTreeMapVisitor::new().visit_map(visitor));
|
||||
Ok(Value::Object(values))
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(ValueVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct WriterFormatter<'a, 'b: 'a> {
|
||||
inner: &'a mut fmt::Formatter<'b>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match self.inner.write_str(str::from_utf8(buf).unwrap()) {
|
||||
Ok(_) => Ok(buf.len()),
|
||||
Err(_) => Err(io::Error::last_os_error()),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value {
|
||||
/// Serializes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut wr = WriterFormatter { inner: f };
|
||||
super::ser::to_writer(&mut wr, self).map_err(|_| fmt::Error)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Value(Value),
|
||||
Array(Vec<Value>),
|
||||
Object(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
pub struct Serializer {
|
||||
state: Vec<State>,
|
||||
}
|
||||
|
||||
impl Serializer {
|
||||
pub fn new() -> Serializer {
|
||||
Serializer {
|
||||
state: Vec::with_capacity(4),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(mut self) -> Value {
|
||||
match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Serializer for Serializer {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::Bool(value)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> Result<(), ()> {
|
||||
if value < 0 {
|
||||
self.state.push(State::Value(Value::I64(value)));
|
||||
} else {
|
||||
self.state.push(State::Value(Value::U64(value as u64)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::U64(value)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::F64(value as f64)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, value: char) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::String(value.to_string())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::String(value.to_string())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none(&mut self) -> Result<(), ()> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), ()>
|
||||
where V: ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit(&mut self) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::Null));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_unit(&mut self, _name: &str, variant: &str) -> Result<(), ()> {
|
||||
let mut values = BTreeMap::new();
|
||||
values.insert(variant.to_string(), Value::Array(vec![]));
|
||||
|
||||
self.state.push(State::Value(Value::Object(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
let len = visitor.len().unwrap_or(0);
|
||||
let values = Vec::with_capacity(len);
|
||||
|
||||
self.state.push(State::Array(values));
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
let values = match self.state.pop().unwrap() {
|
||||
State::Array(values) => values,
|
||||
state => panic!("Expected array, found {:?}", state),
|
||||
};
|
||||
|
||||
self.state.push(State::Value(Value::Array(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_seq<V>(&mut self, _name: &str, variant: &str, visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
try!(self.visit_seq(visitor));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
let mut object = BTreeMap::new();
|
||||
|
||||
object.insert(variant.to_string(), value);
|
||||
|
||||
self.state.push(State::Value(Value::Object(object)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(value.serialize(self));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
match *self.state.last_mut().unwrap() {
|
||||
State::Array(ref mut values) => { values.push(value); }
|
||||
ref state => panic!("expected array, found {:?}", state),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
let values = BTreeMap::new();
|
||||
|
||||
self.state.push(State::Object(values));
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
let values = match self.state.pop().unwrap() {
|
||||
State::Object(values) => values,
|
||||
state => panic!("expected object, found {:?}", state),
|
||||
};
|
||||
|
||||
self.state.push(State::Value(Value::Object(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_map<V>(&mut self, _name: &str, variant: &str, visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
try!(self.visit_map(visitor));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
let mut object = BTreeMap::new();
|
||||
|
||||
object.insert(variant.to_string(), value);
|
||||
|
||||
self.state.push(State::Value(Value::Object(object)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), ()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
try!(key.serialize(self));
|
||||
|
||||
let key = match self.state.pop().unwrap() {
|
||||
State::Value(Value::String(value)) => value,
|
||||
state => panic!("expected key, found {:?}", state),
|
||||
};
|
||||
|
||||
try!(value.serialize(self));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
match *self.state.last_mut().unwrap() {
|
||||
State::Object(ref mut values) => { values.insert(key, value); }
|
||||
ref state => panic!("expected object, found {:?}", state),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Deserializer {
|
||||
value: Option<Value>,
|
||||
}
|
||||
|
||||
impl Deserializer {
|
||||
/// Creates a new deserializer instance for deserializing the specified JSON value.
|
||||
pub fn new(value: Value) -> Deserializer {
|
||||
Deserializer {
|
||||
value: Some(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializer for Deserializer {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
let value = match self.value.take() {
|
||||
Some(value) => value,
|
||||
None => { return Err(de::Error::end_of_stream_error()); }
|
||||
};
|
||||
|
||||
match value {
|
||||
Value::Null => visitor.visit_unit(),
|
||||
Value::Bool(v) => visitor.visit_bool(v),
|
||||
Value::I64(v) => visitor.visit_i64(v),
|
||||
Value::U64(v) => visitor.visit_u64(v),
|
||||
Value::F64(v) => visitor.visit_f64(v),
|
||||
Value::String(v) => visitor.visit_string(v),
|
||||
Value::Array(v) => {
|
||||
let len = v.len();
|
||||
visitor.visit_seq(SeqDeserializer {
|
||||
de: self,
|
||||
iter: v.into_iter(),
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
Value::Object(v) => {
|
||||
let len = v.len();
|
||||
visitor.visit_map(MapDeserializer {
|
||||
de: self,
|
||||
iter: v.into_iter(),
|
||||
value: None,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.value {
|
||||
Some(Value::Null) => visitor.visit_none(),
|
||||
Some(_) => visitor.visit_some(self),
|
||||
None => Err(de::Error::end_of_stream_error()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
let value = match self.value.take() {
|
||||
Some(Value::Object(value)) => value,
|
||||
Some(_) => { return Err(de::Error::syntax_error()); }
|
||||
None => { return Err(de::Error::end_of_stream_error()); }
|
||||
};
|
||||
|
||||
let mut iter = value.into_iter();
|
||||
|
||||
let value = match iter.next() {
|
||||
Some((variant, Value::Array(fields))) => {
|
||||
self.value = Some(Value::String(variant));
|
||||
|
||||
let len = fields.len();
|
||||
try!(visitor.visit(SeqDeserializer {
|
||||
de: self,
|
||||
iter: fields.into_iter(),
|
||||
len: len,
|
||||
}))
|
||||
}
|
||||
Some((variant, Value::Object(fields))) => {
|
||||
let len = fields.len();
|
||||
try!(visitor.visit(MapDeserializer {
|
||||
de: self,
|
||||
iter: fields.into_iter(),
|
||||
value: Some(Value::String(variant)),
|
||||
len: len,
|
||||
}))
|
||||
}
|
||||
Some(_) => { return Err(de::Error::syntax_error()); }
|
||||
None => { return Err(de::Error::syntax_error()); }
|
||||
};
|
||||
|
||||
match iter.next() {
|
||||
Some(_) => Err(de::Error::syntax_error()),
|
||||
None => Ok(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqDeserializer<'a> {
|
||||
de: &'a mut Deserializer,
|
||||
iter: vec::IntoIter<Value>,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::Deserializer for SeqDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
if self.len == 0 {
|
||||
visitor.visit_unit()
|
||||
} else {
|
||||
visitor.visit_seq(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::SeqVisitor for SeqDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
self.len -= 1;
|
||||
self.de.value = Some(value);
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::end_of_stream_error())
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::VariantVisitor for SeqDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error>
|
||||
{
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct MapDeserializer<'a> {
|
||||
de: &'a mut Deserializer,
|
||||
iter: btree_map::IntoIter<String, Value>,
|
||||
value: Option<Value>,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::MapVisitor for MapDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
self.len -= 1;
|
||||
self.value = Some(value);
|
||||
self.de.value = Some(Value::String(key));
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<T>(&mut self) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
let value = self.value.take().unwrap();
|
||||
self.de.value = Some(value);
|
||||
Ok(try!(de::Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::end_of_stream_error())
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
// See if the type can deserialize from a unit.
|
||||
struct UnitDeserializer;
|
||||
|
||||
impl de::Deserializer for UnitDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_none()
|
||||
}
|
||||
}
|
||||
|
||||
Ok(try!(de::Deserialize::deserialize(&mut UnitDeserializer)))
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::Deserializer for MapDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
println!("MapDeserializer!");
|
||||
visitor.visit_map(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::VariantVisitor for MapDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
self.de.value = self.value.take();
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortcut function to encode a `T` into a JSON `Value`
|
||||
pub fn to_value<T>(value: &T) -> Value
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let mut ser = Serializer::new();
|
||||
value.serialize(&mut ser).ok().unwrap();
|
||||
ser.unwrap()
|
||||
}
|
||||
|
||||
/// Shortcut function to decode a JSON `Value` into a `T`
|
||||
pub fn from_value<T>(value: Value) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
let mut de = Deserializer::new(value);
|
||||
de::Deserialize::deserialize(&mut de)
|
||||
}
|
||||
-204
@@ -1,204 +0,0 @@
|
||||
//! Generic serialization framework.
|
||||
|
||||
pub mod impls;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serialize {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializer {
|
||||
type Error;
|
||||
|
||||
/// `visit_bool` serializes a `bool` value.
|
||||
fn visit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_isize` serializes a `isize` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_isize(&mut self, v: isize) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i8` serializes a `i8` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, v: i8) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i16` serializes a `i16` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, v: i16) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i32` serializes a `i32` value. By default it casts the value to a `i64` and
|
||||
/// passes it to the `visit_i64` method.
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, v: i32) -> Result<(), Self::Error> {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
/// `visit_i64` serializes a `i64` value.
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_usize` serializes a `usize` value. By default it casts the value to a `u64` and
|
||||
/// passes it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_usize(&mut self, v: usize) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u8` serializes a `u8` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, v: u8) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u32` serializes a `u32` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, v: u16) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u32` serializes a `u32` value. By default it casts the value to a `u64` and passes
|
||||
/// it to the `visit_u64` method.
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, v: u32) -> Result<(), Self::Error> {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
/// `visit_u64` serializes a `u64` value.
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_f32` serializes a `f32` value. By default it casts the value to a `f64` and passes
|
||||
/// it to the `visit_f64` method.
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, v: f32) -> Result<(), Self::Error> {
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
/// `visit_f64` serializes a `f64` value.
|
||||
fn visit_f64(&mut self, v: f64) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_char` serializes a character. By default it serializes it as a `&str` containing a
|
||||
/// single character.
|
||||
#[inline]
|
||||
fn visit_char(&mut self, v: char) -> Result<(), Self::Error> {
|
||||
// FIXME: this allocation is required in order to be compatible with stable rust, which
|
||||
// doesn't support encoding a `char` into a stack buffer.
|
||||
self.visit_str(&v.to_string())
|
||||
}
|
||||
|
||||
/// `visit_str` serializes a `&str`.
|
||||
fn visit_str(&mut self, value: &str) -> Result<(), Self::Error>;
|
||||
|
||||
/// `visit_bytes` is a hook that enables those serialization formats that support serializing
|
||||
/// byte slices separately from generic arrays. By default it serializes as a regular array.
|
||||
#[inline]
|
||||
fn visit_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
|
||||
self.visit_seq(impls::SeqIteratorVisitor::new(value.iter(), Some(value.len())))
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn visit_named_unit(&mut self, _name: &str) -> Result<(), Self::Error> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_unit(&mut self,
|
||||
_name: &str,
|
||||
_variant: &str) -> Result<(), Self::Error> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
fn visit_none(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), Self::Error>
|
||||
where V: Serialize;
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor;
|
||||
|
||||
#[inline]
|
||||
fn visit_named_seq<V>(&mut self,
|
||||
_name: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_seq<V>(&mut self,
|
||||
_name: &'static str,
|
||||
_variant: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: SeqVisitor,
|
||||
{
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize;
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor;
|
||||
|
||||
#[inline]
|
||||
fn visit_named_map<V>(&mut self,
|
||||
_name: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum_map<V>(&mut self,
|
||||
_name: &'static str,
|
||||
_variant: &'static str,
|
||||
visitor: V) -> Result<(), Self::Error>
|
||||
where V: MapVisitor,
|
||||
{
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize;
|
||||
}
|
||||
|
||||
pub trait SeqVisitor {
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
/// Return the length of the sequence if known.
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub trait MapVisitor {
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
/// Return the length of the map if known.
|
||||
#[inline]
|
||||
fn len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
#![feature(custom_attribute, custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use serde::json;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Default {
|
||||
a1: i32,
|
||||
#[serde(default)]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Rename {
|
||||
a1: i32,
|
||||
#[serde(rename="a3")]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DirectionRename {
|
||||
a1: i32,
|
||||
#[serde(rename_serialize="a3", rename_deserialize="a4")]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default() {
|
||||
let deserialized_value: Default = json::from_str(&"{\"a1\":1,\"a2\":2}").unwrap();
|
||||
assert_eq!(deserialized_value, Default { a1: 1, a2: 2 });
|
||||
|
||||
let deserialized_value: Default = json::from_str(&"{\"a1\":1}").unwrap();
|
||||
assert_eq!(deserialized_value, Default { a1: 1, a2: 0 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename() {
|
||||
let value = Rename { a1: 1, a2: 2 };
|
||||
let serialized_value = json::to_string(&value).unwrap();
|
||||
assert_eq!(serialized_value, "{\"a1\":1,\"a3\":2}");
|
||||
|
||||
let deserialized_value: Rename = json::from_str(&serialized_value).unwrap();
|
||||
assert_eq!(value, deserialized_value);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_direction_rename() {
|
||||
let value = DirectionRename { a1: 1, a2: 2 };
|
||||
let serialized_value = json::to_string(&value).unwrap();
|
||||
assert_eq!(serialized_value, "{\"a1\":1,\"a3\":2}");
|
||||
|
||||
let deserialized_value = json::from_str("{\"a1\":1,\"a4\":2}").unwrap();
|
||||
assert_eq!(value, deserialized_value);
|
||||
}
|
||||
@@ -1,865 +0,0 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::iter;
|
||||
use std::vec;
|
||||
|
||||
use serde::de::{self, Deserialize, Deserializer, Visitor};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Token {
|
||||
Bool(bool),
|
||||
Isize(isize),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Usize(usize),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'static str),
|
||||
String(String),
|
||||
|
||||
Option(bool),
|
||||
|
||||
Name(&'static str),
|
||||
|
||||
Unit,
|
||||
|
||||
SeqStart(usize),
|
||||
SeqSep,
|
||||
SeqEnd,
|
||||
|
||||
MapStart(usize),
|
||||
MapSep,
|
||||
MapEnd,
|
||||
|
||||
EnumStart(&'static str),
|
||||
EnumEnd,
|
||||
}
|
||||
|
||||
struct TokenDeserializer {
|
||||
tokens: iter::Peekable<vec::IntoIter<Token>>,
|
||||
}
|
||||
|
||||
impl<'a> TokenDeserializer {
|
||||
fn new(tokens: Vec<Token>) -> TokenDeserializer {
|
||||
TokenDeserializer {
|
||||
tokens: tokens.into_iter().peekable(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
enum Error {
|
||||
SyntaxError,
|
||||
EndOfStreamError,
|
||||
UnknownFieldError(String),
|
||||
MissingFieldError(&'static str),
|
||||
InvalidName(&'static str),
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax_error() -> Error { Error::SyntaxError }
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(field: &str) -> Error {
|
||||
Error::UnknownFieldError(field.to_string())
|
||||
}
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserializer for TokenDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: Visitor,
|
||||
{
|
||||
match self.tokens.next() {
|
||||
Some(Token::Bool(v)) => visitor.visit_bool(v),
|
||||
Some(Token::Isize(v)) => visitor.visit_isize(v),
|
||||
Some(Token::I8(v)) => visitor.visit_i8(v),
|
||||
Some(Token::I16(v)) => visitor.visit_i16(v),
|
||||
Some(Token::I32(v)) => visitor.visit_i32(v),
|
||||
Some(Token::I64(v)) => visitor.visit_i64(v),
|
||||
Some(Token::Usize(v)) => visitor.visit_usize(v),
|
||||
Some(Token::U8(v)) => visitor.visit_u8(v),
|
||||
Some(Token::U16(v)) => visitor.visit_u16(v),
|
||||
Some(Token::U32(v)) => visitor.visit_u32(v),
|
||||
Some(Token::U64(v)) => visitor.visit_u64(v),
|
||||
Some(Token::F32(v)) => visitor.visit_f32(v),
|
||||
Some(Token::F64(v)) => visitor.visit_f64(v),
|
||||
Some(Token::Char(v)) => visitor.visit_char(v),
|
||||
Some(Token::Str(v)) => visitor.visit_str(v),
|
||||
Some(Token::String(v)) => visitor.visit_string(v),
|
||||
Some(Token::Option(false)) => visitor.visit_none(),
|
||||
Some(Token::Option(true)) => visitor.visit_some(self),
|
||||
Some(Token::Unit) => visitor.visit_unit(),
|
||||
Some(Token::SeqStart(len)) => {
|
||||
visitor.visit_seq(TokenDeserializerSeqVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
Some(Token::MapStart(len)) => {
|
||||
visitor.visit_map(TokenDeserializerMapVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
Some(Token::Name(_)) => self.visit(visitor),
|
||||
Some(_) => Err(Error::SyntaxError),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
||||
/// `None`, or a regular value as `Some(value)`.
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: 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),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_enum<V>(&mut self, name: &str, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
match self.tokens.next() {
|
||||
Some(Token::EnumStart(n)) => {
|
||||
if name == n {
|
||||
visitor.visit(TokenDeserializerVariantVisitor {
|
||||
de: self,
|
||||
})
|
||||
} else {
|
||||
Err(Error::SyntaxError)
|
||||
}
|
||||
}
|
||||
Some(_) => Err(Error::SyntaxError),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_named_unit<V>(&mut self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::Name(n)) => {
|
||||
if name == n {
|
||||
self.tokens.next();
|
||||
self.visit_seq(visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(_) => self.visit(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_named_seq<V>(&mut self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::Name(n)) => {
|
||||
if name == n {
|
||||
self.tokens.next();
|
||||
self.visit_seq(visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(_) => self.visit_seq(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_named_map<V>(&mut self, name: &str, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.tokens.peek() {
|
||||
Some(&Token::Name(n)) => {
|
||||
if name == n {
|
||||
self.tokens.next();
|
||||
self.visit_map(visitor)
|
||||
} else {
|
||||
Err(Error::InvalidName(n))
|
||||
}
|
||||
}
|
||||
Some(_) => self.visit_map(visitor),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct TokenDeserializerSeqVisitor<'a> {
|
||||
de: &'a mut TokenDeserializer,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::SeqVisitor for TokenDeserializerSeqVisitor<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::SeqSep) => {
|
||||
self.len -= 1;
|
||||
self.de.tokens.next();
|
||||
Ok(Some(try!(Deserialize::deserialize(self.de))))
|
||||
}
|
||||
Some(&Token::SeqEnd) => Ok(None),
|
||||
Some(_) => {
|
||||
Err(Error::SyntaxError)
|
||||
}
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.len, 0);
|
||||
match self.de.tokens.next() {
|
||||
Some(Token::SeqEnd) => Ok(()),
|
||||
Some(_) => Err(Error::SyntaxError),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct TokenDeserializerMapVisitor<'a> {
|
||||
de: &'a mut TokenDeserializer,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::MapVisitor for TokenDeserializerMapVisitor<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>, Error>
|
||||
where K: Deserialize,
|
||||
{
|
||||
match self.de.tokens.peek() {
|
||||
Some(&Token::MapSep) => {
|
||||
self.de.tokens.next();
|
||||
self.len -= 1;
|
||||
Ok(Some(try!(Deserialize::deserialize(self.de))))
|
||||
}
|
||||
Some(&Token::MapEnd) => Ok(None),
|
||||
Some(_) => Err(Error::SyntaxError),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V, Error>
|
||||
where V: Deserialize,
|
||||
{
|
||||
Ok(try!(Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.len, 0);
|
||||
match self.de.tokens.next() {
|
||||
Some(Token::MapEnd) => Ok(()),
|
||||
Some(_) => Err(Error::SyntaxError),
|
||||
None => Err(Error::EndOfStreamError),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct TokenDeserializerVariantVisitor<'a> {
|
||||
de: &'a mut TokenDeserializer,
|
||||
}
|
||||
|
||||
impl<'a> de::VariantVisitor for TokenDeserializerVariantVisitor<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||
de::Deserialize::deserialize(self.de)
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self.de, visitor)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(self.de, visitor)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
|
||||
struct NamedUnit;
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
struct NamedSeq(i32, i32, i32);
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
struct NamedMap {
|
||||
a: i32,
|
||||
b: i32,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Deserialize)]
|
||||
enum Enum {
|
||||
Unit,
|
||||
Seq(i32, i32, i32),
|
||||
Map { a: i32, b: i32, c: i32 }
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! btreeset {
|
||||
() => {
|
||||
BTreeSet::new()
|
||||
};
|
||||
($($value:expr),+) => {
|
||||
{
|
||||
let mut set = BTreeSet::new();
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! btreemap {
|
||||
() => {
|
||||
BTreeMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = BTreeMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! hashset {
|
||||
() => {
|
||||
HashSet::new()
|
||||
};
|
||||
($($value:expr),+) => {
|
||||
{
|
||||
let mut set = HashSet::new();
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! hashmap {
|
||||
() => {
|
||||
HashMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = HashMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_test {
|
||||
($name:ident { $($value:expr => $tokens:expr,)+ }) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
let mut de = TokenDeserializer::new($tokens);
|
||||
let value: Result<_, Error> = Deserialize::deserialize(&mut de);
|
||||
assert_eq!(value, Ok($value));
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_tests {
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
declare_test!($name { $($value => $tokens,)+ });
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
declare_tests! {
|
||||
test_bool {
|
||||
true => vec![Token::Bool(true)],
|
||||
false => vec![Token::Bool(false)],
|
||||
}
|
||||
test_isize {
|
||||
0isize => vec![Token::Isize(0)],
|
||||
0isize => vec![Token::I8(0)],
|
||||
0isize => vec![Token::I16(0)],
|
||||
0isize => vec![Token::I32(0)],
|
||||
0isize => vec![Token::I64(0)],
|
||||
0isize => vec![Token::Usize(0)],
|
||||
0isize => vec![Token::U8(0)],
|
||||
0isize => vec![Token::U16(0)],
|
||||
0isize => vec![Token::U32(0)],
|
||||
0isize => vec![Token::U64(0)],
|
||||
0isize => vec![Token::F32(0.)],
|
||||
0isize => vec![Token::F64(0.)],
|
||||
}
|
||||
test_ints {
|
||||
0isize => vec![Token::Isize(0)],
|
||||
0i8 => vec![Token::I8(0)],
|
||||
0i16 => vec![Token::I16(0)],
|
||||
0i32 => vec![Token::I32(0)],
|
||||
0i64 => vec![Token::I64(0)],
|
||||
}
|
||||
test_uints {
|
||||
0usize => vec![Token::Usize(0)],
|
||||
0u8 => vec![Token::U8(0)],
|
||||
0u16 => vec![Token::U16(0)],
|
||||
0u32 => vec![Token::U32(0)],
|
||||
0u64 => vec![Token::U64(0)],
|
||||
}
|
||||
test_floats {
|
||||
0f32 => vec![Token::F32(0.)],
|
||||
0f64 => vec![Token::F64(0.)],
|
||||
}
|
||||
test_char {
|
||||
'a' => vec![Token::Char('a')],
|
||||
'a' => vec![Token::Str("a")],
|
||||
'a' => vec![Token::String("a".to_string())],
|
||||
}
|
||||
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')],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => vec![Token::Unit],
|
||||
None::<i32> => vec![Token::Option(false)],
|
||||
Some(1) => vec![Token::I32(1)],
|
||||
Some(1) => vec![
|
||||
Token::Option(true),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_unit {
|
||||
() => vec![Token::Unit],
|
||||
() => vec![
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_named_unit {
|
||||
NamedUnit => vec![Token::Unit],
|
||||
NamedUnit => vec![
|
||||
Token::Name("NamedUnit"),
|
||||
Token::Unit,
|
||||
],
|
||||
NamedUnit => vec![
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_named_seq {
|
||||
NamedSeq(1, 2, 3) => vec![
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
NamedSeq(1, 2, 3) => vec![
|
||||
Token::Name("NamedSeq"),
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreeset {
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => vec![
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(1),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(2),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeSet::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_hashset {
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
hashset![1, 2, 3] => vec![
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::Unit,
|
||||
],
|
||||
HashSet::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
vec![vec![], vec![1], vec![2, 3]] => vec![
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(1),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(2),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::Unit,
|
||||
],
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple {
|
||||
(1,) => vec![
|
||||
Token::SeqStart(1),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
(1, 2, 3) => vec![
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreemap {
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2] => vec![
|
||||
Token::MapStart(1),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2, 3 => 4] => vec![
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => vec![
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::Unit,
|
||||
],
|
||||
BTreeMap::<isize, isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_hashmap {
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::Unit,
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => 2] => vec![
|
||||
Token::MapStart(1),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => 2, 3 => 4] => vec![
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => vec![
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(2),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::Unit,
|
||||
],
|
||||
HashMap::<isize, isize>::new() => vec![
|
||||
Token::Name("Anything"),
|
||||
Token::MapStart(0),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_named_map {
|
||||
NamedMap { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::MapStart(3),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
NamedMap { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::Name("NamedMap"),
|
||||
Token::MapStart(3),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_enum_unit {
|
||||
Enum::Unit => vec![
|
||||
Token::EnumStart("Enum"),
|
||||
Token::Str("Unit"),
|
||||
Token::Unit,
|
||||
Token::EnumEnd,
|
||||
],
|
||||
}
|
||||
test_enum_seq {
|
||||
Enum::Seq(1, 2, 3) => vec![
|
||||
Token::EnumStart("Enum"),
|
||||
Token::Str("Seq"),
|
||||
Token::SeqStart(3),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::EnumEnd,
|
||||
],
|
||||
}
|
||||
test_enum_map {
|
||||
Enum::Map { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::EnumStart("Enum"),
|
||||
Token::Str("Map"),
|
||||
Token::MapStart(3),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
Token::EnumEnd,
|
||||
],
|
||||
}
|
||||
}
|
||||
-1044
File diff suppressed because it is too large
Load Diff
@@ -1,52 +0,0 @@
|
||||
extern crate serde;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::json::value::Value;
|
||||
use serde::json::builder::{ArrayBuilder, ObjectBuilder};
|
||||
|
||||
#[test]
|
||||
fn test_array_builder() {
|
||||
let value = ArrayBuilder::new().unwrap();
|
||||
assert_eq!(value, Value::Array(Vec::new()));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push(1)
|
||||
.push(2)
|
||||
.push(3)
|
||||
.unwrap();
|
||||
assert_eq!(value, Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push_array(|bld| bld.push(1).push(2).push(3))
|
||||
.unwrap();
|
||||
assert_eq!(value, Value::Array(vec!(Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))))));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push_object(|bld|
|
||||
bld
|
||||
.insert("a".to_string(), 1)
|
||||
.insert("b".to_string(), 2))
|
||||
.unwrap();
|
||||
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("a".to_string(), Value::U64(1));
|
||||
map.insert("b".to_string(), Value::U64(2));
|
||||
assert_eq!(value, Value::Array(vec!(Value::Object(map))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_object_builder() {
|
||||
let value = ObjectBuilder::new().unwrap();
|
||||
assert_eq!(value, Value::Object(BTreeMap::new()));
|
||||
|
||||
let value = ObjectBuilder::new()
|
||||
.insert("a".to_string(), 1)
|
||||
.insert("b".to_string(), 2)
|
||||
.unwrap();
|
||||
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("a".to_string(), Value::U64(1));
|
||||
map.insert("b".to_string(), Value::U64(2));
|
||||
assert_eq!(value, Value::Object(map));
|
||||
}
|
||||
@@ -1,445 +0,0 @@
|
||||
#![feature(custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use serde::json::{self, Value};
|
||||
|
||||
macro_rules! btreemap {
|
||||
() => {
|
||||
BTreeMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = BTreeMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
trait Trait {
|
||||
type Type;
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct NamedUnit;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerNamedTuple<'a, 'b, A: 'a, B: 'b, C>(&'a A, &'b mut B, C);
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeNamedTuple<A, B, C>(A, B, C);
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
struct SerNamedMap<'a, 'b, A: 'a, B: 'b, C> {
|
||||
a: &'a A,
|
||||
b: &'b mut B,
|
||||
c: C,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct DeNamedMap<A, B, C> {
|
||||
a: A,
|
||||
b: B,
|
||||
c: C,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
enum SerEnum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
//C::Type,
|
||||
&'a mut D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
//d: C::Type,
|
||||
e: &'a mut D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
&'a C,
|
||||
//C::Type,
|
||||
&'a mut D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: &'a C,
|
||||
//d: C::Type,
|
||||
e: &'a mut D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
enum DeEnum<B, C: /* Trait */, D> /* where D: Trait */ {
|
||||
Unit,
|
||||
Seq(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
//C::Type,
|
||||
D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
Map {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
//d: C::Type,
|
||||
e: D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
|
||||
// Make sure we can support more than one variant.
|
||||
_Unit2,
|
||||
_Seq2(
|
||||
i8,
|
||||
B,
|
||||
C,
|
||||
//C::Type,
|
||||
D,
|
||||
//<D as Trait>::Type,
|
||||
),
|
||||
_Map2 {
|
||||
a: i8,
|
||||
b: B,
|
||||
c: C,
|
||||
//d: C::Type,
|
||||
e: D,
|
||||
//f: <D as Trait>::Type,
|
||||
},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_named_unit() {
|
||||
let named_unit = NamedUnit;
|
||||
|
||||
assert_eq!(
|
||||
json::to_string(&named_unit).unwrap(),
|
||||
"null".to_string()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&named_unit),
|
||||
Value::Null
|
||||
);
|
||||
|
||||
let v: NamedUnit = json::from_str("null").unwrap();
|
||||
assert_eq!(v, named_unit);
|
||||
|
||||
let v: NamedUnit = json::from_value(Value::Null).unwrap();
|
||||
assert_eq!(v, named_unit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_named_tuple() {
|
||||
let a = 5;
|
||||
let mut b = 6;
|
||||
let c = 7;
|
||||
let named_tuple = SerNamedTuple(&a, &mut b, c);
|
||||
|
||||
assert_eq!(
|
||||
json::to_string(&named_tuple).unwrap(),
|
||||
"[5,6,7]"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&named_tuple),
|
||||
Value::Array(vec![Value::U64(5), Value::U64(6), Value::U64(7)])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_named_tuple() {
|
||||
let v: DeNamedTuple<i32, i32, i32> = json::from_str("[1,2,3]").unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeNamedTuple(1, 2, 3)
|
||||
);
|
||||
|
||||
let v: Value = json::from_str("[1,2,3]").unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
Value::Array(vec![
|
||||
Value::U64(1),
|
||||
Value::U64(2),
|
||||
Value::U64(3),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_named_map() {
|
||||
let a = 5;
|
||||
let mut b = 6;
|
||||
let c = 7;
|
||||
let named_map = SerNamedMap {
|
||||
a: &a,
|
||||
b: &mut b,
|
||||
c: c,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
json::to_string(&named_map).unwrap(),
|
||||
"{\"a\":5,\"b\":6,\"c\":7}"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&named_map),
|
||||
Value::Object(btreemap![
|
||||
"a".to_string() => Value::U64(5),
|
||||
"b".to_string() => Value::U64(6),
|
||||
"c".to_string() => Value::U64(7)
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_named_map() {
|
||||
let v = DeNamedMap {
|
||||
a: 5,
|
||||
b: 6,
|
||||
c: 7,
|
||||
};
|
||||
|
||||
let v2: DeNamedMap<i32, i32, i32> = json::from_str(
|
||||
"{\"a\":5,\"b\":6,\"c\":7}"
|
||||
).unwrap();
|
||||
assert_eq!(v, v2);
|
||||
|
||||
let v2 = json::from_value(Value::Object(btreemap![
|
||||
"a".to_string() => Value::U64(5),
|
||||
"b".to_string() => Value::U64(6),
|
||||
"c".to_string() => Value::U64(7)
|
||||
])).unwrap();
|
||||
assert_eq!(v, v2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_unit() {
|
||||
assert_eq!(
|
||||
json::to_string(&SerEnum::Unit::<u32, u32, u32>).unwrap(),
|
||||
"{\"Unit\":[]}"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&SerEnum::Unit::<u32, u32, u32>),
|
||||
Value::Object(btreemap!(
|
||||
"Unit".to_string() => Value::Array(vec![]))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_seq() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let mut e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_eq!(
|
||||
json::to_string(&SerEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
&c,
|
||||
//d,
|
||||
&mut e,
|
||||
//f,
|
||||
)).unwrap(),
|
||||
"{\"Seq\":[1,2,3,5]}".to_string()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&SerEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
&c,
|
||||
//d,
|
||||
&mut e,
|
||||
//e,
|
||||
)),
|
||||
Value::Object(btreemap!(
|
||||
"Seq".to_string() => Value::Array(vec![
|
||||
Value::U64(1),
|
||||
Value::U64(2),
|
||||
Value::U64(3),
|
||||
//Value::U64(4),
|
||||
Value::U64(5),
|
||||
//Value::U64(6),
|
||||
])
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ser_enum_map() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let mut e = 5;
|
||||
//let f = 6;
|
||||
|
||||
assert_eq!(
|
||||
json::to_string(&SerEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: &c,
|
||||
//d: d,
|
||||
e: &mut e,
|
||||
//f: f,
|
||||
}).unwrap(),
|
||||
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}".to_string()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
json::to_value(&SerEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: &c,
|
||||
//d: d,
|
||||
e: &mut e,
|
||||
//f: f,
|
||||
}),
|
||||
Value::Object(btreemap!(
|
||||
"Map".to_string() => Value::Object(btreemap![
|
||||
"a".to_string() => Value::U64(1),
|
||||
"b".to_string() => Value::U64(2),
|
||||
"c".to_string() => Value::U64(3),
|
||||
//"d".to_string() => Value::U64(4)
|
||||
"e".to_string() => Value::U64(5)
|
||||
//"f".to_string() => Value::U64(6)
|
||||
])
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_unit() {
|
||||
let v: DeEnum<_, _, _> = json::from_str("{\"Unit\":[]}").unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Unit::<u32, u32, u32>
|
||||
);
|
||||
|
||||
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
||||
"Unit".to_string() => Value::Array(vec![]))
|
||||
)).unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Unit::<u32, u32, u32>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_seq() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let e = 5;
|
||||
//let f = 6;
|
||||
|
||||
let v: DeEnum<_, _, _> = json::from_str("{\"Seq\":[1,2,3,5]}").unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
//d,
|
||||
e,
|
||||
//f,
|
||||
)
|
||||
);
|
||||
|
||||
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
||||
"Seq".to_string() => Value::Array(vec![
|
||||
Value::U64(1),
|
||||
Value::U64(2),
|
||||
Value::U64(3),
|
||||
//Value::U64(4),
|
||||
Value::U64(5),
|
||||
//Value::U64(6),
|
||||
])
|
||||
))).unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Seq(
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
//d,
|
||||
e,
|
||||
//e,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_de_enum_map() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
//let d = 4;
|
||||
let e = 5;
|
||||
//let f = 6;
|
||||
|
||||
let v: DeEnum<_, _, _> = json::from_str(
|
||||
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}"
|
||||
).unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: c,
|
||||
//d: d,
|
||||
e: e,
|
||||
//f: f,
|
||||
}
|
||||
);
|
||||
|
||||
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
||||
"Map".to_string() => Value::Object(btreemap![
|
||||
"a".to_string() => Value::U64(1),
|
||||
"b".to_string() => Value::U64(2),
|
||||
"c".to_string() => Value::U64(3),
|
||||
//"d".to_string() => Value::U64(4)
|
||||
"e".to_string() => Value::U64(5)
|
||||
//"f".to_string() => Value::U64(6)
|
||||
])
|
||||
))).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
v,
|
||||
DeEnum::Map {
|
||||
a: a,
|
||||
b: b,
|
||||
c: c,
|
||||
//d: d,
|
||||
e: e,
|
||||
//f: f,
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -1,528 +0,0 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use std::vec;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::ser::{Serialize, Serializer, SeqVisitor, MapVisitor};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Token<'a> {
|
||||
Bool(bool),
|
||||
Isize(isize),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Usize(usize),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'a str),
|
||||
|
||||
Option(bool),
|
||||
|
||||
Unit,
|
||||
NamedUnit(&'a str),
|
||||
EnumUnit(&'a str, &'a str),
|
||||
|
||||
SeqStart(Option<usize>),
|
||||
NamedSeqStart(&'a str, Option<usize>),
|
||||
EnumSeqStart(&'a str, &'a str, Option<usize>),
|
||||
SeqSep,
|
||||
SeqEnd,
|
||||
|
||||
MapStart(Option<usize>),
|
||||
NamedMapStart(&'a str, Option<usize>),
|
||||
EnumMapStart(&'a str, &'a str, Option<usize>),
|
||||
MapSep,
|
||||
MapEnd,
|
||||
}
|
||||
|
||||
struct AssertSerializer<'a> {
|
||||
iter: vec::IntoIter<Token<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> AssertSerializer<'a> {
|
||||
fn new(values: Vec<Token<'a>>) -> AssertSerializer {
|
||||
AssertSerializer {
|
||||
iter: values.into_iter(),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_sequence<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: SeqVisitor
|
||||
{
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
assert_eq!(self.iter.next(), Some(Token::SeqEnd));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_mapping<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: MapVisitor
|
||||
{
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
assert_eq!(self.iter.next(), Some(Token::MapEnd));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Serializer for AssertSerializer<'a> {
|
||||
type Error = ();
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Unit));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_named_unit(&mut self, name: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next().unwrap(), Token::NamedUnit(name));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_enum_unit(&mut self, name: &str, variant: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next().unwrap(), Token::EnumUnit(name, variant));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_bool(&mut self, v: bool) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Bool(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_isize(&mut self, v: isize) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Isize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i8(&mut self, v: i8) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::I8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i16(&mut self, v: i16) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::I16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i32(&mut self, v: i32) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::I32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_i64(&mut self, v: i64) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::I64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_usize(&mut self, v: usize) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Usize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u8(&mut self, v: u8) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::U8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u16(&mut self, v: u16) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::U16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u32(&mut self, v: u32) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::U32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_u64(&mut self, v: u64) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::U64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_f32(&mut self, v: f32) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::F32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, v: f64) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::F64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_char(&mut self, v: char) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Char(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_str(&mut self, v: &str) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next().unwrap(), Token::Str(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_none(&mut self) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Option(false)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), ()>
|
||||
where V: Serialize,
|
||||
{
|
||||
assert_eq!(self.iter.next(), Some(Token::Option(true)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), ()>
|
||||
where V: SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next(), Some(Token::SeqStart(len)));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_named_seq<V>(&mut self, name: &str, visitor: V) -> Result<(), ()>
|
||||
where V: SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next().unwrap(), Token::NamedSeqStart(name, len));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_enum_seq<V>(&mut self,
|
||||
name: &str,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<(), ()>
|
||||
where V: SeqVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next().unwrap(), Token::EnumSeqStart(name, variant, len));
|
||||
|
||||
self.visit_sequence(visitor)
|
||||
}
|
||||
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), ()>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.iter.next(), Some(Token::SeqSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<(), ()>
|
||||
where V: MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next(), Some(Token::MapStart(len)));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_named_map<V>(&mut self, name: &str, visitor: V) -> Result<(), ()>
|
||||
where V: MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next().unwrap(), Token::NamedMapStart(name, len));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_enum_map<V>(&mut self, name: &str, variant: &str, visitor: V) -> Result<(), ()>
|
||||
where V: MapVisitor
|
||||
{
|
||||
let len = visitor.len();
|
||||
|
||||
assert_eq!(self.iter.next().unwrap(), Token::EnumMapStart(name, variant, len));
|
||||
|
||||
self.visit_mapping(visitor)
|
||||
}
|
||||
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), ()>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
{
|
||||
assert_eq!(self.iter.next(), Some(Token::MapSep));
|
||||
|
||||
try!(key.serialize(self));
|
||||
value.serialize(self)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct NamedUnit;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct NamedSeq(i32, i32, i32);
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct NamedMap {
|
||||
a: i32,
|
||||
b: i32,
|
||||
c: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
enum Enum {
|
||||
Unit,
|
||||
Seq(i32, i32),
|
||||
Map { a: i32, b: i32 },
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! btreemap {
|
||||
() => {
|
||||
BTreeMap::new()
|
||||
};
|
||||
($($key:expr => $value:expr),+) => {
|
||||
{
|
||||
let mut map = BTreeMap::new();
|
||||
$(map.insert($key, $value);)+
|
||||
map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_test {
|
||||
($name:ident { $($value:expr => $tokens:expr,)+ }) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
$(
|
||||
let mut ser = AssertSerializer::new($tokens);
|
||||
assert_eq!($value.serialize(&mut ser), Ok(()));
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! declare_tests {
|
||||
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
||||
$(
|
||||
declare_test!($name { $($value => $tokens,)+ });
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
declare_tests! {
|
||||
test_unit {
|
||||
() => vec![Token::Unit],
|
||||
}
|
||||
test_bool {
|
||||
true => vec![Token::Bool(true)],
|
||||
false => vec![Token::Bool(false)],
|
||||
}
|
||||
test_isizes {
|
||||
0isize => vec![Token::Isize(0)],
|
||||
0i8 => vec![Token::I8(0)],
|
||||
0i16 => vec![Token::I16(0)],
|
||||
0i32 => vec![Token::I32(0)],
|
||||
0i64 => vec![Token::I64(0)],
|
||||
}
|
||||
test_usizes {
|
||||
0usize => vec![Token::Usize(0)],
|
||||
0u8 => vec![Token::U8(0)],
|
||||
0u16 => vec![Token::U16(0)],
|
||||
0u32 => vec![Token::U32(0)],
|
||||
0u64 => vec![Token::U64(0)],
|
||||
}
|
||||
test_floats {
|
||||
0f32 => vec![Token::F32(0.)],
|
||||
0f64 => vec![Token::F64(0.)],
|
||||
}
|
||||
test_char {
|
||||
'a' => vec![Token::Char('a')],
|
||||
}
|
||||
test_str {
|
||||
"abc" => vec![Token::Str("abc")],
|
||||
"abc".to_string() => vec![Token::Str("abc")],
|
||||
}
|
||||
test_option {
|
||||
None::<i32> => vec![Token::Option(false)],
|
||||
Some(1) => vec![
|
||||
Token::Option(true),
|
||||
Token::I32(1),
|
||||
],
|
||||
}
|
||||
test_slice {
|
||||
&[0][..0] => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
&[1, 2, 3][..] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
Vec::<isize>::new() => vec![
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
vec![vec![], vec![1], vec![2, 3]] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
Token::SeqSep,
|
||||
Token::SeqStart(Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple {
|
||||
(1,) => vec![
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
(1, 2, 3) => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_btreemap {
|
||||
btreemap![1 => 2] => vec![
|
||||
Token::MapStart(Some(1)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => 2, 3 => 4] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
Token::MapEnd,
|
||||
],
|
||||
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => vec![
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(1),
|
||||
Token::MapStart(Some(0)),
|
||||
Token::MapEnd,
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(2),
|
||||
Token::MapStart(Some(2)),
|
||||
Token::MapSep,
|
||||
Token::I32(3),
|
||||
Token::I32(4),
|
||||
|
||||
Token::MapSep,
|
||||
Token::I32(5),
|
||||
Token::I32(6),
|
||||
Token::MapEnd,
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_named_unit {
|
||||
NamedUnit => vec![Token::NamedUnit("NamedUnit")],
|
||||
}
|
||||
test_named_seq {
|
||||
NamedSeq(1, 2, 3) => vec![
|
||||
Token::NamedSeqStart("NamedSeq", Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_named_map {
|
||||
NamedMap { a: 1, b: 2, c: 3 } => vec![
|
||||
Token::NamedMapStart("NamedMap", Some(3)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("c"),
|
||||
Token::I32(3),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
test_enum {
|
||||
Enum::Unit => vec![Token::EnumUnit("Enum", "Unit")],
|
||||
Enum::Seq(1, 2) => vec![
|
||||
Token::EnumSeqStart("Enum", "Seq", Some(2)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
Enum::Map { a: 1, b: 2 } => vec![
|
||||
Token::EnumMapStart("Enum", "Map", Some(2)),
|
||||
Token::MapSep,
|
||||
Token::Str("a"),
|
||||
Token::I32(1),
|
||||
|
||||
Token::MapSep,
|
||||
Token::Str("b"),
|
||||
Token::I32(2),
|
||||
Token::MapEnd,
|
||||
],
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user