use std::fmt;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct NativeError {
pub step: &'static str,
pub message: String,
pub path: Option<PathBuf>,
}
impl NativeError {
pub fn new(step: &'static str, message: impl Into<String>) -> Self {
NativeError {
step,
message: message.into(),
path: None,
}
}
pub fn at(step: &'static str, path: impl AsRef<Path>, message: impl Into<String>) -> Self {
NativeError {
step,
message: message.into(),
path: Some(path.as_ref().to_path_buf()),
}
}
pub fn io(step: &'static str, path: impl AsRef<Path>, error: std::io::Error) -> Self {
NativeError::at(step, path, error.to_string())
}
}
impl fmt::Display for NativeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.path {
Some(path) => write!(f, "{} ({})", self.message, path.display()),
None => write!(f, "{}", self.message),
}
}
}
impl std::error::Error for NativeError {}