2016-06-28 23:47:54 -05:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
use serde::{ser, Serialize};
|
2016-06-28 23:47:54 -05:00
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
use token::Token;
|
|
|
|
|
|
|
|
pub struct Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
tokens: I,
|
|
|
|
phantom: PhantomData<&'a Token<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, I> Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
pub fn new(tokens: I) -> Serializer<'a, I> {
|
|
|
|
Serializer {
|
|
|
|
tokens: tokens,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_token(&mut self) -> Option<&'a Token<'a>> {
|
|
|
|
self.tokens.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
impl<'s, 'a, I> ser::Serializer for &'s mut Serializer<'a, I>
|
2016-06-28 23:47:54 -05:00
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
2016-06-28 23:47:54 -05:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
type SerializeSeq = Self;
|
|
|
|
type SerializeTuple = Self;
|
|
|
|
type SerializeTupleStruct = Self;
|
|
|
|
type SerializeTupleVariant = Self;
|
|
|
|
type SerializeMap = Self;
|
|
|
|
type SerializeStruct = Self;
|
|
|
|
type SerializeStructVariant = Self;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Bool(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::I8(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::I16(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::I32(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::I64(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::U8(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::U16(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::U32(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::U64(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::F32(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::F64(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_char(self, v: char) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Char(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Str(v)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Bytes(value)));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_unit(self) -> Result<(), Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(self, name: &str) -> Result<(), Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::UnitStruct(name)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized>(self,
|
|
|
|
name: &'static str,
|
|
|
|
value: &T) -> Result<(), Error>
|
2017-01-14 17:36:51 -06:00
|
|
|
where T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::StructNewType(name)));
|
2016-06-28 23:47:54 -05:00
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_newtype_variant<T: ?Sized>(self,
|
|
|
|
name: &str,
|
|
|
|
_variant_index: usize,
|
|
|
|
variant: &str,
|
|
|
|
value: &T) -> Result<(), Error>
|
2017-01-14 17:36:51 -06:00
|
|
|
where T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumNewType(name, variant)));
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<(), Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Option(false)));
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
|
|
|
where T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::Option(true)));
|
2016-06-28 23:47:54 -05:00
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
|
|
|
Ok(self)
|
2016-07-12 08:44:19 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_seq_fixed_size(self, len: usize) -> Result<Self, Error> {
|
2016-07-12 08:44:19 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
2016-07-12 08:44:19 -05:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
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<Self, Error>
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
|
|
|
Ok(self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
|
|
|
Ok(self)
|
2016-07-12 08:44:19 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_struct(self, name: &str, len: usize) -> Result<Self, Error> {
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_variant(self,
|
|
|
|
name: &str,
|
|
|
|
_variant_index: usize,
|
|
|
|
variant: &str,
|
|
|
|
len: usize) -> Result<Self, Error>
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
|
|
|
Ok(self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'a, I> ser::SerializeSeq for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-01-14 17:36:51 -06:00
|
|
|
where T: Serialize
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::SeqSep));
|
|
|
|
value.serialize(&mut **self)
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'a, I> ser::SerializeTuple for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2016-07-12 08:44:19 -05:00
|
|
|
where T: Serialize
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::TupleSep));
|
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
impl<'s, 'a, I> ser::SerializeTupleStruct for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-01-14 17:36:51 -06:00
|
|
|
where T: Serialize
|
2016-07-12 08:44:19 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStructSep));
|
|
|
|
value.serialize(&mut **self)
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-01-14 17:36:51 -06:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'a, I> ser::SerializeTupleVariant for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2016-07-12 08:44:19 -05:00
|
|
|
where T: Serialize
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqSep));
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2016-07-12 08:44:19 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
impl<'s, 'a, I> ser::SerializeMap for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> where T: Serialize {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::MapSep));
|
2017-01-14 17:36:51 -06:00
|
|
|
key.serialize(&mut **self)
|
2016-07-27 14:12:07 -05:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> where T: Serialize {
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2016-07-12 08:44:19 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
impl<'s, 'a, I> ser::SerializeStruct for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: Serialize {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::StructSep));
|
2017-01-14 17:36:51 -06:00
|
|
|
try!(key.serialize(&mut **self));
|
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2016-07-12 08:44:19 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
impl<'s, 'a, I> ser::SerializeStructVariant for &'s mut Serializer<'a, I>
|
|
|
|
where I: Iterator<Item=&'a Token<'a>>,
|
|
|
|
{
|
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> where T: Serialize {
|
2016-06-28 23:47:54 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapSep));
|
2017-01-14 17:36:51 -06:00
|
|
|
try!(key.serialize(&mut **self));
|
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2016-07-12 08:44:19 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2016-07-12 08:44:19 -05:00
|
|
|
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|