mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-29 22:11:09 +00:00
Rename json::ParserError to json::Error, use the new error framework
This commit is contained in:
parent
2b121b0db8
commit
34ee45b794
163
src/json/mod.rs
163
src/json/mod.rs
@ -293,6 +293,7 @@ fn main() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use std::char;
|
use std::char;
|
||||||
|
use std::error;
|
||||||
use std::f32;
|
use std::f32;
|
||||||
use std::f64;
|
use std::f64;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@ -361,29 +362,6 @@ pub enum ErrorCode {
|
|||||||
UnrecognizedHex,
|
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 {
|
impl fmt::Show for ErrorCode {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
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<String> {
|
||||||
|
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<io::IoError> for Error {
|
||||||
|
fn from_error(error: io::IoError) -> Error {
|
||||||
|
IoError(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
fn io_error_to_error(io: io::IoError) -> ParserError {
|
#[deriving(Clone, Eq, Show)]
|
||||||
IoError(io.kind, io.desc)
|
pub enum DecoderError {
|
||||||
|
ParseError(Error),
|
||||||
|
ExpectedError(String, String),
|
||||||
|
MissingFieldError(String),
|
||||||
|
UnknownVariantError(String),
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
pub type SerializeResult = io::IoResult<()>;
|
pub type SerializeResult = io::IoResult<()>;
|
||||||
|
|
||||||
pub fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
pub fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
||||||
@ -1038,7 +1067,7 @@ pub enum JsonEvent {
|
|||||||
NumberValue(f64),
|
NumberValue(f64),
|
||||||
StringValue(String),
|
StringValue(String),
|
||||||
NullValue,
|
NullValue,
|
||||||
Error(ParserError),
|
Error(Error),
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -1216,9 +1245,9 @@ pub struct Parser<Iter> {
|
|||||||
buf: Vec<u8>,
|
buf: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<u8>> Iterator<Result<de::Token, ParserError>> for Parser<Iter> {
|
impl<Iter: Iterator<u8>> Iterator<Result<de::Token, Error>> for Parser<Iter> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<Result<de::Token, ParserError>> {
|
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||||
let state = match self.state_stack.pop() {
|
let state = match self.state_stack.pop() {
|
||||||
Some(state) => state,
|
Some(state) => state,
|
||||||
None => {
|
None => {
|
||||||
@ -1305,7 +1334,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn error<T>(&self, reason: ErrorCode) -> Result<T, ParserError> {
|
fn error<T>(&self, reason: ErrorCode) -> Result<T, Error> {
|
||||||
Err(SyntaxError(reason, self.line, self.col))
|
Err(SyntaxError(reason, self.line, self.col))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1318,7 +1347,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_number(&mut self) -> Result<de::Token, ParserError> {
|
fn parse_number(&mut self) -> Result<de::Token, Error> {
|
||||||
let mut neg = 1;
|
let mut neg = 1;
|
||||||
|
|
||||||
if self.ch_is(b'-') {
|
if self.ch_is(b'-') {
|
||||||
@ -1347,7 +1376,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_integer(&mut self) -> Result<i64, ParserError> {
|
fn parse_integer(&mut self) -> Result<i64, Error> {
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
|
|
||||||
match self.ch_or_null() {
|
match self.ch_or_null() {
|
||||||
@ -1379,7 +1408,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_decimal(&mut self, res: f64) -> Result<f64, ParserError> {
|
fn parse_decimal(&mut self, res: f64) -> Result<f64, Error> {
|
||||||
self.bump();
|
self.bump();
|
||||||
|
|
||||||
// Make sure a digit follows the decimal place.
|
// Make sure a digit follows the decimal place.
|
||||||
@ -1405,7 +1434,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> {
|
fn parse_exponent(&mut self, mut res: f64) -> Result<f64, Error> {
|
||||||
self.bump();
|
self.bump();
|
||||||
|
|
||||||
let mut exp = 0u;
|
let mut exp = 0u;
|
||||||
@ -1446,7 +1475,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
|
fn decode_hex_escape(&mut self) -> Result<u16, Error> {
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
let mut n = 0u16;
|
let mut n = 0u16;
|
||||||
while i < 4u && !self.eof() {
|
while i < 4u && !self.eof() {
|
||||||
@ -1474,7 +1503,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_string(&mut self) -> Result<&str, ParserError> {
|
fn parse_string(&mut self) -> Result<&str, Error> {
|
||||||
self.buf.clear();
|
self.buf.clear();
|
||||||
|
|
||||||
let mut escape = false;
|
let mut escape = false;
|
||||||
@ -1548,7 +1577,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_list_start(&mut self) -> Result<de::Token, ParserError> {
|
fn parse_list_start(&mut self) -> Result<de::Token, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.ch_is(b']') {
|
if self.ch_is(b']') {
|
||||||
@ -1561,7 +1590,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_list_comma_or_end(&mut self) -> Result<de::Token, ParserError> {
|
fn parse_list_comma_or_end(&mut self) -> Result<de::Token, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.ch_is(b',') {
|
if self.ch_is(b',') {
|
||||||
@ -1579,7 +1608,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_object_start(&mut self) -> Result<Option<&str>, ParserError> {
|
fn parse_object_start(&mut self) -> Result<Option<&str>, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.ch_is(b'}') {
|
if self.ch_is(b'}') {
|
||||||
@ -1591,7 +1620,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_object_comma_or_end(&mut self) -> Result<Option<&str>, ParserError> {
|
fn parse_object_comma_or_end(&mut self) -> Result<Option<&str>, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.ch_is(b',') {
|
if self.ch_is(b',') {
|
||||||
@ -1608,7 +1637,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_object_key(&mut self) -> Result<&str, ParserError> {
|
fn parse_object_key(&mut self) -> Result<&str, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.eof() {
|
if self.eof() {
|
||||||
@ -1626,7 +1655,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_object_value(&mut self) -> Result<de::Token, ParserError> {
|
fn parse_object_value(&mut self) -> Result<de::Token, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.ch_is(b':') {
|
if self.ch_is(b':') {
|
||||||
@ -1641,7 +1670,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_value(&mut self) -> Result<de::Token, ParserError> {
|
fn parse_value(&mut self) -> Result<de::Token, Error> {
|
||||||
self.parse_whitespace();
|
self.parse_whitespace();
|
||||||
|
|
||||||
if self.eof() {
|
if self.eof() {
|
||||||
@ -1673,7 +1702,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_ident(&mut self, ident: &[u8], token: de::Token) -> Result<de::Token, ParserError> {
|
fn parse_ident(&mut self, ident: &[u8], token: de::Token) -> Result<de::Token, Error> {
|
||||||
if ident.iter().all(|c| Some(*c) == self.next_char()) {
|
if ident.iter().all(|c| Some(*c) == self.next_char()) {
|
||||||
self.bump();
|
self.bump();
|
||||||
Ok(token)
|
Ok(token)
|
||||||
@ -1683,33 +1712,33 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn error_event<T>(&mut self, reason: ErrorCode) -> Result<T, ParserError> {
|
fn error_event<T>(&mut self, reason: ErrorCode) -> Result<T, Error> {
|
||||||
self.state_stack.clear();
|
self.state_stack.clear();
|
||||||
Err(SyntaxError(reason, self.line, self.col))
|
Err(SyntaxError(reason, self.line, self.col))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
|
||||||
fn end_of_stream_error(&mut self) -> ParserError {
|
fn end_of_stream_error(&mut self) -> Error {
|
||||||
SyntaxError(EOFWhileParsingValue, self.line, self.col)
|
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)
|
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)
|
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)
|
SyntaxError(DeserializerError(token, ExpectConversion), self.line, self.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field<
|
fn missing_field<
|
||||||
T: de::Deserialize<Parser<Iter>, ParserError>
|
T: de::Deserialize<Parser<Iter>, Error>
|
||||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||||
// JSON can represent `null` values as a missing value, so this isn't
|
// JSON can represent `null` values as a missing value, so this isn't
|
||||||
// necessarily an error.
|
// necessarily an error.
|
||||||
de::Deserialize::deserialize_token(self, de::Null)
|
de::Deserialize::deserialize_token(self, de::Null)
|
||||||
@ -1718,8 +1747,8 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
// Special case treating options as a nullable value.
|
// Special case treating options as a nullable value.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_option<
|
fn expect_option<
|
||||||
U: de::Deserialize<Parser<Iter>, ParserError>
|
U: de::Deserialize<Parser<Iter>, Error>
|
||||||
>(&mut self, token: de::Token) -> Result<Option<U>, ParserError> {
|
>(&mut self, token: de::Token) -> Result<Option<U>, Error> {
|
||||||
match token {
|
match token {
|
||||||
de::Null => Ok(None),
|
de::Null => Ok(None),
|
||||||
token => {
|
token => {
|
||||||
@ -1734,7 +1763,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
fn expect_enum_start(&mut self,
|
fn expect_enum_start(&mut self,
|
||||||
token: de::Token,
|
token: de::Token,
|
||||||
_name: &str,
|
_name: &str,
|
||||||
variants: &[&str]) -> Result<uint, ParserError> {
|
variants: &[&str]) -> Result<uint, Error> {
|
||||||
match token {
|
match token {
|
||||||
de::MapStart(_) => { }
|
de::MapStart(_) => { }
|
||||||
_ => { return self.error(InvalidSyntax(EnumMapStart)); }
|
_ => { return self.error(InvalidSyntax(EnumMapStart)); }
|
||||||
@ -1758,7 +1787,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
// There will be one `End` for the list, and one for the object.
|
||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
de::End => {
|
de::End => {
|
||||||
@ -1772,7 +1801,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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 {
|
match token {
|
||||||
de::MapStart(_) => Ok(()),
|
de::MapStart(_) => Ok(()),
|
||||||
_ => Err(self.syntax_error(token, [de::MapStartKind])),
|
_ => Err(self.syntax_error(token, [de::MapStartKind])),
|
||||||
@ -1782,7 +1811,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn expect_struct_field_or_end(&mut self,
|
fn expect_struct_field_or_end(&mut self,
|
||||||
fields: &'static [&'static str]
|
fields: &'static [&'static str]
|
||||||
) -> Result<Option<Option<uint>>, ParserError> {
|
) -> Result<Option<Option<uint>>, Error> {
|
||||||
let result = match self.state_stack.pop() {
|
let result = match self.state_stack.pop() {
|
||||||
Some(ParseObjectStart) => {
|
Some(ParseObjectStart) => {
|
||||||
try!(self.parse_object_start())
|
try!(self.parse_object_start())
|
||||||
@ -1805,8 +1834,8 @@ impl<Iter: Iterator<u8>> de::Deserializer<ParserError> for Parser<Iter> {
|
|||||||
/// Decodes a json value from an `Iterator<u8>`.
|
/// Decodes a json value from an `Iterator<u8>`.
|
||||||
pub fn from_iter<
|
pub fn from_iter<
|
||||||
Iter: Iterator<u8>,
|
Iter: Iterator<u8>,
|
||||||
T: de::Deserialize<Parser<Iter>, ParserError>
|
T: de::Deserialize<Parser<Iter>, Error>
|
||||||
>(iter: Iter) -> Result<T, ParserError> {
|
>(iter: Iter) -> Result<T, Error> {
|
||||||
let mut parser = Parser::new(iter);
|
let mut parser = Parser::new(iter);
|
||||||
let value = try!(de::Deserialize::deserialize(&mut parser));
|
let value = try!(de::Deserialize::deserialize(&mut parser));
|
||||||
|
|
||||||
@ -1821,8 +1850,8 @@ pub fn from_iter<
|
|||||||
/// Decodes a json value from a string
|
/// Decodes a json value from a string
|
||||||
pub fn from_str<
|
pub fn from_str<
|
||||||
'a,
|
'a,
|
||||||
T: de::Deserialize<Parser<str::Bytes<'a>>, ParserError>
|
T: de::Deserialize<Parser<str::Bytes<'a>>, Error>
|
||||||
>(s: &'a str) -> Result<T, BuilderError> {
|
>(s: &'a str) -> Result<T, Error> {
|
||||||
from_iter(s.bytes())
|
from_iter(s.bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1862,7 +1891,7 @@ mod tests {
|
|||||||
List,
|
List,
|
||||||
Object,
|
Object,
|
||||||
};
|
};
|
||||||
use super::{Parser, ParserError, from_str};
|
use super::{Parser, Error, from_str};
|
||||||
use super::value;
|
use super::value;
|
||||||
use super::value::{ToJson, from_json};
|
use super::value::{ToJson, from_json};
|
||||||
use super::{
|
use super::{
|
||||||
@ -2312,17 +2341,17 @@ mod tests {
|
|||||||
// FIXME (#5527): these could be merged once UFCS is finished.
|
// FIXME (#5527): these could be merged once UFCS is finished.
|
||||||
fn test_parse_err<
|
fn test_parse_err<
|
||||||
'a,
|
'a,
|
||||||
T: Show + de::Deserialize<Parser<str::Bytes<'a>>, ParserError>
|
T: Show + de::Deserialize<Parser<str::Bytes<'a>>, Error>
|
||||||
>(errors: &[(&'a str, ParserError)]) {
|
>(errors: &[(&'a str, Error)]) {
|
||||||
for &(s, ref err) in errors.iter() {
|
for &(s, ref err) in errors.iter() {
|
||||||
let v: Result<T, ParserError> = from_str(s);
|
let v: Result<T, Error> = from_str(s);
|
||||||
assert_eq!(v.unwrap_err(), *err);
|
assert_eq!(v.unwrap_err(), *err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_parse_ok<
|
fn test_parse_ok<
|
||||||
'a,
|
'a,
|
||||||
T: PartialEq + Show + ToJson + de::Deserialize<Parser<str::Bytes<'a>>, ParserError>
|
T: PartialEq + Show + ToJson + de::Deserialize<Parser<str::Bytes<'a>>, Error>
|
||||||
>(errors: &[(&'a str, T)]) {
|
>(errors: &[(&'a str, T)]) {
|
||||||
for &(s, ref value) in errors.iter() {
|
for &(s, ref value) in errors.iter() {
|
||||||
let v: T = from_str(s).unwrap();
|
let v: T = from_str(s).unwrap();
|
||||||
@ -2334,7 +2363,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_json_deserialize_ok<
|
fn test_json_deserialize_ok<
|
||||||
T: PartialEq + Show + ToJson + de::Deserialize<value::Deserializer, ParserError>
|
T: PartialEq + Show + ToJson + de::Deserialize<value::Deserializer, Error>
|
||||||
>(errors: &[T]) {
|
>(errors: &[T]) {
|
||||||
for value in errors.iter() {
|
for value in errors.iter() {
|
||||||
let v: T = from_json(value.to_json()).unwrap();
|
let v: T = from_json(value.to_json()).unwrap();
|
||||||
|
@ -13,7 +13,7 @@ use ser;
|
|||||||
use super::PrettySerializer;
|
use super::PrettySerializer;
|
||||||
use super::Serializer;
|
use super::Serializer;
|
||||||
use super::SerializeResult;
|
use super::SerializeResult;
|
||||||
use super::ParserError;
|
use super::Error;
|
||||||
use super::{
|
use super::{
|
||||||
MissingFieldError,
|
MissingFieldError,
|
||||||
SyntaxError,
|
SyntaxError,
|
||||||
@ -329,9 +329,9 @@ impl Deserializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator<Result<de::Token, ParserError>> for Deserializer {
|
impl Iterator<Result<de::Token, Error>> for Deserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<Result<de::Token, ParserError>> {
|
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||||
loop {
|
loop {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(DeserializerValueState(value)) => {
|
Some(DeserializerValueState(value)) => {
|
||||||
@ -388,27 +388,27 @@ impl Iterator<Result<de::Token, ParserError>> for Deserializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl de::Deserializer<ParserError> for Deserializer {
|
impl de::Deserializer<Error> for Deserializer {
|
||||||
fn end_of_stream_error(&mut self) -> ParserError {
|
fn end_of_stream_error(&mut self) -> Error {
|
||||||
SyntaxError(EOFWhileParsingValue, 0, 0)
|
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)
|
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)
|
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)
|
SyntaxError(DeserializerError(token, ExpectConversion), 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field<
|
fn missing_field<
|
||||||
T: de::Deserialize<Deserializer, ParserError>
|
T: de::Deserialize<Deserializer, Error>
|
||||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||||
// JSON can represent `null` values as a missing value, so this isn't
|
// JSON can represent `null` values as a missing value, so this isn't
|
||||||
// necessarily an error.
|
// necessarily an error.
|
||||||
de::Deserialize::deserialize_token(self, de::Null)
|
de::Deserialize::deserialize_token(self, de::Null)
|
||||||
@ -417,8 +417,8 @@ impl de::Deserializer<ParserError> for Deserializer {
|
|||||||
// Special case treating options as a nullable value.
|
// Special case treating options as a nullable value.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_option<
|
fn expect_option<
|
||||||
U: de::Deserialize<Deserializer, ParserError>
|
U: de::Deserialize<Deserializer, Error>
|
||||||
>(&mut self, token: de::Token) -> Result<Option<U>, ParserError> {
|
>(&mut self, token: de::Token) -> Result<Option<U>, Error> {
|
||||||
match token {
|
match token {
|
||||||
de::Null => Ok(None),
|
de::Null => Ok(None),
|
||||||
token => {
|
token => {
|
||||||
@ -433,7 +433,7 @@ impl de::Deserializer<ParserError> for Deserializer {
|
|||||||
fn expect_enum_start(&mut self,
|
fn expect_enum_start(&mut self,
|
||||||
token: de::Token,
|
token: de::Token,
|
||||||
_name: &str,
|
_name: &str,
|
||||||
variants: &[&str]) -> Result<uint, ParserError> {
|
variants: &[&str]) -> Result<uint, Error> {
|
||||||
let variant = match token {
|
let variant = match token {
|
||||||
de::MapStart(_) => {
|
de::MapStart(_) => {
|
||||||
let state = match self.stack.pop() {
|
let state = match self.stack.pop() {
|
||||||
@ -483,7 +483,7 @@ impl de::Deserializer<ParserError> for Deserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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 {
|
match token {
|
||||||
de::MapStart(_) => Ok(()),
|
de::MapStart(_) => Ok(()),
|
||||||
_ => Err(self.syntax_error(token, [de::MapStartKind])),
|
_ => Err(self.syntax_error(token, [de::MapStartKind])),
|
||||||
@ -493,8 +493,8 @@ impl de::Deserializer<ParserError> for Deserializer {
|
|||||||
|
|
||||||
/// Decodes a json value from a `Value`.
|
/// Decodes a json value from a `Value`.
|
||||||
pub fn from_json<
|
pub fn from_json<
|
||||||
T: de::Deserialize<Deserializer, ParserError>
|
T: de::Deserialize<Deserializer, Error>
|
||||||
>(json: Value) -> Result<T, ParserError> {
|
>(json: Value) -> Result<T, Error> {
|
||||||
let mut d = Deserializer::new(json);
|
let mut d = Deserializer::new(json);
|
||||||
de::Deserialize::deserialize(&mut d)
|
de::Deserialize::deserialize(&mut d)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user