use std::fmt;
use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Parse {
offset: usize,
message: String,
},
Source {
root: PathBuf,
source: std::io::Error,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse { offset, message } => {
write!(f, "parse error at offset {offset}: {message}")
}
Self::Source { root, source } => {
write!(f, "cannot scan {}: {source}", root.display())
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Parse { .. } => None,
Self::Source { source, .. } => Some(source),
}
}
}
impl Error {
pub(crate) fn parse(offset: usize, message: impl Into<String>) -> Self {
Self::Parse {
offset,
message: message.into(),
}
}
}