use super::ErrorKind;
use crate::parser::ErrorInput;
use nom::error::{ErrorKind as NomErrorKind, ParseError};
#[derive(Debug)]
pub(crate) struct InternalError<I, E>
where
I: ErrorInput,
E: std::error::Error + Send,
{
pub(crate) input: I,
pub(crate) error: ErrorKind<E>,
pub(crate) prev: Option<Box<Self>>,
}
impl<I, E> InternalError<I, E>
where
I: ErrorInput,
E: std::error::Error + Send,
{
pub(crate) fn new(input: I, error: ErrorKind<E>) -> Self {
Self {
input,
error,
prev: None,
}
}
pub(crate) fn from_kind(input: I, error: E) -> Self {
Self::new(input, ErrorKind::Other(error))
}
pub(crate) fn with_previous(mut self, prev: Self) -> Self {
self.prev = Some(Box::new(prev));
self
}
}
impl<I, E> ParseError<I> for InternalError<I, E>
where
I: ErrorInput,
E: std::error::Error + Send,
{
fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
Self::new(input, ErrorKind::Internal(GenericParseError(kind)))
}
fn append(input: I, kind: nom::error::ErrorKind, other: Self) -> Self {
Self::new(input, ErrorKind::Internal(GenericParseError(kind))).with_previous(other)
}
}
impl<I, E> nom::error::FromExternalError<I, Self> for InternalError<I, E>
where
I: ErrorInput,
E: std::error::Error + Send,
{
fn from_external_error(_input: I, _kind: nom::error::ErrorKind, error: Self) -> Self {
error
}
}
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
#[error("internal parsing error: {0:?}")]
pub struct GenericParseError(NomErrorKind);
impl GenericParseError {
pub(crate) fn new(kind: NomErrorKind) -> Self {
Self(kind)
}
}