Update serde_test for Serializer changes
This commit is contained in:
parent
2fea5f3e1c
commit
04f25eb122
@ -1,9 +1,6 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use serde::ser::{
|
||||
self,
|
||||
Serialize,
|
||||
};
|
||||
use serde::{ser, Serialize};
|
||||
|
||||
use error::Error;
|
||||
use token::Token;
|
||||
@ -30,178 +27,119 @@ impl<'a, I> Serializer<'a, I>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
type MapState = ();
|
||||
type SeqState = ();
|
||||
type TupleState = ();
|
||||
type TupleStructState = ();
|
||||
type TupleVariantState = ();
|
||||
type StructState = ();
|
||||
type StructVariantState = ();
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
Ok(())
|
||||
}
|
||||
type SerializeSeq = Self;
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Self;
|
||||
type SerializeMap = Self;
|
||||
type SerializeStruct = Self;
|
||||
type SerializeStructVariant = Self;
|
||||
|
||||
fn serialize_newtype_variant<T>(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
value: T) -> Result<(), Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(&mut self, name: &str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(&mut self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_isize(&mut self, v: isize) -> Result<(), Error> {
|
||||
fn serialize_isize(self, v: isize) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Isize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
||||
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
||||
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
||||
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
||||
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_usize(&mut self, v: usize) -> Result<(), Error> {
|
||||
fn serialize_usize(self, v: usize) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Usize(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
||||
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
||||
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
||||
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
||||
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
||||
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
||||
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
||||
fn serialize_char(self, v: char) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
||||
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_none(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
||||
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Bytes(value)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_some<V>(&mut self, value: V) -> Result<(), Error>
|
||||
where V: Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq<'b>(&'b mut self, len: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
||||
fn serialize_unit(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq_end(&mut self, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||
fn serialize_unit_struct(self, name: &str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
||||
fn serialize_unit_variant(self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple(&mut self, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T>(&mut self,
|
||||
fn serialize_newtype_struct<T>(self,
|
||||
name: &'static str,
|
||||
value: T) -> Result<(), Error>
|
||||
where T: Serialize,
|
||||
@ -210,114 +148,209 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(&mut self, name: &'static str, len: usize) -> Result<(), Error>
|
||||
fn serialize_newtype_variant<T>(self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
value: T) -> Result<(), Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_end(&mut self, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
||||
fn serialize_none(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(&mut self,
|
||||
fn serialize_some<V>(self, value: V) -> Result<(), Error>
|
||||
where V: Serialize,
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(self, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
len: usize) -> Result<(), Error>
|
||||
len: usize) -> Result<Self, Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
||||
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant_elt<T>(&mut self, _: &mut (), value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant_end(&mut self, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
||||
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_map_key<T>(&mut self, _: &mut (), key: T) -> Result<(), Self::Error> where T: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||
key.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_map_value<T>(&mut self, _: &mut (), value: T) -> Result<(), Self::Error> where T: Serialize {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_map_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_struct(&mut self, name: &str, len: usize) -> Result<(), Error>
|
||||
{
|
||||
fn serialize_struct(self, name: &str, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
||||
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_elt<V>(&mut self, _: &mut (), key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
||||
try!(key.serialize(self));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(&mut self,
|
||||
fn serialize_struct_variant(self,
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
len: usize) -> Result<(), Error>
|
||||
len: usize) -> Result<Self, Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elem<T>(&mut self, value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_elt<V>(&mut self, _: &mut (), key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
||||
try!(key.serialize(self));
|
||||
value.serialize(self)
|
||||
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elem<T>(&mut self, value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_end(&mut self, _: ()) -> Result<(), Self::Error> {
|
||||
fn serialize_end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elem<T>(&mut self, value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elem<T>(&mut self, value: T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T>(&mut self, key: T) -> Result<(), Self::Error> where T: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||
key.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_value<T>(&mut self, value: T) -> Result<(), Self::Error> where T: Serialize {
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<V>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<V>(&mut self, key: &'static str, value: V) -> Result<(), Self::Error> where V: Serialize {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
|
||||
let mut state = try!(self.serialize_seq(Some(value.len())));
|
||||
for c in value {
|
||||
try!(self.serialize_seq_elt(&mut state, c));
|
||||
}
|
||||
self.serialize_seq_end(state)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user