use std::fmt::{Display, Formatter};
use std::path::PathBuf;
#[derive(Debug)]
pub enum Error {
InvalidDirectory,
InvalidFile,
InvalidAffix,
Io(std::io::Error),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidDirectory => write!(f, "An invalid directory was specified"),
Self::InvalidFile => write!(f, "An invalid file name was specified"),
Self::InvalidAffix => {
write!(f, "A name prefix or suffix contained a path separator")
}
Self::Io(e) => Display::fmt(e, f),
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
#[derive(Debug)]
pub struct PersistError {
pub error: Error,
pub path: PathBuf,
}
impl Display for PersistError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"failed to persist temporary at {:?}: {}",
self.path, self.error
)
}
}
impl std::error::Error for PersistError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}