2014-05-16 09:15:02 -05:00
|
|
|
extern crate collections;
|
|
|
|
|
|
|
|
use std::hash::Hash;
|
2014-05-19 11:48:58 -05:00
|
|
|
use std::num;
|
2014-05-26 14:53:37 -05:00
|
|
|
use collections::{HashMap, TreeMap};
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-31 14:28:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
2014-05-16 09:15:02 -05:00
|
|
|
pub enum Token {
|
2014-05-16 23:21:05 -05:00
|
|
|
Null,
|
|
|
|
Bool(bool),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(int),
|
2014-05-19 09:18:35 -05:00
|
|
|
I8(i8),
|
|
|
|
I16(i16),
|
|
|
|
I32(i32),
|
|
|
|
I64(i64),
|
|
|
|
Uint(uint),
|
|
|
|
U8(u8),
|
|
|
|
U16(u16),
|
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
|
|
|
F32(f32),
|
2014-05-16 23:21:05 -05:00
|
|
|
F64(f64),
|
2014-05-19 09:18:35 -05:00
|
|
|
Char(char),
|
|
|
|
Str(&'static str),
|
2014-05-26 09:18:18 -05:00
|
|
|
String(String),
|
2014-05-19 21:41:38 -05:00
|
|
|
Option(bool),
|
2014-05-20 21:22:23 -05:00
|
|
|
|
|
|
|
TupleStart(uint),
|
2014-05-28 12:46:29 -05:00
|
|
|
StructStart(&'static str, uint),
|
|
|
|
EnumStart(&'static str, &'static str, uint),
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(uint),
|
|
|
|
MapStart(uint),
|
|
|
|
|
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:18:35 -05:00
|
|
|
macro_rules! to_result {
|
|
|
|
($expr:expr, $err:expr) => {
|
|
|
|
match $expr {
|
|
|
|
Some(value) => Ok(value),
|
2014-05-31 01:28:46 -05:00
|
|
|
None => $err,
|
2014-05-19 09:18:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
2014-05-31 01:28:46 -05:00
|
|
|
fn end_of_stream_error<T>(&self) -> Result<T, E>;
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-31 01:28:46 -05:00
|
|
|
fn syntax_error<T>(&self) -> Result<T, E>;
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-27 09:21:37 -05:00
|
|
|
#[inline]
|
|
|
|
fn expect_token(&mut self) -> Result<Token, E> {
|
|
|
|
match self.next() {
|
|
|
|
Some(Ok(token)) => Ok(token),
|
|
|
|
Some(Err(err)) => Err(err),
|
2014-05-31 01:28:46 -05:00
|
|
|
None => self.end_of_stream_error(),
|
2014-05-27 09:21:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 09:55:05 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn expect_null(&mut self, token: Token) -> Result<(), E> {
|
|
|
|
match token {
|
2014-05-19 09:18:35 -05:00
|
|
|
Null => Ok(()),
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(_) => {
|
2014-05-27 09:21:37 -05:00
|
|
|
match try!(self.expect_token()) {
|
|
|
|
End => Ok(()),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-20 21:22:23 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 21:41:38 -05:00
|
|
|
}
|
2014-05-16 23:21:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn expect_bool(&mut self, token: Token) -> Result<bool, E> {
|
|
|
|
match token {
|
|
|
|
Bool(value) => Ok(value),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 21:41:38 -05:00
|
|
|
}
|
2014-05-16 23:21:05 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn expect_num<T: NumCast>(&mut self, token: Token) -> Result<T, E> {
|
|
|
|
match token {
|
2014-05-19 11:48:58 -05:00
|
|
|
Int(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
I8(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
I16(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
I32(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
I64(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
Uint(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
U8(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
U16(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
U32(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
U64(x) => to_result!(num::cast(x), self.syntax_error()),
|
|
|
|
F32(x) => to_result!(num::cast(x), self.syntax_error()),
|
2014-05-24 00:30:53 -05:00
|
|
|
F64(x) => to_result!(num::cast(x), self.syntax_error()),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 11:48:58 -05:00
|
|
|
}
|
2014-05-16 23:21:05 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn expect_char(&mut self, token: Token) -> Result<char, E> {
|
|
|
|
match token {
|
|
|
|
Char(value) => Ok(value),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 21:41:38 -05:00
|
|
|
}
|
2014-05-19 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn expect_str(&mut self, token: Token) -> Result<&'static str, E> {
|
|
|
|
match token {
|
|
|
|
Str(value) => Ok(value),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 21:41:38 -05:00
|
|
|
}
|
2014-05-19 09:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-05-26 09:18:18 -05:00
|
|
|
fn expect_strbuf(&mut self, token: Token) -> Result<String, E> {
|
2014-05-24 16:08:35 -05:00
|
|
|
match token {
|
2014-05-29 08:44:38 -05:00
|
|
|
Str(value) => Ok(value.to_string()),
|
2014-05-26 09:18:18 -05:00
|
|
|
String(value) => Ok(value),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 21:41:38 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:55:05 -05:00
|
|
|
#[inline]
|
|
|
|
fn expect_option<
|
|
|
|
T: Deserializable<E, Self>
|
2014-05-25 23:59:58 -05:00
|
|
|
>(&mut self, token: Token) -> Result<Option<T>, E> {
|
|
|
|
match token {
|
2014-05-19 21:41:38 -05:00
|
|
|
Option(false) => Ok(None),
|
|
|
|
Option(true) => {
|
|
|
|
let value: T = try!(Deserializable::deserialize(self));
|
2014-05-19 11:47:16 -05:00
|
|
|
Ok(Some(value))
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-19 09:55:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn expect_tuple_start(&mut self, token: Token, len: uint) -> Result<(), E> {
|
|
|
|
match token {
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(l) => {
|
|
|
|
if len == l {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2014-05-31 01:28:46 -05:00
|
|
|
self.syntax_error()
|
2014-05-20 21:22:23 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-20 21:22:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn expect_struct_start(&mut self, token: Token, name: &str) -> Result<(), E> {
|
|
|
|
match token {
|
2014-05-28 12:46:29 -05:00
|
|
|
StructStart(n, _) => {
|
2014-05-21 10:25:28 -05:00
|
|
|
if name == n {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2014-05-31 01:28:46 -05:00
|
|
|
self.syntax_error()
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn expect_struct_field<
|
|
|
|
T: Deserializable<E, Self>
|
|
|
|
>(&mut self, name: &str) -> Result<T, E> {
|
2014-05-27 09:21:37 -05:00
|
|
|
match try!(self.expect_token()) {
|
2014-05-26 10:26:34 -05:00
|
|
|
Str(n) => {
|
|
|
|
if name != n {
|
2014-05-31 01:28:46 -05:00
|
|
|
return self.syntax_error();
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-26 10:26:34 -05:00
|
|
|
String(n) => {
|
|
|
|
if name != n.as_slice() {
|
2014-05-31 01:28:46 -05:00
|
|
|
return self.syntax_error();
|
2014-05-26 10:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => { return self.syntax_error(); }
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
2014-05-26 10:26:34 -05:00
|
|
|
|
|
|
|
Deserializable::deserialize(self)
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
|
2014-05-21 10:44:48 -05:00
|
|
|
#[inline]
|
2014-05-28 12:46:29 -05:00
|
|
|
fn expect_enum_start(&mut self, token: Token, name: &str, variants: &[&str]) -> Result<uint, E> {
|
2014-05-24 16:08:35 -05:00
|
|
|
match token {
|
2014-05-28 12:46:29 -05:00
|
|
|
EnumStart(n, v, _) => {
|
2014-05-21 10:44:48 -05:00
|
|
|
if name == n {
|
2014-05-24 16:11:25 -05:00
|
|
|
match variants.iter().position(|variant| *variant == v) {
|
|
|
|
Some(position) => Ok(position),
|
2014-05-31 01:28:46 -05:00
|
|
|
None => self.syntax_error(),
|
2014-05-21 10:44:48 -05:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-31 01:28:46 -05:00
|
|
|
self.syntax_error()
|
2014-05-21 10:44:48 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-21 10:44:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 00:08:37 -05:00
|
|
|
/*
|
2014-05-20 21:22:23 -05:00
|
|
|
#[inline]
|
2014-05-21 08:47:58 -05:00
|
|
|
fn expect_collection<
|
2014-05-16 09:15:02 -05:00
|
|
|
T: Deserializable<E, Self>,
|
|
|
|
C: FromIterator<T>
|
2014-05-24 00:30:53 -05:00
|
|
|
>(&mut self, token: Token) -> Result<C, E> {
|
2014-05-20 21:38:48 -05:00
|
|
|
// By default we don't care what our source input was. We can take
|
|
|
|
// anything that's a Collection<T>. We'll error out later if the types
|
|
|
|
// are wrong.
|
2014-05-24 00:30:53 -05:00
|
|
|
let len = match token {
|
2014-05-20 21:38:48 -05:00
|
|
|
TupleStart(len) => len,
|
|
|
|
SeqStart(len) => len,
|
2014-05-24 00:30:53 -05:00
|
|
|
MapStart(len) => len,
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => { return self.syntax_error(); }
|
2014-05-20 21:22:23 -05:00
|
|
|
};
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 08:47:58 -05:00
|
|
|
expect_rest_of_collection(self, len)
|
|
|
|
}
|
2014-05-26 00:08:37 -05:00
|
|
|
*/
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 08:47:58 -05:00
|
|
|
#[inline]
|
2014-05-25 23:36:56 -05:00
|
|
|
fn expect_seq_start(&mut self, token: Token) -> Result<uint, E> {
|
|
|
|
match token {
|
|
|
|
SeqStart(len) => Ok(len),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-25 23:36:56 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-05-26 00:08:37 -05:00
|
|
|
fn expect_map_start(&mut self, token: Token) -> Result<uint, E> {
|
|
|
|
match token {
|
|
|
|
MapStart(len) => Ok(len),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-26 00:08:37 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
2014-05-24 16:08:35 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn expect_end(&mut self) -> Result<(), E> {
|
2014-05-27 09:21:37 -05:00
|
|
|
match try!(self.expect_token()) {
|
|
|
|
End => Ok(()),
|
2014-05-31 01:28:46 -05:00
|
|
|
_ => self.syntax_error(),
|
2014-05-24 16:08:35 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 23:21:05 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-26 00:08:37 -05:00
|
|
|
/*
|
2014-05-25 23:36:56 -05:00
|
|
|
// FIXME: https://github.com/mozilla/rust/issues/11751
|
2014-05-25 21:41:29 -05:00
|
|
|
#[inline]
|
2014-05-21 08:47:58 -05:00
|
|
|
fn expect_rest_of_collection<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
|
|
|
T: Deserializable<E, D>,
|
|
|
|
C: FromIterator<T>
|
|
|
|
>(d: &mut D, len: uint) -> Result<C, E> {
|
|
|
|
let iter = d.by_ref().batch(|d| {
|
|
|
|
let d = d.iter();
|
2014-05-24 17:02:24 -05:00
|
|
|
|
2014-05-27 09:21:37 -05:00
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => None,
|
|
|
|
token => {
|
2014-05-24 17:02:24 -05:00
|
|
|
let value: Result<T, E> = Deserializable::deserialize_token(d, token);
|
|
|
|
Some(value)
|
2014-05-23 22:58:09 -05:00
|
|
|
}
|
2014-05-24 17:02:24 -05:00
|
|
|
}
|
2014-05-21 08:47:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
result::collect_with_capacity(iter, len)
|
|
|
|
}
|
2014-05-26 00:08:37 -05:00
|
|
|
*/
|
2014-05-21 08:47:58 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-19 09:18:35 -05:00
|
|
|
pub trait Deserializable<E, D: Deserializer<E>> {
|
2014-05-24 00:30:53 -05:00
|
|
|
#[inline]
|
|
|
|
fn deserialize(d: &mut D) -> Result<Self, E> {
|
|
|
|
match d.next() {
|
|
|
|
Some(Ok(token)) => Deserializable::deserialize_token(d, token),
|
|
|
|
Some(Err(err)) => Err(err),
|
2014-05-31 01:28:46 -05:00
|
|
|
None => d.end_of_stream_error(),
|
2014-05-24 00:30:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Self, E>;
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-19 09:18:35 -05:00
|
|
|
macro_rules! impl_deserializable {
|
2014-05-19 21:41:38 -05:00
|
|
|
($ty:ty, $method:ident) => {
|
2014-05-19 09:18:35 -05:00
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>
|
|
|
|
> Deserializable<E, D> for $ty {
|
2014-05-19 20:44:20 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<$ty, E> {
|
|
|
|
d.$method(token)
|
2014-05-19 20:44:20 -05:00
|
|
|
}
|
2014-05-19 09:18:35 -05:00
|
|
|
}
|
2014-05-16 23:21:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 23:59:58 -05:00
|
|
|
impl_deserializable!(bool, expect_bool)
|
2014-05-19 21:41:38 -05:00
|
|
|
impl_deserializable!(int, expect_num)
|
|
|
|
impl_deserializable!(i8, expect_num)
|
|
|
|
impl_deserializable!(i16, expect_num)
|
|
|
|
impl_deserializable!(i32, expect_num)
|
|
|
|
impl_deserializable!(i64, expect_num)
|
|
|
|
impl_deserializable!(uint, expect_num)
|
|
|
|
impl_deserializable!(u8, expect_num)
|
|
|
|
impl_deserializable!(u16, expect_num)
|
|
|
|
impl_deserializable!(u32, expect_num)
|
|
|
|
impl_deserializable!(u64, expect_num)
|
|
|
|
impl_deserializable!(f32, expect_num)
|
|
|
|
impl_deserializable!(f64, expect_num)
|
|
|
|
impl_deserializable!(char, expect_char)
|
|
|
|
impl_deserializable!(&'static str, expect_str)
|
2014-05-26 09:18:18 -05:00
|
|
|
impl_deserializable!(String, expect_strbuf)
|
2014-05-16 09:15:02 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
|
|
|
T: Deserializable<E, D>
|
|
|
|
> Deserializable<E, D> for Option<T> {
|
2014-05-19 20:44:20 -05:00
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Option<T>, E> {
|
|
|
|
d.expect_option(token)
|
2014-05-19 20:44:20 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-26 00:08:37 -05:00
|
|
|
macro_rules! deserialize_seq {
|
|
|
|
($seq:expr) => {
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match d.next() {
|
|
|
|
Some(Ok(End)) => { break; }
|
|
|
|
Some(Ok(token)) => {
|
|
|
|
let v = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
$seq.push(v)
|
|
|
|
}
|
|
|
|
Some(Err(err)) => { return Err(err); }
|
2014-05-31 01:28:46 -05:00
|
|
|
None => { return d.end_of_stream_error(); }
|
2014-05-26 00:08:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok($seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
|
|
|
T: Deserializable<E, D>
|
|
|
|
> Deserializable<E, D> for Vec<T> {
|
2014-05-19 20:44:20 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Vec<T>, E> {
|
2014-05-25 23:36:56 -05:00
|
|
|
let len = try!(d.expect_seq_start(token));
|
|
|
|
let mut value = Vec::with_capacity(len);
|
|
|
|
|
2014-05-26 00:08:37 -05:00
|
|
|
deserialize_seq!(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
macro_rules! deserialize_map {
|
|
|
|
($seq:expr) => {
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match d.next() {
|
|
|
|
Some(Ok(End)) => { break; }
|
|
|
|
Some(Ok(token)) => {
|
2014-05-26 11:01:22 -05:00
|
|
|
let k = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
let v = try!(Deserializable::deserialize(d));
|
2014-05-26 00:08:37 -05:00
|
|
|
$seq.insert(k, v);
|
|
|
|
}
|
|
|
|
Some(Err(err)) => { return Err(err); }
|
2014-05-31 01:28:46 -05:00
|
|
|
None => { return d.end_of_stream_error(); }
|
2014-05-25 23:36:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 00:08:37 -05:00
|
|
|
Ok($seq)
|
|
|
|
}
|
2014-05-19 20:44:20 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
|
|
|
K: Deserializable<E, D> + TotalEq + Hash,
|
|
|
|
V: Deserializable<E, D>
|
|
|
|
> Deserializable<E, D> for HashMap<K, V> {
|
2014-05-19 20:44:20 -05:00
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<HashMap<K, V>, E> {
|
2014-05-26 00:08:37 -05:00
|
|
|
let len = try!(d.expect_map_start(token));
|
|
|
|
let mut value = HashMap::with_capacity(len);
|
|
|
|
|
|
|
|
deserialize_map!(value)
|
2014-05-19 20:44:20 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 14:53:37 -05:00
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
2014-05-31 14:28:30 -05:00
|
|
|
K: Deserializable<E, D> + TotalOrd,
|
2014-05-26 14:53:37 -05:00
|
|
|
V: Deserializable<E, D>
|
|
|
|
> Deserializable<E, D> for TreeMap<K, V> {
|
|
|
|
#[inline]
|
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<TreeMap<K, V>, E> {
|
|
|
|
let _len = try!(d.expect_map_start(token));
|
|
|
|
let mut value = TreeMap::new();
|
|
|
|
|
|
|
|
deserialize_map!(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>
|
|
|
|
> Deserializable<E, D> for () {
|
2014-05-19 20:44:20 -05:00
|
|
|
#[inline]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<(), E> {
|
|
|
|
d.expect_null(token)
|
2014-05-19 20:44:20 -05:00
|
|
|
}
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-19 19:53:24 -05:00
|
|
|
macro_rules! peel(($name:ident, $($other:ident,)*) => (deserialize_tuple!($($other,)*)))
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-19 19:53:24 -05:00
|
|
|
macro_rules! deserialize_tuple (
|
|
|
|
() => ();
|
|
|
|
( $($name:ident,)+ ) => (
|
|
|
|
impl<
|
|
|
|
E,
|
|
|
|
D: Deserializer<E>,
|
2014-05-19 20:44:20 -05:00
|
|
|
$($name:Deserializable<E, D>),*
|
|
|
|
> Deserializable<E, D> for ($($name,)*) {
|
|
|
|
#[inline]
|
|
|
|
#[allow(uppercase_variables)]
|
2014-05-24 00:30:53 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<($($name,)*), E> {
|
2014-05-20 21:22:23 -05:00
|
|
|
// FIXME: how can we count macro args?
|
|
|
|
let mut len = 0;
|
|
|
|
$({ let $name = 1; len += $name; })*;
|
|
|
|
|
2014-05-24 00:30:53 -05:00
|
|
|
try!(d.expect_tuple_start(token, len));
|
2014-05-19 20:44:20 -05:00
|
|
|
|
2014-05-21 08:47:58 -05:00
|
|
|
let result = ($({
|
2014-05-24 00:30:53 -05:00
|
|
|
let $name = try!(Deserializable::deserialize(d));
|
2014-05-21 08:47:58 -05:00
|
|
|
$name
|
|
|
|
},)*);
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-31 01:28:46 -05:00
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => Ok(result),
|
|
|
|
_ => d.syntax_error(),
|
2014-05-24 00:30:53 -05:00
|
|
|
}
|
2014-05-19 19:53:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
peel!($($name,)*)
|
|
|
|
)
|
|
|
|
)
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-19 19:53:24 -05:00
|
|
|
deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
2014-05-16 09:15:02 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
/// Helper struct that will ignore tokens while taking in consideration
|
|
|
|
/// recursive structures.
|
|
|
|
pub struct IgnoreTokens;
|
2014-05-27 09:36:57 -05:00
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for IgnoreTokens {
|
|
|
|
#[inline]
|
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<IgnoreTokens, E> {
|
|
|
|
match token {
|
|
|
|
Option(true) => {
|
|
|
|
Deserializable::deserialize(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumStart(_, _, _) => {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => { return Ok(IgnoreTokens); }
|
|
|
|
token => {
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
StructStart(_, _) => {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => { return Ok(IgnoreTokens); }
|
|
|
|
Str(_) | String(_) => {
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize(d));
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_token => { return d.syntax_error(); }
|
2014-05-30 21:13:50 -05:00
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
TupleStart(_) => {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => { return Ok(IgnoreTokens); }
|
|
|
|
token => {
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
SeqStart(_) => {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => { return Ok(IgnoreTokens); }
|
|
|
|
token => {
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:13:50 -05:00
|
|
|
MapStart(_) => {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => { return Ok(IgnoreTokens); }
|
|
|
|
token => {
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize_token(d, token));
|
|
|
|
let _: IgnoreTokens = try!(Deserializable::deserialize(d));
|
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 01:28:46 -05:00
|
|
|
End => d.syntax_error(),
|
2014-05-30 21:13:50 -05:00
|
|
|
|
|
|
|
_ => Ok(IgnoreTokens),
|
2014-05-30 21:15:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// Helper struct that will gather tokens while taking in consideration
|
|
|
|
/// recursive structures.
|
|
|
|
pub struct GatherTokens {
|
|
|
|
tokens: Vec<Token>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GatherTokens {
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap(self) -> Vec<Token> {
|
|
|
|
self.tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn gather<E, D: Deserializer<E>>(&mut self, d: &mut D) -> Result<(), E> {
|
|
|
|
let token = try!(d.expect_token());
|
|
|
|
self.gather_token(d, token)
|
|
|
|
}
|
2014-05-30 21:13:50 -05:00
|
|
|
|
2014-05-30 21:15:05 -05:00
|
|
|
#[inline]
|
|
|
|
fn gather_token<E, D: Deserializer<E>>(&mut self, d: &mut D, token: Token) -> Result<(), E> {
|
|
|
|
match token {
|
|
|
|
token @ Option(true) => {
|
|
|
|
self.tokens.push(token);
|
|
|
|
self.gather(d)
|
|
|
|
}
|
|
|
|
EnumStart(name, variant, len) => {
|
|
|
|
self.tokens.reserve_additional(len + 1);
|
|
|
|
self.tokens.push(EnumStart(name, variant, len));
|
|
|
|
self.gather_seq(d)
|
|
|
|
}
|
|
|
|
StructStart(name, len) => {
|
|
|
|
self.tokens.reserve_additional(len + 1);
|
|
|
|
self.tokens.push(StructStart(name, len));
|
2014-05-31 00:30:02 -05:00
|
|
|
self.gather_struct(d)
|
2014-05-30 21:15:05 -05:00
|
|
|
}
|
|
|
|
TupleStart(len) => {
|
|
|
|
self.tokens.reserve_additional(len + 1);
|
|
|
|
self.tokens.push(TupleStart(len));
|
|
|
|
self.gather_seq(d)
|
|
|
|
}
|
|
|
|
SeqStart(len) => {
|
|
|
|
self.tokens.reserve_additional(len + 1);
|
|
|
|
self.tokens.push(SeqStart(len));
|
|
|
|
self.gather_seq(d)
|
|
|
|
}
|
|
|
|
MapStart(len) => {
|
|
|
|
self.tokens.reserve_additional(len + 1);
|
|
|
|
self.tokens.push(MapStart(len));
|
|
|
|
self.gather_map(d)
|
|
|
|
}
|
|
|
|
End => {
|
2014-05-31 01:28:46 -05:00
|
|
|
d.syntax_error()
|
2014-05-30 21:15:05 -05:00
|
|
|
}
|
|
|
|
token => {
|
|
|
|
self.tokens.push(token);
|
|
|
|
Ok(())
|
|
|
|
}
|
2014-05-30 21:13:50 -05:00
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
2014-05-30 21:15:05 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn gather_seq<E, D: Deserializer<E>>(&mut self, d: &mut D) -> Result<(), E> {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
token @ End => {
|
|
|
|
self.tokens.push(token);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
token => {
|
|
|
|
try!(self.gather_token(d, token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn gather_struct<E, D: Deserializer<E>>(&mut self, d: &mut D) -> Result<(), E> {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
token @ End => {
|
|
|
|
self.tokens.push(token);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
token @ Str(_) | token @ String(_) => {
|
|
|
|
self.tokens.push(token);
|
|
|
|
try!(self.gather(d))
|
|
|
|
}
|
2014-05-31 01:28:46 -05:00
|
|
|
_token => { return d.syntax_error(); }
|
2014-05-30 21:15:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn gather_map<E, D: Deserializer<E>>(&mut self, d: &mut D) -> Result<(), E> {
|
|
|
|
loop {
|
|
|
|
match try!(d.expect_token()) {
|
|
|
|
End => {
|
|
|
|
self.tokens.push(End);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
token => {
|
|
|
|
try!(self.gather_token(d, token));
|
|
|
|
try!(self.gather(d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for GatherTokens {
|
|
|
|
#[inline]
|
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<GatherTokens, E> {
|
|
|
|
let mut tokens = GatherTokens {
|
|
|
|
tokens: vec!(),
|
|
|
|
};
|
|
|
|
try!(tokens.gather_token(d, token));
|
|
|
|
Ok(tokens)
|
|
|
|
}
|
2014-05-27 09:36:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-05-16 23:21:05 -05:00
|
|
|
use collections::HashMap;
|
|
|
|
|
2014-05-23 22:41:29 -05:00
|
|
|
use serialize::Decoder;
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-26 09:18:18 -05:00
|
|
|
use super::{Token, Null, Int, Uint, Str, String, Char, Option};
|
2014-05-26 10:26:34 -05:00
|
|
|
use super::{TupleStart, StructStart, EnumStart};
|
2014-05-25 23:59:58 -05:00
|
|
|
use super::{SeqStart, MapStart, End};
|
2014-05-16 09:15:02 -05:00
|
|
|
use super::{Deserializer, Deserializable};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-31 14:28:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
2014-05-21 10:25:28 -05:00
|
|
|
struct Inner {
|
|
|
|
a: (),
|
|
|
|
b: uint,
|
2014-05-26 09:18:18 -05:00
|
|
|
c: HashMap<String, Option<char>>,
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for Inner {
|
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Inner, E> {
|
|
|
|
try!(d.expect_struct_start(token, "Inner"));
|
2014-05-21 10:25:28 -05:00
|
|
|
let a = try!(d.expect_struct_field("a"));
|
|
|
|
let b = try!(d.expect_struct_field("b"));
|
|
|
|
let c = try!(d.expect_struct_field("c"));
|
|
|
|
try!(d.expect_end());
|
|
|
|
Ok(Inner { a: a, b: b, c: c })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-31 14:28:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
2014-05-21 10:25:28 -05:00
|
|
|
struct Outer {
|
|
|
|
inner: Vec<Inner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for Outer {
|
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Outer, E> {
|
|
|
|
try!(d.expect_struct_start(token, "Outer"));
|
2014-05-21 10:25:28 -05:00
|
|
|
let inner = try!(d.expect_struct_field("inner"));
|
|
|
|
try!(d.expect_end());
|
|
|
|
Ok(Outer { inner: inner })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-31 14:28:30 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show, Decodable)]
|
2014-05-21 10:44:48 -05:00
|
|
|
enum Animal {
|
|
|
|
Dog,
|
2014-05-26 09:18:18 -05:00
|
|
|
Frog(String, int)
|
2014-05-21 10:44:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal {
|
|
|
|
#[inline]
|
2014-05-25 23:59:58 -05:00
|
|
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Animal, E> {
|
|
|
|
match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) {
|
2014-05-21 10:44:48 -05:00
|
|
|
0 => {
|
|
|
|
try!(d.expect_end());
|
|
|
|
Ok(Dog)
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let x0 = try!(Deserializable::deserialize(d));
|
|
|
|
let x1 = try!(Deserializable::deserialize(d));
|
|
|
|
try!(d.expect_end());
|
|
|
|
Ok(Frog(x0, x1))
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[deriving(Show)]
|
|
|
|
enum Error {
|
|
|
|
EndOfStream,
|
|
|
|
SyntaxError,
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct TokenDeserializer {
|
|
|
|
tokens: Vec<Token>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenDeserializer {
|
|
|
|
#[inline]
|
|
|
|
fn new(tokens: Vec<Token>) -> TokenDeserializer {
|
|
|
|
TokenDeserializer {
|
|
|
|
tokens: tokens,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator<Result<Token, Error>> for TokenDeserializer {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Result<Token, Error>> {
|
|
|
|
match self.tokens.shift() {
|
|
|
|
None => None,
|
|
|
|
Some(token) => Some(Ok(token)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserializer<Error> for TokenDeserializer {
|
2014-05-31 01:28:46 -05:00
|
|
|
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
|
|
|
Err(EndOfStream)
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-31 01:28:46 -05:00
|
|
|
fn syntax_error<T>(&self) -> Result<T, Error> {
|
|
|
|
Err(SyntaxError)
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_int() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Int(5),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: int = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_str() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Str("a"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: &'static str = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, "a");
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_strbuf() {
|
|
|
|
let tokens = vec!(
|
2014-05-29 08:44:38 -05:00
|
|
|
String("a".to_string()),
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-26 09:18:18 -05:00
|
|
|
let value: String = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
assert_eq!(value, "a".to_string());
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-20 21:22:23 -05:00
|
|
|
#[test]
|
|
|
|
fn test_tokens_null() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Null,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: () = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-20 21:22:23 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, ());
|
2014-05-20 21:22:23 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_tokens_tuple_empty() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(0),
|
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: () = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, ());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_option_none() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Option(false),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Option<int> = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_option_some() {
|
|
|
|
let tokens = vec!(
|
|
|
|
Option(true),
|
|
|
|
Int(5),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Option<int> = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, Some(5));
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_tuple() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(2),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(5),
|
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
String("a".to_string()),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-26 09:18:18 -05:00
|
|
|
let value: (int, String) = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
assert_eq!(value, (5, "a".to_string()));
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_tuple_compound() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(3),
|
|
|
|
Null,
|
2014-05-20 21:38:48 -05:00
|
|
|
|
2014-05-20 21:22:23 -05:00
|
|
|
TupleStart(0),
|
|
|
|
End,
|
|
|
|
|
|
|
|
TupleStart(2),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(5),
|
2014-05-19 21:41:38 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
String("a".to_string()),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-26 09:18:18 -05:00
|
|
|
let value: ((), (), (int, String)) = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
assert_eq!(value, ((), (), (5, "a".to_string())));
|
2014-05-21 10:25:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_struct_empty() {
|
|
|
|
let tokens = vec!(
|
2014-05-28 12:46:29 -05:00
|
|
|
StructStart("Outer", 1),
|
2014-05-26 10:26:34 -05:00
|
|
|
Str("inner"),
|
2014-05-21 10:25:28 -05:00
|
|
|
SeqStart(0),
|
|
|
|
End,
|
|
|
|
End,
|
|
|
|
);
|
2014-05-21 22:54:10 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Outer = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, Outer { inner: vec!() });
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_struct() {
|
|
|
|
let tokens = vec!(
|
2014-05-28 12:46:29 -05:00
|
|
|
StructStart("Outer", 1),
|
2014-05-26 10:26:34 -05:00
|
|
|
Str("inner"),
|
2014-05-21 10:25:28 -05:00
|
|
|
SeqStart(1),
|
2014-05-28 12:46:29 -05:00
|
|
|
StructStart("Inner", 3),
|
2014-05-26 10:26:34 -05:00
|
|
|
Str("a"),
|
2014-05-21 10:25:28 -05:00
|
|
|
Null,
|
|
|
|
|
2014-05-26 10:26:34 -05:00
|
|
|
Str("b"),
|
2014-05-21 10:25:28 -05:00
|
|
|
Uint(5),
|
|
|
|
|
2014-05-26 10:26:34 -05:00
|
|
|
Str("c"),
|
2014-05-21 10:25:28 -05:00
|
|
|
MapStart(1),
|
2014-05-29 08:44:38 -05:00
|
|
|
String("abc".to_string()),
|
2014-05-21 10:25:28 -05:00
|
|
|
|
2014-05-26 11:01:22 -05:00
|
|
|
Option(true),
|
|
|
|
Char('c'),
|
2014-05-21 10:25:28 -05:00
|
|
|
End,
|
|
|
|
End,
|
|
|
|
End,
|
|
|
|
End,
|
|
|
|
);
|
2014-05-21 22:54:10 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Outer = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
2014-05-29 08:44:38 -05:00
|
|
|
map.insert("abc".to_string(), Some('c'));
|
2014-05-21 10:25:28 -05:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
value,
|
|
|
|
Outer {
|
|
|
|
inner: vec!(
|
|
|
|
Inner {
|
|
|
|
a: (),
|
|
|
|
b: 5,
|
|
|
|
c: map,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-21 10:44:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_tokens_enum() {
|
|
|
|
let tokens = vec!(
|
2014-05-28 12:46:29 -05:00
|
|
|
EnumStart("Animal", "Dog", 0),
|
2014-05-21 10:44:48 -05:00
|
|
|
End,
|
|
|
|
);
|
2014-05-21 22:54:10 -05:00
|
|
|
|
2014-05-21 10:44:48 -05:00
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Animal = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, Dog);
|
|
|
|
|
|
|
|
let tokens = vec!(
|
2014-05-28 12:46:29 -05:00
|
|
|
EnumStart("Animal", "Frog", 2),
|
2014-05-29 08:44:38 -05:00
|
|
|
String("Henry".to_string()),
|
2014-05-21 10:44:48 -05:00
|
|
|
Int(349),
|
|
|
|
End,
|
|
|
|
);
|
2014-05-21 22:54:10 -05:00
|
|
|
|
2014-05-21 10:44:48 -05:00
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
|
|
|
let value: Animal = Deserializable::deserialize(&mut deserializer).unwrap();
|
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
assert_eq!(value, Frog("Henry".to_string(), 349));
|
2014-05-21 10:44:48 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 09:15:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec_empty() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(0),
|
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: Vec<int> = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, vec!());
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(3),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(5),
|
|
|
|
|
|
|
|
Int(6),
|
|
|
|
|
|
|
|
Int(7),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: Vec<int> = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, vec!(5, 6, 7));
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_vec_compound() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(0),
|
|
|
|
SeqStart(1),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(1),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(2),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(2),
|
|
|
|
|
|
|
|
Int(3),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-20 21:22:23 -05:00
|
|
|
SeqStart(3),
|
2014-05-16 09:15:02 -05:00
|
|
|
Int(4),
|
|
|
|
|
|
|
|
Int(5),
|
|
|
|
|
|
|
|
Int(6),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-21 10:25:28 -05:00
|
|
|
let value: Vec<Vec<int>> = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, vec!(vec!(1), vec!(2, 3), vec!(4, 5, 6)));
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tokens_hashmap() {
|
|
|
|
let tokens = vec!(
|
2014-05-20 21:22:23 -05:00
|
|
|
MapStart(2),
|
2014-05-26 11:01:22 -05:00
|
|
|
Int(5),
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
String("a".to_string()),
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-26 11:01:22 -05:00
|
|
|
Int(6),
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-29 08:44:38 -05:00
|
|
|
String("b".to_string()),
|
2014-05-20 21:22:23 -05:00
|
|
|
End,
|
2014-05-16 09:15:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut deserializer = TokenDeserializer::new(tokens);
|
2014-05-26 09:18:18 -05:00
|
|
|
let value: HashMap<int, String> = Deserializable::deserialize(&mut deserializer).unwrap();
|
2014-05-16 09:15:02 -05:00
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
2014-05-29 08:44:38 -05:00
|
|
|
map.insert(5, "a".to_string());
|
|
|
|
map.insert(6, "b".to_string());
|
2014-05-16 09:15:02 -05:00
|
|
|
|
2014-05-21 10:25:28 -05:00
|
|
|
assert_eq!(value, map);
|
2014-05-16 09:15:02 -05:00
|
|
|
}
|
|
|
|
}
|