use std::{
error::Error as StdError,
fmt::{self, Display},
};
use winnow::error::ContextError;
#[derive(Debug)]
pub enum Error {
ParseError(Context),
EmptyTemplate,
}
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ParseError(error) => {
write!(f, "parsing stopped at byte offset `{}`", error.offset)?;
if error.inner.context().next().is_some() {
write!(f, " ({})", error.inner)?;
}
}
Self::EmptyTemplate => {
write!(f, "Template is empty")?;
}
}
Ok(())
}
}
impl Error {
pub(crate) fn parse_error(offset: usize, inner: ContextError) -> Self {
Self::ParseError(Context { offset, inner })
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::ParseError(error) => error.inner.cause().map(|v| v as &(dyn StdError + 'static)),
Self::EmptyTemplate => None,
}
}
}
#[derive(Debug)]
pub struct Context {
pub(crate) offset: usize,
pub(crate) inner: ContextError,
}