2023-04-11 14:50:37 -04:00
|
|
|
use nom::error::ErrorKind;
|
|
|
|
use nom::error::ParseError;
|
|
|
|
use nom::IResult;
|
|
|
|
|
2023-09-11 13:13:28 -04:00
|
|
|
pub(crate) type Res<T, U> = IResult<T, U, CustomError<T>>;
|
2023-04-11 14:50:37 -04:00
|
|
|
|
2023-09-04 16:53:02 -04:00
|
|
|
#[derive(Debug)]
|
2023-09-11 13:13:28 -04:00
|
|
|
pub enum CustomError<I> {
|
2023-09-29 11:26:23 -04:00
|
|
|
MyError(MyError<&'static str>),
|
2023-04-11 14:50:37 -04:00
|
|
|
Nom(I, ErrorKind),
|
2023-09-04 16:53:02 -04:00
|
|
|
IO(std::io::Error),
|
2023-10-02 15:49:51 -04:00
|
|
|
BoxedError(Box<dyn std::error::Error>),
|
2023-04-11 14:50:37 -04:00
|
|
|
}
|
|
|
|
|
2023-09-04 16:53:02 -04:00
|
|
|
#[derive(Debug)]
|
2023-09-11 13:13:28 -04:00
|
|
|
pub struct MyError<I>(pub(crate) I);
|
2023-04-11 14:50:37 -04:00
|
|
|
|
|
|
|
impl<I> ParseError<I> for CustomError<I> {
|
|
|
|
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
|
|
|
|
CustomError::Nom(input, kind)
|
|
|
|
}
|
|
|
|
|
2023-04-21 18:42:31 -04:00
|
|
|
fn append(_input: I, _kind: ErrorKind, /*mut*/ other: Self) -> Self {
|
2023-04-11 14:50:37 -04:00
|
|
|
// Doesn't do append like VerboseError
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
2023-09-04 16:53:02 -04:00
|
|
|
|
|
|
|
impl<I> From<std::io::Error> for CustomError<I> {
|
|
|
|
fn from(value: std::io::Error) -> Self {
|
|
|
|
CustomError::IO(value)
|
|
|
|
}
|
|
|
|
}
|
2023-09-29 11:26:23 -04:00
|
|
|
|
|
|
|
impl<I> From<&'static str> for CustomError<I> {
|
|
|
|
fn from(value: &'static str) -> Self {
|
|
|
|
CustomError::MyError(MyError(value))
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 15:49:51 -04:00
|
|
|
|
|
|
|
impl<I> From<Box<dyn std::error::Error>> for CustomError<I> {
|
|
|
|
fn from(value: Box<dyn std::error::Error>) -> Self {
|
|
|
|
CustomError::BoxedError(value)
|
|
|
|
}
|
|
|
|
}
|