Function winnow::combinator::cut_err

source ·
pub fn cut_err<I, O, E: ParserError<I>, F>(parser: F) -> impl Parser<I, O, E>
where I: Stream, F: Parser<I, O, E>,
Expand description

Transforms an ErrMode::Backtrack (recoverable) to ErrMode::Cut (unrecoverable)

This commits the parse result, preventing alternative branch paths like with winnow::combinator::alt.

§Example

Without cut_err:


fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of(['+', '-']), digit1),
    rest
  )).parse_peek(input)
}

assert_eq!(parser("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser("ab"), Ok(("", "ab")));
assert_eq!(parser("+"), Ok(("", "+")));

With cut_err:

use winnow::combinator::cut_err;

fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of(['+', '-']), cut_err(digit1)),
    rest
  )).parse_peek(input)
}

assert_eq!(parser("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser("ab"), Ok(("", "ab")));
assert_eq!(parser("+"), Err(ErrMode::Cut(InputError::new("", ErrorKind::Slice ))));