diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index ee809ef1..608d8443 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -24,6 +24,16 @@ enum Error { EndOfStreamError, } +impl de::Error for Error { + fn syntax_error() -> Error { + Error::SyntaxError + } + + fn end_of_stream_error() -> Error { + Error::EndOfStreamError + } +} + /////////////////////////////////////////////////////////////////////////////// struct MyDeserializer { @@ -63,6 +73,8 @@ impl> Deserializer for MyDeserializer { R, V: de::Visitor, R, Error>, >(&mut self, visitor: &mut V) -> Result { + use serde2::de::Error; + match self.next() { Some(Token::Null) => { visitor.visit_null(self) @@ -85,10 +97,10 @@ impl> Deserializer for MyDeserializer { visitor.visit_map(self, MyMapVisitor { len: len }) } Some(Token::End) => { - Err(self.syntax_error()) + Err(Error::syntax_error()) } None => { - Err(self.end_of_stream_error()) + Err(Error::end_of_stream_error()) } } } @@ -117,14 +129,6 @@ impl> Deserializer for MyDeserializer { } } } - - fn syntax_error(&mut self) -> Error { - Error::SyntaxError - } - - fn end_of_stream_error(&mut self) -> Error { - Error::EndOfStreamError - } } struct MyOptionVisitor { @@ -157,6 +161,8 @@ impl< fn visit< T: Deserialize, Error> >(&mut self, d: &mut MyDeserializer) -> Result, Error> { + use serde2::de::Error; + match d.peek() { Some(&Token::End) => { d.next(); @@ -168,16 +174,18 @@ impl< Ok(Some(value)) } None => { - Err(d.syntax_error()) + Err(Error::syntax_error()) } } } fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { + use serde2::de::Error; + match d.next() { Some(Token::End) => Ok(()), - Some(_) => Err(d.syntax_error()), - None => Err(d.end_of_stream_error()), + Some(_) => Err(Error::syntax_error()), + None => Err(Error::end_of_stream_error()), } } @@ -196,6 +204,8 @@ impl< fn visit_key< K: Deserialize, Error>, >(&mut self, d: &mut MyDeserializer) -> Result, Error> { + use serde2::de::Error; + match d.peek() { Some(&Token::End) => { d.next(); @@ -207,7 +217,7 @@ impl< Ok(Some(try!(Deserialize::deserialize(d)))) } None => { - Err(d.syntax_error()) + Err(Error::syntax_error()) } } } @@ -219,10 +229,12 @@ impl< } fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { + use serde2::de::Error; + match d.next() { Some(Token::End) => Ok(()), - Some(_) => Err(d.syntax_error()), - None => Err(d.end_of_stream_error()), + Some(_) => Err(Error::syntax_error()), + None => Err(Error::end_of_stream_error()), } } @@ -249,14 +261,14 @@ mod json { impl< D: de::Deserializer, - E, + E: de::Error, > de::Deserialize for Value { fn deserialize(d: &mut D) -> Result { struct Visitor; impl< D: de::Deserializer, - E, + E: de::Error, > de::Visitor for Visitor { fn visit_null(&mut self, _d: &mut D) -> Result { Ok(Value::Null) diff --git a/serde2/src/de.rs b/serde2/src/de.rs index d79e17b1..ed317410 100644 --- a/serde2/src/de.rs +++ b/serde2/src/de.rs @@ -3,6 +3,12 @@ use std::hash::Hash; /////////////////////////////////////////////////////////////////////////////// +pub trait Error { + fn syntax_error() -> Self; + + fn end_of_stream_error() -> Self; +} + pub trait Deserialize { fn deserialize(state: &mut S) -> Result; } @@ -19,19 +25,15 @@ pub trait Deserializer { >(&mut self, visitor: &mut V) -> Result { self.visit(visitor) } - - fn syntax_error(&mut self) -> E; - - fn end_of_stream_error(&mut self) -> E; } -pub trait Visitor, R, E> { +pub trait Visitor, R, E: Error> { fn visit_null(&mut self, state: &mut S) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_bool(&mut self, state: &mut S, _v: bool) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_int(&mut self, state: &mut S, v: int) -> Result { @@ -51,7 +53,7 @@ pub trait Visitor, R, E> { } fn visit_i64(&mut self, state: &mut S, _v: i64) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_uint(&mut self, state: &mut S, v: uint) -> Result { @@ -71,7 +73,7 @@ pub trait Visitor, R, E> { } fn visit_u64(&mut self, state: &mut S, _v: u64) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_f32(&mut self, state: &mut S, v: f32) -> Result { @@ -79,11 +81,11 @@ pub trait Visitor, R, E> { } fn visit_f64(&mut self, state: &mut S, _v: f64) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_str(&mut self, state: &mut S, _v: &str) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_string(&mut self, state: &mut S, v: String) -> Result { @@ -93,19 +95,19 @@ pub trait Visitor, R, E> { fn visit_option< V: OptionVisitor, >(&mut self, state: &mut S, _visitor: V) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_seq< V: SeqVisitor, >(&mut self, state: &mut S, _visitor: V) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } fn visit_map< V: MapVisitor, >(&mut self, state: &mut S, _visitor: V) -> Result { - Err(state.syntax_error()) + Err(Error::syntax_error()) } } @@ -162,12 +164,15 @@ pub trait MapVisitor { impl< S: Deserializer, - E, + E: Error, > Deserialize for () { fn deserialize(state: &mut S) -> Result<(), E> { struct Visitor; - impl, E> self::Visitor for Visitor { + impl< + S: Deserializer, + E: Error, + > self::Visitor for Visitor { fn visit_null(&mut self, _state: &mut S) -> Result<(), E> { Ok(()) } @@ -188,12 +193,15 @@ impl< impl< S: Deserializer, - E, + E: Error, > Deserialize for bool { fn deserialize(state: &mut S) -> Result { struct Visitor; - impl, E> self::Visitor for Visitor { + impl< + S: Deserializer, + E: Error, + > self::Visitor for Visitor { fn visit_bool(&mut self, _state: &mut S, v: bool) -> Result { Ok(v) } @@ -210,7 +218,7 @@ macro_rules! impl_deserialize_num_method { fn $method(&mut self, state: &mut S, v: $src_ty) -> Result { match FromPrimitive::$from_method(v) { Some(v) => Ok(v), - None => Err(state.syntax_error()), + None => Err(Error::syntax_error()), } } } @@ -219,14 +227,14 @@ macro_rules! impl_deserialize_num_method { #[inline] pub fn deserialize_from_primitive< S: Deserializer, - E, + E: Error, T: Deserialize + FromPrimitive >(state: &mut S) -> Result { struct Visitor; impl< S: Deserializer, - E, + E: Error, T: Deserialize + FromPrimitive > self::Visitor for Visitor { impl_deserialize_num_method!(int, visit_int, from_int) @@ -248,7 +256,10 @@ pub fn deserialize_from_primitive< macro_rules! impl_deserialize_num { ($ty:ty) => { - impl, E> Deserialize for $ty { + impl< + S: Deserializer, + E: Error, + > Deserialize for $ty { #[inline] fn deserialize(state: &mut S) -> Result<$ty, E> { deserialize_from_primitive(state) @@ -274,12 +285,15 @@ impl_deserialize_num!(f64) impl< S: Deserializer, - E, + E: Error, > Deserialize for String { fn deserialize(state: &mut S) -> Result { struct Visitor; - impl, E> self::Visitor for Visitor { + impl< + S: Deserializer, + E: Error, + > self::Visitor for Visitor { fn visit_str(&mut self, _state: &mut S, v: &str) -> Result { Ok(v.to_string()) } @@ -298,7 +312,7 @@ impl< impl< T: Deserialize, S: Deserializer, - E, + E: Error, > Deserialize for Option { fn deserialize(state: &mut S) -> Result, E> { struct Visitor; @@ -306,7 +320,7 @@ impl< impl< T: Deserialize, S: Deserializer, - E, + E: Error, > self::Visitor, E> for Visitor { fn visit_option< V: OptionVisitor, @@ -324,7 +338,7 @@ impl< impl< T: Deserialize, S: Deserializer, - E, + E: Error, > Deserialize for Vec { fn deserialize(state: &mut S) -> Result, E> { struct Visitor; @@ -332,7 +346,7 @@ impl< impl< T: Deserialize, S: Deserializer, - E, + E: Error, > self::Visitor, E> for Visitor { fn visit_seq< V: SeqVisitor, @@ -374,7 +388,7 @@ macro_rules! impl_deserialize_tuple { impl< S: Deserializer, - E, + E: Error, $($name: Deserialize),+ > Deserialize for ($($name,)+) { #[inline] @@ -384,7 +398,7 @@ macro_rules! impl_deserialize_tuple { impl< S: Deserializer, - E, + E: Error, $($name: Deserialize,)+ > self::Visitor for Visitor { fn visit_seq< @@ -393,7 +407,7 @@ macro_rules! impl_deserialize_tuple { $( let $name = match try!(visitor.visit(state)) { Some(value) => value, - None => { return Err(state.end_of_stream_error()); } + None => { return Err(Error::end_of_stream_error()); } }; )+; @@ -417,7 +431,7 @@ impl< K: Deserialize + Eq + Hash, V: Deserialize, S: Deserializer, - E, + E: Error, > Deserialize for HashMap { fn deserialize(state: &mut S) -> Result, E> { struct Visitor; @@ -426,7 +440,7 @@ impl< K: Deserialize + Eq + Hash, V: Deserialize, S: Deserializer, - E, + E: Error, > self::Visitor, E> for Visitor { fn visit_map< Visitor: MapVisitor, @@ -457,7 +471,7 @@ impl< K: Deserialize + Eq + Ord, V: Deserialize, S: Deserializer, - E, + E: Error, > Deserialize for TreeMap { fn deserialize(state: &mut S) -> Result, E> { struct Visitor; @@ -466,7 +480,7 @@ impl< K: Deserialize + Eq + Ord, V: Deserialize, S: Deserializer, - E, + E: Error, > self::Visitor, E> for Visitor { fn visit_map< Visitor: MapVisitor, diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index 56841b05..6b91e00a 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -387,14 +387,6 @@ impl> Deserializer for Parser { >(&mut self, visitor: &mut V) -> Result { self.parse_value(visitor) } - - fn syntax_error(&mut self) -> Error { - Error::SyntaxError(ErrorCode::ExpectedSomeValue, self.line, self.col) - } - - fn end_of_stream_error(&mut self) -> Error { - Error::SyntaxError(ErrorCode::EOFWhileParsingValue, self.line, self.col) - } } struct SeqVisitor { diff --git a/serde2/src/json/error.rs b/serde2/src/json/error.rs index 911f94d2..616b54f8 100644 --- a/serde2/src/json/error.rs +++ b/serde2/src/json/error.rs @@ -2,6 +2,8 @@ use std::error; use std::fmt; use std::io; +use de; + /// The errors that can arise while parsing a JSON stream. #[deriving(Clone, PartialEq)] pub enum ErrorCode { @@ -124,3 +126,13 @@ impl error::FromError for Error { Error::IoError(error) } } + +impl de::Error for Error { + fn syntax_error() -> Error { + Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0) + } + + fn end_of_stream_error() -> Error { + Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0) + } +}