use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Format {
file: String,
message: String,
},
NotFound(String),
}
impl Error {
pub(crate) fn format(file: &str, message: impl Into<String>) -> Self {
Self::Format {
file: file.to_owned(),
message: message.into(),
}
}
pub(crate) fn in_file(self, file: &str) -> Self {
match self {
Self::Format { message, .. } => Self::format(file, message),
other => other,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "{e}"),
Self::Format { file, message } => write!(f, "{file}: {message}"),
Self::NotFound(what) => write!(f, "{what} not found"),
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}