Add Error::unknown_field_error
This commit is contained in:
parent
1da47c0870
commit
e0eff942b1
@ -34,6 +34,8 @@ impl serde::de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error { Error::SyntaxError }
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,8 @@ impl serde::de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStream }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error {
|
||||
Error::MissingField
|
||||
}
|
||||
|
@ -45,6 +45,8 @@ impl serde::de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStream }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error {
|
||||
Error::MissingField
|
||||
}
|
||||
|
@ -25,6 +25,8 @@ impl serde::de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(_: &str) -> Error { Error::SyntaxError }
|
||||
|
||||
fn missing_field_error(_: &'static str) -> Error { Error::SyntaxError }
|
||||
}
|
||||
|
||||
|
@ -575,7 +575,7 @@ fn deserialize_field_visitor(
|
||||
{
|
||||
match value {
|
||||
$field_arms
|
||||
_ => Err(::serde::de::Error::syntax_error()),
|
||||
_ => Err(::serde::de::Error::unknown_field_error(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ pub trait Error {
|
||||
|
||||
fn end_of_stream_error() -> Self;
|
||||
|
||||
fn missing_field_error(&'static str) -> Self;
|
||||
fn unknown_field_error(field: &str) -> Self;
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Self;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -18,12 +18,14 @@ use de;
|
||||
pub enum Error {
|
||||
SyntaxError,
|
||||
EndOfStreamError,
|
||||
UnknownFieldError(String),
|
||||
MissingFieldError(&'static str),
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax_error() -> Self { Error::SyntaxError }
|
||||
fn end_of_stream_error() -> Self { Error::EndOfStreamError }
|
||||
fn unknown_field_error(field: &str) -> Self { Error::UnknownFieldError(field.to_string()) }
|
||||
fn missing_field_error(field: &'static str) -> Self { Error::MissingFieldError(field) }
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ use std::io;
|
||||
use de;
|
||||
|
||||
/// The errors that can arise while parsing a JSON stream.
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum ErrorCode {
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
@ -28,6 +28,7 @@ pub enum ErrorCode {
|
||||
InvalidUnicodeCodePoint,
|
||||
KeyMustBeAString,
|
||||
LoneLeadingSurrogateInHexEscape,
|
||||
UnknownField(String),
|
||||
MissingField(&'static str),
|
||||
NotFourDigit,
|
||||
NotUtf8,
|
||||
@ -65,6 +66,7 @@ impl fmt::Debug for ErrorCode {
|
||||
ErrorCode::InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f),
|
||||
ErrorCode::KeyMustBeAString => "key must be a string".fmt(f),
|
||||
ErrorCode::LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f),
|
||||
ErrorCode::UnknownField(ref field) => write!(f, "unknown field \"{}\"", field),
|
||||
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
|
||||
ErrorCode::NotFourDigit => "invalid \\u escape (not four digits)".fmt(f),
|
||||
ErrorCode::NotUtf8 => "contents not utf-8".fmt(f),
|
||||
@ -154,6 +156,9 @@ impl From<de::value::Error> for Error {
|
||||
de::value::Error::EndOfStreamError => {
|
||||
de::Error::end_of_stream_error()
|
||||
}
|
||||
de::value::Error::UnknownFieldError(field) => {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field), 0, 0)
|
||||
}
|
||||
de::value::Error::MissingFieldError(field) => {
|
||||
de::Error::missing_field_error(field)
|
||||
}
|
||||
@ -170,6 +175,10 @@ impl de::Error for Error {
|
||||
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0)
|
||||
}
|
||||
|
||||
fn unknown_field_error(field: &str) -> Error {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field.to_string()), 0, 0)
|
||||
}
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ impl serde::de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error }
|
||||
|
||||
fn unknown_field_error(_field: &str) -> Error { Error }
|
||||
|
||||
fn missing_field_error(_field: &'static str) -> Error { Error }
|
||||
}
|
||||
|
||||
|
@ -59,10 +59,11 @@ impl<'a> TokenDeserializer {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
enum Error {
|
||||
SyntaxError,
|
||||
EndOfStreamError,
|
||||
UnknownFieldError(String),
|
||||
MissingFieldError(&'static str),
|
||||
InvalidName(&'static str),
|
||||
}
|
||||
@ -72,6 +73,10 @@ impl de::Error for Error {
|
||||
|
||||
fn end_of_stream_error() -> Error { Error::EndOfStreamError }
|
||||
|
||||
fn unknown_field_error(field: &str) -> Error {
|
||||
Error::UnknownFieldError(field.to_string())
|
||||
}
|
||||
|
||||
fn missing_field_error(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
|
@ -935,7 +935,7 @@ fn test_parse_enum_errors() {
|
||||
("{}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 2)),
|
||||
("{\"Dog\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 8)),
|
||||
("{\"Dog\":}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 8)),
|
||||
("{\"unknown\":[]}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 11)),
|
||||
("{\"unknown\":[]}", Error::SyntaxError(ErrorCode::UnknownField("unknown".to_string()), 1, 11)),
|
||||
("{\"Dog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 9)),
|
||||
("{\"Frog\":{}}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 10)),
|
||||
("{\"Cat\":[]}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 1, 9)),
|
||||
|
Loading…
Reference in New Issue
Block a user