pub fn cut_err<Input, Output, Error, ParseNext>(
parser: ParseNext,
) -> impl Parser<Input, Output, Error>where
Input: Stream,
Error: ParserError<Input> + ModalError,
ParseNext: Parser<Input, Output, Error>,
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
.
See the [tutorial][crate::_tutorial::chapter_7] for more details.
§Example
Without cut_err
:
fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
alt((
preceded(one_of(['+', '-']), digit1),
rest
)).parse_next(input)
}
assert_eq!(parser.parse_peek("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser.parse_peek("ab"), Ok(("", "ab")));
assert_eq!(parser.parse_peek("+"), Ok(("", "+")));
With cut_err
:
use winnow::combinator::cut_err;
fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
alt((
preceded(one_of(['+', '-']), cut_err(digit1)),
rest
)).parse_next(input)
}
assert_eq!(parser.parse_peek("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser.parse_peek("ab"), Ok(("", "ab")));
assert_eq!(parser.parse_peek("+"), Err(ErrMode::Cut(ContextError::new())));