From 0a651536349480a47ea80cb137ec450d0b708b6e Mon Sep 17 00:00:00 2001 From: Thomas Bahn Date: Mon, 5 Oct 2015 17:05:03 +0200 Subject: [PATCH] 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`. --- serde/src/de/mod.rs | 1 + serde/src/de/value.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 51626a4e..b078cde9 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -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, diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 7e386619..2f1951fc 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -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) }