feat(de): Rename de::Error trait methods

This commit is contained in:
Erick Tryzelaar 2016-02-23 19:51:49 -08:00
parent 6caf8a0074
commit 118476b98b
11 changed files with 83 additions and 79 deletions

View File

@ -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<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable +
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, 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<T, E> Deserialize for Result<T, E> 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)),
}
}
}

View File

@ -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<V::Value, Self::Error>
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<u8>`. This allows
@ -460,7 +466,7 @@ pub trait Visitor {
fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E>
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<E>(&mut self, _v: i64) -> Result<Self::Value, E>
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<E>(&mut self, _v: u64) -> Result<Self::Value, E>
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<E>(&mut self, _v: f64) -> Result<Self::Value, E>
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<E>(&mut self, _v: &str) -> Result<Self::Value, E>
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<E>(&mut self) -> Result<Self::Value, E>
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<E>(&mut self) -> Result<Self::Value, E>
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<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
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<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error>
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<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
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<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error>
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<E>(&mut self, _v: &[u8]) -> Result<Self::Value, E>
where E: Error,
{
Err(Error::type_mismatch(Type::Bytes))
Err(Error::invalid_type(Type::Bytes))
}
/// `visit_byte_buf` deserializes a `Vec<u8>` 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<V::Value, Self::Error>
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<V::Value, Self::Error>
where V: Visitor
{
Err(Error::type_mismatch(Type::StructVariant))
Err(Error::invalid_type(Type::StructVariant))
}
}

View File

@ -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<I, T, E> de::SeqVisitor for SeqDeserializer<I, E>
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<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
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<I, K, V, E> de::MapVisitor for MapDeserializer<I, K, V, E>
if self.len == 0 {
Ok(())
} else {
Err(de::Error::length_mismatch(self.len))
Err(de::Error::invalid_length(self.len))
}
}

View File

@ -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))
}
}

View File

@ -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"
)
)

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 {

View File

@ -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 }