Update to serde 0.8.0

This commit is contained in:
David Tolnay 2016-07-28 11:01:22 -07:00
parent c7d96b191e
commit 492690669e
3 changed files with 139 additions and 9 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.7" } serde = { optional = true, version = "0.8" }
[features] [features]
default = ["rustc-serialize"] default = ["rustc-serialize"]

View file

@ -3,6 +3,43 @@ use Value;
use super::{Decoder, DecodeError, DecodeErrorKind}; use super::{Decoder, DecodeError, DecodeErrorKind};
use std::collections::BTreeMap; use std::collections::BTreeMap;
macro_rules! forward_to_deserialize {
($(
$name:ident ( $( $arg:ident : $ty:ty ),* );
)*) => {
$(
forward_to_deserialize!{
func: $name ( $( $arg: $ty ),* );
}
)*
};
(func: deserialize_enum ( $( $arg:ident : $ty:ty ),* );) => {
fn deserialize_enum<V>(
&mut self,
$(_: $ty,)*
_visitor: V,
) -> ::std::result::Result<V::Value, Self::Error>
where V: ::serde::de::EnumVisitor
{
Err(::serde::de::Error::invalid_type(::serde::de::Type::Enum))
}
};
(func: $name:ident ( $( $arg:ident : $ty:ty ),* );) => {
#[inline]
fn $name<V>(
&mut self,
$(_: $ty,)*
visitor: V,
) -> ::std::result::Result<V::Value, Self::Error>
where V: ::serde::de::Visitor
{
self.deserialize(visitor)
}
};
}
impl de::Deserializer for Decoder { impl de::Deserializer for Decoder {
type Error = DecodeError; type Error = DecodeError;
@ -186,6 +223,28 @@ impl de::Deserializer for Decoder {
let mut d = <() as ValueDeserializer<Self::Error>>::into_deserializer(()); let mut d = <() as ValueDeserializer<Self::Error>>::into_deserializer(());
d.deserialize(visitor) d.deserialize(visitor)
} }
forward_to_deserialize!{
deserialize_usize();
deserialize_u8();
deserialize_u16();
deserialize_u32();
deserialize_isize();
deserialize_i8();
deserialize_i16();
deserialize_i32();
deserialize_f32();
deserialize_string();
deserialize_unit();
deserialize_seq_fixed_size(len: usize);
deserialize_bytes();
deserialize_unit_struct(name: &'static str);
deserialize_newtype_struct(name: &'static str);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_struct_field();
deserialize_tuple(len: usize);
}
} }
struct VariantVisitor { struct VariantVisitor {
@ -270,6 +329,39 @@ impl<'a, I> de::Deserializer for SeqDeserializer<'a, I>
{ {
visitor.visit_seq(self) visitor.visit_seq(self)
} }
forward_to_deserialize!{
deserialize_bool();
deserialize_usize();
deserialize_u8();
deserialize_u16();
deserialize_u32();
deserialize_u64();
deserialize_isize();
deserialize_i8();
deserialize_i16();
deserialize_i32();
deserialize_i64();
deserialize_f32();
deserialize_f64();
deserialize_char();
deserialize_str();
deserialize_string();
deserialize_unit();
deserialize_option();
deserialize_seq();
deserialize_seq_fixed_size(len: usize);
deserialize_bytes();
deserialize_map();
deserialize_unit_struct(name: &'static str);
deserialize_newtype_struct(name: &'static str);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_struct_field();
deserialize_tuple(len: usize);
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
deserialize_ignored_any();
}
} }
impl<'a, I> de::SeqVisitor for SeqDeserializer<'a, I> impl<'a, I> de::SeqVisitor for SeqDeserializer<'a, I>
@ -492,6 +584,38 @@ impl de::Deserializer for UnitDeserializer {
{ {
visitor.visit_none() visitor.visit_none()
} }
forward_to_deserialize!{
deserialize_bool();
deserialize_usize();
deserialize_u8();
deserialize_u16();
deserialize_u32();
deserialize_u64();
deserialize_isize();
deserialize_i8();
deserialize_i16();
deserialize_i32();
deserialize_i64();
deserialize_f32();
deserialize_f64();
deserialize_char();
deserialize_str();
deserialize_string();
deserialize_unit();
deserialize_seq();
deserialize_seq_fixed_size(len: usize);
deserialize_bytes();
deserialize_map();
deserialize_unit_struct(name: &'static str);
deserialize_newtype_struct(name: &'static str);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_struct_field();
deserialize_tuple(len: usize);
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
deserialize_ignored_any();
}
} }
impl de::Deserialize for Value { impl de::Deserialize for Value {

View file

@ -128,12 +128,15 @@ impl ser::Serializer for Encoder {
fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> { fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
self.table_begin() self.table_begin()
} }
fn serialize_map_elt<K, V>(&mut self, _state: &mut Encoder, key: K, value: V) -> Result<(), Error> fn serialize_map_key<K>(&mut self, _state: &mut Encoder, key: K) -> Result<(), Error>
where K: ser::Serialize, V: ser::Serialize where K: ser::Serialize
{ {
try!(self.table_key(|me| key.serialize(me))); self.table_key(|me| key.serialize(me))
try!(value.serialize(self)); }
Ok(()) fn serialize_map_value<V>(&mut self, _state: &mut Encoder, value: V) -> Result<(), Error>
where V: ser::Serialize
{
value.serialize(self)
} }
fn serialize_map_end(&mut self, state: Self) -> Result<(), Error> { fn serialize_map_end(&mut self, state: Self) -> Result<(), Error> {
self.table_end(state) self.table_end(state)
@ -144,7 +147,8 @@ impl ser::Serializer for Encoder {
fn serialize_struct_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error> fn serialize_struct_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
where V: ser::Serialize where V: ser::Serialize
{ {
self.serialize_map_elt(state, key, value) try!(self.serialize_map_key(state, key));
self.serialize_map_value(state, value)
} }
fn serialize_struct_end(&mut self, state: Self) -> Result<(), Error> { fn serialize_struct_end(&mut self, state: Self) -> Result<(), Error> {
self.serialize_map_end(state) self.serialize_map_end(state)
@ -155,7 +159,8 @@ impl ser::Serializer for Encoder {
fn serialize_struct_variant_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error> fn serialize_struct_variant_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
where V: ser::Serialize where V: ser::Serialize
{ {
self.serialize_map_elt(state, key, value) try!(self.serialize_map_key(state, key));
self.serialize_map_value(state, value)
} }
fn serialize_struct_variant_end(&mut self, state: Self) -> Result<(), Error> { fn serialize_struct_variant_end(&mut self, state: Self) -> Result<(), Error> {
self.serialize_map_end(state) self.serialize_map_end(state)
@ -208,7 +213,8 @@ impl ser::Serialize for Value {
Value::Table(ref t) => { Value::Table(ref t) => {
let mut state = try!(e.serialize_map(Some(t.len()))); let mut state = try!(e.serialize_map(Some(t.len())));
for (k, v) in t.iter() { for (k, v) in t.iter() {
try!(e.serialize_map_elt(&mut state, k, v)); try!(e.serialize_map_key(&mut state, k));
try!(e.serialize_map_value(&mut state, v));
} }
e.serialize_map_end(state) e.serialize_map_end(state)
} }