mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 11:41:01 +00:00
simplify error reporting
This commit is contained in:
@@ -320,7 +320,7 @@ fn deserialize_struct(
|
|||||||
match $token {
|
match $token {
|
||||||
::serde::de::StructStart(_, _) => $struct_block,
|
::serde::de::StructStart(_, _) => $struct_block,
|
||||||
::serde::de::MapStart(_) => $map_block,
|
::serde::de::MapStart(_) => $map_block,
|
||||||
token => $deserializer.syntax_error(token),
|
token => Err($deserializer.syntax_error(token)),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -430,7 +430,9 @@ fn deserialize_struct_from_map(
|
|||||||
let pat = cx.pat_tuple(span, pats);
|
let pat = cx.pat_tuple(span, pats);
|
||||||
let s = cx.expr_str(span, token::get_ident(name));
|
let s = cx.expr_str(span, token::get_ident(name));
|
||||||
|
|
||||||
quote_arm!(cx, $pat => { return $deserializer.missing_field_error($s); })
|
quote_arm!(cx,
|
||||||
|
$pat => Err($deserializer.missing_field_error($s)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -447,7 +449,7 @@ fn deserialize_struct_from_map(
|
|||||||
let key = match token {
|
let key = match token {
|
||||||
::serde::de::Str(s) => s,
|
::serde::de::Str(s) => s,
|
||||||
::serde::de::String(ref s) => s.as_slice(),
|
::serde::de::String(ref s) => s.as_slice(),
|
||||||
token => { return $deserializer.syntax_error(token); }
|
token => { return Err($deserializer.syntax_error(token)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
match key {
|
match key {
|
||||||
@@ -457,16 +459,14 @@ fn deserialize_struct_from_map(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if error {
|
if error {
|
||||||
return $deserializer.syntax_error(token);
|
return Err($deserializer.syntax_error(token));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = match $fields_tuple {
|
match $fields_tuple {
|
||||||
$fields_pat => $result,
|
$fields_pat => Ok($result),
|
||||||
$error_arms
|
$error_arms
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-5
@@ -180,18 +180,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for BytesDeserializer {
|
impl de::Deserializer<Error> for BytesDeserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -231,18 +231,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for AnimalDeserializer {
|
impl de::Deserializer<Error> for AnimalDeserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -227,18 +227,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for IntDeserializer {
|
impl de::Deserializer<Error> for IntDeserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -351,18 +351,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for OuterDeserializer {
|
impl de::Deserializer<Error> for OuterDeserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -295,18 +295,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for IntDeserializer {
|
impl de::Deserializer<Error> for IntDeserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,18 +355,18 @@ mod deserializer {
|
|||||||
|
|
||||||
impl de::Deserializer<Error> for U8Deserializer {
|
impl de::Deserializer<Error> for U8Deserializer {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: de::Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,18 +55,18 @@ macro_rules! to_result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, E>;
|
fn end_of_stream_error(&self) -> E;
|
||||||
|
|
||||||
fn syntax_error<T>(&self, token: Token) -> Result<T, E>;
|
fn syntax_error(&self, token: Token) -> E;
|
||||||
|
|
||||||
fn missing_field_error<T>(&self, field: &'static str) -> Result<T, E>;
|
fn missing_field_error(&self, field: &'static str) -> E;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_token(&mut self) -> Result<Token, E> {
|
fn expect_token(&mut self) -> Result<Token, E> {
|
||||||
match self.next() {
|
match self.next() {
|
||||||
Some(Ok(token)) => Ok(token),
|
Some(Ok(token)) => Ok(token),
|
||||||
Some(Err(err)) => Err(err),
|
Some(Err(err)) => Err(err),
|
||||||
None => self.end_of_stream_error(),
|
None => Err(self.end_of_stream_error()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,10 +77,10 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
TupleStart(_) => {
|
TupleStart(_) => {
|
||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
End => Ok(()),
|
End => Ok(()),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,45 +88,45 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_bool(&mut self, token: Token) -> Result<bool, E> {
|
fn expect_bool(&mut self, token: Token) -> Result<bool, E> {
|
||||||
match token {
|
match token {
|
||||||
Bool(value) => Ok(value),
|
Bool(value) => Ok(value),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_num<T: NumCast>(&mut self, token: Token) -> Result<T, E> {
|
fn expect_num<T: NumCast>(&mut self, token: Token) -> Result<T, E> {
|
||||||
match token {
|
match token {
|
||||||
Int(x) => to_result!(num::cast(x), self.syntax_error(Int(x))),
|
Int(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
I8(x) => to_result!(num::cast(x), self.syntax_error(I8(x))),
|
I8(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
I16(x) => to_result!(num::cast(x), self.syntax_error(I16(x))),
|
I16(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
I32(x) => to_result!(num::cast(x), self.syntax_error(I32(x))),
|
I32(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
I64(x) => to_result!(num::cast(x), self.syntax_error(I64(x))),
|
I64(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
Uint(x) => to_result!(num::cast(x), self.syntax_error(Uint(x))),
|
Uint(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
U8(x) => to_result!(num::cast(x), self.syntax_error(U8(x))),
|
U8(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
U16(x) => to_result!(num::cast(x), self.syntax_error(U16(x))),
|
U16(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
U32(x) => to_result!(num::cast(x), self.syntax_error(U32(x))),
|
U32(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
U64(x) => to_result!(num::cast(x), self.syntax_error(U64(x))),
|
U64(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
F32(x) => to_result!(num::cast(x), self.syntax_error(F32(x))),
|
F32(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
F64(x) => to_result!(num::cast(x), self.syntax_error(F64(x))),
|
F64(x) => to_result!(num::cast(x), Err(self.syntax_error(token))),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_from_primitive<T: FromPrimitive>(&mut self, token: Token) -> Result<T, E> {
|
fn expect_from_primitive<T: FromPrimitive>(&mut self, token: Token) -> Result<T, E> {
|
||||||
match token {
|
match token {
|
||||||
Int(x) => to_result!(num::from_int(x), self.syntax_error(Int(x))),
|
Int(x) => to_result!(num::from_int(x), Err(self.syntax_error(token))),
|
||||||
I8(x) => to_result!(num::from_i8(x), self.syntax_error(I8(x))),
|
I8(x) => to_result!(num::from_i8(x), Err(self.syntax_error(token))),
|
||||||
I16(x) => to_result!(num::from_i16(x), self.syntax_error(I16(x))),
|
I16(x) => to_result!(num::from_i16(x), Err(self.syntax_error(token))),
|
||||||
I32(x) => to_result!(num::from_i32(x), self.syntax_error(I32(x))),
|
I32(x) => to_result!(num::from_i32(x), Err(self.syntax_error(token))),
|
||||||
I64(x) => to_result!(num::from_i64(x), self.syntax_error(I64(x))),
|
I64(x) => to_result!(num::from_i64(x), Err(self.syntax_error(token))),
|
||||||
Uint(x) => to_result!(num::from_uint(x), self.syntax_error(Uint(x))),
|
Uint(x) => to_result!(num::from_uint(x), Err(self.syntax_error(token))),
|
||||||
U8(x) => to_result!(num::from_u8(x), self.syntax_error(U8(x))),
|
U8(x) => to_result!(num::from_u8(x), Err(self.syntax_error(token))),
|
||||||
U16(x) => to_result!(num::from_u16(x), self.syntax_error(U16(x))),
|
U16(x) => to_result!(num::from_u16(x), Err(self.syntax_error(token))),
|
||||||
U32(x) => to_result!(num::from_u32(x), self.syntax_error(U32(x))),
|
U32(x) => to_result!(num::from_u32(x), Err(self.syntax_error(token))),
|
||||||
U64(x) => to_result!(num::from_u64(x), self.syntax_error(U64(x))),
|
U64(x) => to_result!(num::from_u64(x), Err(self.syntax_error(token))),
|
||||||
F32(x) => to_result!(num::from_f32(x), self.syntax_error(F32(x))),
|
F32(x) => to_result!(num::from_f32(x), Err(self.syntax_error(token))),
|
||||||
F64(x) => to_result!(num::from_f64(x), self.syntax_error(F64(x))),
|
F64(x) => to_result!(num::from_f64(x), Err(self.syntax_error(token))),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_char(&mut self, token: Token) -> Result<char, E> {
|
fn expect_char(&mut self, token: Token) -> Result<char, E> {
|
||||||
match token {
|
match token {
|
||||||
Char(value) => Ok(value),
|
Char(value) => Ok(value),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_str(&mut self, token: Token) -> Result<&'static str, E> {
|
fn expect_str(&mut self, token: Token) -> Result<&'static str, E> {
|
||||||
match token {
|
match token {
|
||||||
Str(value) => Ok(value),
|
Str(value) => Ok(value),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
match token {
|
match token {
|
||||||
Str(value) => Ok(value.to_string()),
|
Str(value) => Ok(value.to_string()),
|
||||||
String(value) => Ok(value),
|
String(value) => Ok(value),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +165,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
let value: T = try!(Deserializable::deserialize(self));
|
let value: T = try!(Deserializable::deserialize(self));
|
||||||
Ok(Some(value))
|
Ok(Some(value))
|
||||||
}
|
}
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_tuple_start(&mut self, token: Token) -> Result<uint, E> {
|
fn expect_tuple_start(&mut self, token: Token) -> Result<uint, E> {
|
||||||
match token {
|
match token {
|
||||||
TupleStart(len) => Ok(len),
|
TupleStart(len) => Ok(len),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +188,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_tuple_end(&mut self) -> Result<(), E> {
|
fn expect_tuple_end(&mut self) -> Result<(), E> {
|
||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
End => Ok(()),
|
End => Ok(()),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,10 +199,10 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
if name == n {
|
if name == n {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
self.syntax_error(token)
|
Err(self.syntax_error(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => self.syntax_error(token),
|
_ => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,15 +213,17 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
Str(n) => {
|
Str(n) => {
|
||||||
if name != n {
|
if name != n {
|
||||||
return self.syntax_error(Str(n));
|
return Err(self.syntax_error(Str(n)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String(n) => {
|
String(n) => {
|
||||||
if name != n.as_slice() {
|
if name != n.as_slice() {
|
||||||
return self.syntax_error(String(n));
|
return Err(self.syntax_error(String(n)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token => { return self.syntax_error(token); }
|
token => {
|
||||||
|
return Err(self.syntax_error(token));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Deserializable::deserialize(self)
|
Deserializable::deserialize(self)
|
||||||
@@ -231,7 +233,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_struct_end(&mut self) -> Result<(), E> {
|
fn expect_struct_end(&mut self) -> Result<(), E> {
|
||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
End => Ok(()),
|
End => Ok(()),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,13 +244,13 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
if name == n {
|
if name == n {
|
||||||
match variants.iter().position(|variant| *variant == v) {
|
match variants.iter().position(|variant| *variant == v) {
|
||||||
Some(position) => Ok(position),
|
Some(position) => Ok(position),
|
||||||
None => self.syntax_error(token),
|
None => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.syntax_error(token)
|
Err(self.syntax_error(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => self.syntax_error(token),
|
_ => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,7 +265,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_enum_end(&mut self) -> Result<(), E> {
|
fn expect_enum_end(&mut self) -> Result<(), E> {
|
||||||
match try!(self.expect_token()) {
|
match try!(self.expect_token()) {
|
||||||
End => Ok(()),
|
End => Ok(()),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,7 +274,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
match token {
|
match token {
|
||||||
TupleStart(len) => Ok(len),
|
TupleStart(len) => Ok(len),
|
||||||
SeqStart(len) => Ok(len),
|
SeqStart(len) => Ok(len),
|
||||||
token => self.syntax_error(token),
|
token => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,7 +317,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn expect_map_start(&mut self, token: Token) -> Result<uint, E> {
|
fn expect_map_start(&mut self, token: Token) -> Result<uint, E> {
|
||||||
match token {
|
match token {
|
||||||
MapStart(len) => Ok(len),
|
MapStart(len) => Ok(len),
|
||||||
_ => self.syntax_error(token),
|
_ => Err(self.syntax_error(token)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -673,7 +675,7 @@ impl Deserializable for IgnoreTokens {
|
|||||||
Str(_) | String(_) => {
|
Str(_) | String(_) => {
|
||||||
let _: IgnoreTokens = try!(Deserializable::deserialize(d));
|
let _: IgnoreTokens = try!(Deserializable::deserialize(d));
|
||||||
}
|
}
|
||||||
_token => { return d.syntax_error(token); }
|
_token => { return Err(d.syntax_error(token)); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -712,7 +714,7 @@ impl Deserializable for IgnoreTokens {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
End => d.syntax_error(token),
|
End => Err(d.syntax_error(token)),
|
||||||
|
|
||||||
_ => Ok(IgnoreTokens),
|
_ => Ok(IgnoreTokens),
|
||||||
}
|
}
|
||||||
@@ -772,7 +774,7 @@ impl GatherTokens {
|
|||||||
self.gather_map(d)
|
self.gather_map(d)
|
||||||
}
|
}
|
||||||
End => {
|
End => {
|
||||||
d.syntax_error(token)
|
Err(d.syntax_error(token))
|
||||||
}
|
}
|
||||||
token => {
|
token => {
|
||||||
self.tokens.push(token);
|
self.tokens.push(token);
|
||||||
@@ -808,7 +810,7 @@ impl GatherTokens {
|
|||||||
self.tokens.push(token);
|
self.tokens.push(token);
|
||||||
try!(self.gather(d))
|
try!(self.gather(d))
|
||||||
}
|
}
|
||||||
token => { return d.syntax_error(token); }
|
token => { return Err(d.syntax_error(token)); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -993,16 +995,16 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Iter: Iterator<Token>> Deserializer<Error> for TokenDeserializer<Iter> {
|
impl<Iter: Iterator<Token>> Deserializer<Error> for TokenDeserializer<Iter> {
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
fn end_of_stream_error(&self) -> Error {
|
||||||
Err(EndOfStream)
|
EndOfStream
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syntax_error<T>(&self, _token: Token) -> Result<T, Error> {
|
fn syntax_error(&self, _token: Token) -> Error {
|
||||||
Err(SyntaxError)
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
fn missing_field_error<T>(&self, _field: &'static str) -> Result<T, Error> {
|
fn missing_field_error(&self, _field: &'static str) -> Error {
|
||||||
Err(IncompleteValue)
|
IncompleteValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-13
@@ -526,7 +526,7 @@ impl de::Deserializable for Json {
|
|||||||
object.insert(name.to_string(), List(fields));
|
object.insert(name.to_string(), List(fields));
|
||||||
Ok(Object(object))
|
Ok(Object(object))
|
||||||
}
|
}
|
||||||
de::End => d.syntax_error(de::End),
|
de::End => Err(d.syntax_error(de::End)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -611,16 +611,16 @@ impl Iterator<Result<de::Token, ParserError>> for JsonDeserializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl de::Deserializer<ParserError> for JsonDeserializer {
|
impl de::Deserializer<ParserError> for JsonDeserializer {
|
||||||
fn end_of_stream_error<T>(&self) -> Result<T, ParserError> {
|
fn end_of_stream_error(&self) -> ParserError {
|
||||||
Err(SyntaxError(EOFWhileParsingValue, 0, 0))
|
SyntaxError(EOFWhileParsingValue, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syntax_error<T>(&self, _token: de::Token) -> Result<T, ParserError> {
|
fn syntax_error(&self, _token: de::Token) -> ParserError {
|
||||||
Err(SyntaxError(InvalidSyntax, 0, 0))
|
SyntaxError(InvalidSyntax, 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn missing_field_error<T>(&self, field: &'static str) -> Result<T, ParserError> {
|
fn missing_field_error(&self, field: &'static str) -> ParserError {
|
||||||
Err(SyntaxError(MissingField(field), 0, 0))
|
SyntaxError(MissingField(field), 0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case treating options as a nullable value.
|
// Special case treating options as a nullable value.
|
||||||
@@ -1977,16 +1977,16 @@ impl<T: Iterator<char>> Parser<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||||
fn end_of_stream_error<U>(&self) -> Result<U, ParserError> {
|
fn end_of_stream_error(&self) -> ParserError {
|
||||||
Err(SyntaxError(EOFWhileParsingValue, self.line, self.col))
|
SyntaxError(EOFWhileParsingValue, self.line, self.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syntax_error<U>(&self, _token: de::Token) -> Result<U, ParserError> {
|
fn syntax_error(&self, _token: de::Token) -> ParserError {
|
||||||
Err(SyntaxError(InvalidSyntax, self.line, self.col))
|
SyntaxError(InvalidSyntax, self.line, self.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn missing_field_error<T>(&self, field: &'static str) -> Result<T, ParserError> {
|
fn missing_field_error(&self, field: &'static str) -> ParserError {
|
||||||
Err(SyntaxError(MissingField(field), self.line, self.col))
|
SyntaxError(MissingField(field), self.line, self.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case treating options as a nullable value.
|
// Special case treating options as a nullable value.
|
||||||
|
|||||||
Reference in New Issue
Block a user