diff options
Diffstat (limited to 'src/decoder')
-rw-r--r-- | src/decoder/mod.rs | 40 | ||||
-rw-r--r-- | src/decoder/serde.rs | 233 |
2 files changed, 258 insertions, 15 deletions
diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index b223e03..9720528 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,6 +1,9 @@ use std::error; use std::fmt; + +#[cfg(feature = "rustc-serialize")] use std::collections::{btree_map, BTreeMap}; +#[cfg(feature = "rustc-serialize")] use std::iter::Peekable; use Value; @@ -19,7 +22,9 @@ pub struct Decoder { /// whether fields were decoded or not. pub toml: Option<Value>, cur_field: Option<String>, + #[cfg(feature = "rustc-serialize")] cur_map: Peekable<btree_map::IntoIter<String, Value>>, + #[cfg(feature = "rustc-serialize")] leftover_map: ::Table, } @@ -115,27 +120,36 @@ impl Decoder { /// This decoder can be passed to the `Decodable` methods or driven /// manually. pub fn new(toml: Value) -> Decoder { + Decoder::new_empty(Some(toml), None) + } + + fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder { + let cur_field = if field.is_empty() { + self.cur_field.clone() + } else { + match self.cur_field { + None => Some(field.to_string()), + Some(ref s) => Some(format!("{}.{}", s, field)) + } + }; + Decoder::new_empty(toml, cur_field) + } + + #[cfg(feature = "rustc-serialize")] + fn new_empty(toml: Option<Value>, cur_field: Option<String>) -> Decoder { Decoder { - toml: Some(toml), - cur_field: None, + toml: toml, + cur_field: cur_field, leftover_map: BTreeMap::new(), cur_map: BTreeMap::new().into_iter().peekable(), } } - fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder { + #[cfg(not(feature = "rustc-serialize"))] + fn new_empty(toml: Option<Value>, cur_field: Option<String>) -> Decoder { Decoder { toml: toml, - cur_field: if field.is_empty() { - self.cur_field.clone() - } else { - match self.cur_field { - None => Some(field.to_string()), - Some(ref s) => Some(format!("{}.{}", s, field)) - } - }, - leftover_map: BTreeMap::new(), - cur_map: BTreeMap::new().into_iter().peekable(), + cur_field: cur_field, } } diff --git a/src/decoder/serde.rs b/src/decoder/serde.rs index 91bc8ac..81a2bae 100644 --- a/src/decoder/serde.rs +++ b/src/decoder/serde.rs @@ -3,6 +3,43 @@ use Value; use super::{Decoder, DecodeError, DecodeErrorKind}; 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 { type Error = DecodeError; @@ -43,6 +80,27 @@ impl de::Deserializer for Decoder { } } + fn deserialize_i8<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_i16<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_i32<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + fn deserialize_i64<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> where V: de::Visitor @@ -53,10 +111,53 @@ impl de::Deserializer for Decoder { } } - fn deserialize_u64<V>(&mut self, v: V) -> Result<V::Value, DecodeError> + fn deserialize_isize<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_u8<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_u16<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_u32<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_u64<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_i64(visitor) + } + + fn deserialize_usize<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> where V: de::Visitor { - self.deserialize_i64(v) + self.deserialize_i64(visitor) + } + + fn deserialize_f32<V>(&mut self, visitor: V) + -> Result<V::Value, DecodeError> + where V: de::Visitor + { + self.deserialize_f64(visitor) } fn deserialize_f64<V>(&mut self, mut visitor: V) @@ -79,6 +180,13 @@ impl de::Deserializer for Decoder { } } + fn deserialize_string<V>(&mut self, visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor, + { + self.deserialize_str(visitor) + } + fn deserialize_char<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> where V: de::Visitor @@ -186,6 +294,62 @@ impl de::Deserializer for Decoder { let mut d = <() as ValueDeserializer<Self::Error>>::into_deserializer(()); d.deserialize(visitor) } + + fn deserialize_bytes<V>(&mut self, visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_seq(visitor) + } + + fn deserialize_seq_fixed_size<V>(&mut self, _len: usize, visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_seq(visitor) + } + + fn deserialize_newtype_struct<V>(&mut self, _name: &'static str, visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_seq(visitor) + } + + fn deserialize_tuple_struct<V>(&mut self, + _name: &'static str, + _len: usize, + visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_seq(visitor) + } + + fn deserialize_struct<V>(&mut self, + _name: &'static str, + _fields: &'static [&'static str], + visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_map(visitor) + } + + fn deserialize_tuple<V>(&mut self, + _len: usize, + visitor: V) + -> Result<V::Value, Self::Error> + where V: de::Visitor + { + self.deserialize_seq(visitor) + } + + forward_to_deserialize!{ + deserialize_unit(); + deserialize_unit_struct(name: &'static str); + deserialize_struct_field(); + } } struct VariantVisitor { @@ -270,6 +434,39 @@ impl<'a, I> de::Deserializer for SeqDeserializer<'a, I> { 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> @@ -492,6 +689,38 @@ impl de::Deserializer for UnitDeserializer { { 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 { |