use super::Position;
use std::{error::Error as StdError, fmt, io};
#[derive(Debug)]
pub enum Error {
IO(io::Error),
Syntax(Box<str>, Position),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::IO(err)
}
}
impl Error {
pub(super) fn syntax<M, P>(err: M, pos: P) -> Self
where
M: Into<Box<str>>,
P: Into<Position>,
{
Self::Syntax(err.into(), pos.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IO(e) => write!(f, "I/O error: {e}"),
Self::Syntax(e, pos) => write!(f, "Syntax Error: {e} at position: {pos}"),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::IO(err) => Some(err),
Self::Syntax(_, _) => None,
}
}
}