use std::fmt;
#[derive(Debug)]
pub struct Error {
pub code: &'static str,
pub message: String,
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn new(code: &'static str, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for Error {}
pub fn io(context: impl AsRef<str>, error: std::io::Error) -> Error {
Error::new("E_IO", format!("{}: {error}", context.as_ref()))
}