use std::fmt;
#[derive(PartialEq, Eq)]
pub struct Error {
repr: ErrorRepr,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
NotFound,
Unknown,
}
impl ErrorKind {
pub fn as_str(&self) -> &str {
match *self {
ErrorKind::NotFound => "handler not found",
ErrorKind::Unknown => "unknown error",
}
}
}
#[derive(PartialEq, Eq)]
enum ErrorRepr {
Kind(ErrorKind),
WithDescription(ErrorKind, String),
}
impl Error {
pub fn new<S: Into<String>>(kind: ErrorKind, description: S) -> Error {
Error {
repr: ErrorRepr::WithDescription(kind, description.into()),
}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
repr: ErrorRepr::Kind(kind),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.repr {
ErrorRepr::Kind(ref kind) => {
write!(f, "{}", kind.as_str())
}
ErrorRepr::WithDescription(ref kind, ref description) => match *kind {
ErrorKind::Unknown => {
write!(f, "{}", description)
}
_ => {
write!(f, "{}: {}", kind.as_str(), description)
}
},
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl std::error::Error for Error {}