Fix serde to compile on master again
This commit is contained in:
parent
ff8c8c77cc
commit
857723dff6
@ -13,6 +13,8 @@ use serialize::{Decoder, Decodable};
|
||||
|
||||
use serde::de::{Deserializer, Deserialize};
|
||||
|
||||
use Animal::{Dog, Frog};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
@ -36,7 +38,10 @@ pub enum Error {
|
||||
mod decoder {
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Animal, Dog, Frog, Error, SyntaxError, OtherError};
|
||||
use super::{Animal, Error};
|
||||
use super::Animal::{Dog, Frog};
|
||||
use super::Error::{SyntaxError, OtherError};
|
||||
use self::State::{AnimalState, IntState, StringState};
|
||||
|
||||
enum State {
|
||||
AnimalState(Animal),
|
||||
@ -184,7 +189,10 @@ mod decoder {
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
use super::{Animal, Dog, Frog, Error, EndOfStream, SyntaxError};
|
||||
use super::{Animal, Error};
|
||||
use super::Animal::{Dog, Frog};
|
||||
use super::Error::{EndOfStream, SyntaxError};
|
||||
use self::State::{AnimalState, IntState, StringState, EndState};
|
||||
|
||||
use serde::de;
|
||||
|
||||
|
@ -456,13 +456,13 @@ impl Log {
|
||||
Log {
|
||||
timestamp: 2837513946597,
|
||||
zone_id: 123456,
|
||||
zone_plan: FREE,
|
||||
zone_plan: ZonePlan::FREE,
|
||||
http: Http {
|
||||
protocol: HTTP11,
|
||||
protocol: HttpProtocol::HTTP11,
|
||||
status: 200,
|
||||
host_status: 503,
|
||||
up_status: 520,
|
||||
method: GET,
|
||||
method: HttpMethod::GET,
|
||||
content_type: "text/html".to_string(),
|
||||
user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36".to_string(),
|
||||
referer: "https://www.cloudflare.com/".to_string(),
|
||||
@ -472,10 +472,10 @@ impl Log {
|
||||
ip: "1.2.3.4".to_string(),
|
||||
port: 8000,
|
||||
hostname: "www.example.com".to_string(),
|
||||
protocol: HTTPS,
|
||||
protocol: OriginProtocol::HTTPS,
|
||||
},
|
||||
country: US,
|
||||
cache_status: Hit,
|
||||
country: Country::US,
|
||||
cache_status: CacheStatus::Hit,
|
||||
server_ip: "192.168.1.1".to_string(),
|
||||
server_name: "metal.cloudflare.com".to_string(),
|
||||
remote_ip: "10.1.2.3".to_string(),
|
||||
@ -673,58 +673,58 @@ fn bench_copy(b: &mut Bencher) {
|
||||
|
||||
fn manual_no_escape<W: Writer>(mut wr: W, log: &Log) {
|
||||
wr.write_str("{\"timestamp\":").unwrap();
|
||||
(write!(wr, "{}", log.timestamp)).unwrap();
|
||||
(write!(&mut wr, "{}", log.timestamp)).unwrap();
|
||||
wr.write_str(",\"zone_id\":").unwrap();
|
||||
(write!(wr, "{}", log.zone_id)).unwrap();
|
||||
(write!(&mut wr, "{}", log.zone_id)).unwrap();
|
||||
wr.write_str(",\"zone_plan\":").unwrap();
|
||||
(write!(wr, "{}", log.zone_plan as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.zone_plan as uint)).unwrap();
|
||||
|
||||
wr.write_str(",\"http\":{\"protocol\":").unwrap();
|
||||
(write!(wr, "{}", log.http.protocol as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.protocol as uint)).unwrap();
|
||||
wr.write_str(",\"status\":").unwrap();
|
||||
(write!(wr, "{}", log.http.status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.status)).unwrap();
|
||||
wr.write_str(",\"host_status\":").unwrap();
|
||||
(write!(wr, "{}", log.http.host_status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.host_status)).unwrap();
|
||||
wr.write_str(",\"up_status\":").unwrap();
|
||||
(write!(wr, "{}", log.http.up_status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.up_status)).unwrap();
|
||||
wr.write_str(",\"method\":").unwrap();
|
||||
(write!(wr, "{}", log.http.method as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.method as uint)).unwrap();
|
||||
wr.write_str(",\"content_type\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.content_type)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.http.content_type)).unwrap();
|
||||
wr.write_str(",\"user_agent\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.user_agent)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.http.user_agent)).unwrap();
|
||||
wr.write_str(",\"referer\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.referer)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.http.referer)).unwrap();
|
||||
wr.write_str(",\"request_uri\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.request_uri)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.http.request_uri)).unwrap();
|
||||
|
||||
wr.write_str("},\"origin\":{").unwrap();
|
||||
|
||||
wr.write_str("\"ip\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.origin.ip)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.origin.ip)).unwrap();
|
||||
wr.write_str(",\"port\":").unwrap();
|
||||
(write!(wr, "{}", log.origin.port)).unwrap();
|
||||
(write!(&mut wr, "{}", log.origin.port)).unwrap();
|
||||
wr.write_str(",\"hostname\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.origin.hostname)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.origin.hostname)).unwrap();
|
||||
|
||||
wr.write_str(",\"protocol\":").unwrap();
|
||||
(write!(wr, "{}", log.origin.protocol as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.origin.protocol as uint)).unwrap();
|
||||
|
||||
wr.write_str("},\"country\":").unwrap();
|
||||
(write!(wr, "{}", log.country as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.country as uint)).unwrap();
|
||||
wr.write_str(",\"cache_status\":").unwrap();
|
||||
(write!(wr, "{}", log.cache_status as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.cache_status as uint)).unwrap();
|
||||
wr.write_str(",\"server_ip\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.server_ip)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.server_ip)).unwrap();
|
||||
wr.write_str(",\"server_name\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.server_name)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.server_name)).unwrap();
|
||||
wr.write_str(",\"remote_ip\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.remote_ip)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.remote_ip)).unwrap();
|
||||
wr.write_str(",\"bytes_dlv\":").unwrap();
|
||||
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
||||
(write!(&mut wr, "{}", log.bytes_dlv)).unwrap();
|
||||
|
||||
wr.write_str(",\"ray_id\":").unwrap();
|
||||
(write!(wr, "\"{}\"", log.ray_id)).unwrap();
|
||||
(write!(&mut wr, "\"{}\"", log.ray_id)).unwrap();
|
||||
wr.write_str("}").unwrap();
|
||||
}
|
||||
|
||||
@ -732,38 +732,38 @@ fn manual_escape<W: Writer>(mut wr: W, log: &Log) {
|
||||
wr.write_str("{").unwrap();
|
||||
escape_str(&mut wr, "timestamp").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.timestamp)).unwrap();
|
||||
(write!(&mut wr, "{}", log.timestamp)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "zone_id").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.zone_id)).unwrap();
|
||||
(write!(&mut wr, "{}", log.zone_id)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "zone_plan").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.zone_plan as int)).unwrap();
|
||||
(write!(&mut wr, "{}", log.zone_plan as int)).unwrap();
|
||||
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "http").unwrap();
|
||||
wr.write_str(":{").unwrap();
|
||||
escape_str(&mut wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.http.protocol as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.protocol as uint)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.http.status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "host_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.http.host_status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.host_status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "up_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.http.up_status)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.up_status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "method").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.http.method as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.http.method as uint)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "content_type").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -791,7 +791,7 @@ fn manual_escape<W: Writer>(mut wr: W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "port").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.origin.port)).unwrap();
|
||||
(write!(&mut wr, "{}", log.origin.port)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "hostname").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -799,16 +799,16 @@ fn manual_escape<W: Writer>(mut wr: W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.origin.protocol as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.origin.protocol as uint)).unwrap();
|
||||
|
||||
wr.write_str("},").unwrap();
|
||||
escape_str(&mut wr, "country").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.country as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.country as uint)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "cache_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.cache_status as uint)).unwrap();
|
||||
(write!(&mut wr, "{}", log.cache_status as uint)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "server_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
@ -824,7 +824,7 @@ fn manual_escape<W: Writer>(mut wr: W, log: &Log) {
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "bytes_dlv").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
||||
(write!(&mut wr, "{}", log.bytes_dlv)).unwrap();
|
||||
|
||||
wr.write_str(",").unwrap();
|
||||
escape_str(&mut wr, "ray_id").unwrap();
|
||||
|
@ -31,7 +31,9 @@ mod decoder {
|
||||
use std::collections::hash_map::MoveEntries;
|
||||
use serialize;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError, OtherError};
|
||||
use super::Error;
|
||||
use super::Error::{EndOfStream, SyntaxError, OtherError};
|
||||
use self::Value::{StringValue, IntValue};
|
||||
|
||||
enum Value {
|
||||
StringValue(String),
|
||||
@ -172,7 +174,9 @@ mod deserializer {
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::MoveEntries;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
use super::Error;
|
||||
use super::Error::{EndOfStream, SyntaxError};
|
||||
use self::State::{StartState, KeyOrEndState, ValueState, EndState};
|
||||
|
||||
use serde::de;
|
||||
|
||||
|
@ -48,7 +48,20 @@ mod decoder {
|
||||
use std::collections::HashMap;
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Outer, Inner, Error, SyntaxError, OtherError};
|
||||
use super::{Outer, Inner, Error};
|
||||
|
||||
use self::State::{
|
||||
OuterState,
|
||||
InnerState,
|
||||
NullState,
|
||||
UintState,
|
||||
CharState,
|
||||
StringState,
|
||||
FieldState,
|
||||
VecState,
|
||||
MapState,
|
||||
OptionState,
|
||||
};
|
||||
|
||||
#[deriving(Show)]
|
||||
enum State {
|
||||
@ -80,7 +93,7 @@ mod decoder {
|
||||
|
||||
impl Decoder<Error> for OuterDecoder {
|
||||
fn error(&mut self, msg: &str) -> Error {
|
||||
OtherError(msg.to_string())
|
||||
Error::OtherError(msg.to_string())
|
||||
}
|
||||
|
||||
// Primitive types:
|
||||
@ -88,64 +101,64 @@ mod decoder {
|
||||
fn read_nil(&mut self) -> Result<(), Error> {
|
||||
match self.stack.pop() {
|
||||
Some(NullState) => Ok(()),
|
||||
_ => Err(SyntaxError("NullState".to_string())),
|
||||
_ => Err(Error::SyntaxError("NullState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_uint(&mut self) -> Result<uint, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(UintState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError("UintState".to_string())),
|
||||
_ => Err(Error::SyntaxError("UintState".to_string())),
|
||||
}
|
||||
}
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
#[inline]
|
||||
fn read_char(&mut self) -> Result<char, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(CharState(c)) => Ok(c),
|
||||
_ => Err(SyntaxError("".to_string())),
|
||||
_ => Err(Error::SyntaxError("".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<String, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(StringState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError("".to_string())),
|
||||
_ => Err(Error::SyntaxError("".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
|
||||
#[inline]
|
||||
fn read_struct<T>(&mut self, s_name: &str, _len: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
@ -156,7 +169,7 @@ mod decoder {
|
||||
self.stack.push(FieldState("inner"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError("expected Outer".to_string()))
|
||||
Err(Error::SyntaxError("expected Outer".to_string()))
|
||||
}
|
||||
}
|
||||
Some(InnerState(Inner { a: (), b, c })) => {
|
||||
@ -171,10 +184,10 @@ mod decoder {
|
||||
self.stack.push(FieldState("a"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError("expected Inner".to_string()))
|
||||
Err(Error::SyntaxError("expected Inner".to_string()))
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError("expected InnerState or OuterState".to_string())),
|
||||
_ => Err(Error::SyntaxError("expected InnerState or OuterState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -184,32 +197,32 @@ mod decoder {
|
||||
if f_name == name {
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError("expected FieldState".to_string()))
|
||||
Err(Error::SyntaxError("expected FieldState".to_string()))
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError("expected FieldState".to_string()))
|
||||
_ => Err(Error::SyntaxError("expected FieldState".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
fn read_tuple<T>(&mut self, _len: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_tuple<T>(&mut self, _len: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_len: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
-> Result<T, Error> { Err(Error::SyntaxError("".to_string())) }
|
||||
|
||||
// Specialized types:
|
||||
#[inline]
|
||||
fn read_option<T>(&mut self, f: |&mut OuterDecoder, bool| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(OptionState(b)) => f(self, b),
|
||||
_ => Err(SyntaxError("expected OptionState".to_string())),
|
||||
_ => Err(Error::SyntaxError("expected OptionState".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,7 +236,7 @@ mod decoder {
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError("expected VecState".to_string()))
|
||||
_ => Err(Error::SyntaxError("expected VecState".to_string()))
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -250,7 +263,7 @@ mod decoder {
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError("expected MapState".to_string())),
|
||||
_ => Err(Error::SyntaxError("expected MapState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -269,9 +282,24 @@ mod decoder {
|
||||
mod deserializer {
|
||||
use std::collections::HashMap;
|
||||
use super::{Outer, Inner};
|
||||
use super::{Error, EndOfStream, SyntaxError, UnexpectedName, MissingField};
|
||||
use super::Error;
|
||||
use serde::de;
|
||||
|
||||
use self::State::{
|
||||
OuterState,
|
||||
InnerState,
|
||||
FieldState,
|
||||
NullState,
|
||||
UintState,
|
||||
CharState,
|
||||
StringState,
|
||||
OptionState,
|
||||
//TupleState(uint),
|
||||
VecState,
|
||||
MapState,
|
||||
EndState,
|
||||
};
|
||||
|
||||
#[deriving(Show)]
|
||||
enum State {
|
||||
OuterState(Outer),
|
||||
@ -286,7 +314,6 @@ mod deserializer {
|
||||
VecState(Vec<Inner>),
|
||||
MapState(HashMap<String, Option<char>>),
|
||||
EndState,
|
||||
|
||||
}
|
||||
|
||||
pub struct OuterDeserializer {
|
||||
@ -367,29 +394,29 @@ mod deserializer {
|
||||
impl de::Deserializer<Error> for OuterDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error(&mut self) -> Error {
|
||||
EndOfStream
|
||||
Error::EndOfStream
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> Error {
|
||||
SyntaxError(format!("expected {}, found {}", expected, token))
|
||||
Error::SyntaxError(format!("expected {}, found {}", expected, token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, token: de::Token) -> Error {
|
||||
UnexpectedName(format!("found {}", token))
|
||||
Error::UnexpectedName(format!("found {}", token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn conversion_error(&mut self, token: de::Token) -> Error {
|
||||
UnexpectedName(format!("found {}", token))
|
||||
Error::UnexpectedName(format!("found {}", token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserialize<OuterDeserializer, Error>
|
||||
>(&mut self, field: &'static str) -> Result<T, Error> {
|
||||
Err(MissingField(field))
|
||||
Err(Error::MissingField(field))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,8 @@ mod decoder {
|
||||
use std::vec;
|
||||
use serialize;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError, OtherError};
|
||||
use super::Error;
|
||||
use super::Error::{EndOfStream, SyntaxError, OtherError};
|
||||
|
||||
pub struct IntDecoder {
|
||||
len: uint,
|
||||
@ -250,7 +251,9 @@ mod deserializer {
|
||||
//use std::num;
|
||||
use std::vec;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
use super::Error;
|
||||
use super::Error::{EndOfStream, SyntaxError};
|
||||
use self::State::{StartState, SepOrEndState, EndState};
|
||||
|
||||
use serde::de;
|
||||
|
||||
|
@ -409,10 +409,11 @@ fn deserialize_enum(
|
||||
let arms: Vec<ast::Arm> = fields.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(name, span, ref parts))| {
|
||||
let path = cx.path(span, vec![type_ident, name]);
|
||||
let call = deserialize_static_fields(
|
||||
cx,
|
||||
span,
|
||||
name,
|
||||
path,
|
||||
serial_names.as_slice(),
|
||||
parts,
|
||||
|cx, _, _| {
|
||||
@ -425,7 +426,7 @@ fn deserialize_enum(
|
||||
.collect();
|
||||
|
||||
quote_expr!(cx, {
|
||||
let i = try!($deserializer.expect_enum_start($token, $type_name, $variants));
|
||||
let i = try!($deserializer.expect_enum_start($token, $type_name, &$variants));
|
||||
|
||||
let result = match i {
|
||||
$arms
|
||||
@ -444,15 +445,16 @@ fn deserialize_enum(
|
||||
fn deserialize_static_fields(
|
||||
cx: &ExtCtxt,
|
||||
span: Span,
|
||||
outer_pat_ident: Ident,
|
||||
outer_pat_path: ast::Path,
|
||||
serial_names: &[Option<token::InternedString>],
|
||||
fields: &StaticFields,
|
||||
getarg: |&ExtCtxt, Span, token::InternedString| -> P<Expr>
|
||||
) -> P<Expr> {
|
||||
match *fields {
|
||||
Unnamed(ref fields) => {
|
||||
let path_expr = cx.expr_path(outer_pat_path);
|
||||
if fields.is_empty() {
|
||||
cx.expr_ident(span, outer_pat_ident)
|
||||
path_expr
|
||||
} else {
|
||||
let fields = fields.iter().enumerate().map(|(i, &span)| {
|
||||
getarg(
|
||||
@ -462,7 +464,7 @@ fn deserialize_static_fields(
|
||||
)
|
||||
}).collect();
|
||||
|
||||
cx.expr_call_ident(span, outer_pat_ident, fields)
|
||||
cx.expr_call(span, path_expr, fields)
|
||||
}
|
||||
}
|
||||
Named(ref fields) => {
|
||||
@ -480,7 +482,7 @@ fn deserialize_static_fields(
|
||||
cx.field_imm(span, name, arg)
|
||||
}).collect();
|
||||
|
||||
cx.expr_struct_ident(span, outer_pat_ident, fields)
|
||||
cx.expr_struct(span, outer_pat_path, fields)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
use std::collections::TreeMap;
|
||||
use std::str::StrAllocating;
|
||||
|
||||
use json::value::{Value, List, Object, ToJson};
|
||||
use json::value::{ToJson, Value};
|
||||
|
||||
pub struct ListBuilder {
|
||||
list: Vec<Value>,
|
||||
@ -23,7 +23,7 @@ impl ListBuilder {
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
List(self.list)
|
||||
Value::List(self.list)
|
||||
}
|
||||
|
||||
pub fn push<T: ToJson>(self, value: T) -> ListBuilder {
|
||||
@ -53,7 +53,7 @@ impl ObjectBuilder {
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
Object(self.object)
|
||||
Value::Object(self.object)
|
||||
}
|
||||
|
||||
pub fn insert<K: StrAllocating, V: ToJson>(self, key: K, value: V) -> ObjectBuilder {
|
||||
@ -77,25 +77,25 @@ impl ObjectBuilder {
|
||||
mod tests {
|
||||
use std::collections::TreeMap;
|
||||
|
||||
use json::value::{Integer, List, Object};
|
||||
use json::value::Value;
|
||||
use super::{ListBuilder, ObjectBuilder};
|
||||
|
||||
#[test]
|
||||
fn test_list_builder() {
|
||||
let value = ListBuilder::new().unwrap();
|
||||
assert_eq!(value, List(Vec::new()));
|
||||
assert_eq!(value, Value::List(Vec::new()));
|
||||
|
||||
let value = ListBuilder::new()
|
||||
.push(1i)
|
||||
.push(2i)
|
||||
.push(3i)
|
||||
.unwrap();
|
||||
assert_eq!(value, List(vec!(Integer(1), Integer(2), Integer(3))));
|
||||
assert_eq!(value, Value::List(vec!(Value::Integer(1), Value::Integer(2), Value::Integer(3))));
|
||||
|
||||
let value = ListBuilder::new()
|
||||
.push_list(|bld| bld.push(1i).push(2i).push(3i))
|
||||
.unwrap();
|
||||
assert_eq!(value, List(vec!(List(vec!(Integer(1), Integer(2), Integer(3))))));
|
||||
assert_eq!(value, Value::List(vec!(Value::List(vec!(Value::Integer(1), Value::Integer(2), Value::Integer(3))))));
|
||||
|
||||
let value = ListBuilder::new()
|
||||
.push_object(|bld|
|
||||
@ -105,15 +105,15 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
let mut map = TreeMap::new();
|
||||
map.insert("a".to_string(), Integer(1));
|
||||
map.insert("b".to_string(), Integer(2));
|
||||
assert_eq!(value, List(vec!(Object(map))));
|
||||
map.insert("a".to_string(), Value::Integer(1));
|
||||
map.insert("b".to_string(), Value::Integer(2));
|
||||
assert_eq!(value, Value::List(vec!(Value::Object(map))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_object_builder() {
|
||||
let value = ObjectBuilder::new().unwrap();
|
||||
assert_eq!(value, Object(TreeMap::new()));
|
||||
assert_eq!(value, Value::Object(TreeMap::new()));
|
||||
|
||||
let value = ObjectBuilder::new()
|
||||
.insert("a".to_string(), 1i)
|
||||
@ -121,8 +121,8 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
let mut map = TreeMap::new();
|
||||
map.insert("a".to_string(), Integer(1));
|
||||
map.insert("b".to_string(), Integer(2));
|
||||
assert_eq!(value, Object(map));
|
||||
map.insert("a".to_string(), Value::Integer(1));
|
||||
map.insert("b".to_string(), Value::Integer(2));
|
||||
assert_eq!(value, Value::Object(map));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ use std::char;
|
||||
|
||||
use de;
|
||||
|
||||
use super::{Error, ErrorCode};
|
||||
use super::{
|
||||
use super::error::{Error, ErrorCode};
|
||||
use super::error::ErrorCode::{
|
||||
ConversionError,
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
@ -33,10 +33,19 @@ use super::{
|
||||
UnexpectedName,
|
||||
UnknownVariant,
|
||||
};
|
||||
use super::{
|
||||
use super::error::Error::{
|
||||
SyntaxError,
|
||||
};
|
||||
|
||||
use self::ParserState::{
|
||||
ParseValue,
|
||||
ParseListStart,
|
||||
ParseListCommaOrEnd,
|
||||
ParseObjectStart,
|
||||
ParseObjectCommaOrEnd,
|
||||
ParseObjectValue,
|
||||
};
|
||||
|
||||
#[deriving(PartialEq, Show)]
|
||||
enum ParserState {
|
||||
// Parse a value.
|
||||
@ -92,15 +101,15 @@ impl<Iter: Iterator<u8>> Iterator<Result<de::Token, Error>> for Parser<Iter> {
|
||||
ParseListCommaOrEnd => Some(self.parse_list_comma_or_end()),
|
||||
ParseObjectStart => {
|
||||
match self.parse_object_start() {
|
||||
Ok(Some(s)) => Some(Ok(de::String(s.to_string()))),
|
||||
Ok(None) => Some(Ok(de::End)),
|
||||
Ok(Some(s)) => Some(Ok(de::Token::String(s.to_string()))),
|
||||
Ok(None) => Some(Ok(de::Token::End)),
|
||||
Err(err) => Some(Err(err)),
|
||||
}
|
||||
}
|
||||
ParseObjectCommaOrEnd => {
|
||||
match self.parse_object_comma_or_end() {
|
||||
Ok(Some(s)) => Some(Ok(de::String(s.to_string()))),
|
||||
Ok(None) => Some(Ok(de::End)),
|
||||
Ok(Some(s)) => Some(Ok(de::Token::String(s.to_string()))),
|
||||
Ok(None) => Some(Ok(de::Token::End)),
|
||||
Err(err) => Some(Err(err)),
|
||||
}
|
||||
}
|
||||
@ -191,9 +200,9 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
res = try!(self.parse_exponent(res));
|
||||
}
|
||||
|
||||
Ok(de::F64(neg * res))
|
||||
Ok(de::Token::F64(neg * res))
|
||||
} else {
|
||||
Ok(de::I64(neg * res))
|
||||
Ok(de::Token::I64(neg * res))
|
||||
}
|
||||
}
|
||||
|
||||
@ -361,7 +370,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
_ => return self.error(UnexpectedEndOfHexEscape),
|
||||
}
|
||||
|
||||
let buf = [n1, try!(self.decode_hex_escape())];
|
||||
let buf = &[n1, try!(self.decode_hex_escape())];
|
||||
match str::utf16_items(buf.as_slice()).next() {
|
||||
Some(ScalarValue(c)) => c,
|
||||
_ => return self.error(LoneLeadingSurrogateInHexEscape),
|
||||
@ -374,7 +383,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
}
|
||||
};
|
||||
|
||||
let mut buf = [0u8, .. 4];
|
||||
let buf = &mut [0u8, .. 4];
|
||||
let len = c.encode_utf8(buf).unwrap_or(0);
|
||||
self.buf.extend(buf.slice_to(len).iter().map(|b| *b));
|
||||
}
|
||||
@ -404,7 +413,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
|
||||
if self.ch_is(b']') {
|
||||
self.bump();
|
||||
Ok(de::End)
|
||||
Ok(de::Token::End)
|
||||
} else {
|
||||
self.state_stack.push(ParseListCommaOrEnd);
|
||||
self.parse_value()
|
||||
@ -421,7 +430,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
self.parse_value()
|
||||
} else if self.ch_is(b']') {
|
||||
self.bump();
|
||||
Ok(de::End)
|
||||
Ok(de::Token::End)
|
||||
} else if self.eof() {
|
||||
self.error_event(EOFWhileParsingList)
|
||||
} else {
|
||||
@ -500,22 +509,22 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
||||
}
|
||||
|
||||
match self.ch_or_null() {
|
||||
b'n' => self.parse_ident(b"ull", de::Null),
|
||||
b't' => self.parse_ident(b"rue", de::Bool(true)),
|
||||
b'f' => self.parse_ident(b"alse", de::Bool(false)),
|
||||
b'n' => self.parse_ident(b"ull", de::Token::Null),
|
||||
b't' => self.parse_ident(b"rue", de::Token::Bool(true)),
|
||||
b'f' => self.parse_ident(b"alse", de::Token::Bool(false)),
|
||||
b'0' ... b'9' | b'-' => self.parse_number(),
|
||||
b'"' => {
|
||||
Ok(de::String(try!(self.parse_string()).to_string()))
|
||||
Ok(de::Token::String(try!(self.parse_string()).to_string()))
|
||||
}
|
||||
b'[' => {
|
||||
self.bump();
|
||||
self.state_stack.push(ParseListStart);
|
||||
Ok(de::SeqStart(0))
|
||||
Ok(de::Token::SeqStart(0))
|
||||
}
|
||||
b'{' => {
|
||||
self.bump();
|
||||
self.state_stack.push(ParseObjectStart);
|
||||
Ok(de::MapStart(0))
|
||||
Ok(de::Token::MapStart(0))
|
||||
}
|
||||
_ => {
|
||||
self.error_event(ExpectedSomeValue)
|
||||
@ -563,7 +572,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
de::Deserialize::deserialize_token(self, de::Null)
|
||||
de::Deserialize::deserialize_token(self, de::Token::Null)
|
||||
}
|
||||
|
||||
// Special case treating options as a nullable value.
|
||||
@ -572,7 +581,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||
U: de::Deserialize<Parser<Iter>, Error>
|
||||
>(&mut self, token: de::Token) -> Result<Option<U>, Error> {
|
||||
match token {
|
||||
de::Null => Ok(None),
|
||||
de::Token::Null => Ok(None),
|
||||
token => {
|
||||
let value: U = try!(de::Deserialize::deserialize_token(self, token));
|
||||
Ok(Some(value))
|
||||
@ -587,19 +596,19 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||
_name: &str,
|
||||
variants: &[&str]) -> Result<uint, Error> {
|
||||
match token {
|
||||
de::MapStart(_) => { }
|
||||
de::Token::MapStart(_) => { }
|
||||
_ => { return self.error(ExpectedEnumMapStart); }
|
||||
};
|
||||
|
||||
// Enums only have one field in them, which is the variant name.
|
||||
let variant = match try!(self.expect_token()) {
|
||||
de::String(variant) => variant,
|
||||
de::Token::String(variant) => variant,
|
||||
_ => { return self.error(ExpectedEnumVariantString); }
|
||||
};
|
||||
|
||||
// The variant's field is a list of the values.
|
||||
match try!(self.expect_token()) {
|
||||
de::SeqStart(_) => { }
|
||||
de::Token::SeqStart(_) => { }
|
||||
_ => { return self.error(ExpectedEnumToken); }
|
||||
}
|
||||
|
||||
@ -612,9 +621,9 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||
fn expect_enum_end(&mut self) -> Result<(), Error> {
|
||||
// There will be one `End` for the list, and one for the object.
|
||||
match try!(self.expect_token()) {
|
||||
de::End => {
|
||||
de::Token::End => {
|
||||
match try!(self.expect_token()) {
|
||||
de::End => Ok(()),
|
||||
de::Token::End => Ok(()),
|
||||
_ => self.error(ExpectedEnumEndToken),
|
||||
}
|
||||
}
|
||||
@ -625,10 +634,10 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||
#[inline]
|
||||
fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), Error> {
|
||||
match token {
|
||||
de::MapStart(_) => Ok(()),
|
||||
de::Token::MapStart(_) => Ok(()),
|
||||
_ => {
|
||||
static EXPECTED_TOKENS: &'static [de::TokenKind] = [
|
||||
de::MapStartKind,
|
||||
static EXPECTED_TOKENS: &'static [de::TokenKind] = &[
|
||||
de::TokenKind::MapStartKind,
|
||||
];
|
||||
Err(self.syntax_error(token, EXPECTED_TOKENS))
|
||||
}
|
||||
|
125
src/json/error.rs
Normal file
125
src/json/error.rs
Normal file
@ -0,0 +1,125 @@
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
||||
use de::{Token, TokenKind};
|
||||
|
||||
/// The errors that can arise while parsing a JSON stream.
|
||||
#[deriving(Clone, PartialEq)]
|
||||
pub enum ErrorCode {
|
||||
ConversionError(Token),
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
EOFWhileParsingString,
|
||||
EOFWhileParsingValue,
|
||||
ExpectedColon,
|
||||
ExpectedConversion,
|
||||
ExpectedEnumEnd,
|
||||
ExpectedEnumEndToken,
|
||||
ExpectedEnumMapStart,
|
||||
ExpectedEnumToken,
|
||||
ExpectedEnumVariantString,
|
||||
ExpectedListCommaOrEnd,
|
||||
ExpectedName,
|
||||
ExpectedObjectCommaOrEnd,
|
||||
ExpectedSomeIdent,
|
||||
ExpectedSomeValue,
|
||||
ExpectedTokens(Token, &'static [TokenKind]),
|
||||
InvalidEscape,
|
||||
InvalidNumber,
|
||||
InvalidUnicodeCodePoint,
|
||||
KeyMustBeAString,
|
||||
LoneLeadingSurrogateInHexEscape,
|
||||
MissingField(&'static str),
|
||||
NotFourDigit,
|
||||
NotUtf8,
|
||||
TrailingCharacters,
|
||||
UnexpectedEndOfHexEscape,
|
||||
UnexpectedName(Token),
|
||||
UnknownVariant,
|
||||
UnrecognizedHex,
|
||||
}
|
||||
|
||||
impl fmt::Show for ErrorCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ErrorCode::ConversionError(ref token) => write!(f, "failed to convert {}", token),
|
||||
ErrorCode::EOFWhileParsingList => "EOF While parsing list".fmt(f),
|
||||
ErrorCode::EOFWhileParsingObject => "EOF While parsing object".fmt(f),
|
||||
ErrorCode::EOFWhileParsingString => "EOF While parsing string".fmt(f),
|
||||
ErrorCode::EOFWhileParsingValue => "EOF While parsing value".fmt(f),
|
||||
ErrorCode::ExpectedColon => "expected `:`".fmt(f),
|
||||
ErrorCode::ExpectedConversion => "expected conversion".fmt(f),
|
||||
ErrorCode::ExpectedEnumEnd => "expected enum end".fmt(f),
|
||||
ErrorCode::ExpectedEnumEndToken => "expected enum map end".fmt(f),
|
||||
ErrorCode::ExpectedEnumMapStart => "expected enum map start".fmt(f),
|
||||
ErrorCode::ExpectedEnumToken => "expected enum token".fmt(f),
|
||||
ErrorCode::ExpectedEnumVariantString => "expected variant".fmt(f),
|
||||
ErrorCode::ExpectedListCommaOrEnd => "expected `,` or `]`".fmt(f),
|
||||
ErrorCode::ExpectedName => "expected name".fmt(f),
|
||||
ErrorCode::ExpectedObjectCommaOrEnd => "expected `,` or `}`".fmt(f),
|
||||
ErrorCode::ExpectedSomeIdent => "expected ident".fmt(f),
|
||||
ErrorCode::ExpectedSomeValue => "expected value".fmt(f),
|
||||
ErrorCode::ExpectedTokens(ref token, tokens) => write!(f, "expected {}, found {}", tokens, token),
|
||||
ErrorCode::InvalidEscape => "invalid escape".fmt(f),
|
||||
ErrorCode::InvalidNumber => "invalid number".fmt(f),
|
||||
ErrorCode::InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f),
|
||||
ErrorCode::KeyMustBeAString => "key must be a string".fmt(f),
|
||||
ErrorCode::LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f),
|
||||
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
|
||||
ErrorCode::NotFourDigit => "invalid \\u escape (not four digits)".fmt(f),
|
||||
ErrorCode::NotUtf8 => "contents not utf-8".fmt(f),
|
||||
ErrorCode::TrailingCharacters => "trailing characters".fmt(f),
|
||||
ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f),
|
||||
ErrorCode::UnexpectedName(ref name) => write!(f, "unexpected name {}", name),
|
||||
ErrorCode::UnknownVariant => "unknown variant".fmt(f),
|
||||
ErrorCode::UnrecognizedHex => "invalid \\u escape (unrecognized hex)".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
pub enum Error {
|
||||
/// msg, line, col
|
||||
SyntaxError(ErrorCode, uint, uint),
|
||||
IoError(io::IoError),
|
||||
ExpectedError(String, String),
|
||||
MissingFieldError(String),
|
||||
UnknownVariantError(String),
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::SyntaxError(..) => "syntax error",
|
||||
Error::IoError(ref error) => error.description(),
|
||||
Error::ExpectedError(ref expected, _) => expected.as_slice(),
|
||||
Error::MissingFieldError(_) => "missing field",
|
||||
Error::UnknownVariantError(_) => "unknown variant",
|
||||
}
|
||||
}
|
||||
|
||||
fn detail(&self) -> Option<String> {
|
||||
match *self {
|
||||
Error::SyntaxError(ref code, line, col) => {
|
||||
Some(format!("{} at line {} column {}", code, line, col))
|
||||
}
|
||||
Error::IoError(ref error) => error.detail(),
|
||||
Error::ExpectedError(ref expected, ref found) => {
|
||||
Some(format!("expected {}, found {}", expected, found))
|
||||
}
|
||||
Error::MissingFieldError(ref field) => {
|
||||
Some(format!("missing field {}", field))
|
||||
}
|
||||
Error::UnknownVariantError(ref variant) => {
|
||||
Some(format!("unknown variant {}", variant))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::FromError<io::IoError> for Error {
|
||||
fn from_error(error: io::IoError) -> Error {
|
||||
Error::IoError(error)
|
||||
}
|
||||
}
|
556
src/json/mod.rs
556
src/json/mod.rs
File diff suppressed because it is too large
Load Diff
@ -44,7 +44,7 @@ pub fn escape_str<W: Writer>(wr: &mut W, v: &str) -> IoResult<()> {
|
||||
}
|
||||
|
||||
fn escape_char<W: Writer>(wr: &mut W, v: char) -> IoResult<()> {
|
||||
let mut buf = [0, .. 4];
|
||||
let buf = &mut [0, .. 4];
|
||||
v.encode_utf8(buf);
|
||||
escape_bytes(wr, buf)
|
||||
}
|
||||
@ -65,7 +65,7 @@ fn fmt_f64_or_null<W: Writer>(wr: &mut W, v: f64) -> IoResult<()> {
|
||||
|
||||
fn spaces<W: Writer>(wr: &mut W, mut n: uint) -> IoResult<()> {
|
||||
const LEN: uint = 16;
|
||||
const BUF: [u8, ..LEN] = [b' ', ..LEN];
|
||||
const BUF: &'static [u8, ..LEN] = &[b' ', ..LEN];
|
||||
|
||||
while n >= LEN {
|
||||
try!(wr.write(BUF));
|
||||
@ -128,52 +128,52 @@ impl<W: Writer> ser::Serializer<IoError> for Serializer<W> {
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -391,52 +391,52 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> IoResult<()> {
|
||||
write!(self.wr, "{}", v)
|
||||
write!(&mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -6,23 +6,12 @@ use std::str;
|
||||
use std::string;
|
||||
use std::vec;
|
||||
|
||||
use de;
|
||||
use de::{mod, Token, TokenKind};
|
||||
use ser::Serialize;
|
||||
use ser;
|
||||
|
||||
use super::PrettySerializer;
|
||||
use super::Serializer;
|
||||
use super::Error;
|
||||
use super::{
|
||||
ConversionError,
|
||||
EOFWhileParsingValue,
|
||||
ExpectedError,
|
||||
ExpectedTokens,
|
||||
MissingFieldError,
|
||||
SyntaxError,
|
||||
UnexpectedName,
|
||||
UnknownVariantError,
|
||||
};
|
||||
use super::ser::{Serializer, PrettySerializer};
|
||||
use super::error::{Error, ErrorCode};
|
||||
|
||||
/// Represents a JSON value
|
||||
#[deriving(Clone, PartialEq, PartialOrd)]
|
||||
@ -61,7 +50,7 @@ impl Value {
|
||||
/// Otherwise, returns None.
|
||||
pub fn find<'a>(&'a self, key: &string::String) -> Option<&'a Value>{
|
||||
match self {
|
||||
&Object(ref map) => map.get(key),
|
||||
&Value::Object(ref map) => map.get(key),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -85,7 +74,7 @@ impl Value {
|
||||
/// or the Json value is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &string::String) -> Option<&'a Value> {
|
||||
match self {
|
||||
&Object(ref map) => {
|
||||
&Value::Object(ref map) => {
|
||||
match map.get(key) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
@ -113,7 +102,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_object<'a>(&'a self) -> Option<&'a TreeMap<string::String, Value>> {
|
||||
match *self {
|
||||
Object(ref map) => Some(map),
|
||||
Value::Object(ref map) => Some(map),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -127,7 +116,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_list<'a>(&'a self) -> Option<&'a Vec<Value>> {
|
||||
match *self {
|
||||
List(ref list) => Some(list),
|
||||
Value::List(ref list) => Some(list),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -141,7 +130,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_string<'a>(&'a self) -> Option<&'a str> {
|
||||
match *self {
|
||||
String(ref s) => Some(s.as_slice()),
|
||||
Value::String(ref s) => Some(s.as_slice()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -149,7 +138,7 @@ impl Value {
|
||||
/// Returns true if the Json value is a i64 or f64. Returns false otherwise.
|
||||
pub fn is_number(&self) -> bool {
|
||||
match *self {
|
||||
Integer(_) | Floating(_) => true,
|
||||
Value::Integer(_) | Value::Floating(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -157,7 +146,7 @@ impl Value {
|
||||
/// Returns true if the Json value is a i64. Returns false otherwise.
|
||||
pub fn is_i64(&self) -> bool {
|
||||
match *self {
|
||||
Integer(_) => true,
|
||||
Value::Integer(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -165,7 +154,7 @@ impl Value {
|
||||
/// Returns true if the Json value is a f64. Returns false otherwise.
|
||||
pub fn is_f64(&self) -> bool {
|
||||
match *self {
|
||||
Floating(_) => true,
|
||||
Value::Floating(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -174,8 +163,8 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_i64(&self) -> Option<i64> {
|
||||
match *self {
|
||||
Integer(n) => Some(n),
|
||||
Floating(n) => Some(n as i64),
|
||||
Value::Integer(n) => Some(n),
|
||||
Value::Floating(n) => Some(n as i64),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -184,8 +173,8 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_f64(&self) -> Option<f64> {
|
||||
match *self {
|
||||
Integer(n) => Some(n as f64),
|
||||
Floating(n) => Some(n),
|
||||
Value::Integer(n) => Some(n as f64),
|
||||
Value::Floating(n) => Some(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -199,7 +188,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_boolean(&self) -> Option<bool> {
|
||||
match *self {
|
||||
Boolean(b) => Some(b),
|
||||
Value::Boolean(b) => Some(b),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -213,7 +202,7 @@ impl Value {
|
||||
/// Returns None otherwise.
|
||||
pub fn as_null(&self) -> Option<()> {
|
||||
match *self {
|
||||
Null => Some(()),
|
||||
Value::Null => Some(()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@ -231,7 +220,7 @@ impl<'a, 'b> Writer for WriterFormatter<'a, 'b> {
|
||||
impl fmt::Show for Value {
|
||||
/// Serializes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.to_writer(WriterFormatter(f)).map_err(|_| fmt::WriteError)
|
||||
self.to_writer(WriterFormatter(f)).map_err(|_| fmt::Error)
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,25 +228,25 @@ impl<S: ser::Serializer<E>, E> ser::Serialize<S, E> for Value {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
match *self {
|
||||
Null => {
|
||||
Value::Null => {
|
||||
().serialize(s)
|
||||
}
|
||||
Boolean(v) => {
|
||||
Value::Boolean(v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
Integer(v) => {
|
||||
Value::Integer(v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
Floating(v) => {
|
||||
Value::Floating(v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
String(ref v) => {
|
||||
Value::String(ref v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
List(ref v) => {
|
||||
Value::List(ref v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
Object(ref v) => {
|
||||
Value::Object(ref v) => {
|
||||
v.serialize(s)
|
||||
}
|
||||
}
|
||||
@ -266,124 +255,124 @@ impl<S: ser::Serializer<E>, E> ser::Serialize<S, E> for Value {
|
||||
|
||||
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for Value {
|
||||
#[inline]
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<Value, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Value, E> {
|
||||
match token {
|
||||
de::Null => Ok(Null),
|
||||
de::Bool(x) => Ok(Boolean(x)),
|
||||
de::Int(x) => Ok(Integer(x as i64)),
|
||||
de::I8(x) => Ok(Integer(x as i64)),
|
||||
de::I16(x) => Ok(Integer(x as i64)),
|
||||
de::I32(x) => Ok(Integer(x as i64)),
|
||||
de::I64(x) => Ok(Integer(x)),
|
||||
de::Uint(x) => Ok(Integer(x as i64)),
|
||||
de::U8(x) => Ok(Integer(x as i64)),
|
||||
de::U16(x) => Ok(Integer(x as i64)),
|
||||
de::U32(x) => Ok(Integer(x as i64)),
|
||||
de::U64(x) => Ok(Integer(x as i64)),
|
||||
de::F32(x) => Ok(Floating(x as f64)),
|
||||
de::F64(x) => Ok(Floating(x)),
|
||||
de::Char(x) => Ok(String(x.to_string())),
|
||||
de::Str(x) => Ok(String(x.to_string())),
|
||||
de::String(x) => Ok(String(x)),
|
||||
de::Option(false) => Ok(Null),
|
||||
de::Option(true) => de::Deserialize::deserialize(d),
|
||||
de::TupleStart(_) | de::SeqStart(_) => {
|
||||
Token::Null => Ok(Value::Null),
|
||||
Token::Bool(x) => Ok(Value::Boolean(x)),
|
||||
Token::Int(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::I8(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::I16(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::I32(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::I64(x) => Ok(Value::Integer(x)),
|
||||
Token::Uint(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::U8(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::U16(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::U32(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::U64(x) => Ok(Value::Integer(x as i64)),
|
||||
Token::F32(x) => Ok(Value::Floating(x as f64)),
|
||||
Token::F64(x) => Ok(Value::Floating(x)),
|
||||
Token::Char(x) => Ok(Value::String(x.to_string())),
|
||||
Token::Str(x) => Ok(Value::String(x.to_string())),
|
||||
Token::String(x) => Ok(Value::String(x)),
|
||||
Token::Option(false) => Ok(Value::Null),
|
||||
Token::Option(true) => de::Deserialize::deserialize(d),
|
||||
Token::TupleStart(_) | Token::SeqStart(_) => {
|
||||
let list = try!(de::Deserialize::deserialize_token(d, token));
|
||||
Ok(List(list))
|
||||
Ok(Value::List(list))
|
||||
}
|
||||
de::StructStart(_, _) | de::MapStart(_) => {
|
||||
Token::StructStart(_, _) | Token::MapStart(_) => {
|
||||
let object = try!(de::Deserialize::deserialize_token(d, token));
|
||||
Ok(Object(object))
|
||||
Ok(Value::Object(object))
|
||||
}
|
||||
de::EnumStart(_, name, len) => {
|
||||
let token = de::SeqStart(len);
|
||||
Token::EnumStart(_, name, len) => {
|
||||
let token = Token::SeqStart(len);
|
||||
let fields: Vec<Value> = try!(de::Deserialize::deserialize_token(d, token));
|
||||
let mut object = TreeMap::new();
|
||||
object.insert(name.to_string(), List(fields));
|
||||
Ok(Object(object))
|
||||
object.insert(name.to_string(), Value::List(fields));
|
||||
Ok(Value::Object(object))
|
||||
}
|
||||
de::End => {
|
||||
static EXPECTED_TOKENS: &'static [de::TokenKind] = [
|
||||
de::EndKind,
|
||||
Token::End => {
|
||||
static EXPECTED_TOKENS: &'static [TokenKind] = &[
|
||||
TokenKind::EndKind,
|
||||
];
|
||||
Err(d.syntax_error(de::End, EXPECTED_TOKENS))
|
||||
Err(d.syntax_error(Token::End, EXPECTED_TOKENS))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum DeserializerState {
|
||||
DeserializerValueState(Value),
|
||||
DeserializerListState(vec::MoveItems<Value>),
|
||||
DeserializerObjectState(tree_map::MoveEntries<string::String, Value>),
|
||||
DeserializerEndState,
|
||||
enum State {
|
||||
Value(Value),
|
||||
List(vec::MoveItems<Value>),
|
||||
Object(tree_map::MoveEntries<string::String, Value>),
|
||||
End,
|
||||
}
|
||||
|
||||
pub struct Deserializer {
|
||||
stack: Vec<DeserializerState>,
|
||||
stack: Vec<State>,
|
||||
}
|
||||
|
||||
impl Deserializer {
|
||||
/// Creates a new deserializer instance for deserializing the specified JSON value.
|
||||
pub fn new(json: Value) -> Deserializer {
|
||||
Deserializer {
|
||||
stack: vec!(DeserializerValueState(json)),
|
||||
stack: vec!(State::Value(json)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<de::Token, Error>> for Deserializer {
|
||||
impl Iterator<Result<Token, Error>> for Deserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||
fn next(&mut self) -> Option<Result<Token, Error>> {
|
||||
loop {
|
||||
match self.stack.pop() {
|
||||
Some(DeserializerValueState(value)) => {
|
||||
Some(State::Value(value)) => {
|
||||
let token = match value {
|
||||
Null => de::Null,
|
||||
Boolean(x) => de::Bool(x),
|
||||
Integer(x) => de::I64(x),
|
||||
Floating(x) => de::F64(x),
|
||||
String(x) => de::String(x),
|
||||
List(x) => {
|
||||
Value::Null => Token::Null,
|
||||
Value::Boolean(x) => Token::Bool(x),
|
||||
Value::Integer(x) => Token::I64(x),
|
||||
Value::Floating(x) => Token::F64(x),
|
||||
Value::String(x) => Token::String(x),
|
||||
Value::List(x) => {
|
||||
let len = x.len();
|
||||
self.stack.push(DeserializerListState(x.into_iter()));
|
||||
de::SeqStart(len)
|
||||
self.stack.push(State::List(x.into_iter()));
|
||||
Token::SeqStart(len)
|
||||
}
|
||||
Object(x) => {
|
||||
Value::Object(x) => {
|
||||
let len = x.len();
|
||||
self.stack.push(DeserializerObjectState(x.into_iter()));
|
||||
de::MapStart(len)
|
||||
self.stack.push(State::Object(x.into_iter()));
|
||||
Token::MapStart(len)
|
||||
}
|
||||
};
|
||||
|
||||
return Some(Ok(token));
|
||||
}
|
||||
Some(DeserializerListState(mut iter)) => {
|
||||
Some(State::List(mut iter)) => {
|
||||
match iter.next() {
|
||||
Some(value) => {
|
||||
self.stack.push(DeserializerListState(iter));
|
||||
self.stack.push(DeserializerValueState(value));
|
||||
self.stack.push(State::List(iter));
|
||||
self.stack.push(State::Value(value));
|
||||
// loop around.
|
||||
}
|
||||
None => {
|
||||
return Some(Ok(de::End));
|
||||
return Some(Ok(Token::End));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(DeserializerObjectState(mut iter)) => {
|
||||
Some(State::Object(mut iter)) => {
|
||||
match iter.next() {
|
||||
Some((key, value)) => {
|
||||
self.stack.push(DeserializerObjectState(iter));
|
||||
self.stack.push(DeserializerValueState(value));
|
||||
return Some(Ok(de::String(key)));
|
||||
self.stack.push(State::Object(iter));
|
||||
self.stack.push(State::Value(value));
|
||||
return Some(Ok(Token::String(key)));
|
||||
}
|
||||
None => {
|
||||
return Some(Ok(de::End));
|
||||
return Some(Ok(Token::End));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(DeserializerEndState) => {
|
||||
return Some(Ok(de::End));
|
||||
Some(State::End) => {
|
||||
return Some(Ok(Token::End));
|
||||
}
|
||||
None => { return None; }
|
||||
}
|
||||
@ -393,21 +382,21 @@ impl Iterator<Result<de::Token, Error>> for Deserializer {
|
||||
|
||||
impl de::Deserializer<Error> for Deserializer {
|
||||
fn end_of_stream_error(&mut self) -> Error {
|
||||
SyntaxError(EOFWhileParsingValue, 0, 0)
|
||||
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0)
|
||||
}
|
||||
|
||||
fn syntax_error(&mut self,
|
||||
token: de::Token,
|
||||
expected: &'static [de::TokenKind]) -> Error {
|
||||
SyntaxError(ExpectedTokens(token, expected), 0, 0)
|
||||
token: Token,
|
||||
expected: &'static [TokenKind]) -> Error {
|
||||
Error::SyntaxError(ErrorCode::ExpectedTokens(token, expected), 0, 0)
|
||||
}
|
||||
|
||||
fn unexpected_name_error(&mut self, token: de::Token) -> Error {
|
||||
SyntaxError(UnexpectedName(token), 0, 0)
|
||||
fn unexpected_name_error(&mut self, token: Token) -> Error {
|
||||
Error::SyntaxError(ErrorCode::UnexpectedName(token), 0, 0)
|
||||
}
|
||||
|
||||
fn conversion_error(&mut self, token: de::Token) -> Error {
|
||||
SyntaxError(ConversionError(token), 0, 0)
|
||||
fn conversion_error(&mut self, token: Token) -> Error {
|
||||
Error::SyntaxError(ErrorCode::ConversionError(token), 0, 0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -416,16 +405,16 @@ impl de::Deserializer<Error> for Deserializer {
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
de::Deserialize::deserialize_token(self, de::Null)
|
||||
de::Deserialize::deserialize_token(self, Token::Null)
|
||||
}
|
||||
|
||||
// Special case treating options as a nullable value.
|
||||
#[inline]
|
||||
fn expect_option<
|
||||
U: de::Deserialize<Deserializer, Error>
|
||||
>(&mut self, token: de::Token) -> Result<Option<U>, Error> {
|
||||
>(&mut self, token: Token) -> Result<Option<U>, Error> {
|
||||
match token {
|
||||
de::Null => Ok(None),
|
||||
Token::Null => Ok(None),
|
||||
token => {
|
||||
let value: U = try!(de::Deserialize::deserialize_token(self, token));
|
||||
Ok(Some(value))
|
||||
@ -436,64 +425,80 @@ impl de::Deserializer<Error> for Deserializer {
|
||||
// Special case treating enums as a String or a `{"variant": "...", "fields": [...]}`.
|
||||
#[inline]
|
||||
fn expect_enum_start(&mut self,
|
||||
token: de::Token,
|
||||
token: Token,
|
||||
_name: &str,
|
||||
variants: &[&str]) -> Result<uint, Error> {
|
||||
let variant = match token {
|
||||
de::MapStart(_) => {
|
||||
Token::MapStart(_) => {
|
||||
let state = match self.stack.pop() {
|
||||
Some(state) => state,
|
||||
None => { panic!("state machine error, state stack empty"); }
|
||||
};
|
||||
|
||||
let mut iter = match state {
|
||||
DeserializerObjectState(iter) => iter,
|
||||
State::Object(iter) => iter,
|
||||
_ => { panic!("state machine error, expected an object"); }
|
||||
};
|
||||
|
||||
let (variant, fields) = match iter.next() {
|
||||
Some((variant, List(fields))) => (variant, fields),
|
||||
Some((variant, Value::List(fields))) => (variant, fields),
|
||||
Some((key, value)) => {
|
||||
return Err(ExpectedError("List".to_string(), format!("{} => {}", key, value)));
|
||||
return Err(
|
||||
Error::ExpectedError(
|
||||
"List".to_string(),
|
||||
format!("{} => {}", key, value)
|
||||
)
|
||||
);
|
||||
}
|
||||
None => {
|
||||
return Err(Error::MissingFieldError("<variant-name>".to_string()));
|
||||
}
|
||||
None => { return Err(MissingFieldError("<variant-name>".to_string())); }
|
||||
};
|
||||
|
||||
// Error out if there are other fields in the enum.
|
||||
match iter.next() {
|
||||
Some((key, value)) => {
|
||||
return Err(ExpectedError("None".to_string(), format!("{} => {}", key, value)));
|
||||
return Err(
|
||||
Error::ExpectedError(
|
||||
"None".to_string(),
|
||||
format!("{} => {}", key, value)
|
||||
)
|
||||
);
|
||||
}
|
||||
None => { }
|
||||
}
|
||||
|
||||
self.stack.push(DeserializerEndState);
|
||||
self.stack.push(State::End);
|
||||
|
||||
for field in fields.into_iter().rev() {
|
||||
self.stack.push(DeserializerValueState(field));
|
||||
self.stack.push(State::Value(field));
|
||||
}
|
||||
|
||||
variant
|
||||
}
|
||||
token => {
|
||||
return Err(ExpectedError("String or Object".to_string(),
|
||||
format!("{}", token)))
|
||||
return Err(
|
||||
Error::ExpectedError(
|
||||
"String or Object".to_string(),
|
||||
format!("{}", token)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
match variants.iter().position(|v| *v == variant.as_slice()) {
|
||||
Some(idx) => Ok(idx),
|
||||
None => Err(UnknownVariantError(variant)),
|
||||
None => Err(Error::UnknownVariantError(variant)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), Error> {
|
||||
fn expect_struct_start(&mut self, token: Token, _name: &str) -> Result<(), Error> {
|
||||
match token {
|
||||
de::MapStart(_) => Ok(()),
|
||||
Token::MapStart(_) => Ok(()),
|
||||
_ => {
|
||||
static EXPECTED_TOKENS: &'static [de::TokenKind] = [
|
||||
de::MapStartKind
|
||||
static EXPECTED_TOKENS: &'static [TokenKind] = &[
|
||||
TokenKind::MapStartKind
|
||||
];
|
||||
Err(self.syntax_error(token, EXPECTED_TOKENS))
|
||||
}
|
||||
@ -520,63 +525,63 @@ impl ToJson for Value {
|
||||
}
|
||||
|
||||
impl ToJson for int {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for i8 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for i16 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for i32 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for i64 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for uint {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for u8 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for u16 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for u32 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for u64 {
|
||||
fn to_json(&self) -> Value { Integer(*self as i64) }
|
||||
fn to_json(&self) -> Value { Value::Integer(*self as i64) }
|
||||
}
|
||||
|
||||
impl ToJson for f32 {
|
||||
fn to_json(&self) -> Value { Floating(*self as f64) }
|
||||
fn to_json(&self) -> Value { Value::Floating(*self as f64) }
|
||||
}
|
||||
|
||||
impl ToJson for f64 {
|
||||
fn to_json(&self) -> Value { Floating(*self) }
|
||||
fn to_json(&self) -> Value { Value::Floating(*self) }
|
||||
}
|
||||
|
||||
impl ToJson for bool {
|
||||
fn to_json(&self) -> Value { Boolean(*self) }
|
||||
fn to_json(&self) -> Value { Value::Boolean(*self) }
|
||||
}
|
||||
|
||||
impl<'a> ToJson for &'a str {
|
||||
fn to_json(&self) -> Value { String(self.to_string()) }
|
||||
fn to_json(&self) -> Value { Value::String(self.to_string()) }
|
||||
}
|
||||
|
||||
impl ToJson for string::String {
|
||||
fn to_json(&self) -> Value { String((*self).clone()) }
|
||||
fn to_json(&self) -> Value { Value::String((*self).clone()) }
|
||||
}
|
||||
|
||||
macro_rules! peel_to_json_tuple {
|
||||
@ -588,7 +593,7 @@ macro_rules! impl_to_json_tuple {
|
||||
impl<> ToJson for () {
|
||||
#[inline]
|
||||
fn to_json(&self) -> Value {
|
||||
Null
|
||||
Value::Null
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -608,7 +613,7 @@ macro_rules! impl_to_json_tuple {
|
||||
list.push($name.to_json());
|
||||
)*
|
||||
|
||||
List(list)
|
||||
Value::List(list)
|
||||
}
|
||||
}
|
||||
peel_to_json_tuple!($($name,)*)
|
||||
@ -618,7 +623,9 @@ macro_rules! impl_to_json_tuple {
|
||||
impl_to_json_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
||||
|
||||
impl<A:ToJson> ToJson for Vec<A> {
|
||||
fn to_json(&self) -> Value { List(self.iter().map(|elt| elt.to_json()).collect()) }
|
||||
fn to_json(&self) -> Value {
|
||||
Value::List(self.iter().map(|elt| elt.to_json()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl<A:ToJson> ToJson for TreeMap<string::String, A> {
|
||||
@ -627,7 +634,7 @@ impl<A:ToJson> ToJson for TreeMap<string::String, A> {
|
||||
for (key, value) in self.iter() {
|
||||
d.insert((*key).clone(), value.to_json());
|
||||
}
|
||||
Object(d)
|
||||
Value::Object(d)
|
||||
}
|
||||
}
|
||||
|
||||
@ -637,14 +644,14 @@ impl<A:ToJson> ToJson for HashMap<string::String, A> {
|
||||
for (key, value) in self.iter() {
|
||||
d.insert((*key).clone(), value.to_json());
|
||||
}
|
||||
Object(d)
|
||||
Value::Object(d)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A:ToJson> ToJson for Option<A> {
|
||||
fn to_json(&self) -> Value {
|
||||
match *self {
|
||||
None => Null,
|
||||
None => Value::Null,
|
||||
Some(ref value) => value.to_json()
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ extern crate serde_macros;
|
||||
#[cfg(test)]
|
||||
extern crate serialize;
|
||||
|
||||
pub use ser::{Serializer, Serialize};
|
||||
pub use de::{Deserializer, Deserialize};
|
||||
pub use ser::{Serializer, Serialize};
|
||||
|
||||
pub mod de;
|
||||
pub mod ser;
|
||||
|
230
src/ser.rs
230
src/ser.rs
@ -428,110 +428,110 @@ mod tests {
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> Serializer<Error> for AssertSerializer<Iter> {
|
||||
fn serialize_null(&mut self) -> Result<(), Error> {
|
||||
self.serialize(Null)
|
||||
self.serialize(Token::Null)
|
||||
}
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||
self.serialize(Bool(v))
|
||||
self.serialize(Token::Bool(v))
|
||||
}
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), Error> {
|
||||
self.serialize(Int(v))
|
||||
self.serialize(Token::Int(v))
|
||||
}
|
||||
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
||||
self.serialize(I8(v))
|
||||
self.serialize(Token::I8(v))
|
||||
}
|
||||
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
||||
self.serialize(I16(v))
|
||||
self.serialize(Token::I16(v))
|
||||
}
|
||||
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
||||
self.serialize(I32(v))
|
||||
self.serialize(Token::I32(v))
|
||||
}
|
||||
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
||||
self.serialize(I64(v))
|
||||
self.serialize(Token::I64(v))
|
||||
}
|
||||
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), Error> {
|
||||
self.serialize(Uint(v))
|
||||
self.serialize(Token::Uint(v))
|
||||
}
|
||||
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
||||
self.serialize(U8(v))
|
||||
self.serialize(Token::U8(v))
|
||||
}
|
||||
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
||||
self.serialize(U16(v))
|
||||
self.serialize(Token::U16(v))
|
||||
}
|
||||
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
||||
self.serialize(U32(v))
|
||||
self.serialize(Token::U32(v))
|
||||
}
|
||||
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
||||
self.serialize(U64(v))
|
||||
self.serialize(Token::U64(v))
|
||||
}
|
||||
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
||||
self.serialize(F32(v))
|
||||
self.serialize(Token::F32(v))
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
||||
self.serialize(F64(v))
|
||||
self.serialize(Token::F64(v))
|
||||
}
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
||||
self.serialize(Char(v))
|
||||
self.serialize(Token::Char(v))
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
||||
self.serialize(Str(v))
|
||||
self.serialize(Token::Str(v))
|
||||
}
|
||||
|
||||
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), Error> {
|
||||
self.serialize(TupleStart(len))
|
||||
self.serialize(Token::TupleStart(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_elt<
|
||||
T: Serialize<AssertSerializer<Iter>, Error>
|
||||
>(&mut self, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(TupleSep));
|
||||
try!(self.serialize(Token::TupleSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(TupleEnd)
|
||||
self.serialize(Token::TupleEnd)
|
||||
}
|
||||
|
||||
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(StructStart(name, len))
|
||||
self.serialize(Token::StructStart(name, len))
|
||||
}
|
||||
|
||||
fn serialize_struct_elt<
|
||||
T: Serialize<AssertSerializer<Iter>, Error>
|
||||
>(&mut self, name: &str, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(StructSep(name)));
|
||||
try!(self.serialize(Token::StructSep(name)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(StructEnd)
|
||||
self.serialize(Token::StructEnd)
|
||||
}
|
||||
|
||||
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(EnumStart(name, variant, len))
|
||||
self.serialize(Token::EnumStart(name, variant, len))
|
||||
}
|
||||
|
||||
fn serialize_enum_elt<
|
||||
T: Serialize<AssertSerializer<Iter>, Error>
|
||||
>(&mut self, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(EnumSep));
|
||||
try!(self.serialize(Token::EnumSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_enum_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(EnumEnd)
|
||||
self.serialize(Token::EnumEnd)
|
||||
}
|
||||
|
||||
fn serialize_option<
|
||||
@ -539,11 +539,11 @@ mod tests {
|
||||
>(&mut self, v: &option::Option<T>) -> Result<(), Error> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
try!(self.serialize(Option(true)));
|
||||
try!(self.serialize(Token::Option(true)));
|
||||
v.serialize(self)
|
||||
}
|
||||
None => {
|
||||
self.serialize(Option(false))
|
||||
self.serialize(Token::Option(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -553,11 +553,11 @@ mod tests {
|
||||
SeqIter: Iterator<T>
|
||||
>(&mut self, mut iter: SeqIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(SeqStart(len)));
|
||||
try!(self.serialize(Token::SeqStart(len)));
|
||||
for elt in iter {
|
||||
try!(elt.serialize(self));
|
||||
}
|
||||
self.serialize(SeqEnd)
|
||||
self.serialize(Token::SeqEnd)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
@ -566,12 +566,12 @@ mod tests {
|
||||
MapIter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: MapIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(MapStart(len)));
|
||||
try!(self.serialize(Token::MapStart(len)));
|
||||
for (key, value) in iter {
|
||||
try!(key.serialize(self));
|
||||
try!(value.serialize(self));
|
||||
}
|
||||
self.serialize(MapEnd)
|
||||
self.serialize(Token::MapEnd)
|
||||
}
|
||||
}
|
||||
|
||||
@ -580,7 +580,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_int() {
|
||||
let tokens = vec!(
|
||||
Int(5)
|
||||
Token::Int(5)
|
||||
);
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
5i.serialize(&mut serializer).unwrap();
|
||||
@ -590,7 +590,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_str() {
|
||||
let tokens = vec!(
|
||||
Str("a"),
|
||||
Token::Str("a"),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -601,7 +601,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_null() {
|
||||
let tokens = vec!(
|
||||
Null,
|
||||
Token::Null,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -612,7 +612,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_option_none() {
|
||||
let tokens = vec!(
|
||||
Option(false),
|
||||
Token::Option(false),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -623,8 +623,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_option_some() {
|
||||
let tokens = vec!(
|
||||
Option(true),
|
||||
Int(5),
|
||||
Token::Option(true),
|
||||
Token::Int(5),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -635,13 +635,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_tuple() {
|
||||
let tokens = vec!(
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
Token::TupleStart(2),
|
||||
Token::TupleSep,
|
||||
Token::Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
TupleEnd,
|
||||
Token::TupleSep,
|
||||
Token::Str("a"),
|
||||
Token::TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -652,22 +652,22 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_tuple_compound() {
|
||||
let tokens = vec!(
|
||||
TupleStart(3),
|
||||
TupleSep,
|
||||
Null,
|
||||
Token::TupleStart(3),
|
||||
Token::TupleSep,
|
||||
Token::Null,
|
||||
|
||||
TupleSep,
|
||||
Null,
|
||||
Token::TupleSep,
|
||||
Token::Null,
|
||||
|
||||
TupleSep,
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
Token::TupleSep,
|
||||
Token::TupleStart(2),
|
||||
Token::TupleSep,
|
||||
Token::Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
TupleEnd,
|
||||
TupleEnd,
|
||||
Token::TupleSep,
|
||||
Token::Str("a"),
|
||||
Token::TupleEnd,
|
||||
Token::TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -678,11 +678,11 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_struct_empty() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
StructSep("inner"),
|
||||
SeqStart(0),
|
||||
SeqEnd,
|
||||
StructEnd,
|
||||
Token::StructStart("Outer", 1),
|
||||
Token::StructSep("inner"),
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
Token::StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -693,25 +693,25 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_struct() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
StructSep("inner"),
|
||||
SeqStart(1),
|
||||
StructStart("Inner", 3),
|
||||
StructSep("a"),
|
||||
Null,
|
||||
Token::StructStart("Outer", 1),
|
||||
Token::StructSep("inner"),
|
||||
Token::SeqStart(1),
|
||||
Token::StructStart("Inner", 3),
|
||||
Token::StructSep("a"),
|
||||
Token::Null,
|
||||
|
||||
StructSep("b"),
|
||||
Uint(5),
|
||||
Token::StructSep("b"),
|
||||
Token::Uint(5),
|
||||
|
||||
StructSep("c"),
|
||||
MapStart(1),
|
||||
Str("abc"),
|
||||
Option(true),
|
||||
Char('c'),
|
||||
MapEnd,
|
||||
StructEnd,
|
||||
SeqEnd,
|
||||
StructEnd,
|
||||
Token::StructSep("c"),
|
||||
Token::MapStart(1),
|
||||
Token::Str("abc"),
|
||||
Token::Option(true),
|
||||
Token::Char('c'),
|
||||
Token::MapEnd,
|
||||
Token::StructEnd,
|
||||
Token::SeqEnd,
|
||||
Token::StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -734,34 +734,34 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_enum() {
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Dog", 0),
|
||||
EnumEnd,
|
||||
Token::EnumStart("Animal", "Dog", 0),
|
||||
Token::EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
Dog.serialize(&mut serializer).unwrap();
|
||||
Animal::Dog.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Frog", 2),
|
||||
EnumSep,
|
||||
Str("Henry"),
|
||||
Token::EnumStart("Animal", "Frog", 2),
|
||||
Token::EnumSep,
|
||||
Token::Str("Henry"),
|
||||
|
||||
EnumSep,
|
||||
Int(349),
|
||||
EnumEnd,
|
||||
Token::EnumSep,
|
||||
Token::Int(349),
|
||||
Token::EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
Frog("Henry".to_string(), 349).serialize(&mut serializer).unwrap();
|
||||
Animal::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),
|
||||
SeqEnd,
|
||||
Token::SeqStart(0),
|
||||
Token::SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -773,11 +773,11 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_vec() {
|
||||
let tokens = vec!(
|
||||
SeqStart(3),
|
||||
Int(5),
|
||||
Int(6),
|
||||
Int(7),
|
||||
SeqEnd,
|
||||
Token::SeqStart(3),
|
||||
Token::Int(5),
|
||||
Token::Int(6),
|
||||
Token::Int(7),
|
||||
Token::SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -788,22 +788,22 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_vec_compound() {
|
||||
let tokens = vec!(
|
||||
SeqStart(3),
|
||||
SeqStart(1),
|
||||
Int(1),
|
||||
SeqEnd,
|
||||
Token::SeqStart(3),
|
||||
Token::SeqStart(1),
|
||||
Token::Int(1),
|
||||
Token::SeqEnd,
|
||||
|
||||
SeqStart(2),
|
||||
Int(2),
|
||||
Int(3),
|
||||
SeqEnd,
|
||||
Token::SeqStart(2),
|
||||
Token::Int(2),
|
||||
Token::Int(3),
|
||||
Token::SeqEnd,
|
||||
|
||||
SeqStart(3),
|
||||
Int(4),
|
||||
Int(5),
|
||||
Int(6),
|
||||
SeqEnd,
|
||||
SeqEnd,
|
||||
Token::SeqStart(3),
|
||||
Token::Int(4),
|
||||
Token::Int(5),
|
||||
Token::Int(6),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
@ -814,13 +814,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_tokens_treemap() {
|
||||
let tokens = vec!(
|
||||
MapStart(2),
|
||||
Int(5),
|
||||
Str("a"),
|
||||
Token::MapStart(2),
|
||||
Token::Int(5),
|
||||
Token::Str("a"),
|
||||
|
||||
Int(6),
|
||||
Str("b"),
|
||||
MapEnd,
|
||||
Token::Int(6),
|
||||
Token::Str("b"),
|
||||
Token::MapEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.into_iter());
|
||||
|
Loading…
x
Reference in New Issue
Block a user