use std::fmt::{Display, Formatter};
use std::io;
use std::path::PathBuf;
use std::sync::mpsc;
#[derive(Debug)]
pub enum Vex2PdfError {
Io(io::Error),
InvalidOutputDir(PathBuf),
InvalidFileStem(PathBuf),
Parse(String),
UnsupportedFileType,
IgnoredByUser,
VoluntaryLicenseDisplayInterruption,
ConcurrencyError(String),
ProcessingFailures(usize),
}
impl Display for Vex2PdfError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Vex2PdfError::Io(e) => e.fmt(f),
Vex2PdfError::InvalidOutputDir(path) => {
write!(f, "`{}` should be a directory but isn't", path.display())
}
Vex2PdfError::InvalidFileStem(path) => write!(
f,
"failed to extract filename stem from `{}`",
path.display()
),
Vex2PdfError::Parse(message) => write!(f, "{message}"),
Vex2PdfError::UnsupportedFileType => write!(f, "Unsupported file type for parsing"),
Vex2PdfError::IgnoredByUser => write!(f, "file ignored explicitly by user"),
Vex2PdfError::VoluntaryLicenseDisplayInterruption => write!(
f,
"Voluntary interrupted execution for license display by user"
),
Vex2PdfError::ConcurrencyError(s) => write!(f, "Concurrency error : {s}"),
Vex2PdfError::ProcessingFailures(n) => {
write!(f, "{n} file(s) failed to process. See log for details")
}
}
}
}
impl std::error::Error for Vex2PdfError {}
impl From<io::Error> for Vex2PdfError {
fn from(value: io::Error) -> Self {
Vex2PdfError::Io(value)
}
}
impl<T> From<mpsc::SendError<T>> for Vex2PdfError {
fn from(value: mpsc::SendError<T>) -> Self {
Vex2PdfError::ConcurrencyError(format!(
"Attempted to send where there are no more receivers. {value}"
))
}
}