mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-17 15:01:03 +00:00
Add some better error messages
This commit is contained in:
+48
-43
@@ -37,7 +37,10 @@ struct Outer {
|
|||||||
#[deriving(Show)]
|
#[deriving(Show)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
EndOfStream,
|
EndOfStream,
|
||||||
SyntaxError,
|
SyntaxError(String),
|
||||||
|
UnexpectedName(String),
|
||||||
|
ConversionError(String),
|
||||||
|
MissingField(&'static str),
|
||||||
OtherError(String),
|
OtherError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,64 +88,64 @@ mod decoder {
|
|||||||
fn read_nil(&mut self) -> Result<(), Error> {
|
fn read_nil(&mut self) -> Result<(), Error> {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(NullState) => Ok(()),
|
Some(NullState) => Ok(()),
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("NullState".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_uint(&mut self) -> Result<uint, Error> {
|
fn read_uint(&mut self) -> Result<uint, Error> {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(UintState(value)) => Ok(value),
|
Some(UintState(value)) => Ok(value),
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("UintState".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError) }
|
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError("".to_string())) }
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_char(&mut self) -> Result<char, Error> {
|
fn read_char(&mut self) -> Result<char, Error> {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(CharState(c)) => Ok(c),
|
Some(CharState(c)) => Ok(c),
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_str(&mut self) -> Result<String, Error> {
|
fn read_str(&mut self) -> Result<String, Error> {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(StringState(value)) => Ok(value),
|
Some(StringState(value)) => Ok(value),
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compound types:
|
// Compound types:
|
||||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
fn read_enum<T>(&mut self, _name: &str, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
|
|
||||||
fn read_enum_variant<T>(&mut self,
|
fn read_enum_variant<T>(&mut self,
|
||||||
_names: &[&str],
|
_names: &[&str],
|
||||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_enum_variant_arg<T>(&mut self,
|
fn read_enum_variant_arg<T>(&mut self,
|
||||||
_a_idx: uint,
|
_a_idx: uint,
|
||||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
|
|
||||||
fn read_enum_struct_variant<T>(&mut self,
|
fn read_enum_struct_variant<T>(&mut self,
|
||||||
_names: &[&str],
|
_names: &[&str],
|
||||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_enum_struct_variant_field<T>(&mut self,
|
fn read_enum_struct_variant_field<T>(&mut self,
|
||||||
_f_name: &str,
|
_f_name: &str,
|
||||||
_f_idx: uint,
|
_f_idx: uint,
|
||||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_struct<T>(&mut self, s_name: &str, _len: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
fn read_struct<T>(&mut self, s_name: &str, _len: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||||
@@ -153,7 +156,7 @@ mod decoder {
|
|||||||
self.stack.push(FieldState("inner"));
|
self.stack.push(FieldState("inner"));
|
||||||
f(self)
|
f(self)
|
||||||
} else {
|
} else {
|
||||||
Err(SyntaxError)
|
Err(SyntaxError("expected Outer".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(InnerState(Inner { a: (), b: b, c: c })) => {
|
Some(InnerState(Inner { a: (), b: b, c: c })) => {
|
||||||
@@ -168,10 +171,10 @@ mod decoder {
|
|||||||
self.stack.push(FieldState("a"));
|
self.stack.push(FieldState("a"));
|
||||||
f(self)
|
f(self)
|
||||||
} else {
|
} else {
|
||||||
Err(SyntaxError)
|
Err(SyntaxError("expected Inner".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("expected InnerState or OuterState".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -181,31 +184,31 @@ mod decoder {
|
|||||||
if f_name == name {
|
if f_name == name {
|
||||||
f(self)
|
f(self)
|
||||||
} else {
|
} else {
|
||||||
Err(SyntaxError)
|
Err(SyntaxError("expected FieldState".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(SyntaxError)
|
_ => Err(SyntaxError("expected FieldState".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_tuple<T>(&mut self, _f: |&mut OuterDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
fn read_tuple<T>(&mut self, _f: |&mut OuterDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
|
|
||||||
fn read_tuple_struct<T>(&mut self,
|
fn read_tuple_struct<T>(&mut self,
|
||||||
_s_name: &str,
|
_s_name: &str,
|
||||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
fn read_tuple_struct_arg<T>(&mut self,
|
fn read_tuple_struct_arg<T>(&mut self,
|
||||||
_a_idx: uint,
|
_a_idx: uint,
|
||||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||||
-> Result<T, Error> { Err(SyntaxError) }
|
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||||
|
|
||||||
// Specialized types:
|
// Specialized types:
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read_option<T>(&mut self, f: |&mut OuterDecoder, bool| -> Result<T, Error>) -> Result<T, Error> {
|
fn read_option<T>(&mut self, f: |&mut OuterDecoder, bool| -> Result<T, Error>) -> Result<T, Error> {
|
||||||
match self.stack.pop() {
|
match self.stack.pop() {
|
||||||
Some(OptionState(b)) => f(self, b),
|
Some(OptionState(b)) => f(self, b),
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("expected OptionState".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +222,7 @@ mod decoder {
|
|||||||
}
|
}
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
_ => Err(SyntaxError)
|
_ => Err(SyntaxError("expected VecState".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -246,7 +249,7 @@ mod decoder {
|
|||||||
}
|
}
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
_ => Err(SyntaxError),
|
_ => Err(SyntaxError("expected MapState".to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -264,9 +267,11 @@ mod decoder {
|
|||||||
|
|
||||||
mod deserializer {
|
mod deserializer {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use super::{Outer, Inner, Error, EndOfStream, SyntaxError};
|
use super::{Outer, Inner};
|
||||||
|
use super::{Error, EndOfStream, SyntaxError, UnexpectedName, MissingField};
|
||||||
use serde::de;
|
use serde::de;
|
||||||
|
|
||||||
|
#[deriving(Show)]
|
||||||
enum State {
|
enum State {
|
||||||
OuterState(Outer),
|
OuterState(Outer),
|
||||||
InnerState(Inner),
|
InnerState(Inner),
|
||||||
@@ -365,25 +370,25 @@ mod deserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error(&mut self, _token: de::Token, _expected: &[de::TokenKind]) -> Error {
|
fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> Error {
|
||||||
SyntaxError
|
SyntaxError(format!("expected {}, found {}", expected, token))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
fn unexpected_name_error(&mut self, token: de::Token) -> Error {
|
||||||
SyntaxError
|
UnexpectedName(format!("found {}", token))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
fn conversion_error(&mut self, token: de::Token) -> Error {
|
||||||
SyntaxError
|
UnexpectedName(format!("found {}", token))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field<
|
fn missing_field<
|
||||||
T: de::Deserializable<OuterDeserializer, Error>
|
T: de::Deserializable<OuterDeserializer, Error>
|
||||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
>(&mut self, field: &'static str) -> Result<T, Error> {
|
||||||
Err(SyntaxError)
|
Err(MissingField(field))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user