mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-07-24 05:25:43 +00:00
Merge pull request #475 from serde-rs/skeptic
use skeptic to test the readme and fix readme for 0.8
This commit is contained in:
@@ -356,11 +356,8 @@ impl serde::Serialize for i32 {
|
|||||||
```
|
```
|
||||||
|
|
||||||
As you can see it's pretty simple. More complex types like `BTreeMap` need to
|
As you can see it's pretty simple. More complex types like `BTreeMap` need to
|
||||||
pass a
|
use a multi-step process (init, elements, end) in order to walk through the
|
||||||
[MapVisitor](http://serde-rs.github.io/serde/serde/serde/ser/trait.MapVisitor.html)
|
type:
|
||||||
to the
|
|
||||||
[Serializer](http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html)
|
|
||||||
in order to walk through the type:
|
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
impl<K, V> Serialize for BTreeMap<K, V>
|
impl<K, V> Serialize for BTreeMap<K, V>
|
||||||
@@ -371,55 +368,17 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
|||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
|
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
||||||
}
|
for (k, v) in self {
|
||||||
}
|
try!(serializer.serialize_map_elt(&mut state, k, v));
|
||||||
|
|
||||||
pub struct MapIteratorVisitor<Iter> {
|
|
||||||
iter: Iter,
|
|
||||||
len: Option<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V, Iter> MapIteratorVisitor<Iter>
|
|
||||||
where Iter: Iterator<Item=(K, V)>
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
pub fn new(iter: Iter, len: Option<usize>) -> MapIteratorVisitor<Iter> {
|
|
||||||
MapIteratorVisitor {
|
|
||||||
iter: iter,
|
|
||||||
len: len,
|
|
||||||
}
|
}
|
||||||
}
|
serializer.serialize_map_end(state)
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
|
||||||
where K: Serialize,
|
|
||||||
V: Serialize,
|
|
||||||
I: Iterator<Item=(K, V)>,
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
|
||||||
where S: Serializer,
|
|
||||||
{
|
|
||||||
match self.iter.next() {
|
|
||||||
Some((key, value)) => {
|
|
||||||
let value = try!(serializer.serialize_map_elt(key, value));
|
|
||||||
Ok(Some(value))
|
|
||||||
}
|
|
||||||
None => Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn len(&self) -> Option<usize> {
|
|
||||||
self.len
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Serializing structs follow this same pattern. In fact, structs are represented
|
Serializing structs follow this same pattern. In fact, structs are represented
|
||||||
as a named map. Its visitor uses a simple state machine to iterate through all
|
as a named map, with a known length.
|
||||||
the fields:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
@@ -434,35 +393,10 @@ impl serde::Serialize for Point {
|
|||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: serde::Serializer
|
where S: serde::Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_struct("Point", PointMapVisitor {
|
let mut state = try!(serializer.serialize_struct("Point", 2));
|
||||||
value: self,
|
try!(serializer.serialize_struct_elt(&mut state, "x", &self.x));
|
||||||
state: 0,
|
try!(serializer.serialize_struct_elt(&mut state, "y", &self.y));
|
||||||
})
|
serializer.serialize_struct_end(state)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PointMapVisitor<'a> {
|
|
||||||
value: &'a Point,
|
|
||||||
state: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> serde::ser::MapVisitor for PointMapVisitor<'a> {
|
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
|
||||||
where S: serde::Serializer
|
|
||||||
{
|
|
||||||
match self.state {
|
|
||||||
0 => {
|
|
||||||
self.state += 1;
|
|
||||||
Ok(Some(try!(serializer.serialize_struct_elt("x", &self.value.x))))
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
self.state += 1;
|
|
||||||
Ok(Some(try!(serializer.serialize_struct_elt("y", &self.value.y))))
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-1
@@ -8,17 +8,29 @@ repository = "https://github.com/serde-rs/serde"
|
|||||||
documentation = "https://github.com/serde-rs/serde"
|
documentation = "https://github.com/serde-rs/serde"
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
include = ["Cargo.toml", "src/**/*.rs"]
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
|
build = "build.rs"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
plugin = true
|
plugin = true
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
unstable-testing = ["clippy", "serde/unstable-testing", "serde_codegen/unstable-testing"]
|
unstable-testing = [
|
||||||
|
"clippy",
|
||||||
|
"skeptic",
|
||||||
|
"serde_json",
|
||||||
|
"serde/unstable-testing",
|
||||||
|
"serde_codegen/unstable-testing"
|
||||||
|
]
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
skeptic = { version = "^0.6.0", optional = true }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.*", optional = true }
|
clippy = { version = "^0.*", optional = true }
|
||||||
serde_codegen = { version = "=0.8.0", default-features = false, features = ["unstable"] }
|
serde_codegen = { version = "=0.8.0", default-features = false, features = ["unstable"] }
|
||||||
|
skeptic = { version = "^0.6.0", optional = true }
|
||||||
|
serde_json = { version = "0.8.0", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
clippy = "^0.*"
|
clippy = "^0.*"
|
||||||
@@ -32,6 +44,10 @@ serde_test = "0.8.0"
|
|||||||
name = "test"
|
name = "test"
|
||||||
path = "tests/test.rs"
|
path = "tests/test.rs"
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "skeptic"
|
||||||
|
path = "tests/skeptic.rs"
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "bench"
|
name = "bench"
|
||||||
path = "benches/bench.rs"
|
path = "benches/bench.rs"
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#[cfg(feature = "unstable-testing")]
|
||||||
|
mod inner {
|
||||||
|
extern crate skeptic;
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
println!("cargo:rerun-if-changed=../README.md");
|
||||||
|
skeptic::generate_doc_tests(&["../README.md"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "unstable-testing"))]
|
||||||
|
mod inner {
|
||||||
|
pub fn main() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
inner::main()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
#![cfg(feature = "unstable-testing")]
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));
|
||||||
@@ -6,3 +6,4 @@ extern crate test;
|
|||||||
include!("../../testing/tests/test.rs.in");
|
include!("../../testing/tests/test.rs.in");
|
||||||
|
|
||||||
mod compile_tests;
|
mod compile_tests;
|
||||||
|
mod skeptic;
|
||||||
|
|||||||
Reference in New Issue
Block a user