fix: forward more errors in de::value::Error

When using a `ValueDeserializer` errors produced with the methods
`type_mismatch`, `length_mismatch` and `syntax` were not forwarded
correctly. These methods now store all information in the enum
`de::value::Error`.
This commit is contained in:
Thomas Bahn 2015-10-05 17:05:03 +02:00
parent a1bd0c1667
commit 0a65153634
2 changed files with 11 additions and 2 deletions

View File

@ -33,6 +33,7 @@ pub trait Error: Sized {
/// `Type` represents all the primitive types that can be deserialized. This is used by
/// `Error::kind_mismatch`.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Type {
/// Represents a `bool` type.
Bool,

View File

@ -22,7 +22,13 @@ use bytes;
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
/// The value had some syntatic error.
SyntaxError,
SyntaxError(String),
/// The value had an incorrect type.
TypeError(de::Type),
/// The value had an invalid length.
LengthError(usize),
/// EOF while deserializing a value.
EndOfStreamError,
@ -35,7 +41,9 @@ pub enum Error {
}
impl de::Error for Error {
fn syntax(_: &str) -> Self { Error::SyntaxError }
fn syntax(msg: &str) -> Self { Error::SyntaxError(String::from(msg)) }
fn type_mismatch(type_: de::Type) -> Self { Error::TypeError(type_) }
fn length_mismatch(len: usize) -> Self { Error::LengthError(len) }
fn end_of_stream() -> Self { Error::EndOfStreamError }
fn unknown_field(field: &str) -> Self { Error::UnknownFieldError(String::from(field)) }
fn missing_field(field: &'static str) -> Self { Error::MissingFieldError(field) }