diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index c4c5c750..e6c22788 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -1,5 +1,7 @@ //! Generic deserialization framework. +use std::error; + pub mod impls; pub mod value; @@ -7,7 +9,7 @@ pub mod value; /// `Error` is a trait that allows a `Deserialize` to generically create a /// `Deserializer` error. -pub trait Error: Sized { +pub trait Error: Sized + error::Error { /// Raised when there is general error when deserializing a type. fn syntax(msg: &str) -> Self; diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 5f5ee9aa..8b5c3196 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -11,6 +11,8 @@ use std::collections::{ hash_set, }; use std::hash::Hash; +use std::error; +use std::fmt; use std::vec; use std::marker::PhantomData; @@ -50,6 +52,27 @@ impl de::Error for Error { fn missing_field(field: &'static str) -> Self { Error::MissingField(field) } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + Error::SyntaxError => formatter.write_str("SyntaxError"), + Error::EndOfStreamError => formatter.write_str("EndOfStreamError"), + Error::UnknownFieldError(ref field) => formatter.write_str(format!("Unknown field: {}", field).as_ref()), + Error::MissingFieldError(ref field) => formatter.write_str(format!("Missing field: {}", field).as_ref()), + } + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + /////////////////////////////////////////////////////////////////////////////// /// This trait converts primitive types into a deserializer. diff --git a/serde_tests/benches/bench_enum.rs b/serde_tests/benches/bench_enum.rs index 6af46b95..1f6d5124 100644 --- a/serde_tests/benches/bench_enum.rs +++ b/serde_tests/benches/bench_enum.rs @@ -1,4 +1,6 @@ use test::Bencher; +use std::error; +use std::fmt; use rustc_serialize::{Decoder, Decodable}; use serde; use serde::de::{Deserializer, Deserialize}; @@ -29,6 +31,22 @@ impl serde::de::Error for Error { fn missing_field(_: &'static str) -> Error { Error::SyntaxError } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + ////////////////////////////////////////////////////////////////////////////// mod decoder { diff --git a/serde_tests/benches/bench_map.rs b/serde_tests/benches/bench_map.rs index 3b847305..ce25f3f1 100644 --- a/serde_tests/benches/bench_map.rs +++ b/serde_tests/benches/bench_map.rs @@ -1,4 +1,6 @@ use std::fmt::Debug; +use std::fmt; +use std::error; use std::collections::HashMap; use test::Bencher; @@ -28,6 +30,21 @@ impl serde::de::Error for Error { } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} ////////////////////////////////////////////////////////////////////////////// mod decoder { diff --git a/serde_tests/benches/bench_struct.rs b/serde_tests/benches/bench_struct.rs index fa9142cb..5fef379c 100644 --- a/serde_tests/benches/bench_struct.rs +++ b/serde_tests/benches/bench_struct.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; use test::Bencher; +use std::fmt; +use std::error; use rustc_serialize::{Decoder, Decodable}; @@ -44,6 +46,22 @@ impl serde::de::Error for Error { } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + mod decoder { use std::collections::HashMap; use rustc_serialize::Decoder; diff --git a/serde_tests/benches/bench_vec.rs b/serde_tests/benches/bench_vec.rs index 13822bc1..3ce1cc32 100644 --- a/serde_tests/benches/bench_vec.rs +++ b/serde_tests/benches/bench_vec.rs @@ -1,4 +1,6 @@ use std::fmt::Debug; +use std::fmt; +use std::error; use test::Bencher; use rustc_serialize::{Decoder, Decodable}; @@ -24,6 +26,21 @@ impl serde::de::Error for Error { fn missing_field(_: &'static str) -> Error { Error::SyntaxError } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} ////////////////////////////////////////////////////////////////////////////// mod decoder { diff --git a/serde_tests/tests/test_bytes.rs b/serde_tests/tests/test_bytes.rs index 4ddcb0d6..137d0098 100644 --- a/serde_tests/tests/test_bytes.rs +++ b/serde_tests/tests/test_bytes.rs @@ -1,4 +1,6 @@ use serde; +use std::fmt; +use std::error; use serde::Serialize; use serde::bytes::{ByteBuf, Bytes}; @@ -17,6 +19,22 @@ impl serde::de::Error for Error { fn missing_field(_field: &'static str) -> Error { Error } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + /////////////////////////////////////////////////////////////////////////////// struct BytesSerializer { diff --git a/serde_tests/tests/token.rs b/serde_tests/tests/token.rs index c5645582..fa4468b0 100644 --- a/serde_tests/tests/token.rs +++ b/serde_tests/tests/token.rs @@ -1,5 +1,6 @@ use std::fmt; use std::iter; +use std::error; use serde::{ser, de}; use serde::de::value::{self, ValueDeserializer}; @@ -333,6 +334,22 @@ impl de::Error for Error { } } +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(format!("{:?}", self).as_ref()) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + "Serde Deserialization Error" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + impl From for Error { fn from(error: value::Error) -> Error { Error::ValueError(error)