From 34ee45b79477aa1523c7efe387058d766e836625 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 14 Nov 2014 19:41:41 -0800 Subject: [PATCH] Rename json::ParserError to json::Error, use the new error framework --- src/json/mod.rs | 163 +++++++++++++++++++++++++++------------------- src/json/value.rs | 32 ++++----- 2 files changed, 112 insertions(+), 83 deletions(-) diff --git a/src/json/mod.rs b/src/json/mod.rs index 7265035a..a03e12bc 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -293,6 +293,7 @@ fn main() { */ use std::char; +use std::error; use std::f32; use std::f64; use std::fmt; @@ -361,29 +362,6 @@ pub enum ErrorCode { UnrecognizedHex, } -#[deriving(Clone, PartialEq, Show)] -pub enum ParserError { - /// msg, line, col - SyntaxError(ErrorCode, uint, uint), - IoError(io::IoErrorKind, &'static str), - ExpectedError(string::String, string::String), - MissingFieldError(string::String), - UnknownVariantError(string::String), -} - -// Builder and Parser have the same errors. -pub type BuilderError = ParserError; - -/* -#[deriving(Clone, Eq, Show)] -pub enum DecoderError { - ParseError(ParserError), - ExpectedError(String, String), - MissingFieldError(String), - UnknownVariantError(String), -} -*/ - impl fmt::Show for ErrorCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -411,12 +389,63 @@ impl fmt::Show for ErrorCode { } } +#[deriving(Clone, PartialEq, Show)] +pub enum Error { + /// msg, line, col + SyntaxError(ErrorCode, uint, uint), + IoError(io::IoError), + ExpectedError(string::String, string::String), + MissingFieldError(string::String), + UnknownVariantError(string::String), +} + +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + SyntaxError(..) => "syntax error", + IoError(ref error) => error.description(), + ExpectedError(ref expected, _) => expected.as_slice(), + MissingFieldError(_) => "missing field", + UnknownVariantError(_) => "unknown variant", + } + } + + fn detail(&self) -> Option { + match *self { + SyntaxError(ref code, line, col) => { + Some(format!("{} at line {} column {}", code, line, col)) + } + IoError(ref error) => error.detail(), + ExpectedError(ref expected, ref found) => { + Some(format!("expected {}, found {}", expected, found)) + } + MissingFieldError(ref field) => { + Some(format!("missing field {}", field)) + } + UnknownVariantError(ref variant) => { + Some(format!("unknown variant {}", variant)) + } + } + } +} + +impl error::FromError for Error { + fn from_error(error: io::IoError) -> Error { + IoError(error) + } +} + /* -fn io_error_to_error(io: io::IoError) -> ParserError { - IoError(io.kind, io.desc) +#[deriving(Clone, Eq, Show)] +pub enum DecoderError { + ParseError(Error), + ExpectedError(String, String), + MissingFieldError(String), + UnknownVariantError(String), } */ + pub type SerializeResult = io::IoResult<()>; pub fn escape_bytes(wr: &mut W, bytes: &[u8]) -> IoResult<()> { @@ -1038,7 +1067,7 @@ pub enum JsonEvent { NumberValue(f64), StringValue(String), NullValue, - Error(ParserError), + Error(Error), } */ @@ -1216,9 +1245,9 @@ pub struct Parser { buf: Vec, } -impl> Iterator> for Parser { +impl> Iterator> for Parser { #[inline] - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { let state = match self.state_stack.pop() { Some(state) => state, None => { @@ -1305,7 +1334,7 @@ impl> Parser { } #[inline] - fn error(&self, reason: ErrorCode) -> Result { + fn error(&self, reason: ErrorCode) -> Result { Err(SyntaxError(reason, self.line, self.col)) } @@ -1318,7 +1347,7 @@ impl> Parser { } #[inline] - fn parse_number(&mut self) -> Result { + fn parse_number(&mut self) -> Result { let mut neg = 1; if self.ch_is(b'-') { @@ -1347,7 +1376,7 @@ impl> Parser { } #[inline] - fn parse_integer(&mut self) -> Result { + fn parse_integer(&mut self) -> Result { let mut res = 0; match self.ch_or_null() { @@ -1379,7 +1408,7 @@ impl> Parser { } #[inline] - fn parse_decimal(&mut self, res: f64) -> Result { + fn parse_decimal(&mut self, res: f64) -> Result { self.bump(); // Make sure a digit follows the decimal place. @@ -1405,7 +1434,7 @@ impl> Parser { } #[inline] - fn parse_exponent(&mut self, mut res: f64) -> Result { + fn parse_exponent(&mut self, mut res: f64) -> Result { self.bump(); let mut exp = 0u; @@ -1446,7 +1475,7 @@ impl> Parser { } #[inline] - fn decode_hex_escape(&mut self) -> Result { + fn decode_hex_escape(&mut self) -> Result { let mut i = 0u; let mut n = 0u16; while i < 4u && !self.eof() { @@ -1474,7 +1503,7 @@ impl> Parser { } #[inline] - fn parse_string(&mut self) -> Result<&str, ParserError> { + fn parse_string(&mut self) -> Result<&str, Error> { self.buf.clear(); let mut escape = false; @@ -1548,7 +1577,7 @@ impl> Parser { } #[inline] - fn parse_list_start(&mut self) -> Result { + fn parse_list_start(&mut self) -> Result { self.parse_whitespace(); if self.ch_is(b']') { @@ -1561,7 +1590,7 @@ impl> Parser { } #[inline] - fn parse_list_comma_or_end(&mut self) -> Result { + fn parse_list_comma_or_end(&mut self) -> Result { self.parse_whitespace(); if self.ch_is(b',') { @@ -1579,7 +1608,7 @@ impl> Parser { } #[inline] - fn parse_object_start(&mut self) -> Result, ParserError> { + fn parse_object_start(&mut self) -> Result, Error> { self.parse_whitespace(); if self.ch_is(b'}') { @@ -1591,7 +1620,7 @@ impl> Parser { } #[inline] - fn parse_object_comma_or_end(&mut self) -> Result, ParserError> { + fn parse_object_comma_or_end(&mut self) -> Result, Error> { self.parse_whitespace(); if self.ch_is(b',') { @@ -1608,7 +1637,7 @@ impl> Parser { } #[inline] - fn parse_object_key(&mut self) -> Result<&str, ParserError> { + fn parse_object_key(&mut self) -> Result<&str, Error> { self.parse_whitespace(); if self.eof() { @@ -1626,7 +1655,7 @@ impl> Parser { } #[inline] - fn parse_object_value(&mut self) -> Result { + fn parse_object_value(&mut self) -> Result { self.parse_whitespace(); if self.ch_is(b':') { @@ -1641,7 +1670,7 @@ impl> Parser { } #[inline] - fn parse_value(&mut self) -> Result { + fn parse_value(&mut self) -> Result { self.parse_whitespace(); if self.eof() { @@ -1673,7 +1702,7 @@ impl> Parser { } #[inline] - fn parse_ident(&mut self, ident: &[u8], token: de::Token) -> Result { + fn parse_ident(&mut self, ident: &[u8], token: de::Token) -> Result { if ident.iter().all(|c| Some(*c) == self.next_char()) { self.bump(); Ok(token) @@ -1683,33 +1712,33 @@ impl> Parser { } #[inline] - fn error_event(&mut self, reason: ErrorCode) -> Result { + fn error_event(&mut self, reason: ErrorCode) -> Result { self.state_stack.clear(); Err(SyntaxError(reason, self.line, self.col)) } } -impl> de::Deserializer for Parser { - fn end_of_stream_error(&mut self) -> ParserError { +impl> de::Deserializer for Parser { + fn end_of_stream_error(&mut self) -> Error { SyntaxError(EOFWhileParsingValue, self.line, self.col) } - fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> ParserError { + fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> Error { SyntaxError(DeserializerError(token, ExpectTokens(expected.to_vec())), self.line, self.col) } - fn unexpected_name_error(&mut self, token: de::Token) -> ParserError { + fn unexpected_name_error(&mut self, token: de::Token) -> Error { SyntaxError(DeserializerError(token, ExpectName), self.line, self.col) } - fn conversion_error(&mut self, token: de::Token) -> ParserError { + fn conversion_error(&mut self, token: de::Token) -> Error { SyntaxError(DeserializerError(token, ExpectConversion), self.line, self.col) } #[inline] fn missing_field< - T: de::Deserialize, ParserError> - >(&mut self, _field: &'static str) -> Result { + T: de::Deserialize, Error> + >(&mut self, _field: &'static str) -> Result { // JSON can represent `null` values as a missing value, so this isn't // necessarily an error. de::Deserialize::deserialize_token(self, de::Null) @@ -1718,8 +1747,8 @@ impl> de::Deserializer for Parser { // Special case treating options as a nullable value. #[inline] fn expect_option< - U: de::Deserialize, ParserError> - >(&mut self, token: de::Token) -> Result, ParserError> { + U: de::Deserialize, Error> + >(&mut self, token: de::Token) -> Result, Error> { match token { de::Null => Ok(None), token => { @@ -1734,7 +1763,7 @@ impl> de::Deserializer for Parser { fn expect_enum_start(&mut self, token: de::Token, _name: &str, - variants: &[&str]) -> Result { + variants: &[&str]) -> Result { match token { de::MapStart(_) => { } _ => { return self.error(InvalidSyntax(EnumMapStart)); } @@ -1758,7 +1787,7 @@ impl> de::Deserializer for Parser { } } - fn expect_enum_end(&mut self) -> Result<(), ParserError> { + 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 => { @@ -1772,7 +1801,7 @@ impl> de::Deserializer for Parser { } #[inline] - fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), ParserError> { + fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), Error> { match token { de::MapStart(_) => Ok(()), _ => Err(self.syntax_error(token, [de::MapStartKind])), @@ -1782,7 +1811,7 @@ impl> de::Deserializer for Parser { #[inline] fn expect_struct_field_or_end(&mut self, fields: &'static [&'static str] - ) -> Result>, ParserError> { + ) -> Result>, Error> { let result = match self.state_stack.pop() { Some(ParseObjectStart) => { try!(self.parse_object_start()) @@ -1805,8 +1834,8 @@ impl> de::Deserializer for Parser { /// Decodes a json value from an `Iterator`. pub fn from_iter< Iter: Iterator, - T: de::Deserialize, ParserError> ->(iter: Iter) -> Result { + T: de::Deserialize, Error> +>(iter: Iter) -> Result { let mut parser = Parser::new(iter); let value = try!(de::Deserialize::deserialize(&mut parser)); @@ -1821,8 +1850,8 @@ pub fn from_iter< /// Decodes a json value from a string pub fn from_str< 'a, - T: de::Deserialize>, ParserError> ->(s: &'a str) -> Result { + T: de::Deserialize>, Error> +>(s: &'a str) -> Result { from_iter(s.bytes()) } @@ -1862,7 +1891,7 @@ mod tests { List, Object, }; - use super::{Parser, ParserError, from_str}; + use super::{Parser, Error, from_str}; use super::value; use super::value::{ToJson, from_json}; use super::{ @@ -2312,17 +2341,17 @@ mod tests { // FIXME (#5527): these could be merged once UFCS is finished. fn test_parse_err< 'a, - T: Show + de::Deserialize>, ParserError> - >(errors: &[(&'a str, ParserError)]) { + T: Show + de::Deserialize>, Error> + >(errors: &[(&'a str, Error)]) { for &(s, ref err) in errors.iter() { - let v: Result = from_str(s); + let v: Result = from_str(s); assert_eq!(v.unwrap_err(), *err); } } fn test_parse_ok< 'a, - T: PartialEq + Show + ToJson + de::Deserialize>, ParserError> + T: PartialEq + Show + ToJson + de::Deserialize>, Error> >(errors: &[(&'a str, T)]) { for &(s, ref value) in errors.iter() { let v: T = from_str(s).unwrap(); @@ -2334,7 +2363,7 @@ mod tests { } fn test_json_deserialize_ok< - T: PartialEq + Show + ToJson + de::Deserialize + T: PartialEq + Show + ToJson + de::Deserialize >(errors: &[T]) { for value in errors.iter() { let v: T = from_json(value.to_json()).unwrap(); diff --git a/src/json/value.rs b/src/json/value.rs index df15314c..d27427f6 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -13,7 +13,7 @@ use ser; use super::PrettySerializer; use super::Serializer; use super::SerializeResult; -use super::ParserError; +use super::Error; use super::{ MissingFieldError, SyntaxError, @@ -329,9 +329,9 @@ impl Deserializer { } } -impl Iterator> for Deserializer { +impl Iterator> for Deserializer { #[inline] - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { loop { match self.stack.pop() { Some(DeserializerValueState(value)) => { @@ -388,27 +388,27 @@ impl Iterator> for Deserializer { } } -impl de::Deserializer for Deserializer { - fn end_of_stream_error(&mut self) -> ParserError { +impl de::Deserializer for Deserializer { + fn end_of_stream_error(&mut self) -> Error { SyntaxError(EOFWhileParsingValue, 0, 0) } - fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> ParserError { + fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> Error { SyntaxError(DeserializerError(token, ExpectTokens(expected.to_vec())), 0, 0) } - fn unexpected_name_error(&mut self, token: de::Token) -> ParserError { + fn unexpected_name_error(&mut self, token: de::Token) -> Error { SyntaxError(DeserializerError(token, ExpectName), 0, 0) } - fn conversion_error(&mut self, token: de::Token) -> ParserError { + fn conversion_error(&mut self, token: de::Token) -> Error { SyntaxError(DeserializerError(token, ExpectConversion), 0, 0) } #[inline] fn missing_field< - T: de::Deserialize - >(&mut self, _field: &'static str) -> Result { + T: de::Deserialize + >(&mut self, _field: &'static str) -> Result { // JSON can represent `null` values as a missing value, so this isn't // necessarily an error. de::Deserialize::deserialize_token(self, de::Null) @@ -417,8 +417,8 @@ impl de::Deserializer for Deserializer { // Special case treating options as a nullable value. #[inline] fn expect_option< - U: de::Deserialize - >(&mut self, token: de::Token) -> Result, ParserError> { + U: de::Deserialize + >(&mut self, token: de::Token) -> Result, Error> { match token { de::Null => Ok(None), token => { @@ -433,7 +433,7 @@ impl de::Deserializer for Deserializer { fn expect_enum_start(&mut self, token: de::Token, _name: &str, - variants: &[&str]) -> Result { + variants: &[&str]) -> Result { let variant = match token { de::MapStart(_) => { let state = match self.stack.pop() { @@ -483,7 +483,7 @@ impl de::Deserializer for Deserializer { } #[inline] - fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), ParserError> { + fn expect_struct_start(&mut self, token: de::Token, _name: &str) -> Result<(), Error> { match token { de::MapStart(_) => Ok(()), _ => Err(self.syntax_error(token, [de::MapStartKind])), @@ -493,8 +493,8 @@ impl de::Deserializer for Deserializer { /// Decodes a json value from a `Value`. pub fn from_json< - T: de::Deserialize ->(json: Value) -> Result { + T: de::Deserialize +>(json: Value) -> Result { let mut d = Deserializer::new(json); de::Deserialize::deserialize(&mut d) }