46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
use nom::error::ErrorKind;
|
|
use nom::error::ParseError;
|
|
use nom::IResult;
|
|
|
|
pub(crate) type Res<T, U> = IResult<T, U, CustomError<T>>;
|
|
|
|
#[derive(Debug)]
|
|
pub enum CustomError<I> {
|
|
MyError(MyError<&'static str>),
|
|
Nom(I, ErrorKind),
|
|
IO(std::io::Error),
|
|
BoxedError(Box<dyn std::error::Error>),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct MyError<I>(pub(crate) I);
|
|
|
|
impl<I> ParseError<I> for CustomError<I> {
|
|
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
|
|
CustomError::Nom(input, kind)
|
|
}
|
|
|
|
fn append(_input: I, _kind: ErrorKind, /*mut*/ other: Self) -> Self {
|
|
// Doesn't do append like VerboseError
|
|
other
|
|
}
|
|
}
|
|
|
|
impl<I> From<std::io::Error> for CustomError<I> {
|
|
fn from(value: std::io::Error) -> Self {
|
|
CustomError::IO(value)
|
|
}
|
|
}
|
|
|
|
impl<I> From<&'static str> for CustomError<I> {
|
|
fn from(value: &'static str) -> Self {
|
|
CustomError::MyError(MyError(value))
|
|
}
|
|
}
|
|
|
|
impl<I> From<Box<dyn std::error::Error>> for CustomError<I> {
|
|
fn from(value: Box<dyn std::error::Error>) -> Self {
|
|
CustomError::BoxedError(value)
|
|
}
|
|
}
|