Function winnow::combinator::cond

source ·
pub fn cond<Input, Output, Error, ParseNext>(
    cond: bool,
    parser: ParseNext
) -> impl Parser<Input, Option<Output>, Error>
where Input: Stream, ParseNext: Parser<Input, Output, Error>, Error: ParserError<Input>,
Expand description

Calls the parser if the condition is met.

§Example

use winnow::combinator::cond;
use winnow::ascii::alpha1;

fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> {
  cond(b, alpha1).parse_peek(i)
}

assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd"))));
assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None)));
assert_eq!(parser(true, "123;"), Err(ErrMode::Backtrack(InputError::new("123;", ErrorKind::Slice))));
assert_eq!(parser(false, "123;"), Ok(("123;", None)));