extern crate console;
extern crate serde;
use console::style;
use std::fmt;
use std::io;
use crate::config;
#[derive(Debug)]
pub enum HMError {
Io(io::Error),
Regular(ErrorKind),
Other(String),
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ErrorKind {
DependencyUndefinedError {
dependency: String,
},
IncorrectPlatformError {
dependency: String,
platform: config::OS,
target_platform: config::OS,
},
CyclicalDependencyError {
dependency_graph: String,
},
SolutionError {
solution: String,
},
ConfigError {
line_number: usize,
},
Other,
}
impl From<io::Error> for HMError {
fn from(err: io::Error) -> HMError {
HMError::Io(err)
}
}
impl ErrorKind {
fn as_str(&self) -> &str {
match *self {
ErrorKind::ConfigError { line_number: _ } => "configuration error",
ErrorKind::SolutionError { solution: _ } => "solution error",
ErrorKind::DependencyUndefinedError { dependency: _ } => "dependency undefined",
ErrorKind::IncorrectPlatformError {
dependency: _,
platform: _,
target_platform: _,
} => "incorrect platform for dependency",
ErrorKind::CyclicalDependencyError {
dependency_graph: _,
} => "cyclical dependency",
ErrorKind::Other => "other error",
}
}
}
impl fmt::Display for HMError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
HMError::Regular(ref err) => write!(f, "{:?}", err),
HMError::Other(ref err) => write!(f, "{:?}", err),
HMError::Io(ref err) => err.fmt(f),
}
}
}
pub fn happy_print(jubilation: &str) {
println!("{}!", style(jubilation).green().bold())
}
pub fn error(complaint: &str, er: &str) {
eprintln!("{}:\n ↳ Error: {}", style(complaint).red().bold(), er)
}
pub type Result<T> = std::result::Result<T, HMError>;