Merge pull request #162 from skade/std-error

Have serde::de::Error require std::error::Error
This commit is contained in:
Erick Tryzelaar
2015-12-08 14:10:45 -05:00
8 changed files with 131 additions and 1 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
//! Generic deserialization framework. //! Generic deserialization framework.
use std::error;
pub mod impls; pub mod impls;
pub mod value; pub mod value;
@@ -7,7 +9,7 @@ pub mod value;
/// `Error` is a trait that allows a `Deserialize` to generically create a /// `Error` is a trait that allows a `Deserialize` to generically create a
/// `Deserializer` error. /// `Deserializer` error.
pub trait Error: Sized { pub trait Error: Sized + error::Error {
/// Raised when there is general error when deserializing a type. /// Raised when there is general error when deserializing a type.
fn syntax(msg: &str) -> Self; fn syntax(msg: &str) -> Self;
+23
View File
@@ -11,6 +11,8 @@ use std::collections::{
hash_set, hash_set,
}; };
use std::hash::Hash; use std::hash::Hash;
use std::error;
use std::fmt;
use std::vec; use std::vec;
use std::marker::PhantomData; use std::marker::PhantomData;
@@ -50,6 +52,27 @@ impl de::Error for Error {
fn missing_field(field: &'static str) -> Self { Error::MissingField(field) } 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. /// This trait converts primitive types into a deserializer.
+18
View File
@@ -1,4 +1,6 @@
use test::Bencher; use test::Bencher;
use std::error;
use std::fmt;
use rustc_serialize::{Decoder, Decodable}; use rustc_serialize::{Decoder, Decodable};
use serde; use serde;
use serde::de::{Deserializer, Deserialize}; use serde::de::{Deserializer, Deserialize};
@@ -29,6 +31,22 @@ impl serde::de::Error for Error {
fn missing_field(_: &'static str) -> Error { Error::SyntaxError } 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 { mod decoder {
+17
View File
@@ -1,4 +1,6 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt;
use std::error;
use std::collections::HashMap; use std::collections::HashMap;
use test::Bencher; 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 { mod decoder {
+18
View File
@@ -1,5 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use test::Bencher; use test::Bencher;
use std::fmt;
use std::error;
use rustc_serialize::{Decoder, Decodable}; 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 { mod decoder {
use std::collections::HashMap; use std::collections::HashMap;
use rustc_serialize::Decoder; use rustc_serialize::Decoder;
+17
View File
@@ -1,4 +1,6 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt;
use std::error;
use test::Bencher; use test::Bencher;
use rustc_serialize::{Decoder, Decodable}; use rustc_serialize::{Decoder, Decodable};
@@ -24,6 +26,21 @@ impl serde::de::Error for Error {
fn missing_field(_: &'static str) -> Error { Error::SyntaxError } 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 { mod decoder {
+18
View File
@@ -1,4 +1,6 @@
use serde; use serde;
use std::fmt;
use std::error;
use serde::Serialize; use serde::Serialize;
use serde::bytes::{ByteBuf, Bytes}; use serde::bytes::{ByteBuf, Bytes};
@@ -17,6 +19,22 @@ impl serde::de::Error for Error {
fn missing_field(_field: &'static str) -> Error { 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 { struct BytesSerializer {
+17
View File
@@ -1,5 +1,6 @@
use std::fmt; use std::fmt;
use std::iter; use std::iter;
use std::error;
use serde::{ser, de}; use serde::{ser, de};
use serde::de::value::{self, ValueDeserializer}; 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<value::Error> for Error { impl From<value::Error> for Error {
fn from(error: value::Error) -> Error { fn from(error: value::Error) -> Error {
Error::ValueError(error) Error::ValueError(error)