diff --git a/README.md b/README.md index 716cd8d8..e00fa998 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,7 @@ to generate an error for a few common error conditions. Here's how it could be u fn visit_string(&mut self, _: String) -> Result where E: Error, { - Err(serde::de::Error::syntax()) + Err(serde::de::Error::syntax("expect a string")) } ... @@ -366,7 +366,7 @@ impl serde::Deserialize for PointField { match value { "x" => Ok(Field::X), "y" => Ok(Field::Y), - _ => Err(serde::de::Error::syntax()), + _ => Err(serde::de::Error::syntax("expected x or y")), } } } diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index b9f2c90d..48a194f3 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -85,7 +85,7 @@ impl Visitor for BoolVisitor { match s.trim() { "true" => Ok(true), "false" => Ok(false), - _ => Err(Error::syntax()), + _ => Err(Error::syntax("expected `true` or `false`")), } } } @@ -108,7 +108,7 @@ macro_rules! impl_deserialize_num_method { { match FromPrimitive::$from_method(v) { Some(v) => Ok(v), - None => Err(Error::syntax()), + None => Err(Error::syntax("expected a number")), } } } @@ -149,7 +149,7 @@ impl< fn visit_str(&mut self, v: &str) -> Result where E: Error, { - str::FromStr::from_str(v.trim()).or(Err(Error::syntax())) + str::FromStr::from_str(v.trim()).or(Err(Error::syntax("expected a str"))) } } @@ -200,7 +200,7 @@ impl Visitor for CharVisitor { let mut iter = v.chars(); if let Some(v) = iter.next() { if iter.next().is_some() { - Err(Error::syntax()) + Err(Error::syntax("expected a character")) } else { Ok(v) } @@ -243,7 +243,7 @@ impl Visitor for StringVisitor { { match str::from_utf8(v) { Ok(s) => Ok(s.to_string()), - Err(_) => Err(Error::syntax()), + Err(_) => Err(Error::syntax("expected utf8 `&[u8]`")), } } @@ -252,7 +252,7 @@ impl Visitor for StringVisitor { { match String::from_utf8(v) { Ok(s) => Ok(s), - Err(_) => Err(Error::syntax()), + Err(_) => Err(Error::syntax("expected utf8 `&[u8]`")), } } } @@ -848,7 +848,7 @@ impl Deserialize for NonZero where T: Deserialize + PartialEq + Zeroable + fn deserialize(deserializer: &mut D) -> Result, D::Error> where D: Deserializer { let value = try!(Deserialize::deserialize(deserializer)); if value == Zero::zero() { - return Err(Error::syntax()) + return Err(Error::syntax("expected a non-zero value")) } unsafe { Ok(NonZero::new(value)) @@ -900,7 +900,7 @@ impl Deserialize for Result where T: Deserialize, E: Deserialize { _ => { match str::from_utf8(value) { Ok(value) => Err(Error::unknown_field(value)), - Err(_) => Err(Error::syntax()), + Err(_) => Err(Error::syntax("expected a `&[u8]`")), } } } diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 206d312c..db83bdc2 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -6,7 +6,7 @@ pub mod value; /////////////////////////////////////////////////////////////////////////////// pub trait Error { - fn syntax() -> Self; + fn syntax(msg: &str) -> Self; fn end_of_stream() -> Self; @@ -273,7 +273,7 @@ pub trait Deserializer { _visitor: V) -> Result where V: EnumVisitor, { - Err(Error::syntax()) + Err(Error::syntax("expected an enum")) } /// This method hints that the `Deserialize` type is expecting a `Vec`. This allows @@ -304,7 +304,7 @@ pub trait Visitor { fn visit_bool(&mut self, _v: bool) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a bool")) } fn visit_isize(&mut self, v: isize) -> Result @@ -334,7 +334,7 @@ pub trait Visitor { fn visit_i64(&mut self, _v: i64) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a i64")) } fn visit_usize(&mut self, v: usize) -> Result @@ -364,7 +364,7 @@ pub trait Visitor { fn visit_u64(&mut self, _v: u64) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a u64")) } fn visit_f32(&mut self, v: f32) -> Result @@ -376,7 +376,7 @@ pub trait Visitor { fn visit_f64(&mut self, _v: f64) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a f64")) } #[inline] @@ -391,7 +391,7 @@ pub trait Visitor { fn visit_str(&mut self, _v: &str) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a str")) } #[inline] @@ -404,7 +404,7 @@ pub trait Visitor { fn visit_unit(&mut self) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a unit")) } #[inline] @@ -417,37 +417,37 @@ pub trait Visitor { fn visit_none(&mut self) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected an Option::None")) } fn visit_some(&mut self, _deserializer: &mut D) -> Result where D: Deserializer, { - Err(Error::syntax()) + Err(Error::syntax("expected an Option::Some")) } fn visit_newtype_struct(&mut self, _deserializer: &mut D) -> Result where D: Deserializer, { - Err(Error::syntax()) + Err(Error::syntax("expected a newtype struct")) } fn visit_seq(&mut self, _visitor: V) -> Result where V: SeqVisitor, { - Err(Error::syntax()) + Err(Error::syntax("expected a sequence")) } fn visit_map(&mut self, _visitor: V) -> Result where V: MapVisitor, { - Err(Error::syntax()) + Err(Error::syntax("expected a map")) } fn visit_bytes(&mut self, _v: &[u8]) -> Result where E: Error, { - Err(Error::syntax()) + Err(Error::syntax("expected a &[u8]")) } fn visit_byte_buf(&mut self, v: Vec) -> Result @@ -593,7 +593,7 @@ pub trait VariantVisitor { /// `visit_unit` is called when deserializing a variant with no values. fn visit_unit(&mut self) -> Result<(), Self::Error> { - Err(Error::syntax()) + Err(Error::syntax("expected a univ variant")) } /// `visit_newtype` is called when deserializing a variant with a single value. By default this @@ -612,7 +612,7 @@ pub trait VariantVisitor { _visitor: V) -> Result where V: Visitor { - Err(Error::syntax()) + Err(Error::syntax("expected a tuple variant")) } /// `visit_struct` is called when deserializing a struct-like variant. @@ -621,7 +621,7 @@ pub trait VariantVisitor { _visitor: V) -> Result where V: Visitor { - Err(Error::syntax()) + Err(Error::syntax("expected a struct variant")) } } diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 27f46517..b6c6f0fe 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -24,9 +24,9 @@ pub enum Error { } impl de::Error for Error { - fn syntax() -> Self { Error::SyntaxError } + fn syntax(_: &str) -> Self { Error::SyntaxError } fn end_of_stream() -> Self { Error::EndOfStreamError } - fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(field.to_string()) } + fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(String::from(field)) } fn missing_field(field: &'static str) -> Self { Error::MissingFieldError(field) } } @@ -374,7 +374,7 @@ impl de::MapVisitor for MapDeserializer let mut de = value.into_deserializer(); de::Deserialize::deserialize(&mut de) } - None => Err(de::Error::syntax()) + None => Err(de::Error::syntax("expected a map value")) } } diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index 7e493175..8beb1b9e 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -804,7 +804,7 @@ fn deserialize_field_visitor( let index_body = quote_expr!(cx, match value { $index_field_arms - _ => { Err(::serde::de::Error::syntax()) } + _ => { Err(::serde::de::Error::syntax("expected a field")) } } ); @@ -903,7 +903,13 @@ fn deserialize_field_visitor( // TODO: would be better to generate a byte string literal match match ::std::str::from_utf8(value) { Ok(s) => self.visit_str(s), - _ => Err(::serde::de::Error::syntax()), + _ => { + Err( + ::serde::de::Error::syntax( + "could not convert a byte string to a String" + ) + ) + } } } } diff --git a/serde_json/src/error.rs b/serde_json/src/error.rs index 32b46e37..6621188b 100644 --- a/serde_json/src/error.rs +++ b/serde_json/src/error.rs @@ -152,7 +152,7 @@ impl From for Error { fn from(error: de::value::Error) -> Error { match error { de::value::Error::SyntaxError => { - de::Error::syntax() + Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0) } de::value::Error::EndOfStreamError => { de::Error::end_of_stream() @@ -168,7 +168,7 @@ impl From for Error { } impl de::Error for Error { - fn syntax() -> Error { + fn syntax(_: &str) -> Error { Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0) } diff --git a/serde_json/src/value.rs b/serde_json/src/value.rs index 7d3948f0..b873ad2c 100644 --- a/serde_json/src/value.rs +++ b/serde_json/src/value.rs @@ -700,7 +700,7 @@ impl de::Deserializer for Deserializer { { let value = match self.value.take() { Some(Value::Object(value)) => value, - Some(_) => { return Err(de::Error::syntax()); } + Some(_) => { return Err(de::Error::syntax("expected an enum")); } None => { return Err(de::Error::end_of_stream()); } }; @@ -708,12 +708,12 @@ impl de::Deserializer for Deserializer { let (variant, value) = match iter.next() { Some(v) => v, - None => return Err(de::Error::syntax()), + None => return Err(de::Error::syntax("expected a variant name")), }; // enums are encoded in json as maps with a single key:value pair match iter.next() { - Some(_) => Err(de::Error::syntax()), + Some(_) => Err(de::Error::syntax("expected map")), None => visitor.visit(VariantDeserializer { de: self, val: Some(value), @@ -768,7 +768,7 @@ impl<'a> de::VariantVisitor for VariantDeserializer<'a> { visitor, ) } else { - Err(de::Error::syntax()) + Err(de::Error::syntax("expected a tuple")) } } @@ -788,7 +788,7 @@ impl<'a> de::VariantVisitor for VariantDeserializer<'a> { visitor, ) } else { - Err(de::Error::syntax()) + Err(de::Error::syntax("expected a struct")) } } } diff --git a/serde_tests/benches/bench_enum.rs b/serde_tests/benches/bench_enum.rs index 311120e6..0367df8b 100644 --- a/serde_tests/benches/bench_enum.rs +++ b/serde_tests/benches/bench_enum.rs @@ -20,7 +20,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax() -> Error { Error::SyntaxError } + fn syntax(_: &str) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStreamError } diff --git a/serde_tests/benches/bench_map.rs b/serde_tests/benches/bench_map.rs index 24708ac5..2f5b1bca 100644 --- a/serde_tests/benches/bench_map.rs +++ b/serde_tests/benches/bench_map.rs @@ -17,7 +17,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax() -> Error { Error::SyntaxError } + fn syntax(_: &str) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStream } diff --git a/serde_tests/benches/bench_struct.rs b/serde_tests/benches/bench_struct.rs index 1eb2d9ef..0e53d1a3 100644 --- a/serde_tests/benches/bench_struct.rs +++ b/serde_tests/benches/bench_struct.rs @@ -33,7 +33,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax() -> Error { Error::SyntaxError } + fn syntax(_: &str) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStream } diff --git a/serde_tests/benches/bench_vec.rs b/serde_tests/benches/bench_vec.rs index d1b40353..450d3f1b 100644 --- a/serde_tests/benches/bench_vec.rs +++ b/serde_tests/benches/bench_vec.rs @@ -15,7 +15,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax() -> Error { Error::SyntaxError } + fn syntax(_: &str) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStreamError } diff --git a/serde_tests/tests/test_bytes.rs b/serde_tests/tests/test_bytes.rs index d0e44abf..d0e8227a 100644 --- a/serde_tests/tests/test_bytes.rs +++ b/serde_tests/tests/test_bytes.rs @@ -9,7 +9,7 @@ use serde_json; struct Error; impl serde::de::Error for Error { - fn syntax() -> Error { Error } + fn syntax(_: &str) -> Error { Error } fn end_of_stream() -> Error { Error } diff --git a/serde_tests/tests/test_de.rs b/serde_tests/tests/test_de.rs index 99535239..5c944889 100644 --- a/serde_tests/tests/test_de.rs +++ b/serde_tests/tests/test_de.rs @@ -68,7 +68,7 @@ enum Error { } impl de::Error for Error { - fn syntax() -> Error { Error::SyntaxError } + fn syntax(_: &str) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStreamError }