2014-06-08 17:17:18 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use std::collections::{HashMap, TreeMap};
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub trait Serializer<E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
fn serialize_null(&mut self) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_bool(&mut self, v: bool) -> Result<(), E>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_int(&mut self, v: int) -> Result<(), E> {
|
|
|
|
self.serialize_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_i8(&mut self, v: i8) -> Result<(), E> {
|
|
|
|
self.serialize_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_i16(&mut self, v: i16) -> Result<(), E> {
|
|
|
|
self.serialize_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_i32(&mut self, v: i32) -> Result<(), E> {
|
|
|
|
self.serialize_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_i64(&mut self, v: i64) -> Result<(), E>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_uint(&mut self, v: uint) -> Result<(), E> {
|
|
|
|
self.serialize_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_u8(&mut self, v: u8) -> Result<(), E> {
|
|
|
|
self.serialize_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_u16(&mut self, v: u16) -> Result<(), E> {
|
|
|
|
self.serialize_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_u32(&mut self, v: u32) -> Result<(), E> {
|
|
|
|
self.serialize_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_u64(&mut self, v: u64) -> Result<(), E>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn serialize_f32(&mut self, v: f32) -> Result<(), E> {
|
|
|
|
self.serialize_f64(v as f64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f64(&mut self, v: f64) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_char(&mut self, v: char) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_str(&mut self, v: &str) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>;
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_tuple_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
|
2014-06-20 18:32:30 -05:00
|
|
|
fn serialize_tuple_end(&mut self) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>;
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_struct_sep<T: Serializable>(&mut self, name: &str, v: &T) -> Result<(), E>;
|
2014-06-20 18:32:30 -05:00
|
|
|
fn serialize_struct_end(&mut self) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>;
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_enum_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
|
2014-06-20 18:32:30 -05:00
|
|
|
fn serialize_enum_end(&mut self) -> Result<(), E>;
|
|
|
|
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize_option<T: Serializable>(&mut self, v: &Option<T>) -> Result<(), E>;
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
fn serialize_seq<
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable,
|
2014-06-20 18:32:30 -05:00
|
|
|
Iter: Iterator<T>
|
|
|
|
>(&mut self, iter: Iter) -> Result<(), E>;
|
|
|
|
|
|
|
|
fn serialize_map<
|
2014-06-22 20:54:58 -05:00
|
|
|
K: Serializable,
|
|
|
|
V: Serializable,
|
2014-06-20 18:32:30 -05:00
|
|
|
Iter: Iterator<(K, V)>
|
|
|
|
>(&mut self, iter: Iter) -> Result<(), E>;
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-22 20:54:58 -05:00
|
|
|
pub trait Serializable {
|
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E>;
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
macro_rules! impl_serializable {
|
2014-06-20 18:32:30 -05:00
|
|
|
($ty:ty, $method:ident, $variant:expr) => {
|
2014-06-22 20:54:58 -05:00
|
|
|
impl<'a> Serializable for $ty {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.$method($variant)
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
impl_serializable!(bool, serialize_bool, *self)
|
|
|
|
impl_serializable!(int, serialize_int, *self)
|
|
|
|
impl_serializable!(i8, serialize_i8, *self)
|
|
|
|
impl_serializable!(i16, serialize_i16, *self)
|
|
|
|
impl_serializable!(i32, serialize_i32, *self)
|
|
|
|
impl_serializable!(i64, serialize_i64, *self)
|
|
|
|
impl_serializable!(uint, serialize_uint, *self)
|
|
|
|
impl_serializable!(u8, serialize_u8, *self)
|
|
|
|
impl_serializable!(u16, serialize_u16, *self)
|
|
|
|
impl_serializable!(u32, serialize_u32, *self)
|
|
|
|
impl_serializable!(u64, serialize_u64, *self)
|
|
|
|
impl_serializable!(f32, serialize_f32, *self)
|
|
|
|
impl_serializable!(f64, serialize_f64, *self)
|
|
|
|
impl_serializable!(char, serialize_char, *self)
|
|
|
|
impl_serializable!(&'a str, serialize_str, *self)
|
|
|
|
impl_serializable!(String, serialize_str, self.as_slice())
|
|
|
|
|
|
|
|
impl<
|
|
|
|
'a,
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable
|
|
|
|
> Serializable for &'a T {
|
2014-06-20 18:32:30 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
(*self).serialize(s)
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 17:17:18 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable
|
|
|
|
> Serializable for Option<T> {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_option(self)
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable
|
|
|
|
> Serializable for Vec<T> {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_seq(self.iter())
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<
|
2014-06-22 20:54:58 -05:00
|
|
|
K: Serializable + Eq + Hash,
|
|
|
|
V: Serializable
|
|
|
|
> Serializable for HashMap<K, V> {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_map(self.iter())
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-06-22 20:54:58 -05:00
|
|
|
K: Serializable + Ord,
|
|
|
|
V: Serializable
|
|
|
|
> Serializable for TreeMap<K, V> {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_map(self.iter())
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
macro_rules! peel {
|
|
|
|
($name:ident, $($other:ident,)*) => (impl_serialize_tuple!($($other,)*))
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_serialize_tuple {
|
|
|
|
() => {
|
2014-06-22 20:54:58 -05:00
|
|
|
impl Serializable for () {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_null()
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
( $($name:ident,)+ ) => {
|
|
|
|
impl<
|
2014-06-22 20:54:58 -05:00
|
|
|
$($name:Serializable),*
|
|
|
|
> Serializable for ($($name,)*) {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
|
|
|
#[allow(uppercase_variables)]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-08 17:17:18 -05:00
|
|
|
// FIXME: how can we count macro args?
|
|
|
|
let mut len = 0;
|
|
|
|
$({ let $name = 1; len += $name; })*;
|
|
|
|
|
|
|
|
let ($(ref $name,)*) = *self;
|
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
try!(s.serialize_tuple_start(len));
|
2014-06-22 09:33:45 -05:00
|
|
|
$(
|
2014-06-23 20:14:23 -05:00
|
|
|
try!(s.serialize_tuple_sep($name));
|
2014-06-22 09:33:45 -05:00
|
|
|
)*
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_tuple_end()
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
peel!($($name,)*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_serialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-06-09 23:52:10 -05:00
|
|
|
use std::collections::{HashMap, TreeMap};
|
2014-06-08 17:17:18 -05:00
|
|
|
|
|
|
|
use serialize::Decoder;
|
|
|
|
|
|
|
|
use super::{Serializer, Serializable};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: uint,
|
|
|
|
c: HashMap<String, Option<char>>,
|
|
|
|
}
|
|
|
|
|
2014-06-22 20:54:58 -05:00
|
|
|
impl Serializable for Inner {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
try!(s.serialize_struct_start("Inner", 3));
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
try!(s.serialize_struct_sep("a", &self.a));
|
|
|
|
try!(s.serialize_struct_sep("b", &self.b));
|
|
|
|
try!(s.serialize_struct_sep("c", &self.c));
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
s.serialize_struct_end()
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
|
|
|
struct Outer {
|
|
|
|
inner: Vec<Inner>,
|
|
|
|
}
|
|
|
|
|
2014-06-22 20:54:58 -05:00
|
|
|
impl Serializable for Outer {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-20 18:32:30 -05:00
|
|
|
try!(s.serialize_struct_start("Outer", 1));
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
try!(s.serialize_struct_sep("inner", &self.inner));
|
2014-06-20 18:32:30 -05:00
|
|
|
|
2014-06-22 09:33:45 -05:00
|
|
|
s.serialize_struct_end()
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
|
|
|
enum Animal {
|
|
|
|
Dog,
|
|
|
|
Frog(String, int)
|
|
|
|
}
|
|
|
|
|
2014-06-22 20:54:58 -05:00
|
|
|
impl Serializable for Animal {
|
2014-06-08 17:17:18 -05:00
|
|
|
#[inline]
|
2014-06-22 20:54:58 -05:00
|
|
|
fn serialize<
|
|
|
|
S: Serializer<E>,
|
|
|
|
E
|
|
|
|
>(&self, s: &mut S) -> Result<(), E> {
|
2014-06-08 17:17:18 -05:00
|
|
|
match *self {
|
|
|
|
Dog => {
|
2014-06-20 18:32:30 -05:00
|
|
|
try!(s.serialize_enum_start("Animal", "Dog", 0));
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
2014-06-23 20:14:23 -05:00
|
|
|
Frog(ref x0, ref x1) => {
|
2014-06-20 18:32:30 -05:00
|
|
|
try!(s.serialize_enum_start("Animal", "Frog", 2));
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
try!(s.serialize_enum_sep(x0));
|
|
|
|
try!(s.serialize_enum_sep(x1));
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 18:32:30 -05:00
|
|
|
s.serialize_enum_end()
|
2014-06-08 17:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
|
|
|
pub enum Token<'a> {
|
|
|
|
Null,
|
|
|
|
Bool(bool),
|
|
|
|
Int(int),
|
|
|
|
I8(i8),
|
|
|
|
I16(i16),
|
|
|
|
I32(i32),
|
|
|
|
I64(i64),
|
|
|
|
Uint(uint),
|
|
|
|
U8(u8),
|
|
|
|
U16(u16),
|
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
|
|
|
F32(f32),
|
|
|
|
F64(f64),
|
|
|
|
Char(char),
|
|
|
|
Str(&'a str),
|
|
|
|
|
|
|
|
TupleStart(uint),
|
|
|
|
TupleSep,
|
|
|
|
TupleEnd,
|
|
|
|
|
|
|
|
StructStart(&'a str, uint),
|
|
|
|
StructSep(&'a str),
|
|
|
|
StructEnd,
|
|
|
|
|
|
|
|
EnumStart(&'a str, &'a str, uint),
|
|
|
|
EnumSep,
|
|
|
|
EnumEnd,
|
|
|
|
|
|
|
|
Option(bool),
|
|
|
|
|
|
|
|
SeqStart(uint),
|
|
|
|
SeqEnd,
|
|
|
|
|
|
|
|
MapStart(uint),
|
|
|
|
MapEnd,
|
|
|
|
}
|
|
|
|
|
2014-06-08 17:17:18 -05:00
|
|
|
#[deriving(Show)]
|
|
|
|
enum Error {
|
|
|
|
EndOfStream,
|
|
|
|
SyntaxError,
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct AssertSerializer<Iter> {
|
|
|
|
iter: Iter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, Iter: Iterator<Token<'a>>> AssertSerializer<Iter> {
|
|
|
|
fn new(iter: Iter) -> AssertSerializer<Iter> {
|
|
|
|
AssertSerializer {
|
|
|
|
iter: iter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize<'b>(&mut self, token: Token<'b>) -> Result<(), Error> {
|
|
|
|
let t = match self.iter.next() {
|
|
|
|
Some(t) => t,
|
|
|
|
None => { fail!(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(t, token);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
impl<'a, Iter: Iterator<Token<'a>>> Serializer<Error> for AssertSerializer<Iter> {
|
|
|
|
fn serialize_null(&mut self) -> Result<(), Error> {
|
|
|
|
self.serialize(Null)
|
|
|
|
}
|
|
|
|
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
|
|
|
self.serialize(Bool(v))
|
|
|
|
}
|
|
|
|
fn serialize_int(&mut self, v: int) -> Result<(), Error> {
|
|
|
|
self.serialize(Int(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
|
|
|
self.serialize(I8(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
|
|
|
self.serialize(I16(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
|
|
|
self.serialize(I32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
|
|
|
self.serialize(I64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_uint(&mut self, v: uint) -> Result<(), Error> {
|
|
|
|
self.serialize(Uint(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
|
|
|
self.serialize(U8(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
|
|
|
self.serialize(U16(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
|
|
|
self.serialize(U32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
|
|
|
self.serialize(U64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
|
|
|
self.serialize(F32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
|
|
|
self.serialize(F64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
|
|
|
self.serialize(Char(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
|
|
|
self.serialize(Str(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), Error> {
|
|
|
|
self.serialize(TupleStart(len))
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_tuple_sep<
|
|
|
|
T: Serializable
|
|
|
|
>(&mut self, value: &T) -> Result<(), Error> {
|
|
|
|
try!(self.serialize(TupleSep));
|
|
|
|
value.serialize(self)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_end(&mut self) -> Result<(), Error> {
|
|
|
|
self.serialize(TupleEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), Error> {
|
|
|
|
self.serialize(StructStart(name, len))
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_struct_sep<
|
|
|
|
T: Serializable
|
|
|
|
>(&mut self, name: &str, value: &T) -> Result<(), Error> {
|
|
|
|
try!(self.serialize(StructSep(name)));
|
|
|
|
value.serialize(self)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_end(&mut self) -> Result<(), Error> {
|
|
|
|
self.serialize(StructEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), Error> {
|
|
|
|
self.serialize(EnumStart(name, variant, len))
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:14:23 -05:00
|
|
|
fn serialize_enum_sep<
|
|
|
|
T: Serializable
|
|
|
|
>(&mut self, value: &T) -> Result<(), Error> {
|
|
|
|
try!(self.serialize(EnumSep));
|
|
|
|
value.serialize(self)
|
2014-06-20 18:32:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_enum_end(&mut self) -> Result<(), Error> {
|
|
|
|
self.serialize(EnumEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_option<
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable
|
2014-06-20 18:32:30 -05:00
|
|
|
>(&mut self, v: &Option<T>) -> Result<(), Error> {
|
|
|
|
match *v {
|
|
|
|
Some(ref v) => {
|
|
|
|
try!(self.serialize(Option(true)));
|
|
|
|
v.serialize(self)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.serialize(Option(false))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_seq<
|
2014-06-22 20:54:58 -05:00
|
|
|
T: Serializable,
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqIter: Iterator<T>
|
|
|
|
>(&mut self, mut iter: SeqIter) -> Result<(), Error> {
|
|
|
|
let (len, _) = iter.size_hint();
|
|
|
|
try!(self.serialize(SeqStart(len)));
|
|
|
|
for elt in iter {
|
|
|
|
elt.serialize(self).unwrap();
|
|
|
|
}
|
|
|
|
self.serialize(SeqEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map<
|
2014-06-22 20:54:58 -05:00
|
|
|
K: Serializable,
|
|
|
|
V: Serializable,
|
2014-06-20 18:32:30 -05:00
|
|
|
MapIter: Iterator<(K, V)>
|
|
|
|
>(&mut self, mut iter: MapIter) -> Result<(), Error> {
|
|
|
|
let (len, _) = iter.size_hint();
|
|
|
|
try!(self.serialize(MapStart(len)));
|
|
|
|
for (key, value) in iter {
|
|
|
|
try!(key.serialize(self));
|
|
|
|
try!(value.serialize(self));
|
|
|
|
}
|
|
|
|
self.serialize(MapEnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 17:17:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_int() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Int(5)
|
|
|
|
);
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
5i.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_str() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Str("a"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
"a".serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_null() {
|
|
|
|
let tokens = vec!(
|
2014-06-14 12:48:01 -05:00
|
|
|
Null,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
().serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_option_none() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Option(false),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
None::<int>.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_option_some() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Option(true),
|
|
|
|
Int(5),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
2014-06-27 10:07:03 -05:00
|
|
|
Some(5i).serialize(&mut serializer).unwrap();
|
2014-06-08 17:17:18 -05:00
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_tuple() {
|
|
|
|
let tokens = vec!(
|
|
|
|
TupleStart(2),
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Int(5),
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
TupleSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Str("a"),
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
2014-06-27 10:07:03 -05:00
|
|
|
(5i, "a").serialize(&mut serializer).unwrap();
|
2014-06-08 17:17:18 -05:00
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_tuple_compound() {
|
|
|
|
let tokens = vec!(
|
|
|
|
TupleStart(3),
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleSep,
|
2014-06-14 12:48:01 -05:00
|
|
|
Null,
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
TupleSep,
|
2014-06-14 12:48:01 -05:00
|
|
|
Null,
|
2014-06-08 17:17:18 -05:00
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
TupleStart(2),
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Int(5),
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
TupleSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Str("a"),
|
2014-06-20 18:32:30 -05:00
|
|
|
TupleEnd,
|
|
|
|
TupleEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
2014-06-27 10:07:03 -05:00
|
|
|
((), (), (5i, "a")).serialize(&mut serializer).unwrap();
|
2014-06-08 17:17:18 -05:00
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_struct_empty() {
|
|
|
|
let tokens = vec!(
|
|
|
|
StructStart("Outer", 1),
|
2014-06-20 18:32:30 -05:00
|
|
|
StructSep("inner"),
|
2014-06-08 17:17:18 -05:00
|
|
|
SeqStart(0),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
|
|
|
StructEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
Outer { inner: vec!() }.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_struct() {
|
|
|
|
let tokens = vec!(
|
|
|
|
StructStart("Outer", 1),
|
2014-06-20 18:32:30 -05:00
|
|
|
StructSep("inner"),
|
2014-06-08 17:17:18 -05:00
|
|
|
SeqStart(1),
|
|
|
|
StructStart("Inner", 3),
|
2014-06-20 18:32:30 -05:00
|
|
|
StructSep("a"),
|
2014-06-14 12:48:01 -05:00
|
|
|
Null,
|
2014-06-08 17:17:18 -05:00
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
StructSep("b"),
|
2014-06-08 17:17:18 -05:00
|
|
|
Uint(5),
|
|
|
|
|
2014-06-20 18:32:30 -05:00
|
|
|
StructSep("c"),
|
2014-06-08 17:17:18 -05:00
|
|
|
MapStart(1),
|
|
|
|
Str("abc"),
|
|
|
|
Option(true),
|
|
|
|
Char('c'),
|
2014-06-20 18:32:30 -05:00
|
|
|
MapEnd,
|
|
|
|
StructEnd,
|
2014-06-22 09:33:45 -05:00
|
|
|
SeqEnd,
|
2014-06-20 18:32:30 -05:00
|
|
|
StructEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert("abc".to_string(), Some('c'));
|
|
|
|
|
|
|
|
Outer {
|
|
|
|
inner: vec!(
|
|
|
|
Inner {
|
|
|
|
a: (),
|
|
|
|
b: 5,
|
|
|
|
c: map,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_enum() {
|
|
|
|
let tokens = vec!(
|
|
|
|
EnumStart("Animal", "Dog", 0),
|
2014-06-20 18:32:30 -05:00
|
|
|
EnumEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
Dog.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
|
|
|
|
let tokens = vec!(
|
|
|
|
EnumStart("Animal", "Frog", 2),
|
2014-06-20 18:32:30 -05:00
|
|
|
EnumSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Str("Henry"),
|
2014-06-20 18:32:30 -05:00
|
|
|
|
|
|
|
EnumSep,
|
2014-06-08 17:17:18 -05:00
|
|
|
Int(349),
|
2014-06-20 18:32:30 -05:00
|
|
|
EnumEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
Frog("Henry".to_string(), 349).serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec_empty() {
|
|
|
|
let tokens = vec!(
|
|
|
|
SeqStart(0),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
let v: Vec<int> = vec!();
|
|
|
|
v.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec() {
|
|
|
|
let tokens = vec!(
|
|
|
|
SeqStart(3),
|
|
|
|
Int(5),
|
|
|
|
Int(6),
|
|
|
|
Int(7),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
2014-06-27 10:07:03 -05:00
|
|
|
(vec!(5i, 6, 7)).serialize(&mut serializer).unwrap();
|
2014-06-08 17:17:18 -05:00
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec_compound() {
|
|
|
|
let tokens = vec!(
|
|
|
|
SeqStart(3),
|
|
|
|
SeqStart(1),
|
|
|
|
Int(1),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
|
|
|
|
SeqStart(2),
|
|
|
|
Int(2),
|
|
|
|
Int(3),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
|
|
|
|
SeqStart(3),
|
|
|
|
Int(4),
|
|
|
|
Int(5),
|
|
|
|
Int(6),
|
2014-06-20 18:32:30 -05:00
|
|
|
SeqEnd,
|
|
|
|
SeqEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
2014-06-27 10:07:03 -05:00
|
|
|
(vec!(vec!(1i), vec!(2, 3), vec!(4, 5, 6))).serialize(&mut serializer).unwrap();
|
2014-06-08 17:17:18 -05:00
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-06-09 23:52:10 -05:00
|
|
|
fn test_tokens_treemap() {
|
2014-06-08 17:17:18 -05:00
|
|
|
let tokens = vec!(
|
|
|
|
MapStart(2),
|
|
|
|
Int(5),
|
|
|
|
Str("a"),
|
|
|
|
|
|
|
|
Int(6),
|
|
|
|
Str("b"),
|
2014-06-20 18:32:30 -05:00
|
|
|
MapEnd,
|
2014-06-08 17:17:18 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
|
|
|
|
2014-06-09 23:52:10 -05:00
|
|
|
let mut map = TreeMap::new();
|
2014-06-27 10:07:03 -05:00
|
|
|
map.insert(5i, "a".to_string());
|
|
|
|
map.insert(6i, "b".to_string());
|
2014-06-08 17:17:18 -05:00
|
|
|
|
|
|
|
map.serialize(&mut serializer).unwrap();
|
|
|
|
assert_eq!(serializer.iter.next(), None);
|
|
|
|
}
|
|
|
|
}
|