#[derive(Debug)]
pub enum ParseError {
Eof,
Io(std::io::Error),
InvalidLine,
EmptyCharClass,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParseError::Eof => write!(f, "end of data"),
ParseError::Io(e) => write!(f, "io failure: {}", e),
ParseError::InvalidLine => write!(f, "invalid line"),
ParseError::EmptyCharClass => write!(f, "empty char class"),
}
}
}
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let ParseError::Io(ioe) = self {
Some(ioe)
} else {
None
}
}
}
#[derive(Debug)]
pub enum Error {
Parse(ParseError),
InFile(std::path::PathBuf, usize, ParseError),
InvalidCwd(std::io::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Parse(error) => write!(f, "{}", error),
Error::InFile(path, line, error) => {
write!(f, "{}:{}: {}", path.to_string_lossy(), line, error)
}
Error::InvalidCwd(ioe) => write!(f, "invalid cwd: {}", ioe),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Parse(pe) | Error::InFile(_, _, pe) => pe.source(),
Error::InvalidCwd(ioe) => Some(ioe),
}
}
}