Require slice of tokens in serde_test::Serializer
This commit is contained in:
parent
b43554fbb1
commit
93860d0643
@ -19,7 +19,7 @@ pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
where T: Serialize
|
||||
{
|
||||
let mut ser = Serializer::new(tokens.iter());
|
||||
let mut ser = Serializer::new(tokens);
|
||||
assert_eq!(Serialize::serialize(value, &mut ser), Ok(()));
|
||||
assert_eq!(ser.next_token(), None);
|
||||
}
|
||||
@ -28,7 +28,7 @@ pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
|
||||
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: Error)
|
||||
where T: Serialize + PartialEq + Debug
|
||||
{
|
||||
let mut ser = Serializer::new(tokens.iter());
|
||||
let mut ser = Serializer::new(tokens);
|
||||
let v: Result<(), Error> = Serialize::serialize(value, &mut ser);
|
||||
assert_eq!(v.as_ref(), Err(&error));
|
||||
assert_eq!(ser.next_token(), None);
|
||||
|
@ -1,38 +1,35 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use serde::{ser, Serialize};
|
||||
|
||||
use error::Error;
|
||||
use token::Token;
|
||||
|
||||
/// A `Serializer` that ensures that a value serializes to a given list of tokens.
|
||||
pub struct Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
tokens: I,
|
||||
phantom: PhantomData<&'a Token>,
|
||||
pub struct Serializer<'a> {
|
||||
tokens: &'a [Token],
|
||||
}
|
||||
|
||||
impl<'a, I> Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'a> Serializer<'a> {
|
||||
/// Creates the serializer.
|
||||
pub fn new(tokens: I) -> Serializer<'a, I> {
|
||||
pub fn new(tokens: &'a [Token]) -> Self {
|
||||
Serializer {
|
||||
tokens: tokens,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pulls the next token off of the serializer, ignoring it.
|
||||
pub fn next_token(&mut self) -> Option<&'a Token> {
|
||||
self.tokens.next()
|
||||
pub fn next_token(&mut self) -> Option<&Token> {
|
||||
if let Some((first, rest)) = self.tokens.split_first() {
|
||||
self.tokens = rest;
|
||||
Some(first)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_next_token {
|
||||
($self:ident, $expected:ident($arg:expr)) => {
|
||||
match $self.tokens.next() {
|
||||
match $self.next_token() {
|
||||
Some(&Token::$expected(v)) if v == $arg => {}
|
||||
Some(other) => {
|
||||
panic!("expected {}({:?}) but serialized as {:?}", stringify!($expected), $arg, other);
|
||||
@ -44,9 +41,7 @@ macro_rules! assert_next_token {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -59,62 +54,62 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
type SerializeStructVariant = Self;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::Bool(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::I8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::I16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::I32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::I64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::U8(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::U16(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::U32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::U64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::F32(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::F64(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
||||
assert_eq!(self.next_token(), Some(&Token::Char(v)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -129,12 +124,12 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
assert_eq!(self.next_token(), Some(&Token::Unit));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
||||
assert_eq!(self.next_token(), Some(&Token::UnitStruct(name)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -143,14 +138,14 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
_variant_index: usize,
|
||||
variant: &'static str)
|
||||
-> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumUnit(name, variant)));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumUnit(name, variant)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name)));
|
||||
assert_eq!(self.next_token(), Some(&Token::StructNewType(name)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
@ -162,39 +157,39 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
-> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumNewType(name, variant)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
||||
assert_eq!(self.next_token(), Some(&Token::Option(false)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
||||
assert_eq!(self.next_token(), 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)));
|
||||
assert_eq!(self.next_token(), 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)));
|
||||
assert_eq!(self.next_token(), Some(&Token::SeqArrayStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
||||
assert_eq!(self.next_token(), Some(&Token::TupleStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(),
|
||||
assert_eq!(self.next_token(),
|
||||
Some(&Token::TupleStructStart(name, len)));
|
||||
Ok(self)
|
||||
}
|
||||
@ -205,18 +200,18 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(),
|
||||
assert_eq!(self.next_token(),
|
||||
Some(&Token::EnumSeqStart(name, variant, len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
||||
assert_eq!(self.next_token(), Some(&Token::MapStart(len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
||||
assert_eq!(self.next_token(), Some(&Token::StructStart(name, len)));
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
@ -226,98 +221,88 @@ impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
||||
variant: &'static str,
|
||||
len: usize)
|
||||
-> Result<Self, Error> {
|
||||
assert_eq!(self.tokens.next(),
|
||||
assert_eq!(self.next_token(),
|
||||
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>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::SeqSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::SeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeTuple for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::TupleSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::TupleEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::TupleStructSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::TupleStructEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeTupleVariant for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumSeqSep));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumSeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeMap for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::MapSep));
|
||||
key.serialize(&mut **self)
|
||||
}
|
||||
|
||||
@ -328,14 +313,12 @@ impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::MapEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -345,20 +328,18 @@ impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
||||
-> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::StructSep));
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::StructEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
||||
where I: Iterator<Item = &'a Token>
|
||||
{
|
||||
impl<'s, 'a> ser::SerializeStructVariant for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -368,13 +349,13 @@ impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
||||
-> Result<(), Self::Error>
|
||||
where T: Serialize
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumMapSep));
|
||||
try!(key.serialize(&mut **self));
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
||||
assert_eq!(self.next_token(), Some(&Token::EnumMapEnd));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user