2020-05-30 09:55:38 +00:00
|
|
|
extern crate nom;
|
|
|
|
|
|
|
|
use nom::error::ErrorKind;
|
|
|
|
use nom::error::ParseError;
|
|
|
|
use nom::Err::Error;
|
|
|
|
use nom::IResult;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum CustomError<I> {
|
|
|
|
MyError,
|
|
|
|
Nom(I, ErrorKind),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I> ParseError<I> for CustomError<I> {
|
|
|
|
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
|
|
|
|
CustomError::Nom(input, kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append(_: I, _: ErrorKind, other: Self) -> Self {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 15:29:22 +00:00
|
|
|
pub fn parse(_input: &str) -> IResult<&str, &str, CustomError<&str>> {
|
2020-05-30 09:55:38 +00:00
|
|
|
Err(Error(CustomError::MyError))
|
|
|
|
}
|
|
|
|
|
2022-12-30 15:29:22 +00:00
|
|
|
fn main() {}
|
|
|
|
|
2020-05-30 09:55:38 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::parse;
|
|
|
|
use super::CustomError;
|
|
|
|
use nom::Err::Error;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
let err = parse("").unwrap_err();
|
|
|
|
match err {
|
|
|
|
Error(e) => assert_eq!(e, CustomError::MyError),
|
|
|
|
_ => panic!("Unexpected error: {:?}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|