• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
from(error: (&'a str, ErrorKind)) -> Self16   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 {
from_error_kind(_: &'a str, kind: ErrorKind) -> Self22   fn from_error_kind(_: &'a str, kind: ErrorKind) -> Self {
23     CustomError(format!("error code was: {:?}", kind))
24   }
25 
append(_: &'a str, kind: ErrorKind, other: CustomError) -> Self26   fn append(_: &'a str, kind: ErrorKind, other: CustomError) -> Self {
27     CustomError(format!("{:?}\nerror code was: {:?}", other, kind))
28   }
29 }
30 
test1(input: &str) -> IResult<&str, &str, CustomError>31 fn test1(input: &str) -> IResult<&str, &str, CustomError> {
32   //fix_error!(input, CustomError, tag!("abcd"))
33   tag("abcd")(input)
34 }
35 
test2(input: &str) -> IResult<&str, &str, CustomError>36 fn test2(input: &str) -> IResult<&str, &str, CustomError> {
37   //terminated!(input, test1, fix_error!(CustomError, digit))
38   terminated(test1, digit)(input)
39 }
40 
test3(input: &str) -> IResult<&str, &str, CustomError>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")]
test4(input: &str) -> IResult<&str, Vec<&str>, CustomError>46 fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> {
47   count(test1, 4)(input)
48 }
49