2015-03-20 22:35:01 -05:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
2015-03-03 23:19:55 -06:00
|
|
|
use std::iter;
|
|
|
|
use std::vec;
|
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::de::{self, Deserialize, Deserializer, Visitor};
|
2015-03-03 23:19:55 -06:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2015-03-20 10:32:33 -05:00
|
|
|
enum Token {
|
2015-03-03 23:19:55 -06:00
|
|
|
Bool(bool),
|
|
|
|
Isize(isize),
|
|
|
|
I8(i8),
|
|
|
|
I16(i16),
|
|
|
|
I32(i32),
|
|
|
|
I64(i64),
|
|
|
|
Usize(usize),
|
|
|
|
U8(u8),
|
|
|
|
U16(u16),
|
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
|
|
|
F32(f32),
|
|
|
|
F64(f64),
|
|
|
|
Char(char),
|
2015-03-20 10:32:33 -05:00
|
|
|
Str(&'static str),
|
2015-03-03 23:19:55 -06:00
|
|
|
String(String),
|
|
|
|
|
|
|
|
Option(bool),
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
Name(&'static str),
|
|
|
|
|
2015-03-03 23:19:55 -06:00
|
|
|
Unit,
|
|
|
|
|
|
|
|
SeqStart(usize),
|
2015-03-20 22:35:01 -05:00
|
|
|
SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
SeqEnd,
|
|
|
|
|
|
|
|
MapStart(usize),
|
2015-03-20 22:35:01 -05:00
|
|
|
MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
MapEnd,
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
EnumStart(&'static str),
|
2015-03-03 23:19:55 -06:00
|
|
|
EnumEnd,
|
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
struct TokenDeserializer {
|
|
|
|
tokens: iter::Peekable<vec::IntoIter<Token>>,
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
impl<'a> TokenDeserializer {
|
|
|
|
fn new(tokens: Vec<Token>) -> TokenDeserializer {
|
2015-03-03 23:19:55 -06:00
|
|
|
TokenDeserializer {
|
|
|
|
tokens: tokens.into_iter().peekable(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 02:30:20 -05:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2015-03-03 23:19:55 -06:00
|
|
|
enum Error {
|
|
|
|
SyntaxError,
|
|
|
|
EndOfStreamError,
|
2015-04-12 02:30:20 -05:00
|
|
|
UnknownFieldError(String),
|
2015-03-03 23:19:55 -06:00
|
|
|
MissingFieldError(&'static str),
|
2015-03-20 10:32:33 -05:00
|
|
|
InvalidName(&'static str),
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl de::Error for Error {
|
|
|
|
fn syntax_error() -> Error { Error::SyntaxError }
|
|
|
|
|
|
|
|
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
|
|
|
|
2015-04-12 02:30:20 -05:00
|
|
|
fn unknown_field_error(field: &str) -> Error {
|
|
|
|
Error::UnknownFieldError(field.to_string())
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:19:55 -06:00
|
|
|
fn missing_field_error(field: &'static str) -> Error {
|
|
|
|
Error::MissingFieldError(field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
impl Deserializer for TokenDeserializer {
|
2015-03-03 23:19:55 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
|
|
|
where V: Visitor,
|
|
|
|
{
|
|
|
|
match self.tokens.next() {
|
|
|
|
Some(Token::Bool(v)) => visitor.visit_bool(v),
|
|
|
|
Some(Token::Isize(v)) => visitor.visit_isize(v),
|
|
|
|
Some(Token::I8(v)) => visitor.visit_i8(v),
|
|
|
|
Some(Token::I16(v)) => visitor.visit_i16(v),
|
|
|
|
Some(Token::I32(v)) => visitor.visit_i32(v),
|
|
|
|
Some(Token::I64(v)) => visitor.visit_i64(v),
|
|
|
|
Some(Token::Usize(v)) => visitor.visit_usize(v),
|
|
|
|
Some(Token::U8(v)) => visitor.visit_u8(v),
|
|
|
|
Some(Token::U16(v)) => visitor.visit_u16(v),
|
|
|
|
Some(Token::U32(v)) => visitor.visit_u32(v),
|
|
|
|
Some(Token::U64(v)) => visitor.visit_u64(v),
|
|
|
|
Some(Token::F32(v)) => visitor.visit_f32(v),
|
|
|
|
Some(Token::F64(v)) => visitor.visit_f64(v),
|
|
|
|
Some(Token::Char(v)) => visitor.visit_char(v),
|
|
|
|
Some(Token::Str(v)) => visitor.visit_str(v),
|
|
|
|
Some(Token::String(v)) => visitor.visit_string(v),
|
|
|
|
Some(Token::Option(false)) => visitor.visit_none(),
|
|
|
|
Some(Token::Option(true)) => visitor.visit_some(self),
|
|
|
|
Some(Token::Unit) => visitor.visit_unit(),
|
|
|
|
Some(Token::SeqStart(len)) => {
|
|
|
|
visitor.visit_seq(TokenDeserializerSeqVisitor {
|
|
|
|
de: self,
|
|
|
|
len: len,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Some(Token::MapStart(len)) => {
|
|
|
|
visitor.visit_map(TokenDeserializerMapVisitor {
|
|
|
|
de: self,
|
|
|
|
len: len,
|
|
|
|
})
|
|
|
|
}
|
2015-03-20 10:32:33 -05:00
|
|
|
Some(Token::Name(_)) => self.visit(visitor),
|
2015-03-03 23:19:55 -06:00
|
|
|
Some(_) => Err(Error::SyntaxError),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hook into `Option` deserializing so we can treat `Unit` as a
|
|
|
|
/// `None`, or a regular value as `Some(value)`.
|
|
|
|
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
|
|
|
where V: Visitor,
|
|
|
|
{
|
|
|
|
match self.tokens.peek() {
|
|
|
|
Some(&Token::Option(false)) => {
|
|
|
|
self.tokens.next();
|
|
|
|
visitor.visit_none()
|
|
|
|
}
|
|
|
|
Some(&Token::Option(true)) => {
|
|
|
|
self.tokens.next();
|
|
|
|
visitor.visit_some(self)
|
|
|
|
}
|
|
|
|
Some(&Token::Unit) => {
|
|
|
|
self.tokens.next();
|
|
|
|
visitor.visit_none()
|
|
|
|
}
|
|
|
|
Some(_) => visitor.visit_some(self),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 00:04:17 -05:00
|
|
|
|
|
|
|
fn visit_enum<V>(&mut self, name: &str, mut visitor: V) -> Result<V::Value, Error>
|
|
|
|
where V: de::EnumVisitor,
|
|
|
|
{
|
|
|
|
match self.tokens.next() {
|
|
|
|
Some(Token::EnumStart(n)) => {
|
|
|
|
if name == n {
|
|
|
|
visitor.visit(TokenDeserializerVariantVisitor {
|
|
|
|
de: self,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(Error::SyntaxError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => Err(Error::SyntaxError),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
2015-03-20 10:32:33 -05:00
|
|
|
|
2015-07-04 14:11:20 -05:00
|
|
|
fn visit_unit_struct<V>(&mut self, name: &str, visitor: V) -> Result<V::Value, Error>
|
2015-03-20 10:32:33 -05:00
|
|
|
where V: de::Visitor,
|
|
|
|
{
|
|
|
|
match self.tokens.peek() {
|
|
|
|
Some(&Token::Name(n)) => {
|
|
|
|
if name == n {
|
|
|
|
self.tokens.next();
|
|
|
|
self.visit_seq(visitor)
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidName(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => self.visit(visitor),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 14:11:20 -05:00
|
|
|
fn visit_tuple_struct<V>(&mut self, name: &str, visitor: V) -> Result<V::Value, Error>
|
2015-03-20 10:32:33 -05:00
|
|
|
where V: de::Visitor,
|
|
|
|
{
|
|
|
|
match self.tokens.peek() {
|
|
|
|
Some(&Token::Name(n)) => {
|
|
|
|
if name == n {
|
|
|
|
self.tokens.next();
|
|
|
|
self.visit_seq(visitor)
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidName(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => self.visit_seq(visitor),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
fn visit_struct<V>(&mut self,
|
|
|
|
name: &str,
|
|
|
|
_fields: &'static [&'static str],
|
|
|
|
visitor: V) -> Result<V::Value, Error>
|
2015-03-20 10:32:33 -05:00
|
|
|
where V: de::Visitor,
|
|
|
|
{
|
|
|
|
match self.tokens.peek() {
|
|
|
|
Some(&Token::Name(n)) => {
|
|
|
|
if name == n {
|
|
|
|
self.tokens.next();
|
|
|
|
self.visit_map(visitor)
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidName(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => self.visit_map(visitor),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
struct TokenDeserializerSeqVisitor<'a> {
|
|
|
|
de: &'a mut TokenDeserializer,
|
2015-03-03 23:19:55 -06:00
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
impl<'a> de::SeqVisitor for TokenDeserializerSeqVisitor<'a> {
|
2015-03-03 23:19:55 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
|
|
|
where T: Deserialize,
|
|
|
|
{
|
2015-03-16 07:20:34 -05:00
|
|
|
match self.de.tokens.peek() {
|
2015-03-20 22:35:01 -05:00
|
|
|
Some(&Token::SeqSep) => {
|
2015-03-03 23:19:55 -06:00
|
|
|
self.len -= 1;
|
2015-03-16 07:20:34 -05:00
|
|
|
self.de.tokens.next();
|
2015-03-03 23:19:55 -06:00
|
|
|
Ok(Some(try!(Deserialize::deserialize(self.de))))
|
|
|
|
}
|
2015-03-16 07:20:34 -05:00
|
|
|
Some(&Token::SeqEnd) => Ok(None),
|
2015-03-03 23:19:55 -06:00
|
|
|
Some(_) => {
|
|
|
|
Err(Error::SyntaxError)
|
|
|
|
}
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(&mut self) -> Result<(), Error> {
|
2015-03-16 07:20:34 -05:00
|
|
|
assert_eq!(self.len, 0);
|
2015-03-03 23:19:55 -06:00
|
|
|
match self.de.tokens.next() {
|
|
|
|
Some(Token::SeqEnd) => Ok(()),
|
|
|
|
Some(_) => Err(Error::SyntaxError),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.len, Some(self.len))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
struct TokenDeserializerMapVisitor<'a> {
|
|
|
|
de: &'a mut TokenDeserializer,
|
2015-03-03 23:19:55 -06:00
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
impl<'a> de::MapVisitor for TokenDeserializerMapVisitor<'a> {
|
2015-03-03 23:19:55 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn visit_key<K>(&mut self) -> Result<Option<K>, Error>
|
|
|
|
where K: Deserialize,
|
|
|
|
{
|
2015-03-16 07:20:34 -05:00
|
|
|
match self.de.tokens.peek() {
|
2015-03-20 22:35:01 -05:00
|
|
|
Some(&Token::MapSep) => {
|
2015-03-16 07:20:34 -05:00
|
|
|
self.de.tokens.next();
|
|
|
|
self.len -= 1;
|
2015-03-03 23:19:55 -06:00
|
|
|
Ok(Some(try!(Deserialize::deserialize(self.de))))
|
|
|
|
}
|
2015-03-16 07:20:34 -05:00
|
|
|
Some(&Token::MapEnd) => Ok(None),
|
2015-03-03 23:19:55 -06:00
|
|
|
Some(_) => Err(Error::SyntaxError),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_value<V>(&mut self) -> Result<V, Error>
|
|
|
|
where V: Deserialize,
|
|
|
|
{
|
|
|
|
Ok(try!(Deserialize::deserialize(self.de)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(&mut self) -> Result<(), Error> {
|
2015-03-16 07:20:34 -05:00
|
|
|
assert_eq!(self.len, 0);
|
2015-03-03 23:19:55 -06:00
|
|
|
match self.de.tokens.next() {
|
|
|
|
Some(Token::MapEnd) => Ok(()),
|
|
|
|
Some(_) => Err(Error::SyntaxError),
|
|
|
|
None => Err(Error::EndOfStreamError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.len, Some(self.len))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
struct TokenDeserializerVariantVisitor<'a> {
|
|
|
|
de: &'a mut TokenDeserializer,
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
|
2015-03-20 10:32:33 -05:00
|
|
|
impl<'a> de::VariantVisitor for TokenDeserializerVariantVisitor<'a> {
|
2015-03-03 23:19:55 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2015-03-18 09:35:05 -05:00
|
|
|
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
2015-03-16 00:04:17 -05:00
|
|
|
where V: de::Deserialize,
|
|
|
|
{
|
|
|
|
de::Deserialize::deserialize(self.de)
|
|
|
|
}
|
|
|
|
|
2015-04-06 22:04:01 -05:00
|
|
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
|
|
|
de::Deserialize::deserialize(self.de)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Error>
|
|
|
|
where V: de::Visitor,
|
|
|
|
{
|
|
|
|
de::Deserializer::visit(self.de, visitor)
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:39:27 -05:00
|
|
|
fn visit_map<V>(&mut self,
|
|
|
|
_fields: &'static [&'static str],
|
|
|
|
visitor: V) -> Result<V::Value, Error>
|
2015-03-18 09:35:05 -05:00
|
|
|
where V: de::Visitor,
|
2015-03-03 23:27:07 -06:00
|
|
|
{
|
2015-03-18 09:35:05 -05:00
|
|
|
de::Deserializer::visit(self.de, visitor)
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-04-05 12:20:33 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Deserialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
struct UnitStruct;
|
2015-03-03 23:19:55 -06:00
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(PartialEq, Debug, Deserialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
struct TupleStruct(i32, i32, i32);
|
2015-03-03 23:19:55 -06:00
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(PartialEq, Debug, Deserialize)]
|
2015-07-04 14:11:20 -05:00
|
|
|
struct Struct {
|
2015-03-03 23:19:55 -06:00
|
|
|
a: i32,
|
|
|
|
b: i32,
|
|
|
|
c: i32,
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(PartialEq, Debug, Deserialize)]
|
2015-03-03 23:19:55 -06:00
|
|
|
enum Enum {
|
|
|
|
Unit,
|
|
|
|
Seq(i32, i32, i32),
|
|
|
|
Map { a: i32, b: i32, c: i32 }
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
macro_rules! btreeset {
|
|
|
|
() => {
|
|
|
|
BTreeSet::new()
|
|
|
|
};
|
|
|
|
($($value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut set = BTreeSet::new();
|
|
|
|
$(set.insert($value);)+
|
|
|
|
set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:19:55 -06:00
|
|
|
macro_rules! btreemap {
|
|
|
|
() => {
|
|
|
|
BTreeMap::new()
|
|
|
|
};
|
|
|
|
($($key:expr => $value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
$(map.insert($key, $value);)+
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
macro_rules! hashset {
|
|
|
|
() => {
|
|
|
|
HashSet::new()
|
|
|
|
};
|
|
|
|
($($value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
$(set.insert($value);)+
|
|
|
|
set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! hashmap {
|
|
|
|
() => {
|
|
|
|
HashMap::new()
|
|
|
|
};
|
|
|
|
($($key:expr => $value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
$(map.insert($key, $value);)+
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:19:55 -06:00
|
|
|
macro_rules! declare_test {
|
|
|
|
($name:ident { $($value:expr => $tokens:expr,)+ }) => {
|
|
|
|
#[test]
|
|
|
|
fn $name() {
|
|
|
|
$(
|
|
|
|
let mut de = TokenDeserializer::new($tokens);
|
|
|
|
let value: Result<_, Error> = Deserialize::deserialize(&mut de);
|
|
|
|
assert_eq!(value, Ok($value));
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! declare_tests {
|
|
|
|
($($name:ident { $($value:expr => $tokens:expr,)+ })+) => {
|
|
|
|
$(
|
|
|
|
declare_test!($name { $($value => $tokens,)+ });
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
declare_tests! {
|
|
|
|
test_bool {
|
|
|
|
true => vec![Token::Bool(true)],
|
|
|
|
false => vec![Token::Bool(false)],
|
|
|
|
}
|
|
|
|
test_isize {
|
|
|
|
0isize => vec![Token::Isize(0)],
|
|
|
|
0isize => vec![Token::I8(0)],
|
|
|
|
0isize => vec![Token::I16(0)],
|
|
|
|
0isize => vec![Token::I32(0)],
|
|
|
|
0isize => vec![Token::I64(0)],
|
|
|
|
0isize => vec![Token::Usize(0)],
|
|
|
|
0isize => vec![Token::U8(0)],
|
|
|
|
0isize => vec![Token::U16(0)],
|
|
|
|
0isize => vec![Token::U32(0)],
|
|
|
|
0isize => vec![Token::U64(0)],
|
|
|
|
0isize => vec![Token::F32(0.)],
|
|
|
|
0isize => vec![Token::F64(0.)],
|
|
|
|
}
|
|
|
|
test_ints {
|
|
|
|
0isize => vec![Token::Isize(0)],
|
|
|
|
0i8 => vec![Token::I8(0)],
|
|
|
|
0i16 => vec![Token::I16(0)],
|
|
|
|
0i32 => vec![Token::I32(0)],
|
|
|
|
0i64 => vec![Token::I64(0)],
|
|
|
|
}
|
|
|
|
test_uints {
|
|
|
|
0usize => vec![Token::Usize(0)],
|
|
|
|
0u8 => vec![Token::U8(0)],
|
|
|
|
0u16 => vec![Token::U16(0)],
|
|
|
|
0u32 => vec![Token::U32(0)],
|
|
|
|
0u64 => vec![Token::U64(0)],
|
|
|
|
}
|
|
|
|
test_floats {
|
|
|
|
0f32 => vec![Token::F32(0.)],
|
|
|
|
0f64 => vec![Token::F64(0.)],
|
|
|
|
}
|
|
|
|
test_char {
|
|
|
|
'a' => vec![Token::Char('a')],
|
|
|
|
'a' => vec![Token::Str("a")],
|
|
|
|
'a' => vec![Token::String("a".to_string())],
|
|
|
|
}
|
|
|
|
test_string {
|
|
|
|
"abc".to_string() => vec![Token::Str("abc")],
|
|
|
|
"abc".to_string() => vec![Token::String("abc".to_string())],
|
|
|
|
"a".to_string() => vec![Token::Char('a')],
|
|
|
|
}
|
|
|
|
test_option {
|
|
|
|
None::<i32> => vec![Token::Unit],
|
|
|
|
None::<i32> => vec![Token::Option(false)],
|
|
|
|
Some(1) => vec![Token::I32(1)],
|
|
|
|
Some(1) => vec![
|
|
|
|
Token::Option(true),
|
|
|
|
Token::I32(1),
|
|
|
|
],
|
|
|
|
}
|
2015-07-19 13:16:46 -05:00
|
|
|
test_result {
|
|
|
|
Ok::<i32, i32>(0) => vec![
|
|
|
|
Token::EnumStart("Result"),
|
|
|
|
Token::Str("Ok"),
|
|
|
|
Token::SeqStart(1),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::EnumEnd,
|
|
|
|
],
|
|
|
|
Err::<i32, i32>(1) => vec![
|
|
|
|
Token::EnumStart("Result"),
|
|
|
|
Token::Str("Err"),
|
|
|
|
Token::SeqStart(1),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::EnumEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-03 23:19:55 -06:00
|
|
|
test_unit {
|
|
|
|
() => vec![Token::Unit],
|
|
|
|
() => vec![
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
() => vec![
|
2015-03-20 10:32:33 -05:00
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::SeqStart(0),
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
test_unit_struct {
|
|
|
|
UnitStruct => vec![Token::Unit],
|
|
|
|
UnitStruct => vec![
|
|
|
|
Token::Name("UnitStruct"),
|
2015-03-20 10:32:33 -05:00
|
|
|
Token::Unit,
|
|
|
|
],
|
2015-07-04 14:11:20 -05:00
|
|
|
UnitStruct => vec![
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
test_tuple_struct {
|
|
|
|
TupleStruct(1, 2, 3) => vec![
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-07-04 14:11:20 -05:00
|
|
|
TupleStruct(1, 2, 3) => vec![
|
|
|
|
Token::Name("TupleStruct"),
|
2015-03-20 10:32:33 -05:00
|
|
|
Token::SeqStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_btreeset {
|
|
|
|
BTreeSet::<isize>::new() => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
BTreeSet::<isize>::new() => vec![
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
btreeset![btreeset![], btreeset![1], btreeset![2, 3]] => vec![
|
|
|
|
Token::SeqStart(3),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(1),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(2),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
BTreeSet::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
BTreeSet::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_hashset {
|
|
|
|
HashSet::<isize>::new() => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
HashSet::<isize>::new() => vec![
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
hashset![1, 2, 3] => vec![
|
|
|
|
Token::SeqStart(3),
|
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-03-20 22:35:01 -05:00
|
|
|
HashSet::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
HashSet::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
|
|
|
test_vec {
|
2015-03-20 10:47:33 -05:00
|
|
|
Vec::<isize>::new() => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
2015-03-03 23:19:55 -06:00
|
|
|
Vec::<isize>::new() => vec![
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
vec![vec![], vec![1], vec![2, 3]] => vec![
|
|
|
|
Token::SeqStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(1),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(2),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-03-20 10:47:33 -05:00
|
|
|
Vec::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
Vec::<isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
2015-05-16 06:32:58 -05:00
|
|
|
test_array {
|
|
|
|
[0; 0] => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
[0; 0] => vec![
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
([0; 0], [1], [2, 3]) => vec![
|
|
|
|
Token::SeqStart(3),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(1),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::SeqStart(2),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
[0; 0] => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
[0; 0] => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::SeqStart(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-03 23:19:55 -06:00
|
|
|
test_tuple {
|
|
|
|
(1,) => vec![
|
|
|
|
Token::SeqStart(1),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
(1, 2, 3) => vec![
|
|
|
|
Token::SeqStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_btreemap {
|
2015-03-20 10:47:33 -05:00
|
|
|
BTreeMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
BTreeMap::<isize, isize>::new() => vec![
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
2015-03-03 23:19:55 -06:00
|
|
|
btreemap![1 => 2] => vec![
|
|
|
|
Token::MapStart(1),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
btreemap![1 => 2, 3 => 4] => vec![
|
|
|
|
Token::MapStart(2),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::I32(4),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]] => vec![
|
|
|
|
Token::MapStart(2),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
Token::MapStart(2),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::I32(4),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::MapEnd,
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
2015-03-20 10:47:33 -05:00
|
|
|
BTreeMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
BTreeMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
2015-03-03 23:19:55 -06:00
|
|
|
}
|
2015-03-20 22:35:01 -05:00
|
|
|
test_hashmap {
|
|
|
|
HashMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
HashMap::<isize, isize>::new() => vec![
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
hashmap![1 => 2] => vec![
|
|
|
|
Token::MapStart(1),
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
hashmap![1 => 2, 3 => 4] => vec![
|
|
|
|
Token::MapStart(2),
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(3),
|
|
|
|
Token::I32(4),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
hashmap![1 => hashmap![], 2 => hashmap![3 => 4, 5 => 6]] => vec![
|
|
|
|
Token::MapStart(2),
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(1),
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(2),
|
|
|
|
Token::MapStart(2),
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(3),
|
|
|
|
Token::I32(4),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::MapEnd,
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
HashMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::Unit,
|
|
|
|
],
|
|
|
|
HashMap::<isize, isize>::new() => vec![
|
|
|
|
Token::Name("Anything"),
|
|
|
|
Token::MapStart(0),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-07-04 14:11:20 -05:00
|
|
|
test_struct {
|
|
|
|
Struct { a: 1, b: 2, c: 3 } => vec![
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::MapStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
2015-07-04 14:11:20 -05:00
|
|
|
Struct { a: 1, b: 2, c: 3 } => vec![
|
|
|
|
Token::Name("Struct"),
|
2015-03-20 10:32:33 -05:00
|
|
|
Token::MapStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
}
|
2015-03-16 00:04:17 -05:00
|
|
|
test_enum_unit {
|
2015-03-03 23:19:55 -06:00
|
|
|
Enum::Unit => vec![
|
2015-03-16 00:04:17 -05:00
|
|
|
Token::EnumStart("Enum"),
|
|
|
|
Token::Str("Unit"),
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Unit,
|
|
|
|
Token::EnumEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_enum_seq {
|
|
|
|
Enum::Seq(1, 2, 3) => vec![
|
2015-03-16 00:04:17 -05:00
|
|
|
Token::EnumStart("Enum"),
|
|
|
|
Token::Str("Seq"),
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::SeqStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::SeqSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::I32(3),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::EnumEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
test_enum_map {
|
|
|
|
Enum::Map { a: 1, b: 2, c: 3 } => vec![
|
2015-03-16 00:04:17 -05:00
|
|
|
Token::EnumStart("Enum"),
|
|
|
|
Token::Str("Map"),
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::MapStart(3),
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
2015-03-20 22:35:01 -05:00
|
|
|
Token::MapSep,
|
2015-03-03 23:19:55 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
|
|
|
Token::MapEnd,
|
|
|
|
Token::EnumEnd,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|