Function cond

fn cond<I, E: ParseError<I>, F>(b: bool, f: F) -> impl Parser<I, Output = Option<<F as Parser<I>>::Output>, Error = E>
where
    F: Parser<I, Error = E>

Calls the parser if the condition is met.

# use nom::{Err, error::{Error, ErrorKind}, IResult, Parser};
use nom::combinator::cond;
use nom::character::complete::alpha1;
# fn main() {

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

assert_eq!(parser(true, "abcd;"), Ok((";", Some("abcd"))));
assert_eq!(parser(false, "abcd;"), Ok(("abcd;", None)));
assert_eq!(parser(true, "123;"), Err(Err::Error(Error::new("123;", ErrorKind::Alpha))));
assert_eq!(parser(false, "123;"), Ok(("123;", None)));
# }