Function winnow::combinator::cond

source ·
pub fn cond<I, O, E: ParserError<I>, F>(
    b: bool,
    f: F,
) -> impl Parser<I, Option<O>, E>
where I: Stream, F: Parser<I, O, E>,
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)));