From e0eff942b11b70fac408aad2cdcfaf1322310649 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 12 Apr 2015 00:30:20 -0700 Subject: [PATCH] Add Error::unknown_field_error --- benches/bench_enum.rs | 2 ++ benches/bench_map.rs | 2 ++ benches/bench_struct.rs | 2 ++ benches/bench_vec.rs | 2 ++ serde_macros/src/de.rs | 2 +- src/de/mod.rs | 4 +++- src/de/value.rs | 2 ++ src/json/error.rs | 11 ++++++++++- tests/test_bytes.rs | 2 ++ tests/test_de.rs | 7 ++++++- tests/test_json.rs | 2 +- 11 files changed, 33 insertions(+), 5 deletions(-) diff --git a/benches/bench_enum.rs b/benches/bench_enum.rs index 43d8d9d5..190b862a 100644 --- a/benches/bench_enum.rs +++ b/benches/bench_enum.rs @@ -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 } } diff --git a/benches/bench_map.rs b/benches/bench_map.rs index d9427ac6..125fe2d0 100644 --- a/benches/bench_map.rs +++ b/benches/bench_map.rs @@ -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 } diff --git a/benches/bench_struct.rs b/benches/bench_struct.rs index 563bf339..be794d9f 100644 --- a/benches/bench_struct.rs +++ b/benches/bench_struct.rs @@ -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 } diff --git a/benches/bench_vec.rs b/benches/bench_vec.rs index 6f6edb53..aed9adaa 100644 --- a/benches/bench_vec.rs +++ b/benches/bench_vec.rs @@ -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 } } diff --git a/serde_macros/src/de.rs b/serde_macros/src/de.rs index fc7d324a..99f0dd52 100644 --- a/serde_macros/src/de.rs +++ b/serde_macros/src/de.rs @@ -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)), } } } diff --git a/src/de/mod.rs b/src/de/mod.rs index 0b316fb1..6585a589 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -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; } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/de/value.rs b/src/de/value.rs index de26d8cb..b39e45f7 100644 --- a/src/de/value.rs +++ b/src/de/value.rs @@ -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) } } diff --git a/src/json/error.rs b/src/json/error.rs index 48f6df16..29570ebd 100644 --- a/src/json/error.rs +++ b/src/json/error.rs @@ -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 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) } diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index cd100fd6..ef96e281 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -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 } } diff --git a/tests/test_de.rs b/tests/test_de.rs index 2f446b13..75e54d68 100644 --- a/tests/test_de.rs +++ b/tests/test_de.rs @@ -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) } diff --git a/tests/test_json.rs b/tests/test_json.rs index 76ca55bf..a87afc76 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -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)),