Add some better error messages
This commit is contained in:
parent
be0f19fb34
commit
0380346466
@ -37,7 +37,10 @@ struct Outer {
|
||||
#[deriving(Show)]
|
||||
pub enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
SyntaxError(String),
|
||||
UnexpectedName(String),
|
||||
ConversionError(String),
|
||||
MissingField(&'static str),
|
||||
OtherError(String),
|
||||
}
|
||||
|
||||
@ -85,64 +88,64 @@ mod decoder {
|
||||
fn read_nil(&mut self) -> Result<(), Error> {
|
||||
match self.stack.pop() {
|
||||
Some(NullState) => Ok(()),
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("NullState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_uint(&mut self) -> Result<uint, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(UintState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("UintState".to_string())),
|
||||
}
|
||||
}
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, 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("".to_string())) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError("".to_string())) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError("".to_string())) }
|
||||
#[inline]
|
||||
fn read_char(&mut self) -> Result<char, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(CharState(c)) => Ok(c),
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<String, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(StringState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
_names: &[&str],
|
||||
_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,
|
||||
_a_idx: uint,
|
||||
_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,
|
||||
_names: &[&str],
|
||||
_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,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
|
||||
#[inline]
|
||||
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"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
Err(SyntaxError("expected Outer".to_string()))
|
||||
}
|
||||
}
|
||||
Some(InnerState(Inner { a: (), b: b, c: c })) => {
|
||||
@ -168,10 +171,10 @@ mod decoder {
|
||||
self.stack.push(FieldState("a"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
Err(SyntaxError("expected Inner".to_string()))
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("expected InnerState or OuterState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -181,31 +184,31 @@ mod decoder {
|
||||
if f_name == name {
|
||||
f(self)
|
||||
} 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_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> 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("".to_string())) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_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,
|
||||
_a_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
-> Result<T, Error> { Err(SyntaxError("".to_string())) }
|
||||
|
||||
// Specialized types:
|
||||
#[inline]
|
||||
fn read_option<T>(&mut self, f: |&mut OuterDecoder, bool| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(OptionState(b)) => f(self, b),
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("expected OptionState".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,7 +222,7 @@ mod decoder {
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError)
|
||||
_ => Err(SyntaxError("expected VecState".to_string()))
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -246,7 +249,7 @@ mod decoder {
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError),
|
||||
_ => Err(SyntaxError("expected MapState".to_string())),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
@ -264,9 +267,11 @@ mod decoder {
|
||||
|
||||
mod deserializer {
|
||||
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;
|
||||
|
||||
#[deriving(Show)]
|
||||
enum State {
|
||||
OuterState(Outer),
|
||||
InnerState(Inner),
|
||||
@ -365,25 +370,25 @@ mod deserializer {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error(&mut self, _token: de::Token, _expected: &[de::TokenKind]) -> Error {
|
||||
SyntaxError
|
||||
fn syntax_error(&mut self, token: de::Token, expected: &[de::TokenKind]) -> Error {
|
||||
SyntaxError(format!("expected {}, found {}", expected, token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
fn unexpected_name_error(&mut self, token: de::Token) -> Error {
|
||||
UnexpectedName(format!("found {}", token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
fn conversion_error(&mut self, token: de::Token) -> Error {
|
||||
UnexpectedName(format!("found {}", token))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable<OuterDeserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
>(&mut self, field: &'static str) -> Result<T, Error> {
|
||||
Err(MissingField(field))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user