use std::error;
use std::fmt;
use std::io;
use std::result;
use reqwest;
use serde_json;
pub type DokResult<T> = result::Result<T, DokError>;
#[derive(Debug)]
pub enum DokError {
Error(String),
Io(io::Error),
Json(serde_json::Error),
Http(reqwest::Error),
}
impl DokError {
pub fn new(desc: &str) -> Self {
DokError::Error(desc.to_string())
}
}
impl fmt::Display for DokError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DokError::Error(ref e) => write!(f, "[Dok] {}", e),
DokError::Io(ref e) => write!(f, "[IO] {}", e),
DokError::Json(ref e) => write!(f, "[JSON] {}", e),
DokError::Http(ref e) => write!(f, "[HTTP] {}", e),
}
}
}
impl error::Error for DokError {
fn description(&self) -> &str {
match *self {
DokError::Error(ref e) => e,
DokError::Io(ref e) => e.description(),
DokError::Json(ref e) => e.description(),
DokError::Http(ref e) => e.description(),
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
DokError::Error(_) => None,
DokError::Io(ref e) => Some(e),
DokError::Json(ref e) => Some(e),
DokError::Http(ref e) => Some(e),
}
}
}
impl From<io::Error> for DokError {
fn from(e: io::Error) -> DokError {
DokError::Io(e)
}
}
impl From<serde_json::Error> for DokError {
fn from(e: serde_json::Error) -> DokError {
DokError::Json(e)
}
}
impl From<reqwest::Error> for DokError {
fn from(e: reqwest::Error) -> DokError {
DokError::Http(e)
}
}