1 use winnow::error::ErrMode;
2 use winnow::error::ErrorKind;
3 use winnow::error::ParserError;
4 use winnow::prelude::*;
5
6 #[derive(Debug, PartialEq, Eq)]
7 pub enum CustomError<I> {
8 MyError,
9 Nom(I, ErrorKind),
10 }
11
12 impl<I: Clone> ParserError<I> for CustomError<I> {
from_error_kind(input: &I, kind: ErrorKind) -> Self13 fn from_error_kind(input: &I, kind: ErrorKind) -> Self {
14 CustomError::Nom(input.clone(), kind)
15 }
16
append(self, _: &I, _: ErrorKind) -> Self17 fn append(self, _: &I, _: ErrorKind) -> Self {
18 self
19 }
20 }
21
parse<'s>(_input: &mut &'s str) -> PResult<&'s str, CustomError<&'s str>>22 pub fn parse<'s>(_input: &mut &'s str) -> PResult<&'s str, CustomError<&'s str>> {
23 Err(ErrMode::Backtrack(CustomError::MyError))
24 }
25
main()26 fn main() {}
27
28 #[cfg(test)]
29 mod tests {
30 use super::*;
31
32 #[test]
it_works()33 fn it_works() {
34 let err = parse.parse_next(&mut "").unwrap_err();
35 match err {
36 ErrMode::Backtrack(e) => assert_eq!(e, CustomError::MyError),
37 _ => panic!("Unexpected error: {:?}", err),
38 }
39 }
40 }
41