wip: refactor

This commit is contained in:
Erick Tryzelaar 2014-12-08 23:13:17 -08:00
parent fcd276176b
commit 73a9dd86b9
4 changed files with 223 additions and 207 deletions

View File

@ -38,18 +38,18 @@ struct Http {
impl<
S: Deserializer<E>,
E,
> Deserialize<S, E> for Http {
E: de::Error,
> de::Deserialize<S, E> for Http {
fn deserialize(state: &mut S) -> Result<Http, E> {
struct Visitor;
impl<
S: Deserializer<E>,
E,
E: de::Error,
> de::Visitor<S, Http, E> for Visitor {
fn visit_map<
Visitor: de::MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<Http, E> {
>(&mut self, mut visitor: Visitor) -> Result<Http, E> {
let mut protocol = None;
let mut status = None;
let mut host_status = None;
@ -61,19 +61,19 @@ impl<
let mut request_uri = None;
loop {
match try!(visitor.visit_key(state)) {
match try!(visitor.visit_key()) {
Some(s) => {
let s: String = s;
match s.as_slice() {
"protocol" => { protocol = Some(try!(visitor.visit_value(state))); }
"status" => { status = Some(try!(visitor.visit_value(state))); }
"host_status" => { host_status = Some(try!(visitor.visit_value(state))); }
"up_status" => { up_status = Some(try!(visitor.visit_value(state))); }
"method" => { method = Some(try!(visitor.visit_value(state))); }
"content_type" => { content_type = Some(try!(visitor.visit_value(state))); }
"user_agent" => { user_agent = Some(try!(visitor.visit_value(state))); }
"referer" => { referer = Some(try!(visitor.visit_value(state))); }
"request_uri" => { request_uri = Some(try!(visitor.visit_value(state))); }
"protocol" => { protocol = Some(try!(visitor.visit_value())); }
"status" => { status = Some(try!(visitor.visit_value())); }
"host_status" => { host_status = Some(try!(visitor.visit_value())); }
"up_status" => { up_status = Some(try!(visitor.visit_value())); }
"method" => { method = Some(try!(visitor.visit_value())); }
"content_type" => { content_type = Some(try!(visitor.visit_value())); }
"user_agent" => { user_agent = Some(try!(visitor.visit_value())); }
"referer" => { referer = Some(try!(visitor.visit_value())); }
"request_uri" => { request_uri = Some(try!(visitor.visit_value())); }
_ => panic!(),
}
}
@ -133,7 +133,10 @@ impl ser::Serialize for HttpProtocol {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for HttpProtocol {
impl<
S: de::Deserializer<E>,
E: de::Error,
> de::Deserialize<S, E> for HttpProtocol {
#[inline]
fn deserialize(state: &mut S) -> Result<HttpProtocol, E> {
de::deserialize_from_primitive(state)
@ -182,7 +185,10 @@ impl ser::Serialize for HttpMethod {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for HttpMethod {
impl<
S: de::Deserializer<E>,
E: de::Error,
> de::Deserialize<S, E> for HttpMethod {
#[inline]
fn deserialize(state: &mut S) -> Result<HttpMethod, E> {
de::deserialize_from_primitive(state)
@ -224,7 +230,7 @@ impl ser::Serialize for CacheStatus {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for CacheStatus {
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for CacheStatus {
#[inline]
fn deserialize(state: &mut S) -> Result<CacheStatus, E> {
de::deserialize_from_primitive(state)
@ -243,32 +249,32 @@ struct Origin {
impl<
S: Deserializer<E>,
E,
E: de::Error,
> Deserialize<S, E> for Origin {
fn deserialize(state: &mut S) -> Result<Origin, E> {
struct Visitor;
impl<
S: Deserializer<E>,
E,
E: de::Error,
> de::Visitor<S, Origin, E> for Visitor {
fn visit_map<
Visitor: de::MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<Origin, E> {
>(&mut self, mut visitor: Visitor) -> Result<Origin, E> {
let mut ip = None;
let mut port = None;
let mut hostname = None;
let mut protocol = None;
loop {
match try!(visitor.visit_key(state)) {
match try!(visitor.visit_key()) {
Some(s) => {
let s: String = s;
match s.as_slice() {
"ip" => { ip = Some(try!(visitor.visit_value(state))); }
"port" => { port = Some(try!(visitor.visit_value(state))); }
"hostname" => { hostname = Some(try!(visitor.visit_value(state))); }
"protocol" => { protocol = Some(try!(visitor.visit_value(state))); }
"ip" => { ip = Some(try!(visitor.visit_value())); }
"port" => { port = Some(try!(visitor.visit_value())); }
"hostname" => { hostname = Some(try!(visitor.visit_value())); }
"protocol" => { protocol = Some(try!(visitor.visit_value())); }
_ => panic!(),
}
}
@ -323,7 +329,7 @@ impl ser::Serialize for OriginProtocol {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for OriginProtocol {
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for OriginProtocol {
#[inline]
fn deserialize(state: &mut S) -> Result<OriginProtocol, E> {
de::deserialize_from_primitive(state)
@ -366,7 +372,7 @@ impl ser::Serialize for ZonePlan {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for ZonePlan {
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for ZonePlan {
#[inline]
fn deserialize(state: &mut S) -> Result<ZonePlan, E> {
de::deserialize_from_primitive(state)
@ -660,7 +666,7 @@ impl ser::Serialize for Country {
}
}
impl<S: de::Deserializer<E>, E> de::Deserialize<S, E> for Country {
impl<S: de::Deserializer<E>, E: de::Error> de::Deserialize<S, E> for Country {
#[inline]
fn deserialize(state: &mut S) -> Result<Country, E> {
de::deserialize_from_primitive(state)
@ -687,18 +693,18 @@ struct Log {
impl<
S: Deserializer<E>,
E,
E: de::Error,
> Deserialize<S, E> for Log {
fn deserialize(state: &mut S) -> Result<Log, E> {
struct Visitor;
impl<
S: Deserializer<E>,
E,
E: de::Error,
> de::Visitor<S, Log, E> for Visitor {
fn visit_map<
Visitor: de::MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<Log, E> {
>(&mut self, mut visitor: Visitor) -> Result<Log, E> {
let mut timestamp = None;
let mut zone_id = None;
let mut zone_plan = None;
@ -713,22 +719,22 @@ impl<
let mut ray_id = None;
loop {
match try!(visitor.visit_key(state)) {
match try!(visitor.visit_key()) {
Some(s) => {
let s: String = s;
match s.as_slice() {
"timestamp" => { timestamp = Some(try!(visitor.visit_value(state))); }
"zone_id" => { zone_id = Some(try!(visitor.visit_value(state))); }
"zone_plan" => { zone_plan = Some(try!(visitor.visit_value(state))); }
"http" => { http = Some(try!(visitor.visit_value(state))); }
"origin" => { origin = Some(try!(visitor.visit_value(state))); }
"country" => { country = Some(try!(visitor.visit_value(state))); }
"cache_status" => { cache_status = Some(try!(visitor.visit_value(state))); }
"server_ip" => { server_ip = Some(try!(visitor.visit_value(state))); }
"server_name" => { server_name = Some(try!(visitor.visit_value(state))); }
"remote_ip" => { remote_ip = Some(try!(visitor.visit_value(state))); }
"bytes_dlv" => { bytes_dlv = Some(try!(visitor.visit_value(state))); }
"ray_id" => { ray_id = Some(try!(visitor.visit_value(state))); }
"timestamp" => { timestamp = Some(try!(visitor.visit_value())); }
"zone_id" => { zone_id = Some(try!(visitor.visit_value())); }
"zone_plan" => { zone_plan = Some(try!(visitor.visit_value())); }
"http" => { http = Some(try!(visitor.visit_value())); }
"origin" => { origin = Some(try!(visitor.visit_value())); }
"country" => { country = Some(try!(visitor.visit_value())); }
"cache_status" => { cache_status = Some(try!(visitor.visit_value())); }
"server_ip" => { server_ip = Some(try!(visitor.visit_value())); }
"server_name" => { server_name = Some(try!(visitor.visit_value())); }
"remote_ip" => { remote_ip = Some(try!(visitor.visit_value())); }
"bytes_dlv" => { bytes_dlv = Some(try!(visitor.visit_value())); }
"ray_id" => { ray_id = Some(try!(visitor.visit_value())); }
_ => panic!(),
}
}

View File

@ -77,24 +77,22 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
match self.next() {
Some(Token::Null) => {
visitor.visit_null(self)
visitor.visit_null()
}
Some(Token::Int(v)) => {
visitor.visit_int(self, v)
visitor.visit_int(v)
}
Some(Token::String(v)) => {
visitor.visit_string(self, v)
visitor.visit_string(v)
}
Some(Token::Option(is_some)) => {
visitor.visit_option(self, MyOptionVisitor {
is_some: is_some,
})
visitor.visit_option(MyOptionVisitor { d: self, is_some: is_some })
}
Some(Token::SeqStart(len)) => {
visitor.visit_seq(self, MySeqVisitor { len: len })
visitor.visit_seq(MySeqVisitor { d: self, len: len })
}
Some(Token::MapStart(len)) => {
visitor.visit_map(self, MyMapVisitor { len: len })
visitor.visit_map(MyMapVisitor { d: self, len: len })
}
Some(Token::End) => {
Err(Error::syntax_error())
@ -112,18 +110,21 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
match self.peek() {
Some(&Token::Null) => {
self.next();
visitor.visit_option(self, MyOptionVisitor {
visitor.visit_option(MyOptionVisitor {
d: self,
is_some: false,
})
}
Some(&Token::Option(is_some)) => {
self.next();
visitor.visit_option(self, MyOptionVisitor {
visitor.visit_option(MyOptionVisitor {
d: self,
is_some: is_some,
})
}
_ => {
visitor.visit_option(self, MyOptionVisitor {
visitor.visit_option(MyOptionVisitor {
d: self,
is_some: true,
})
}
@ -131,19 +132,21 @@ impl<Iter: Iterator<Token>> Deserializer<Error> for MyDeserializer<Iter> {
}
}
struct MyOptionVisitor {
struct MyOptionVisitor<'a, Iter: 'a> {
d: &'a mut MyDeserializer<Iter>,
is_some: bool,
}
impl<
'a,
Iter: Iterator<Token>,
> de::OptionVisitor<MyDeserializer<Iter>, Error> for MyOptionVisitor {
> de::OptionVisitor<MyDeserializer<Iter>, Error> for MyOptionVisitor<'a, Iter> {
fn visit<
T: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<option::Option<T>, Error> {
>(&mut self) -> Result<option::Option<T>, Error> {
if self.is_some {
self.is_some = false;
let value = try!(Deserialize::deserialize(d));
let value = try!(Deserialize::deserialize(self.d));
Ok(Some(value))
} else {
Ok(None)
@ -151,26 +154,28 @@ impl<
}
}
struct MySeqVisitor {
struct MySeqVisitor<'a, Iter: 'a> {
d: &'a mut MyDeserializer<Iter>,
len: uint,
}
impl<
'a,
Iter: Iterator<Token>,
> de::SeqVisitor<MyDeserializer<Iter>, Error> for MySeqVisitor {
> de::SeqVisitor<MyDeserializer<Iter>, Error> for MySeqVisitor<'a, Iter> {
fn visit<
T: Deserialize<MyDeserializer<Iter>, Error>
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<option::Option<T>, Error> {
>(&mut self) -> Result<option::Option<T>, Error> {
use serde2::de::Error;
match d.peek() {
match self.d.peek() {
Some(&Token::End) => {
d.next();
self.d.next();
Ok(None)
}
Some(_) => {
self.len -= 1;
let value = try!(Deserialize::deserialize(d));
let value = try!(Deserialize::deserialize(self.d));
Ok(Some(value))
}
None => {
@ -179,42 +184,44 @@ impl<
}
}
fn end(&mut self, d: &mut MyDeserializer<Iter>) -> Result<(), Error> {
fn end(&mut self) -> Result<(), Error> {
use serde2::de::Error;
match d.next() {
match self.d.next() {
Some(Token::End) => Ok(()),
Some(_) => Err(Error::syntax_error()),
None => Err(Error::end_of_stream_error()),
}
}
fn size_hint(&self, _d: &mut MyDeserializer<Iter>) -> (uint, option::Option<uint>) {
fn size_hint(&self) -> (uint, option::Option<uint>) {
(self.len, Some(self.len))
}
}
struct MyMapVisitor {
struct MyMapVisitor<'a, Iter: 'a> {
d: &'a mut MyDeserializer<Iter>,
len: uint,
}
impl<
'a,
Iter: Iterator<Token>,
> de::MapVisitor<MyDeserializer<Iter>, Error> for MyMapVisitor {
> de::MapVisitor<MyDeserializer<Iter>, Error> for MyMapVisitor<'a, Iter> {
fn visit_key<
K: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<option::Option<K>, Error> {
>(&mut self) -> Result<option::Option<K>, Error> {
use serde2::de::Error;
match d.peek() {
match self.d.peek() {
Some(&Token::End) => {
d.next();
self.d.next();
Ok(None)
}
Some(_) => {
self.len -= 1;
Ok(Some(try!(Deserialize::deserialize(d))))
Ok(Some(try!(Deserialize::deserialize(self.d))))
}
None => {
Err(Error::syntax_error())
@ -224,21 +231,21 @@ impl<
fn visit_value<
V: Deserialize<MyDeserializer<Iter>, Error>,
>(&mut self, d: &mut MyDeserializer<Iter>) -> Result<V, Error> {
Ok(try!(Deserialize::deserialize(d)))
>(&mut self) -> Result<V, Error> {
Ok(try!(Deserialize::deserialize(self.d)))
}
fn end(&mut self, d: &mut MyDeserializer<Iter>) -> Result<(), Error> {
fn end(&mut self) -> Result<(), Error> {
use serde2::de::Error;
match d.next() {
match self.d.next() {
Some(Token::End) => Ok(()),
Some(_) => Err(Error::syntax_error()),
None => Err(Error::end_of_stream_error()),
}
}
fn size_hint(&self, _d: &mut MyDeserializer<Iter>) -> (uint, option::Option<uint>) {
fn size_hint(&self) -> (uint, option::Option<uint>) {
(self.len, Some(self.len))
}
}
@ -270,11 +277,11 @@ mod json {
D: de::Deserializer<E>,
E: de::Error,
> de::Visitor<D, Value, E> for Visitor {
fn visit_null(&mut self, _d: &mut D) -> Result<Value, E> {
fn visit_null(&mut self) -> Result<Value, E> {
Ok(Value::Null)
}
fn visit_int(&mut self, _d: &mut D, v: int) -> Result<Value, E> {
fn visit_int(&mut self, v: int) -> Result<Value, E> {
Ok(Value::Int(v))
}
@ -286,8 +293,8 @@ mod json {
fn visit_option<
Visitor: de::OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
match try!(visitor.visit(d)) {
>(&mut self, mut visitor: Visitor) -> Result<Value, E> {
match try!(visitor.visit()) {
Some(value) => Ok(value),
None => Ok(Value::Null),
}
@ -295,12 +302,12 @@ mod json {
fn visit_seq<
Visitor: de::SeqVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
let (len, _) = visitor.size_hint(d);
>(&mut self, mut visitor: Visitor) -> Result<Value, E> {
let (len, _) = visitor.size_hint();
let mut values = Vec::with_capacity(len);
loop {
match try!(visitor.visit(d)) {
match try!(visitor.visit()) {
Some(value) => {
values.push(value);
}
@ -315,11 +322,11 @@ mod json {
fn visit_map<
Visitor: de::MapVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<Value, E> {
>(&mut self, mut visitor: Visitor) -> Result<Value, E> {
let mut values = TreeMap::new();
loop {
match try!(visitor.visit(d)) {
match try!(visitor.visit()) {
Some((key, value)) => {
values.insert(key, value);
}

View File

@ -28,85 +28,85 @@ pub trait Deserializer<E> {
}
pub trait Visitor<S: Deserializer<E>, R, E: Error> {
fn visit_null(&mut self, state: &mut S) -> Result<R, E> {
fn visit_null(&mut self) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_bool(&mut self, state: &mut S, _v: bool) -> Result<R, E> {
fn visit_bool(&mut self, _v: bool) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_int(&mut self, state: &mut S, v: int) -> Result<R, E> {
self.visit_i64(state, v as i64)
fn visit_int(&mut self, v: int) -> Result<R, E> {
self.visit_i64(v as i64)
}
fn visit_i8(&mut self, state: &mut S, v: i8) -> Result<R, E> {
self.visit_i64(state, v as i64)
fn visit_i8(&mut self, v: i8) -> Result<R, E> {
self.visit_i64(v as i64)
}
fn visit_i16(&mut self, state: &mut S, v: i16) -> Result<R, E> {
self.visit_i64(state, v as i64)
fn visit_i16(&mut self, v: i16) -> Result<R, E> {
self.visit_i64(v as i64)
}
fn visit_i32(&mut self, state: &mut S, v: i32) -> Result<R, E> {
self.visit_i64(state, v as i64)
fn visit_i32(&mut self, v: i32) -> Result<R, E> {
self.visit_i64(v as i64)
}
fn visit_i64(&mut self, state: &mut S, _v: i64) -> Result<R, E> {
fn visit_i64(&mut self, _v: i64) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_uint(&mut self, state: &mut S, v: uint) -> Result<R, E> {
self.visit_u64(state, v as u64)
fn visit_uint(&mut self, v: uint) -> Result<R, E> {
self.visit_u64(v as u64)
}
fn visit_u8(&mut self, state: &mut S, v: u8) -> Result<R, E> {
self.visit_u64(state, v as u64)
fn visit_u8(&mut self, v: u8) -> Result<R, E> {
self.visit_u64(v as u64)
}
fn visit_u16(&mut self, state: &mut S, v: u16) -> Result<R, E> {
self.visit_u64(state, v as u64)
fn visit_u16(&mut self, v: u16) -> Result<R, E> {
self.visit_u64(v as u64)
}
fn visit_u32(&mut self, state: &mut S, v: u32) -> Result<R, E> {
self.visit_u64(state, v as u64)
fn visit_u32(&mut self, v: u32) -> Result<R, E> {
self.visit_u64(v as u64)
}
fn visit_u64(&mut self, state: &mut S, _v: u64) -> Result<R, E> {
fn visit_u64(&mut self, _v: u64) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_f32(&mut self, state: &mut S, v: f32) -> Result<R, E> {
self.visit_f64(state, v as f64)
fn visit_f32(&mut self, v: f32) -> Result<R, E> {
self.visit_f64(v as f64)
}
fn visit_f64(&mut self, state: &mut S, _v: f64) -> Result<R, E> {
fn visit_f64(&mut self, _v: f64) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_str(&mut self, state: &mut S, _v: &str) -> Result<R, E> {
fn visit_str(&mut self, _v: &str) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_string(&mut self, state: &mut S, v: String) -> Result<R, E> {
self.visit_str(state, v.as_slice())
fn visit_string(&mut self, v: String) -> Result<R, E> {
self.visit_str(v.as_slice())
}
fn visit_option<
V: OptionVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
>(&mut self, _visitor: V) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_seq<
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
>(&mut self, _visitor: V) -> Result<R, E> {
Err(Error::syntax_error())
}
fn visit_map<
V: MapVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
>(&mut self, _visitor: V) -> Result<R, E> {
Err(Error::syntax_error())
}
}
@ -114,18 +114,18 @@ pub trait Visitor<S: Deserializer<E>, R, E: Error> {
pub trait OptionVisitor<S, E> {
fn visit<
T: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<T>, E>;
>(&mut self) -> Result<Option<T>, E>;
}
pub trait SeqVisitor<S, E> {
fn visit<
T: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<T>, E>;
>(&mut self) -> Result<Option<T>, E>;
fn end(&mut self, state: &mut S) -> Result<(), E>;
fn end(&mut self) -> Result<(), E>;
#[inline]
fn size_hint(&self, _state: &mut S) -> (uint, Option<uint>) {
fn size_hint(&self) -> (uint, Option<uint>) {
(0, None)
}
}
@ -134,10 +134,10 @@ pub trait MapVisitor<S, E> {
fn visit<
K: Deserialize<S, E>,
V: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<(K, V)>, E> {
match try!(self.visit_key(state)) {
>(&mut self) -> Result<Option<(K, V)>, E> {
match try!(self.visit_key()) {
Some(key) => {
let value = try!(self.visit_value(state));
let value = try!(self.visit_value());
Ok(Some((key, value)))
}
None => Ok(None)
@ -146,16 +146,16 @@ pub trait MapVisitor<S, E> {
fn visit_key<
K: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<K>, E>;
>(&mut self) -> Result<Option<K>, E>;
fn visit_value<
V: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<V, E>;
>(&mut self) -> Result<V, E>;
fn end(&mut self, state: &mut S) -> Result<(), E>;
fn end(&mut self) -> Result<(), E>;
#[inline]
fn size_hint(&self, _state: &mut S) -> (uint, Option<uint>) {
fn size_hint(&self) -> (uint, Option<uint>) {
(0, None)
}
}
@ -173,14 +173,14 @@ impl<
S: Deserializer<E>,
E: Error,
> self::Visitor<S, (), E> for Visitor {
fn visit_null(&mut self, _state: &mut S) -> Result<(), E> {
fn visit_null(&mut self) -> Result<(), E> {
Ok(())
}
fn visit_seq<
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<(), E> {
try!(visitor.end(state));
>(&mut self, mut visitor: V) -> Result<(), E> {
try!(visitor.end());
Ok(())
}
}
@ -202,7 +202,7 @@ impl<
S: Deserializer<E>,
E: Error,
> self::Visitor<S, bool, E> for Visitor {
fn visit_bool(&mut self, _state: &mut S, v: bool) -> Result<bool, E> {
fn visit_bool(&mut self, v: bool) -> Result<bool, E> {
Ok(v)
}
}
@ -215,7 +215,7 @@ impl<
macro_rules! impl_deserialize_num_method {
($src_ty:ty, $method:ident, $from_method:ident) => {
fn $method(&mut self, state: &mut S, v: $src_ty) -> Result<T, E> {
fn $method(&mut self, v: $src_ty) -> Result<T, E> {
match FromPrimitive::$from_method(v) {
Some(v) => Ok(v),
None => Err(Error::syntax_error()),
@ -294,11 +294,11 @@ impl<
S: Deserializer<E>,
E: Error,
> self::Visitor<S, String, E> for Visitor {
fn visit_str(&mut self, _state: &mut S, v: &str) -> Result<String, E> {
fn visit_str(&mut self, v: &str) -> Result<String, E> {
Ok(v.to_string())
}
fn visit_string(&mut self, _state: &mut S, v: String) -> Result<String, E> {
fn visit_string(&mut self, v: String) -> Result<String, E> {
Ok(v)
}
}
@ -324,8 +324,8 @@ impl<
> self::Visitor<S, Option<T>, E> for Visitor {
fn visit_option<
V: OptionVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<Option<T>, E> {
visitor.visit(state)
>(&mut self, mut visitor: V) -> Result<Option<T>, E> {
visitor.visit()
}
}
@ -350,12 +350,12 @@ impl<
> self::Visitor<S, Vec<T>, E> for Visitor {
fn visit_seq<
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<Vec<T>, E> {
let (len, _) = visitor.size_hint(state);
>(&mut self, mut visitor: V) -> Result<Vec<T>, E> {
let (len, _) = visitor.size_hint();
let mut values = Vec::with_capacity(len);
loop {
match try!(visitor.visit(state)) {
match try!(visitor.visit()) {
Some(value) => {
values.push(value);
}
@ -403,15 +403,15 @@ macro_rules! impl_deserialize_tuple {
> self::Visitor<S, ($($name,)+), E> for Visitor {
fn visit_seq<
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<($($name,)+), E> {
>(&mut self, mut visitor: V) -> Result<($($name,)+), E> {
$(
let $name = match try!(visitor.visit(state)) {
let $name = match try!(visitor.visit()) {
Some(value) => value,
None => { return Err(Error::end_of_stream_error()); }
};
)+;
try!(visitor.end(state));
try!(visitor.end());
Ok(($($name,)+))
}
@ -444,12 +444,12 @@ impl<
> self::Visitor<S, HashMap<K, V>, E> for Visitor {
fn visit_map<
Visitor: MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<HashMap<K, V>, E> {
let (len, _) = visitor.size_hint(state);
>(&mut self, mut visitor: Visitor) -> Result<HashMap<K, V>, E> {
let (len, _) = visitor.size_hint();
let mut values = HashMap::with_capacity(len);
loop {
match try!(visitor.visit(state)) {
match try!(visitor.visit()) {
Some((key, value)) => {
values.insert(key, value);
}
@ -484,11 +484,11 @@ impl<
> self::Visitor<S, TreeMap<K, V>, E> for Visitor {
fn visit_map<
Visitor: MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<TreeMap<K, V>, E> {
>(&mut self, mut visitor: Visitor) -> Result<TreeMap<K, V>, E> {
let mut values = TreeMap::new();
loop {
match try!(visitor.visit(state)) {
match try!(visitor.visit()) {
Some((key, value)) => {
values.insert(key, value);
}

View File

@ -94,29 +94,30 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
match self.ch_or_null() {
b'n' => {
try!(self.parse_ident(b"ull"));
visitor.visit_null(self)
visitor.visit_null()
}
b't' => {
try!(self.parse_ident(b"rue"));
visitor.visit_bool(self, true)
visitor.visit_bool(true)
}
b'f' => {
try!(self.parse_ident(b"alse"));
visitor.visit_bool(self, false)
visitor.visit_bool(false)
}
b'0' ... b'9' | b'-' => self.parse_number(visitor),
b'"' => {
try!(self.parse_string());
let s = String::from_utf8(self.buf.clone()).unwrap();
visitor.visit_string(self, s)
//let s = String::from_utf8(self.buf.clone()).unwrap();
let s = str::from_utf8(self.buf.as_slice()).unwrap();
visitor.visit_str(s)
}
b'[' => {
self.bump();
visitor.visit_seq(self, SeqVisitor { first: true })
visitor.visit_seq(SeqVisitor { parser: self, first: true })
}
b'{' => {
self.bump();
visitor.visit_map(self, MapVisitor { first: true })
visitor.visit_map(MapVisitor { parser: self, first: true })
}
_ => {
Err(self.error(ErrorCode::ExpectedSomeValue))
@ -160,9 +161,9 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
res = try!(self.parse_exponent(res));
}
visitor.visit_f64(self, neg * res)
visitor.visit_f64(neg * res)
} else {
visitor.visit_i64(self, neg * res)
visitor.visit_i64(neg * res)
}
}
@ -389,114 +390,116 @@ impl<Iter: Iterator<u8>> Deserializer<Error> for Parser<Iter> {
}
}
struct SeqVisitor {
struct SeqVisitor<'a, Iter: 'a> {
parser: &'a mut Parser<Iter>,
first: bool,
}
impl<Iter: Iterator<u8>> de::SeqVisitor<Parser<Iter>, Error> for SeqVisitor {
impl<'a, Iter: Iterator<u8>> de::SeqVisitor<Parser<Iter>, Error> for SeqVisitor<'a, Iter> {
fn visit<
T: de::Deserialize<Parser<Iter>, Error>,
>(&mut self, d: &mut Parser<Iter>) -> Result<Option<T>, Error> {
d.parse_whitespace();
>(&mut self) -> Result<Option<T>, Error> {
self.parser.parse_whitespace();
if d.ch_is(b']') {
d.bump();
if self.parser.ch_is(b']') {
self.parser.bump();
return Ok(None);
}
if self.first {
self.first = false;
} else {
if d.ch_is(b',') {
d.bump();
} else if d.eof() {
return Err(d.error(ErrorCode::EOFWhileParsingList));
if self.parser.ch_is(b',') {
self.parser.bump();
} else if self.parser.eof() {
return Err(self.parser.error(ErrorCode::EOFWhileParsingList));
} else {
return Err(d.error(ErrorCode::ExpectedListCommaOrEnd));
return Err(self.parser.error(ErrorCode::ExpectedListCommaOrEnd));
}
}
let value = try!(de::Deserialize::deserialize(d));
let value = try!(de::Deserialize::deserialize(self.parser));
Ok(Some(value))
}
fn end(&mut self, d: &mut Parser<Iter>) -> Result<(), Error> {
if d.ch_is(b']') {
d.bump();
fn end(&mut self) -> Result<(), Error> {
if self.parser.ch_is(b']') {
self.parser.bump();
Ok(())
} else if d.eof() {
Err(d.error(ErrorCode::EOFWhileParsingList))
} else if self.parser.eof() {
Err(self.parser.error(ErrorCode::EOFWhileParsingList))
} else {
Err(d.error(ErrorCode::TrailingCharacters))
Err(self.parser.error(ErrorCode::TrailingCharacters))
}
}
}
struct MapVisitor {
struct MapVisitor<'a, Iter: 'a> {
parser: &'a mut Parser<Iter>,
first: bool,
}
impl<Iter: Iterator<u8>> de::MapVisitor<Parser<Iter>, Error> for MapVisitor {
impl<'a, Iter: Iterator<u8>> de::MapVisitor<Parser<Iter>, Error> for MapVisitor<'a, Iter> {
fn visit_key<
K: de::Deserialize<Parser<Iter>, Error>,
>(&mut self, d: &mut Parser<Iter>) -> Result<Option<K>, Error> {
d.parse_whitespace();
>(&mut self) -> Result<Option<K>, Error> {
self.parser.parse_whitespace();
if d.ch_is(b'}') {
d.bump();
if self.parser.ch_is(b'}') {
self.parser.bump();
return Ok(None);
}
if self.first {
self.first = false;
} else {
if d.ch_is(b',') {
d.bump();
d.parse_whitespace();
} else if d.eof() {
return Err(d.error(ErrorCode::EOFWhileParsingObject));
if self.parser.ch_is(b',') {
self.parser.bump();
self.parser.parse_whitespace();
} else if self.parser.eof() {
return Err(self.parser.error(ErrorCode::EOFWhileParsingObject));
} else {
return Err(d.error(ErrorCode::ExpectedObjectCommaOrEnd));
return Err(self.parser.error(ErrorCode::ExpectedObjectCommaOrEnd));
}
}
if d.eof() {
return Err(d.error(ErrorCode::EOFWhileParsingValue));
if self.parser.eof() {
return Err(self.parser.error(ErrorCode::EOFWhileParsingValue));
}
if !d.ch_is(b'"') {
return Err(d.error(ErrorCode::KeyMustBeAString));
if !self.parser.ch_is(b'"') {
return Err(self.parser.error(ErrorCode::KeyMustBeAString));
}
Ok(Some(try!(de::Deserialize::deserialize(d))))
Ok(Some(try!(de::Deserialize::deserialize(self.parser))))
}
fn visit_value<
V: de::Deserialize<Parser<Iter>, Error>,
>(&mut self, d: &mut Parser<Iter>) -> Result<V, Error> {
d.parse_whitespace();
>(&mut self) -> Result<V, Error> {
self.parser.parse_whitespace();
if d.ch_is(b':') {
d.bump();
} else if d.eof() {
return Err(d.error(ErrorCode::EOFWhileParsingObject));
if self.parser.ch_is(b':') {
self.parser.bump();
} else if self.parser.eof() {
return Err(self.parser.error(ErrorCode::EOFWhileParsingObject));
} else {
return Err(d.error(ErrorCode::ExpectedColon));
return Err(self.parser.error(ErrorCode::ExpectedColon));
}
d.parse_whitespace();
self.parser.parse_whitespace();
Ok(try!(de::Deserialize::deserialize(d)))
Ok(try!(de::Deserialize::deserialize(self.parser)))
}
fn end(&mut self, d: &mut Parser<Iter>) -> Result<(), Error> {
if d.ch_is(b']') {
d.bump();
fn end(&mut self) -> Result<(), Error> {
if self.parser.ch_is(b']') {
self.parser.bump();
Ok(())
} else if d.eof() {
Err(d.error(ErrorCode::EOFWhileParsingList))
} else if self.parser.eof() {
Err(self.parser.error(ErrorCode::EOFWhileParsingList))
} else {
Err(d.error(ErrorCode::TrailingCharacters))
Err(self.parser.error(ErrorCode::TrailingCharacters))
}
}
}