This commit is contained in:
Alex Crichton 2016-03-07 23:48:01 -08:00
parent 77be56048c
commit d02e622330
8 changed files with 134 additions and 128 deletions

View file

@ -17,7 +17,7 @@ facilitate deserializing and serializing Rust structures.
[dependencies] [dependencies]
rustc-serialize = { optional = true, version = "0.3.0" } rustc-serialize = { optional = true, version = "0.3.0" }
serde = { optional = true, version = "0.6" } serde = { optional = true, version = "0.7" }
[features] [features]
default = ["rustc-serialize"] default = ["rustc-serialize"]

75
serde-tests/Cargo.lock generated
View file

@ -2,27 +2,18 @@
name = "serde-tests" name = "serde-tests"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"serde 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde_codegen 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.29.0 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.27", "toml 0.1.27",
] ]
[[package]]
name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "aster" name = "aster"
version = "0.9.1" version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -52,41 +43,22 @@ dependencies = [
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "num"
version = "0.1.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "quasi" name = "quasi"
version = "0.3.10" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "quasi_codegen" name = "quasi_codegen"
version = "0.3.10" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"aster 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "aster 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.29.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -96,35 +68,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "serde" name = "serde"
version = "0.6.6" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "serde_codegen" name = "serde_codegen"
version = "0.6.6" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"aster 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "aster 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", "quasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi_codegen 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", "quasi_codegen 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.29.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "syntex" name = "syntex"
version = "0.22.0" version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"syntex_syntax 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "syntex_syntax" name = "syntex_syntax"
version = "0.23.0" version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -149,7 +118,7 @@ name = "toml"
version = "0.1.27" version = "0.1.27"
dependencies = [ dependencies = [
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View file

@ -5,12 +5,12 @@ authors = ["Alex Crichton <alex@alexcrichton.com>"]
build = "build.rs" build = "build.rs"
[dependencies] [dependencies]
serde = "0.6" serde = "0.7"
toml = { path = "..", features = ["serde"] } toml = { path = "..", features = ["serde"] }
[build-dependencies] [build-dependencies]
syntex = "0.22" syntex = "0.29"
serde_codegen = "0.6" serde_codegen = "0.7"
[lib] [lib]
name = "serde_tests" name = "serde_tests"

View file

@ -82,7 +82,7 @@ fn application_decode_error() {
fn deserialize<D: Deserializer>(d: &mut D) -> Result<Range10, D::Error> { fn deserialize<D: Deserializer>(d: &mut D) -> Result<Range10, D::Error> {
let x: usize = try!(Deserialize::deserialize(d)); let x: usize = try!(Deserialize::deserialize(d));
if x > 10 { if x > 10 {
Err(serde::de::Error::syntax("more than 10")) Err(serde::de::Error::custom("more than 10"))
} else { } else {
Ok(Range10(x)) Ok(Range10(x))
} }

View file

@ -53,6 +53,8 @@ pub enum DecodeErrorKind {
NilTooLong, NilTooLong,
/// There was an error with the syntactical structure of the TOML. /// There was an error with the syntactical structure of the TOML.
SyntaxError, SyntaxError,
/// A custom error was generated when decoding.
CustomError(String),
/// The end of the TOML input was reached too soon /// The end of the TOML input was reached too soon
EndOfStream, EndOfStream,
} }
@ -195,6 +197,9 @@ impl fmt::Display for DecodeError {
EndOfStream => { EndOfStream => {
write!(f, "end of stream") write!(f, "end of stream")
} }
CustomError(ref s) => {
write!(f, "custom error: {}", s)
}
}); });
match self.field { match self.field {
Some(ref s) => { Some(ref s) => {
@ -218,6 +223,7 @@ impl error::Error for DecodeError {
NilTooLong => "nonzero length string representing nil", NilTooLong => "nonzero length string representing nil",
SyntaxError => "syntax error", SyntaxError => "syntax error",
EndOfStream => "end of stream", EndOfStream => "end of stream",
CustomError(..) => "custom error",
} }
} }
} }

View file

@ -5,27 +5,32 @@ use std::collections::BTreeMap;
fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError { fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError {
match err { match err {
de::value::Error::SyntaxError => de::Error::syntax(ty), de::value::Error::Custom(s) => de::Error::custom(s),
de::value::Error::EndOfStreamError => de::Error::end_of_stream(), de::value::Error::EndOfStream => de::Error::end_of_stream(),
de::value::Error::MissingFieldError(s) => { de::value::Error::MissingField(s) => {
DecodeError { DecodeError {
field: Some(s.to_string()), field: Some(s.to_string()),
kind: DecodeErrorKind::ExpectedField(Some(ty)), kind: DecodeErrorKind::ExpectedField(Some(ty)),
} }
}, },
de::value::Error::UnknownFieldError(s) => { de::value::Error::UnknownField(s) => {
DecodeError { DecodeError {
field: Some(s.to_string()), field: Some(s.to_string()),
kind: DecodeErrorKind::UnknownField, kind: DecodeErrorKind::UnknownField,
} }
}, },
de::value::Error::InvalidType(ty) => de::Error::invalid_type(ty),
de::value::Error::InvalidLength(l) => de::Error::invalid_length(l),
de::value::Error::InvalidValue(v) => de::Error::invalid_value(&v),
de::value::Error::UnknownVariant(v) => de::Error::unknown_variant(&v),
} }
} }
impl de::Deserializer for Decoder { impl de::Deserializer for Decoder {
type Error = DecodeError; type Error = DecodeError;
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
match self.toml.take() { match self.toml.take() {
@ -61,30 +66,34 @@ impl de::Deserializer for Decoder {
} }
} }
fn visit_isize<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_isize<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_i8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_i8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_i16<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_i16<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_i32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_i32<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_i64<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize_i64<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
match self.toml.take() { match self.toml.take() {
@ -95,42 +104,47 @@ impl de::Deserializer for Decoder {
} }
} }
fn visit_usize<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_usize<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_u8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_u8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_u16<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_u16<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_u32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_u32<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_u64<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_u64<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_i64(visitor) self.deserialize_i64(visitor)
} }
fn visit_f32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError> fn deserialize_f32<V>(&mut self, visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
self.visit_f64(visitor) self.deserialize_f64(visitor)
} }
fn visit_f64<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize_f64<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
match self.toml.take() { match self.toml.take() {
@ -141,7 +155,8 @@ impl de::Deserializer for Decoder {
} }
} }
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize_option<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {
if self.toml.is_none() { if self.toml.is_none() {
@ -151,7 +166,8 @@ impl de::Deserializer for Decoder {
} }
} }
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize_seq<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
if self.toml.is_none() { if self.toml.is_none() {
@ -159,11 +175,11 @@ impl de::Deserializer for Decoder {
let e = visitor.visit_seq(de::value::SeqDeserializer::new(iter, 0)); let e = visitor.visit_seq(de::value::SeqDeserializer::new(iter, 0));
e.map_err(|e| se2toml(e, "array")) e.map_err(|e| se2toml(e, "array"))
} else { } else {
self.visit(visitor) self.deserialize(visitor)
} }
} }
fn visit_enum<V>(&mut self, fn deserialize_enum<V>(&mut self,
_enum: &str, _enum: &str,
variants: &[&str], variants: &[&str],
mut visitor: V) -> Result<V::Value, DecodeError> mut visitor: V) -> Result<V::Value, DecodeError>
@ -240,7 +256,7 @@ impl de::VariantVisitor for VariantVisitor {
visitor: V) -> Result<V::Value, DecodeError> visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
de::Deserializer::visit(&mut self.de, visitor) de::Deserializer::deserialize(&mut self.de, visitor)
} }
fn visit_struct<V>(&mut self, fn visit_struct<V>(&mut self,
@ -248,7 +264,7 @@ impl de::VariantVisitor for VariantVisitor {
visitor: V) -> Result<V::Value, DecodeError> visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
de::Deserializer::visit(&mut self.de, visitor) de::Deserializer::deserialize(&mut self.de, visitor)
} }
} }
@ -283,7 +299,8 @@ impl<'a, I> de::Deserializer for SeqDeserializer<'a, I>
{ {
type Error = DecodeError; type Error = DecodeError;
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
visitor.visit_seq(self) visitor.visit_seq(self)
@ -326,8 +343,11 @@ impl<'a, I> de::SeqVisitor for SeqDeserializer<'a, I>
} }
impl de::Error for DecodeError { impl de::Error for DecodeError {
fn syntax(_: &str) -> DecodeError { fn custom<T: Into<String>>(msg: T) -> DecodeError {
DecodeError { field: None, kind: DecodeErrorKind::SyntaxError } DecodeError {
field: None,
kind: DecodeErrorKind::CustomError(msg.into()),
}
} }
fn end_of_stream() -> DecodeError { fn end_of_stream() -> DecodeError {
DecodeError { field: None, kind: DecodeErrorKind::EndOfStream } DecodeError { field: None, kind: DecodeErrorKind::EndOfStream }
@ -448,13 +468,15 @@ struct UnitDeserializer;
impl de::Deserializer for UnitDeserializer { impl de::Deserializer for UnitDeserializer {
type Error = DecodeError; type Error = DecodeError;
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
visitor.visit_unit() visitor.visit_unit()
} }
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn deserialize_option<V>(&mut self, mut visitor: V)
-> Result<V::Value, DecodeError>
where V: de::Visitor, where V: de::Visitor,
{ {
visitor.visit_none() visitor.visit_none()
@ -506,6 +528,6 @@ impl de::Deserialize for Value {
} }
} }
deserializer.visit(ValueVisitor) deserializer.deserialize(ValueVisitor)
} }
} }

View file

@ -61,6 +61,8 @@ pub enum Error {
/// Indicates that a type other than a string was attempted to be used as a /// Indicates that a type other than a string was attempted to be used as a
/// map key type. /// map key type.
InvalidMapKeyType, InvalidMapKeyType,
/// A custom error type was generated
Custom(String),
} }
#[derive(PartialEq)] #[derive(PartialEq)]
@ -202,6 +204,7 @@ impl fmt::Display for Error {
at this location"), at this location"),
Error::InvalidMapKeyType => write!(f, "only strings can be used as \ Error::InvalidMapKeyType => write!(f, "only strings can be used as \
key types"), key types"),
Error::Custom(ref s) => write!(f, "custom error: {}", s),
} }
} }
} }

View file

@ -5,33 +5,33 @@ use super::{Encoder, Error};
impl ser::Serializer for Encoder { impl ser::Serializer for Encoder {
type Error = Error; type Error = Error;
fn visit_bool(&mut self, v: bool) -> Result<(), Error> { fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
self.emit_value(Value::Boolean(v)) self.emit_value(Value::Boolean(v))
} }
fn visit_i64(&mut self, v: i64) -> Result<(), Error> { fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
self.emit_value(Value::Integer(v)) self.emit_value(Value::Integer(v))
} }
fn visit_u64(&mut self, v: u64) -> Result<(), Error> { fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
self.visit_i64(v as i64) self.serialize_i64(v as i64)
} }
fn visit_f64(&mut self, v: f64) -> Result<(), Error> { fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
self.emit_value(Value::Float(v)) self.emit_value(Value::Float(v))
} }
fn visit_str(&mut self, value: &str) -> Result<(), Error> { fn serialize_str(&mut self, value: &str) -> Result<(), Error> {
self.emit_value(Value::String(value.to_string())) self.emit_value(Value::String(value.to_string()))
} }
fn visit_unit(&mut self) -> Result<(), Error> { fn serialize_unit(&mut self) -> Result<(), Error> {
Ok(()) Ok(())
} }
fn visit_none(&mut self) -> Result<(), Error> { fn serialize_none(&mut self) -> Result<(), Error> {
self.emit_none() self.emit_none()
} }
fn visit_some<V>(&mut self, value: V) -> Result<(), Error> fn serialize_some<V>(&mut self, value: V) -> Result<(), Error>
where V: ser::Serialize where V: ser::Serialize
{ {
value.serialize(self) value.serialize(self)
} }
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), Error> fn serialize_seq<V>(&mut self, mut visitor: V) -> Result<(), Error>
where V: ser::SeqVisitor where V: ser::SeqVisitor
{ {
self.seq(|me| { self.seq(|me| {
@ -39,12 +39,12 @@ impl ser::Serializer for Encoder {
Ok(()) Ok(())
}) })
} }
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Error> fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
where T: ser::Serialize where T: ser::Serialize
{ {
value.serialize(self) value.serialize(self)
} }
fn visit_map<V>(&mut self, mut visitor: V) -> Result<(), Error> fn serialize_map<V>(&mut self, mut visitor: V) -> Result<(), Error>
where V: ser::MapVisitor where V: ser::MapVisitor
{ {
self.table(|me| { self.table(|me| {
@ -52,14 +52,14 @@ impl ser::Serializer for Encoder {
Ok(()) Ok(())
}) })
} }
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error> fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
where K: ser::Serialize, V: ser::Serialize where K: ser::Serialize, V: ser::Serialize
{ {
try!(self.table_key(|me| key.serialize(me))); try!(self.table_key(|me| key.serialize(me)));
try!(value.serialize(self)); try!(value.serialize(self));
Ok(()) Ok(())
} }
fn visit_newtype_struct<T>(&mut self, fn serialize_newtype_struct<T>(&mut self,
_name: &'static str, _name: &'static str,
value: T) -> Result<(), Self::Error> value: T) -> Result<(), Self::Error>
where T: ser::Serialize, where T: ser::Serialize,
@ -67,7 +67,7 @@ impl ser::Serializer for Encoder {
// Don't serialize the newtype struct in a tuple. // Don't serialize the newtype struct in a tuple.
value.serialize(self) value.serialize(self)
} }
fn visit_newtype_variant<T>(&mut self, fn serialize_newtype_variant<T>(&mut self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: usize,
_variant: &'static str, _variant: &'static str,
@ -84,19 +84,25 @@ impl ser::Serialize for Value {
where E: ser::Serializer where E: ser::Serializer
{ {
match *self { match *self {
Value::String(ref s) => e.visit_str(s), Value::String(ref s) => e.serialize_str(s),
Value::Integer(i) => e.visit_i64(i), Value::Integer(i) => e.serialize_i64(i),
Value::Float(f) => e.visit_f64(f), Value::Float(f) => e.serialize_f64(f),
Value::Boolean(b) => e.visit_bool(b), Value::Boolean(b) => e.serialize_bool(b),
Value::Datetime(ref s) => e.visit_str(s), Value::Datetime(ref s) => e.serialize_str(s),
Value::Array(ref a) => { Value::Array(ref a) => {
e.visit_seq(ser::impls::SeqIteratorVisitor::new(a.iter(), e.serialize_seq(ser::impls::SeqIteratorVisitor::new(a.iter(),
Some(a.len()))) Some(a.len())))
} }
Value::Table(ref t) => { Value::Table(ref t) => {
e.visit_map(ser::impls::MapIteratorVisitor::new(t.iter(), e.serialize_map(ser::impls::MapIteratorVisitor::new(t.iter(),
Some(t.len()))) Some(t.len())))
} }
} }
} }
} }
impl ser::Error for Error {
fn custom<T: Into<String>>(msg: T) -> Error {
Error::Custom(msg.into())
}
}