2016-06-28 21:47:54 -07:00
|
|
|
use std::{error, fmt};
|
|
|
|
|
|
|
|
use serde::{ser, de};
|
|
|
|
|
|
|
|
use token::Token;
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
pub enum Error {
|
2016-06-28 22:57:53 -07:00
|
|
|
// Shared
|
2016-06-28 22:49:13 -07:00
|
|
|
Custom(String),
|
2016-06-28 22:57:53 -07:00
|
|
|
InvalidValue(String),
|
|
|
|
|
|
|
|
// De
|
2016-06-28 22:49:13 -07:00
|
|
|
EndOfStream,
|
2016-06-28 22:57:53 -07:00
|
|
|
InvalidType(de::Type),
|
|
|
|
InvalidLength(usize),
|
2016-06-28 22:49:13 -07:00
|
|
|
UnknownVariant(String),
|
2016-06-28 22:57:53 -07:00
|
|
|
UnknownField(String),
|
2016-06-28 22:49:13 -07:00
|
|
|
MissingField(&'static str),
|
|
|
|
DuplicateField(&'static str),
|
2016-06-28 21:47:54 -07:00
|
|
|
InvalidName(&'static str),
|
|
|
|
UnexpectedToken(Token<'static>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ser::Error for Error {
|
2016-06-28 22:36:29 -07:00
|
|
|
fn custom<T: Into<String>>(msg: T) -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::Custom(msg.into())
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn invalid_value(msg: &str) -> Error {
|
|
|
|
Error::InvalidValue(msg.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl de::Error for Error {
|
2016-06-28 22:36:29 -07:00
|
|
|
fn custom<T: Into<String>>(msg: T) -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::Custom(msg.into())
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn end_of_stream() -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::EndOfStream
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
2016-06-28 22:57:53 -07:00
|
|
|
fn invalid_type(ty: de::Type) -> Error {
|
|
|
|
Error::InvalidType(ty)
|
|
|
|
}
|
|
|
|
|
2016-06-28 21:47:54 -07:00
|
|
|
fn invalid_value(msg: &str) -> Error {
|
|
|
|
Error::InvalidValue(msg.to_owned())
|
|
|
|
}
|
|
|
|
|
2016-06-28 22:57:53 -07:00
|
|
|
fn invalid_length(len: usize) -> Error {
|
|
|
|
Error::InvalidLength(len)
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unknown_variant(variant: &str) -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::UnknownVariant(variant.to_owned())
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
2016-06-28 22:57:53 -07:00
|
|
|
fn unknown_field(field: &str) -> Error {
|
|
|
|
Error::UnknownField(field.to_owned())
|
|
|
|
}
|
|
|
|
|
2016-06-28 21:47:54 -07:00
|
|
|
fn missing_field(field: &'static str) -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::MissingField(field)
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn duplicate_field(field: &'static str) -> Error {
|
2016-06-28 22:49:13 -07:00
|
|
|
Error::DuplicateField(field)
|
2016-06-28 21:47:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|