use std::{
fmt::{self, Display, Formatter},
io,
};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Toml(String),
Io(io::Error),
Generic(String),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Self::Generic(ref e) => write!(f, "{}", e),
Self::Toml(ref e) => write!(f, "Could not parse toml file: {}", e),
Self::Io(ref e) => write!(f, "{}", e),
}
}
}
impl Error {
pub fn exit(&self) -> ! {
eprintln!("error: {}", self);
::std::process::exit(1)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
impl From<toml::de::Error> for Error {
fn from(err: toml::de::Error) -> Self {
Self::Toml(format!("Could not parse input as TOML: {}", err))
}
}
impl<T> From<std::io::IntoInnerError<T>> for Error {
fn from(err: std::io::IntoInnerError<T>) -> Self {
Self::Generic(format!("Error: {}", err))
}
}