diff --git a/de.rs b/de.rs index 7f3d72c8..0c4ae17d 100644 --- a/de.rs +++ b/de.rs @@ -88,14 +88,13 @@ pub trait Deserializer: Iterator> { } } - /* #[inline] - fn expect_bool(&mut self) -> Result { - match_token! { - Bool(value) => Ok(value) + fn expect_bool(&mut self, token: Token) -> Result { + match token { + Bool(value) => Ok(value), + _ => Err(self.syntax_error()), } } - */ #[inline] fn expect_num(&mut self, token: Token) -> Result { @@ -112,25 +111,25 @@ pub trait Deserializer: Iterator> { U64(x) => to_result!(num::cast(x), self.syntax_error()), F32(x) => to_result!(num::cast(x), self.syntax_error()), F64(x) => to_result!(num::cast(x), self.syntax_error()), - _ => { return Err(self.syntax_error()); } - } - } - - /* - #[inline] - fn expect_char(&mut self) -> Result { - match_token! { - Char(value) => Ok(value) + _ => Err(self.syntax_error()), } } #[inline] - fn expect_str(&mut self) -> Result<&'static str, E> { - match_token! { - Str(value) => Ok(value) + fn expect_char(&mut self, token: Token) -> Result { + match token { + Char(value) => Ok(value), + _ => Err(self.syntax_error()), + } + } + + #[inline] + fn expect_str(&mut self, token: Token) -> Result<&'static str, E> { + match token { + Str(value) => Ok(value), + _ => Err(self.syntax_error()), } } - */ #[inline] fn expect_strbuf(&mut self, token: Token) -> Result { @@ -141,20 +140,19 @@ pub trait Deserializer: Iterator> { } } - /* #[inline] fn expect_option< T: Deserializable - >(&mut self) -> Result, E> { - match_token! { + >(&mut self, token: Token) -> Result, E> { + match token { Option(false) => Ok(None), Option(true) => { let value: T = try!(Deserializable::deserialize(self)); Ok(Some(value)) } + _ => Err(self.syntax_error()), } } - */ #[inline] fn expect_tuple_start(&mut self, token: Token, len: uint) -> Result<(), E> { @@ -170,17 +168,9 @@ pub trait Deserializer: Iterator> { } } - /* #[inline] - fn expect_tuple_elt>(&mut self) -> Result { - match_token! { - Sep => Deserializable::deserialize(self) - } - } - - #[inline] - fn expect_struct_start(&mut self, name: &str) -> Result<(), E> { - match_token! { + fn expect_struct_start(&mut self, token: Token, name: &str) -> Result<(), E> { + match token { StructStart(n) => { if name == n { Ok(()) @@ -188,6 +178,7 @@ pub trait Deserializer: Iterator> { Err(self.syntax_error()) } } + _ => Err(self.syntax_error()), } } @@ -195,7 +186,13 @@ pub trait Deserializer: Iterator> { fn expect_struct_field< T: Deserializable >(&mut self, name: &str) -> Result { - match_token! { + let token = match self.next() { + Some(Ok(token)) => token, + Some(Err(err)) => { return Err(err); } + None => { return Err(self.end_of_stream_error()); } + }; + + match token { StructField(n) => { if name == n { Deserializable::deserialize(self) @@ -203,9 +200,9 @@ pub trait Deserializer: Iterator> { Err(self.syntax_error()) } } + _ => Err(self.syntax_error()), } } - */ #[inline] fn expect_enum_start<'a>(&mut self, token: Token, name: &str, variants: &[&str]) -> Result { @@ -224,7 +221,6 @@ pub trait Deserializer: Iterator> { } } - /* #[inline] fn expect_collection< T: Deserializable, @@ -242,7 +238,6 @@ pub trait Deserializer: Iterator> { expect_rest_of_collection(self, len) } - */ #[inline] fn expect_seq_start(&mut self, token: Token) -> Result { @@ -252,7 +247,6 @@ pub trait Deserializer: Iterator> { } } - /* #[inline] fn expect_map< K: Deserializable, @@ -266,7 +260,6 @@ pub trait Deserializer: Iterator> { expect_rest_of_collection(self, len) } - */ #[inline] fn expect_end(&mut self) -> Result<(), E> { @@ -281,7 +274,6 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// -/* // FIXME: https://github.com/mozilla/rust/issues/11751 #[inline] fn expect_rest_of_collection< @@ -306,7 +298,6 @@ fn expect_rest_of_collection< result::collect_with_capacity(iter, len) } -*/ ////////////////////////////////////////////////////////////////////////////// @@ -339,7 +330,7 @@ macro_rules! impl_deserializable { } } -//impl_deserializable!(bool, expect_bool) +impl_deserializable!(bool, expect_bool) impl_deserializable!(int, expect_num) impl_deserializable!(i8, expect_num) impl_deserializable!(i16, expect_num) @@ -352,14 +343,10 @@ impl_deserializable!(u32, expect_num) impl_deserializable!(u64, expect_num) impl_deserializable!(f32, expect_num) impl_deserializable!(f64, expect_num) - /* impl_deserializable!(char, expect_char) impl_deserializable!(&'static str, expect_str) -*/ impl_deserializable!(StrBuf, expect_strbuf) -/* - ////////////////////////////////////////////////////////////////////////////// impl< @@ -368,11 +355,10 @@ impl< T: Deserializable > Deserializable for Option { #[inline] - fn deserialize(d: &mut D) -> Result, E> { - d.expect_option() + fn deserialize_token(d: &mut D, token: Token) -> Result, E> { + d.expect_option(token) } } -*/ ////////////////////////////////////////////////////////////////////////////// @@ -402,7 +388,6 @@ impl< } } -/* impl< E, D: Deserializer, @@ -410,11 +395,10 @@ impl< V: Deserializable > Deserializable for HashMap { #[inline] - fn deserialize(d: &mut D) -> Result, E> { - d.expect_map() + fn deserialize_token(d: &mut D, token: Token) -> Result, E> { + d.expect_map(token) } } -*/ ////////////////////////////////////////////////////////////////////////////// @@ -470,7 +454,6 @@ deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } ////////////////////////////////////////////////////////////////////////////// -/* #[cfg(test)] mod tests { use collections::HashMap; @@ -480,7 +463,7 @@ mod tests { use super::{Token, Null, Int, Uint, Str, StrBuf, Char, Option}; use super::{TupleStart, StructStart, StructField, EnumStart}; - use super::{SeqStart, MapStart, Sep, End}; + use super::{SeqStart, MapStart, End}; use super::{Deserializer, Deserializable}; ////////////////////////////////////////////////////////////////////////////// @@ -494,8 +477,8 @@ mod tests { impl> Deserializable for Inner { #[inline] - fn deserialize(d: &mut D) -> Result { - try!(d.expect_struct_start("Inner")); + fn deserialize_token(d: &mut D, token: Token) -> Result { + try!(d.expect_struct_start(token, "Inner")); let a = try!(d.expect_struct_field("a")); let b = try!(d.expect_struct_field("b")); let c = try!(d.expect_struct_field("c")); @@ -513,8 +496,8 @@ mod tests { impl> Deserializable for Outer { #[inline] - fn deserialize(d: &mut D) -> Result { - try!(d.expect_struct_start("Outer")); + fn deserialize_token(d: &mut D, token: Token) -> Result { + try!(d.expect_struct_start(token, "Outer")); let inner = try!(d.expect_struct_field("inner")); try!(d.expect_end()); Ok(Outer { inner: inner }) @@ -531,8 +514,8 @@ mod tests { impl> Deserializable for Animal { #[inline] - fn deserialize(d: &mut D) -> Result { - match try!(d.expect_enum_start("Animal", ["Dog", "Frog"])) { + fn deserialize_token(d: &mut D, token: Token) -> Result { + match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) { 0 => { try!(d.expect_end()); Ok(Dog) @@ -685,10 +668,8 @@ mod tests { fn test_tokens_tuple() { let tokens = vec!( TupleStart(2), - Sep, Int(5), - Sep, StrBuf("a".to_strbuf()), End, ); @@ -703,19 +684,14 @@ mod tests { fn test_tokens_tuple_compound() { let tokens = vec!( TupleStart(3), - Sep, Null, - Sep, TupleStart(0), End, - Sep, TupleStart(2), - Sep, Int(5), - Sep, StrBuf("a".to_strbuf()), End, End, @@ -749,7 +725,6 @@ mod tests { StructStart("Outer"), StructField("inner"), SeqStart(1), - Sep, StructStart("Inner"), StructField("a"), Null, @@ -759,12 +734,9 @@ mod tests { StructField("c"), MapStart(1), - Sep, TupleStart(2), - Sep, StrBuf("abc".to_strbuf()), - Sep, Option(true), Char('c'), End, @@ -835,13 +807,10 @@ mod tests { fn test_tokens_vec() { let tokens = vec!( SeqStart(3), - Sep, Int(5), - Sep, Int(6), - Sep, Int(7), End, ); @@ -856,30 +825,21 @@ mod tests { fn test_tokens_vec_compound() { let tokens = vec!( SeqStart(0), - Sep, SeqStart(1), - Sep, Int(1), End, - Sep, SeqStart(2), - Sep, Int(2), - Sep, Int(3), End, - Sep, SeqStart(3), - Sep, Int(4), - Sep, Int(5), - Sep, Int(6), End, End, @@ -895,21 +855,15 @@ mod tests { fn test_tokens_hashmap() { let tokens = vec!( MapStart(2), - Sep, TupleStart(2), - Sep, Int(5), - Sep, StrBuf("a".to_strbuf()), End, - Sep, TupleStart(2), - Sep, Int(6), - Sep, StrBuf("b".to_strbuf()), End, End, @@ -930,13 +884,10 @@ mod tests { b.iter(|| { let tokens = vec!( SeqStart(3), - Sep, Int(5), - Sep, Int(6), - Sep, Int(7), End, ); @@ -948,4 +899,3 @@ mod tests { }) } } -*/