use hyper::Error;
use std::convert::From;
use std::net::AddrParseError;

#[derive(Debug)]
pub enum CGError {
    Hyper(Error),
    AddrParse(AddrParseError),
    SQLite(::rusqlite::Error),
    Env(::std::env::VarError),
    IO(::std::io::Error),
}

impl CGError {
    pub fn pretty(&self) {
        match self {
            &CGError::Hyper(ref err) =>
                println!("Web server error: {}", err),
            &CGError::AddrParse(ref err) =>
                println!("Error parsing address: {}", err),
            &CGError::SQLite(ref err) =>
                println!("Error accessing db: {}", err),
            &CGError::Env(ref err) =>
                println!("Error reading env var: {}", err),
            &CGError::IO(ref err) =>
                println!("System I/O error: {}", err),
        }
    }
}

impl From<Error> for CGError {
    fn from(err: Error) -> Self {
        CGError::Hyper(err)
    }
}

impl From<AddrParseError> for CGError {
    fn from(err: AddrParseError) -> Self {
        CGError::AddrParse(err)
    }
}

impl From<::rusqlite::Error> for CGError {
    fn from(err: ::rusqlite::Error) -> Self {
        CGError::SQLite(err)
    }
}

impl From<::std::io::Error> for CGError {
    fn from(err: ::std::io::Error) -> Self {
        CGError::IO(err)
    }
}

#[derive(Debug)]
pub enum RuntimeError {
    SQLite(::rusqlite::Error),
    Mustache(::mustache::Error),
    IOError(::std::io::Error),
    UnknownPath(String),
}

impl From<::rusqlite::Error> for RuntimeError {
    fn from(err: ::rusqlite::Error) -> Self {
        RuntimeError::SQLite(err)
    }
}

impl From<::mustache::Error> for RuntimeError {
    fn from(err: ::mustache::Error) -> Self {
        RuntimeError::Mustache(err)
    }
}

impl From<::std::io::Error> for RuntimeError {
    fn from(err: ::std::io::Error) -> Self {
        RuntimeError::IOError(err)
    }
}
