#[derive(Debug)]
pub enum Error {
Unsupported(&'static str),
InvalidInput(&'static str),
PathNotAbsolute,
PathDoesNotExist,
Io(std::io::Error),
WindowsApi {
context: &'static str,
code: i32,
},
}
pub type Result<T> = std::result::Result<T, Error>;
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unsupported(msg) => write!(f, "unsupported operation: {msg}"),
Self::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
Self::PathNotAbsolute => write!(f, "path must be absolute"),
Self::PathDoesNotExist => write!(f, "path does not exist"),
Self::Io(err) => write!(f, "I/O error: {err}"),
Self::WindowsApi { context, code } => {
write!(f, "Windows API error in {context} (code {code})")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}