use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io {
path: PathBuf,
source: std::io::Error,
},
NotFound(PathBuf),
Parse { path: PathBuf, message: String },
Table {
path: PathBuf,
row: usize,
message: String,
},
Deserialize(String),
Unsupported(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io { path, source } => write!(f, "{}: {source}", path.display()),
Error::NotFound(p) => write!(f, "source not found: {}", p.display()),
Error::Parse { path, message } => write!(f, "{}: {message}", path.display()),
Error::Table { path, row, message } => {
write!(f, "{} row {row}: {message}", path.display())
}
Error::Deserialize(m) => write!(f, "deserialize error: {m}"),
Error::Unsupported(m) => write!(f, "unsupported: {m}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io { source, .. } => Some(source),
_ => None,
}
}
}
pub(crate) fn io(path: &std::path::Path) -> impl FnOnce(std::io::Error) -> Error + '_ {
move |source| Error::Io {
path: path.to_path_buf(),
source,
}
}