diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 7bb02491..8066ab9f 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -88,7 +88,7 @@ impl Visitor for BoolVisitor { match s.trim() { "true" => Ok(true), "false" => Ok(false), - _ => Err(Error::type_mismatch(Type::Bool)), + _ => Err(Error::invalid_type(Type::Bool)), } } } @@ -111,7 +111,7 @@ macro_rules! impl_deserialize_num_method { { match FromPrimitive::$from_method(v) { Some(v) => Ok(v), - None => Err(Error::type_mismatch($ty)), + None => Err(Error::invalid_type($ty)), } } } @@ -155,7 +155,7 @@ impl< where E: Error, { str::FromStr::from_str(v.trim()).or_else(|_| { - Err(Error::type_mismatch(Type::Str)) + Err(Error::invalid_type(Type::Str)) }) } } @@ -207,7 +207,7 @@ impl Visitor for CharVisitor { let mut iter = v.chars(); if let Some(v) = iter.next() { if iter.next().is_some() { - Err(Error::type_mismatch(Type::Char)) + Err(Error::invalid_type(Type::Char)) } else { Ok(v) } @@ -250,7 +250,7 @@ impl Visitor for StringVisitor { { match str::from_utf8(v) { Ok(s) => Ok(s.to_owned()), - Err(_) => Err(Error::type_mismatch(Type::String)), + Err(_) => Err(Error::invalid_type(Type::String)), } } @@ -259,7 +259,7 @@ impl Visitor for StringVisitor { { match String::from_utf8(v) { Ok(s) => Ok(s), - Err(_) => Err(Error::type_mismatch(Type::String)), + Err(_) => Err(Error::invalid_type(Type::String)), } } } @@ -889,7 +889,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("expected a non-zero value")) + return Err(Error::invalid_value("expected a non-zero value")) } unsafe { Ok(NonZero::new(value)) @@ -941,7 +941,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::type_mismatch(Type::String)), + Err(_) => Err(Error::invalid_type(Type::String)), } } } diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 21212f52..ef7baedc 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -12,44 +12,44 @@ mod from_primitive; /// `Deserializer` error. pub trait Error: Sized + error::Error { /// Raised when there is general error when deserializing a type. - fn syntax(msg: &str) -> Self; - - /// Raised when a fixed sized sequence or map was passed in the wrong amount of arguments. - fn length_mismatch(_len: usize) -> Self { - Error::syntax("incorrect length") - } - - /// Raised when a `Deserialize` was passed an incorrect type. - fn type_mismatch(_type: Type) -> Self { - Error::syntax("incorrect type") - } - - /// Raised when a `Deserialize` was passed an incorrect value. - fn invalid_value(msg: &str) -> Self { - Error::syntax(msg) - } + fn custom(msg: String) -> Self; /// Raised when a `Deserialize` type unexpectedly hit the end of the stream. fn end_of_stream() -> Self; + /// Raised when a `Deserialize` was passed an incorrect type. + fn invalid_type(ty: Type) -> Self { + Error::custom(format!("Invalid type. Expected `{:?}`", ty)) + } + + /// Raised when a `Deserialize` was passed an incorrect value. + fn invalid_value(msg: &str) -> Self { + Error::custom(format!("Invalid value: {}", msg)) + } + + /// Raised when a fixed sized sequence or map was passed in the wrong amount of arguments. + fn invalid_length(len: usize) -> Self { + Error::custom(format!("Invalid length: {}", len)) + } + /// Raised when a `Deserialize` enum type received an unexpected variant. fn unknown_variant(field: &str) -> Self { - Error::syntax(&format!("Unknown variant `{}`", field)) + Error::custom(format!("Unknown variant `{}`", field)) } /// Raised when a `Deserialize` struct type received an unexpected struct field. fn unknown_field(field: &str) -> Self { - Error::syntax(&format!("Unknown field `{}`", field)) + Error::custom(format!("Unknown field `{}`", field)) } /// raised when a `deserialize` struct type did not receive a field. fn missing_field(field: &'static str) -> Self { - Error::syntax(&format!("Missing field `{}`", field)) + Error::custom(format!("Missing field `{}`", field)) } } /// `Type` represents all the primitive types that can be deserialized. This is used by -/// `Error::kind_mismatch`. +/// `Error::invalid_type`. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Type { /// Represents a `bool` type. @@ -124,12 +124,18 @@ pub enum Type { /// Represents a struct type. Struct, + /// Represents a struct field name. + FieldName, + /// Represents a tuple type. Tuple, /// Represents an `enum` type. Enum, + /// Represents an enum variant name. + VariantName, + /// Represents a struct variant. StructVariant, @@ -416,7 +422,7 @@ pub trait Deserializer { _visitor: V) -> Result where V: EnumVisitor, { - Err(Error::syntax("expected an enum")) + Err(Error::invalid_type(Type::Enum)) } /// This method hints that the `Deserialize` type is expecting a `Vec`. This allows @@ -460,7 +466,7 @@ pub trait Visitor { fn visit_bool(&mut self, _v: bool) -> Result where E: Error, { - Err(Error::type_mismatch(Type::Bool)) + Err(Error::invalid_type(Type::Bool)) } /// `visit_isize` deserializes a `isize` into a `Value`. @@ -495,7 +501,7 @@ pub trait Visitor { fn visit_i64(&mut self, _v: i64) -> Result where E: Error, { - Err(Error::type_mismatch(Type::I64)) + Err(Error::invalid_type(Type::I64)) } /// `visit_usize` deserializes a `usize` into a `Value`. @@ -530,7 +536,7 @@ pub trait Visitor { fn visit_u64(&mut self, _v: u64) -> Result where E: Error, { - Err(Error::type_mismatch(Type::U64)) + Err(Error::invalid_type(Type::U64)) } /// `visit_f32` deserializes a `f32` into a `Value`. @@ -544,7 +550,7 @@ pub trait Visitor { fn visit_f64(&mut self, _v: f64) -> Result where E: Error, { - Err(Error::type_mismatch(Type::F64)) + Err(Error::invalid_type(Type::F64)) } /// `visit_char` deserializes a `char` into a `Value`. @@ -561,7 +567,7 @@ pub trait Visitor { fn visit_str(&mut self, _v: &str) -> Result where E: Error, { - Err(Error::type_mismatch(Type::Str)) + Err(Error::invalid_type(Type::Str)) } /// `visit_string` deserializes a `String` into a `Value`. This allows a deserializer to avoid @@ -578,7 +584,7 @@ pub trait Visitor { fn visit_unit(&mut self) -> Result where E: Error, { - Err(Error::type_mismatch(Type::Unit)) + Err(Error::invalid_type(Type::Unit)) } /// `visit_unit_struct` deserializes a unit struct into a `Value`. @@ -593,42 +599,42 @@ pub trait Visitor { fn visit_none(&mut self) -> Result where E: Error, { - Err(Error::type_mismatch(Type::Option)) + Err(Error::invalid_type(Type::Option)) } /// `visit_some` deserializes a value into a `Value`. fn visit_some(&mut self, _deserializer: &mut D) -> Result where D: Deserializer, { - Err(Error::type_mismatch(Type::Option)) + Err(Error::invalid_type(Type::Option)) } /// `visit_newtype_struct` deserializes a value into a `Value`. fn visit_newtype_struct(&mut self, _deserializer: &mut D) -> Result where D: Deserializer, { - Err(Error::type_mismatch(Type::NewtypeStruct)) + Err(Error::invalid_type(Type::NewtypeStruct)) } /// `visit_bool` deserializes a `SeqVisitor` into a `Value`. fn visit_seq(&mut self, _visitor: V) -> Result where V: SeqVisitor, { - Err(Error::type_mismatch(Type::Seq)) + Err(Error::invalid_type(Type::Seq)) } /// `visit_map` deserializes a `MapVisitor` into a `Value`. fn visit_map(&mut self, _visitor: V) -> Result where V: MapVisitor, { - Err(Error::type_mismatch(Type::Map)) + Err(Error::invalid_type(Type::Map)) } /// `visit_bytes` deserializes a `&[u8]` into a `Value`. fn visit_bytes(&mut self, _v: &[u8]) -> Result where E: Error, { - Err(Error::type_mismatch(Type::Bytes)) + Err(Error::invalid_type(Type::Bytes)) } /// `visit_byte_buf` deserializes a `Vec` into a `Value`. @@ -799,7 +805,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::type_mismatch(Type::UnitVariant)) + Err(Error::invalid_type(Type::UnitVariant)) } /// `visit_newtype` is called when deserializing a variant with a single value. By default this @@ -818,7 +824,7 @@ pub trait VariantVisitor { _visitor: V) -> Result where V: Visitor { - Err(Error::type_mismatch(Type::TupleVariant)) + Err(Error::invalid_type(Type::TupleVariant)) } /// `visit_struct` is called when deserializing a struct-like variant. @@ -827,7 +833,7 @@ pub trait VariantVisitor { _visitor: V) -> Result where V: Visitor { - Err(Error::type_mismatch(Type::StructVariant)) + Err(Error::invalid_type(Type::StructVariant)) } } diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 3d084ba1..773c19df 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -24,17 +24,17 @@ use bytes; /// This represents all the possible errors that can occur using the `ValueDeserializer`. #[derive(Clone, Debug, PartialEq)] pub enum Error { - /// The value had some syntatic error. - Syntax(String), + /// The value had some custom error. + Custom(String), /// The value had an incorrect type. - Type(de::Type), + InvalidType(de::Type), /// The value had an invalid length. - Length(usize), + InvalidLength(usize), /// The value is invalid and cannot be deserialized. - Value(String), + InvalidValue(String), /// EOF while deserializing a value. EndOfStream, @@ -50,11 +50,11 @@ pub enum Error { } impl de::Error for Error { - fn syntax(msg: &str) -> Self { Error::Syntax(String::from(msg)) } - fn type_mismatch(type_: de::Type) -> Self { Error::Type(type_) } - fn length_mismatch(len: usize) -> Self { Error::Length(len) } - fn invalid_value(msg: &str) -> Self { Error::Value(msg.to_owned()) } + fn custom(msg: String) -> Self { Error::Custom(msg) } fn end_of_stream() -> Self { Error::EndOfStream } + fn invalid_type(ty: de::Type) -> Self { Error::InvalidType(ty) } + fn invalid_value(msg: &str) -> Self { Error::InvalidValue(msg.to_owned()) } + fn invalid_length(len: usize) -> Self { Error::InvalidLength(len) } fn unknown_variant(variant: &str) -> Self { Error::UnknownVariant(String::from(variant)) } fn unknown_field(field: &str) -> Self { Error::UnknownField(String::from(field)) } fn missing_field(field: &'static str) -> Self { Error::MissingField(field) } @@ -63,12 +63,14 @@ impl de::Error for Error { impl fmt::Display for Error { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { - Error::Syntax(ref s) => write!(formatter, "Syntax error: {}", s), - Error::Type(ty) => write!(formatter, "Invalid type: {:?}", ty), - Error::Length(len) => write!(formatter, "Invalid length: {}", len), - Error::Value(ref value) => write!(formatter, "Invalid value: {}", value), - Error::EndOfStream => formatter.write_str("EndOfStreamError"), - Error::UnknownVariant(ref variant) => write!(formatter, "Unknown varian: {}", variant), + Error::Custom(ref s) => write!(formatter, "{}", s), + Error::EndOfStream => formatter.write_str("End of stream"), + Error::InvalidType(ty) => write!(formatter, "Invalid type, expected `{:?}`", ty), + Error::InvalidValue(ref value) => write!(formatter, "Invalid value: {}", value), + Error::InvalidLength(len) => write!(formatter, "Invalid length: {}", len), + Error::UnknownVariant(ref variant) => { + write!(formatter, "Unknown variant: {}", variant) + } Error::UnknownField(ref field) => write!(formatter, "Unknown field: {}", field), Error::MissingField(ref field) => write!(formatter, "Missing field: {}", field), } @@ -348,7 +350,7 @@ impl de::SeqVisitor for SeqDeserializer if self.len == 0 { Ok(()) } else { - Err(de::Error::length_mismatch(self.len)) + Err(de::Error::invalid_length(self.len)) } } @@ -504,7 +506,9 @@ impl de::MapVisitor for MapDeserializer let mut de = value.into_deserializer(); de::Deserialize::deserialize(&mut de) } - None => Err(de::Error::syntax("expected a map value")) + None => { + Err(de::Error::end_of_stream()) + } } } @@ -512,7 +516,7 @@ impl de::MapVisitor for MapDeserializer if self.len == 0 { Ok(()) } else { - Err(de::Error::length_mismatch(self.len)) + Err(de::Error::invalid_length(self.len)) } } diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index e700b44f..01368afb 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -10,11 +10,11 @@ pub mod impls; /// `Serializer` error. pub trait Error: Sized + error::Error { /// Raised when there is general error when deserializing a type. - fn syntax(msg: &str) -> Self; + fn custom(msg: String) -> Self; /// Raised when a `Serialize` was passed an incorrect value. fn invalid_value(msg: &str) -> Self { - Error::syntax(msg) + Error::custom(format!("invalid value: {}", msg)) } } diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index a94fd02b..ba05af6c 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -911,7 +911,7 @@ fn deserialize_field_visitor( Ok(s) => self.visit_str(s), _ => { Err( - ::serde::de::Error::syntax( + ::serde::de::Error::invalid_value( "could not convert a byte string to a String" ) ) diff --git a/serde_tests/benches/bench_enum.rs b/serde_tests/benches/bench_enum.rs index ecd3114b..0618e1f7 100644 --- a/serde_tests/benches/bench_enum.rs +++ b/serde_tests/benches/bench_enum.rs @@ -22,7 +22,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> 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 ce25f3f1..f753953c 100644 --- a/serde_tests/benches/bench_map.rs +++ b/serde_tests/benches/bench_map.rs @@ -19,7 +19,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> 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 03ae4310..d8e62e23 100644 --- a/serde_tests/benches/bench_struct.rs +++ b/serde_tests/benches/bench_struct.rs @@ -35,7 +35,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> 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 3ce1cc32..eac501aa 100644 --- a/serde_tests/benches/bench_vec.rs +++ b/serde_tests/benches/bench_vec.rs @@ -17,7 +17,7 @@ pub enum Error { } impl serde::de::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> 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 785ff550..93fe9705 100644 --- a/serde_tests/tests/test_bytes.rs +++ b/serde_tests/tests/test_bytes.rs @@ -10,19 +10,13 @@ use serde::bytes::{ByteBuf, Bytes}; struct Error; impl serde::ser::Error for Error { - fn syntax(_: &str) -> Error { Error } - - fn invalid_value(_field: &str) -> Error { Error } + fn custom(_: String) -> Error { Error } } impl serde::de::Error for Error { - fn syntax(_: &str) -> Error { Error } + fn custom(_: String) -> Error { Error } fn end_of_stream() -> Error { Error } - - fn unknown_field(_field: &str) -> Error { Error } - - fn missing_field(_field: &'static str) -> Error { Error } } impl fmt::Display for Error { diff --git a/serde_tests/tests/token.rs b/serde_tests/tests/token.rs index 11c0dc8e..16be21b1 100644 --- a/serde_tests/tests/token.rs +++ b/serde_tests/tests/token.rs @@ -416,7 +416,7 @@ pub enum Error { } impl ser::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> Error { Error::SyntaxError } fn invalid_value(msg: &str) -> Error { Error::InvalidValue(msg.to_owned()) @@ -424,7 +424,7 @@ impl ser::Error for Error { } impl de::Error for Error { - fn syntax(_: &str) -> Error { Error::SyntaxError } + fn custom(_: String) -> Error { Error::SyntaxError } fn end_of_stream() -> Error { Error::EndOfStreamError }