rocket::mtls::oid::asn1_rs::nom::combinator

Function cut

Source
pub fn cut<I, O, E, F>(parser: F) -> impl FnMut(I)
where E: ParseError<I>, F: Parser<I, O, E>,
Available on crate feature mtls only.
Expand description

Transforms an Err::Error (recoverable) to Err::Failure (unrecoverable)

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

ยงExample

Without cut:


fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of("+-"), digit1),
    rest
  ))(input)
}

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

With cut:

use nom::combinator::cut;

fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of("+-"), cut(digit1)),
    rest
  ))(input)
}

assert_eq!(parser("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser("ab"), Ok(("", "ab")));
assert_eq!(parser("+"), Err(Err::Failure(Error { input: "", code: ErrorKind::Digit })));