| 1 |
#![allow(dead_code)] |
| 2 |
|
| 3 |
use nom::bytes::streaming::tag; |
| 4 |
use nom::character::streaming::digit1 as digit; |
| 5 |
use nom::combinator::verify; |
| 6 |
use nom::error::{ErrorKind, ParseError}; |
| 7 |
#[cfg(feature = "alloc")] |
| 8 |
use nom::multi::count; |
| 9 |
use nom::sequence::terminated; |
| 10 |
use nom::IResult; |
| 11 |
|
| 12 |
#[derive(Debug)] |
| 13 |
pub struct CustomError(String); |
| 14 |
|
| 15 |
impl<'a> From<(&'a str, ErrorKind)> for CustomError { |
| 16 |
fn from(error: (&'a str, ErrorKind)) -> Self { |
| 17 |
CustomError(format!("error code was: {:?}", error)) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<'a> ParseError<&'a str> for CustomError { |
| 22 |
fn from_error_kind(_: &'a str, kind: ErrorKind) -> Self { |
| 23 |
CustomError(format!("error code was: {:?}", kind)) |
| 24 |
} |
| 25 |
|
| 26 |
fn append(_: &'a str, kind: ErrorKind, other: CustomError) -> Self { |
| 27 |
CustomError(format!("{:?}\nerror code was: {:?}", other, kind)) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
fn test1(input: &str) -> IResult<&str, &str, CustomError> { |
| 32 |
//fix_error!(input, CustomError, tag!("abcd")) |
| 33 |
tag("abcd")(input) |
| 34 |
} |
| 35 |
|
| 36 |
fn test2(input: &str) -> IResult<&str, &str, CustomError> { |
| 37 |
//terminated!(input, test1, fix_error!(CustomError, digit)) |
| 38 |
terminated(test1, digit)(input) |
| 39 |
} |
| 40 |
|
| 41 |
fn test3(input: &str) -> IResult<&str, &str, CustomError> { |
| 42 |
verify(test1, |s: &str| s.starts_with("abcd"))(input) |
| 43 |
} |
| 44 |
|
| 45 |
#[cfg(feature = "alloc")] |
| 46 |
fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> { |
| 47 |
count(test1, 4)(input) |
| 48 |
} |