make Deserializer pure

This commit is contained in:
Oliver Schneider 2016-07-20 10:26:31 +02:00
parent 89f0ad99a6
commit 36a7bf6244
No known key found for this signature in database
GPG Key ID: 56D6EEA0FC67AC46
6 changed files with 293 additions and 120 deletions

View File

@ -10,6 +10,68 @@ use collections::{String, Vec};
use core::fmt;
///////////////////////////////////////////////////////////////////////////////
/// Macro helper to not have to re-implement all the defaulted methods.
/// Every given method ignores all arguments and forwards to `deserialize`.
/// Note that `deserialize_enum` simply returns an `Error::invalid_type`.
#[macro_export]
macro_rules! de_forward_to_deserialize {
($($func:ident),*) => {
$(de_forward_to_deserialize!{func: $func})*
};
(func: deserialize_unit_struct) => {
de_forward_to_deserialize!{named: deserialize_unit_struct}
};
(func: deserialize_newtype_struct) => {
de_forward_to_deserialize!{named: deserialize_newtype_struct}
};
(func: deserialize_tuple) => {
de_forward_to_deserialize!{tup_fn: deserialize_tuple}
};
(func: deserialize_fixed_size_array) => {
de_forward_to_deserialize!{tup_fn: deserialize_fixed_size_array}
};
(func: deserialize_tuple_struct) => {
fn deserialize_tuple_struct<__V>(&mut self, _: &str, _: usize, visitor: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor {
self.deserialize(visitor)
}
};
(func: deserialize_struct) => {
fn deserialize_struct<__V>(&mut self, _: &str, _: &[&str], visitor: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor {
self.deserialize(visitor)
}
};
(func: deserialize_enum) => {
fn deserialize_enum<__V>(&mut self, _: &str, _: &[&str], _: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::EnumVisitor {
Err($crate::de::Error::invalid_type($crate::de::Type::Enum))
}
};
(named: $func:ident) => {
fn $func<__V>(&mut self, _: &str, visitor: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor {
self.deserialize(visitor)
}
};
(tup_fn: $func: ident) => {
fn $func<__V>(&mut self, _: usize, visitor: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor {
self.deserialize(visitor)
}
};
(func: $func:ident) => {
fn $func<__V>(&mut self, visitor: __V) -> Result<__V::Value, Self::Error>
where __V: $crate::de::Visitor {
self.deserialize(visitor)
}
};
}
///////////////////////////////////////////////////////////////////////////////
// modules come after macros
pub mod impls;
pub mod value;
mod from_primitive;
@ -245,157 +307,100 @@ pub trait Deserializer {
/// This method hints that the `Deserialize` type is expecting a `bool` value.
#[inline]
fn deserialize_bool<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `usize` value.
#[inline]
fn deserialize_usize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_u64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `u8` value.
#[inline]
fn deserialize_u8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_u64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `u16` value.
#[inline]
fn deserialize_u16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_u64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `u32` value.
#[inline]
fn deserialize_u32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_u64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `u64` value.
#[inline]
fn deserialize_u64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `isize` value.
#[inline]
fn deserialize_isize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_i64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `i8` value.
#[inline]
fn deserialize_i8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_i64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `i16` value.
#[inline]
fn deserialize_i16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_i64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `i32` value.
#[inline]
fn deserialize_i32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_i64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `i64` value.
#[inline]
fn deserialize_i64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `f32` value.
#[inline]
fn deserialize_f32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_f64(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `f64` value.
#[inline]
fn deserialize_f64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `char` value.
#[inline]
fn deserialize_char<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `&str` value.
#[inline]
fn deserialize_str<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `String` value.
#[inline]
fn deserialize_string<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_str(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting an `unit` value.
#[inline]
fn deserialize_unit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: 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 deserialize_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: 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 deserialize_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a fixed size array. This allows
/// deserializers to parse arrays that aren't tagged as arrays.
@ -405,29 +410,20 @@ pub trait Deserializer {
fn deserialize_fixed_size_array<V>(&mut self,
_len: usize,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: Visitor;
/// 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 deserialize_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_seq(visitor)
}
where V: 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 deserialize_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: 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.
@ -435,10 +431,7 @@ pub trait Deserializer {
fn deserialize_unit_struct<V>(&mut self,
_name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_unit(visitor)
}
where V: 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.
@ -446,10 +439,7 @@ pub trait Deserializer {
fn deserialize_newtype_struct<V>(&mut self,
name: &'static str,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_tuple_struct(name, 1, visitor)
}
where V: 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.
@ -458,10 +448,7 @@ pub trait Deserializer {
_name: &'static str,
len: usize,
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_tuple(len, visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a struct. This allows
/// deserializers to parse sequences that aren't tagged as maps.
@ -470,29 +457,20 @@ pub trait Deserializer {
_name: &'static str,
_fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_map(visitor)
}
where V: Visitor;
/// This method hints that the `Deserialize` type is expecting some sort of struct field
/// name. This allows deserializers to choose between &str, usize, or &[u8] to properly
/// deserialize a struct field.
#[inline]
fn deserialize_struct_field<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize(visitor)
}
where V: 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 deserialize_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.deserialize_seq(visitor)
}
where V: 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
@ -502,19 +480,13 @@ pub trait Deserializer {
_enum: &'static str,
_variants: &'static [&'static str],
_visitor: V) -> Result<V::Value, Self::Error>
where V: EnumVisitor,
{
Err(Error::invalid_type(Type::Enum))
}
where V: EnumVisitor;
/// This method hints that the `Deserialize` type needs to deserialize a value whose type
/// doesn't matter because it is ignored.
#[inline]
fn deserialize_ignored_any<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor
{
self.deserialize(visitor)
}
where V: Visitor;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -174,6 +174,22 @@ impl<E> de::Deserializer for UnitDeserializer<E>
{
type Error = E;
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct
}
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
@ -209,6 +225,23 @@ macro_rules! primitive_deserializer {
{
type Error = E;
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor,
{
@ -273,6 +306,22 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E>
{
visitor.visit(self)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
impl<'a, E> de::VariantVisitor for StrDeserializer<'a, E>
@ -331,6 +380,22 @@ impl<E> de::Deserializer for StringDeserializer<E>
{
visitor.visit(self)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
#[cfg(any(feature = "std", feature = "collections"))]
@ -384,6 +449,23 @@ impl<I, T, E> de::Deserializer for SeqDeserializer<I, E>
{
visitor.visit_seq(self)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
impl<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
@ -490,6 +572,23 @@ impl<V_, E> de::Deserializer for SeqVisitorDeserializer<V_, E>
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_seq(&mut self.visitor)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
///////////////////////////////////////////////////////////////////////////////
@ -537,6 +636,23 @@ impl<I, K, V, E> de::Deserializer for MapDeserializer<I, K, V, E>
{
visitor.visit_map(self)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
impl<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
@ -648,6 +764,23 @@ impl<V_, E> de::Deserializer for MapVisitorDeserializer<V_, E>
fn deserialize<V: de::Visitor>(&mut self, mut visitor: V) -> Result<V::Value, Self::Error> {
visitor.visit_map(&mut self.visitor)
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
///////////////////////////////////////////////////////////////////////////////
@ -678,6 +811,23 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E>
None => Err(de::Error::end_of_stream()),
}
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
@ -712,4 +862,21 @@ impl<E> de::Deserializer for ByteBufDeserializer<E>
None => Err(de::Error::end_of_stream()),
}
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}

View File

@ -110,6 +110,20 @@ impl<I> de::Deserializer for Deserializer<I>
{
type Error = Error;
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_bytes,
deserialize_unit,
deserialize_seq,
deserialize_map,
deserialize_struct_field
}
fn deserialize<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{

View File

@ -1,3 +1,4 @@
#[macro_use]
extern crate serde;
mod assert;

View File

@ -1,3 +1,6 @@
#[macro_use]
extern crate serde;
#[macro_use]
mod macros;

View File

@ -306,6 +306,22 @@ impl serde::Deserializer for BytesDeserializer {
{
visitor.visit_byte_buf(self.bytes.take().unwrap())
}
de_forward_to_deserialize!{
deserialize_bool,
deserialize_f64, deserialize_f32,
deserialize_u8, deserialize_u16, deserialize_u32, deserialize_u64, deserialize_usize,
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64, deserialize_isize,
deserialize_char, deserialize_str, deserialize_string,
deserialize_ignored_any,
deserialize_unit_struct, deserialize_unit,
deserialize_seq, deserialize_fixed_size_array,
deserialize_map, deserialize_newtype_struct, deserialize_struct_field,
deserialize_tuple,
deserialize_enum,
deserialize_struct, deserialize_tuple_struct,
deserialize_option
}
}
///////////////////////////////////////////////////////////////////////////////