use std::error;
use std::fmt;
use std::io;
use std::string;
#[derive(Debug)]
pub enum DecodeError {
Io(io::Error),
String(string::FromUtf8Error),
Unknown {
marker: u8,
},
Unsupported {
marker: u8,
},
UnexpectedObjectEnd,
CircularReference {
index: usize,
},
OutOfRangeReference {
index: usize,
},
NonZeroTimeZone {
offset: i16,
},
InvalidDate {
millis: f64,
},
ExternalizableType {
name: String,
},
}
impl error::Error for DecodeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
use self::DecodeError::*;
match *self {
Io(ref x) => x.source(),
String(ref x) => x.source(),
_ => None,
}
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::DecodeError::*;
match *self {
Io(ref x) => write!(f, "I/O Error: {}", x),
String(ref x) => write!(f, "Invalid String: {}", x),
Unknown { marker } => write!(f, "Unknown marker: {}", marker),
Unsupported { marker } => write!(f, "Unsupported type: maker={}", marker),
UnexpectedObjectEnd => write!(f, "Unexpected occurrence of object-end-marker"),
CircularReference { index } => {
write!(f, "Circular references are unsupported: index={}", index)
}
OutOfRangeReference { index } => write!(f, "Reference index {} is out-of-range", index),
NonZeroTimeZone { offset } => {
write!(f, "Non zero time zone offset {} is unsupported", offset)
}
InvalidDate { millis } => write!(f, "Invalid date value {}", millis),
ExternalizableType { ref name } => {
write!(f, "Externalizable type {:?} is unsupported", name)
}
}
}
}
impl PartialEq for DecodeError {
fn eq(&self, other: &Self) -> bool {
use self::DecodeError::*;
match (self, other) {
(&Unknown { marker: x }, &Unknown { marker: y }) => x == y,
(&Unsupported { marker: x }, &Unsupported { marker: y }) => x == y,
(&UnexpectedObjectEnd, &UnexpectedObjectEnd) => true,
(&CircularReference { index: x }, &CircularReference { index: y }) => x == y,
(&OutOfRangeReference { index: x }, &OutOfRangeReference { index: y }) => x == y,
(&NonZeroTimeZone { offset: x }, &NonZeroTimeZone { offset: y }) => x == y,
(&InvalidDate { millis: x }, &InvalidDate { millis: y }) => x == y,
(&ExternalizableType { name: ref x }, &ExternalizableType { name: ref y }) => x == y,
_ => false,
}
}
}
impl From<io::Error> for DecodeError {
fn from(f: io::Error) -> Self {
DecodeError::Io(f)
}
}
impl From<string::FromUtf8Error> for DecodeError {
fn from(f: string::FromUtf8Error) -> Self {
DecodeError::String(f)
}
}