use nom::error::{ContextError, ErrorKind, ParseError};
pub trait NomErrExt<E> {
fn unwrap_error(self) -> E;
}
impl<E> NomErrExt<E> for nom::Err<E> {
fn unwrap_error(self) -> E {
match self {
nom::Err::Incomplete(needed) => panic!("unwrap_error on Incomplete: {needed:?}"),
nom::Err::Error(e) => e,
nom::Err::Failure(e) => e,
}
}
}
pub trait ParseErrorExt<I>
where
Self: Sized
{
fn from_context(input: I, ctx: &'static str) -> Self;
fn into_err(self) -> nom::Err<Self> {
nom::Err::Error(self)
}
}
impl<T, I> ParseErrorExt<I> for T
where
T: ParseError<I> + ContextError<I>,
I: Clone,
{
fn from_context(input: I, ctx: &'static str) -> Self {
ContextError::add_context(
input.clone(),
ctx,
ParseError::from_error_kind(input, ErrorKind::Fail),
)
}
}