use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Exception(String),
MissingFile(String),
}
impl Error {
pub fn msg<S: Into<String>>(s: S) -> Self {
Error::Exception(s.into())
}
pub fn missing_file<S: Into<String>>(s: S) -> Self {
Error::MissingFile(s.into())
}
pub fn message(&self) -> &str {
match self {
Error::Exception(s) | Error::MissingFile(s) => s,
}
}
pub fn prefixed(&self, prefix: &str) -> Self {
match self {
Error::Exception(s) => Error::Exception(format!("{prefix}{s}")),
Error::MissingFile(s) => Error::MissingFile(format!("{prefix}{s}")),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Exception(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[macro_export]
macro_rules! bail {
($($arg:tt)*) => {
return Err($crate::Error::Exception(format!($($arg)*)))
};
}