diff --git a/de.rs b/de.rs index 7283153c..f482faac 100644 --- a/de.rs +++ b/de.rs @@ -31,11 +31,9 @@ pub enum Token { macro_rules! decode_primitive { ($( $Variant:pat => $E:expr ),+) => { - match self.next() { - $( Some(Ok($Variant)) => $E ),+, - Some(Ok(_)) => Err(self.syntax_error()), - Some(Err(err)) => Err(err), - None => Err(self.end_of_stream_error()), + match token { + $( $Variant => $E ),+, + _ => Err(self.syntax_error()), } } } @@ -74,103 +72,124 @@ pub trait Deserializer: Iterator> { fn syntax_error(&self) -> E; #[inline] - fn expect_null(&mut self) -> Result<(), E> { + fn expect_token(&mut self) -> Result { + match self.next() { + Some(Ok(token)) => Ok(token), + Some(Err(err)) => Err(err), + None => Err(self.end_of_stream_error()), + } + } + + #[inline] + fn expect_null(&mut self, token: Token) -> Result<(), E> { decode_primitive!( Null => Ok(()), - CollectionStart => self.expect_collection_end() + CollectionStart => { + let token = try!(self.expect_token()); + self.expect_collection_end(token) + } ) } #[inline] - fn expect_bool(&mut self) -> Result { + fn expect_bool(&mut self, token: Token) -> Result { decode_primitive!(Bool(value) => Ok(value)) } #[inline] - fn expect_int(&mut self) -> Result { + fn expect_int(&mut self, token: Token) -> Result { decode_primitive_num!(to_int) } #[inline] - fn expect_i8(&mut self) -> Result { + fn expect_i8(&mut self, token: Token) -> Result { decode_primitive_num!(to_i8) } - #[inline] - fn expect_i16(&mut self) -> Result { + fn expect_i16(&mut self, token: Token) -> Result { decode_primitive_num!(to_i16) } #[inline] - fn expect_i32(&mut self) -> Result { + fn expect_i32(&mut self, token: Token) -> Result { decode_primitive_num!(to_i32) } #[inline] - fn expect_i64(&mut self) -> Result { + fn expect_i64(&mut self, token: Token) -> Result { decode_primitive_num!(to_i64) } #[inline] - fn expect_uint(&mut self) -> Result { + fn expect_uint(&mut self, token: Token) -> Result { decode_primitive_num!(to_uint) } #[inline] - fn expect_u8(&mut self) -> Result { + fn expect_u8(&mut self, token: Token) -> Result { decode_primitive_num!(to_u8) } #[inline] - fn expect_u16(&mut self) -> Result { + fn expect_u16(&mut self, token: Token) -> Result { decode_primitive_num!(to_u16) } #[inline] - fn expect_u32(&mut self) -> Result { + fn expect_u32(&mut self, token: Token) -> Result { decode_primitive_num!(to_u32) } #[inline] - fn expect_u64(&mut self) -> Result { + fn expect_u64(&mut self, token: Token) -> Result { decode_primitive_num!(to_u64) } #[inline] - fn expect_f32(&mut self) -> Result { + fn expect_f32(&mut self, token: Token) -> Result { decode_primitive_num!(to_f32) } #[inline] - fn expect_f64(&mut self) -> Result { + fn expect_f64(&mut self, token: Token) -> Result { decode_primitive_num!(to_f64) } #[inline] - fn expect_char(&mut self) -> Result { + fn expect_char(&mut self, token: Token) -> Result { decode_primitive!(Char(value) => Ok(value)) } #[inline] - fn expect_str(&mut self) -> Result<&'static str, E> { + fn expect_str(&mut self, token: Token) -> Result<&'static str, E> { decode_primitive!(Str(value) => Ok(value)) } #[inline] - fn expect_strbuf(&mut self) -> Result { + fn expect_strbuf(&mut self, token: Token) -> Result { decode_primitive!( Str(value) => Ok(value.to_strbuf()), StrBuf(value) => Ok(value) ) } + #[inline] + fn expect_option< + T: Deserializable + >(&mut self, token: Token) -> Result, E> { + match token { + Null => Ok(None), + _ => fail!(), + } + } + #[inline] fn expect_collection< T: Deserializable, C: FromIterator - >(&mut self) -> Result { - try!(self.expect_collection_start()); + >(&mut self, token: Token) -> Result { + try!(self.expect_collection_start(token)); let iter = self.by_ref().batch(|d| { let d = d.iter(); @@ -201,43 +220,35 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_collection_start(&mut self) -> Result<(), E> { - match self.next() { - Some(Ok(CollectionStart)) => Ok(()), - Some(Ok(_)) => Err(self.syntax_error()), - Some(Err(err)) => Err(err), - None => Err(self.end_of_stream_error()), + fn expect_collection_start(&mut self, token: Token) -> Result<(), E> { + match token { + CollectionStart => Ok(()), + _ => Err(self.syntax_error()), } } #[inline] - fn expect_collection_sep(&mut self) -> Result<(), E> { - match self.next() { - Some(Ok(CollectionSep)) => Ok(()), - Some(Ok(_)) => Err(self.syntax_error()), - Some(Err(err)) => Err(err), - None => Err(self.end_of_stream_error()), + fn expect_collection_sep(&mut self, token: Token) -> Result<(), E> { + match token { + CollectionSep => Ok(()), + _ => Err(self.syntax_error()), } } #[inline] - fn expect_collection_end(&mut self) -> Result<(), E> { - match self.next() { - Some(Ok(CollectionEnd)) => Ok(()), - Some(Ok(_)) => Err(self.syntax_error()), - Some(Err(err)) => Err(err), - None => Err(self.end_of_stream_error()), + fn expect_collection_end(&mut self, token: Token) -> Result<(), E> { + match token { + CollectionEnd => Ok(()), + _ => Err(self.syntax_error()), } } #[inline] - fn expect_collection_sep_or_end(&mut self) -> Result { - match self.next() { - Some(Ok(CollectionSep)) => Ok(false), - Some(Ok(CollectionEnd)) => Ok(true), - Some(Ok(_)) => Err(self.syntax_error()), - Some(Err(err)) => Err(err), - None => Err(self.end_of_stream_error()), + fn expect_collection_sep_or_end(&mut self, token: Token) -> Result { + match token { + CollectionSep => Ok(false), + CollectionEnd => Ok(true), + _ => Err(self.syntax_error()), } } } @@ -245,7 +256,12 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// pub trait Deserializable> { - fn deserialize(d: &mut D) -> Result; + fn deserialize(d: &mut D) -> Result { + let token = try!(d.expect_token()); + Deserializable::deserialize_token(d, token) + } + + fn deserialize_token(d: &mut D, token: Token) -> Result; } ////////////////////////////////////////////////////////////////////////////// @@ -257,8 +273,8 @@ macro_rules! impl_deserializable { D: Deserializer > Deserializable for $ty { #[inline] - fn deserialize(d: &mut D) -> Result<$ty, E> { - d.$method() + fn deserialize_token(d: &mut D, token: Token) -> Result<$ty, E> { + d.$method(token) } } } @@ -289,9 +305,8 @@ impl< T: Deserializable > Deserializable for Option { #[inline] - fn deserialize(_d: &mut D) -> Result, E> { - fail!() - //d.expect_collection() + fn deserialize_token(d: &mut D, token: Token) -> Result, E> { + d.expect_option(token) } } @@ -303,8 +318,8 @@ impl< T: Deserializable > Deserializable for Vec { #[inline] - fn deserialize(d: &mut D) -> Result, E> { - d.expect_collection() + fn deserialize_token(d: &mut D, token: Token) -> Result, E> { + d.expect_collection(token) } } @@ -315,8 +330,8 @@ impl< V: Deserializable > Deserializable for HashMap { #[inline] - fn deserialize(d: &mut D) -> Result, E> { - d.expect_collection() + fn deserialize_token(d: &mut D, token: Token) -> Result, E> { + d.expect_collection(token) } } @@ -327,8 +342,8 @@ impl< D: Deserializer > Deserializable for () { #[inline] - fn deserialize(d: &mut D) -> Result<(), E> { - d.expect_null() + fn deserialize_token(d: &mut D, token: Token) -> Result<(), E> { + d.expect_null(token) } } @@ -340,13 +355,15 @@ impl< T0: Deserializable > Deserializable for (T0,) { #[inline] - fn deserialize(d: &mut D) -> Result<(T0,), E> { - try!(d.expect_collection_start()); + fn deserialize_token(d: &mut D, token: Token) -> Result<(T0,), E> { + try!(d.expect_collection_start(token)); - try!(d.expect_collection_sep()); + let token = try!(d.expect_token()); + try!(d.expect_collection_sep(token)); let x0 = try!(Deserializable::deserialize(d)); - try!(d.expect_collection_end()); + let token = try!(d.expect_token()); + try!(d.expect_collection_end(token)); Ok((x0,)) } @@ -361,16 +378,19 @@ impl< T1: Deserializable > Deserializable for (T0, T1) { #[inline] - fn deserialize(d: &mut D) -> Result<(T0, T1), E> { - try!(d.expect_collection_start()); + fn deserialize_token(d: &mut D, token: Token) -> Result<(T0, T1), E> { + try!(d.expect_collection_start(token)); - try!(d.expect_collection_sep()); + let token = try!(d.expect_token()); + try!(d.expect_collection_sep(token)); let x0 = try!(Deserializable::deserialize(d)); - try!(d.expect_collection_sep()); + let token = try!(d.expect_token()); + try!(d.expect_collection_sep(token)); let x1 = try!(Deserializable::deserialize(d)); - try!(d.expect_collection_end()); + let token = try!(d.expect_token()); + try!(d.expect_collection_end(token)); Ok((x0, x1)) } @@ -509,8 +529,9 @@ mod tests { SyntaxError } + /* #[inline] - fn expect_int(&mut self) -> Result { + fn expect_int(&mut self, token: Token) -> Result { assert_eq!(self.state, Value); self.state = Sep; @@ -520,6 +541,7 @@ mod tests { None => Err(self.end_of_stream_error()), } } + */ } struct IntsDecoder {