Function nom::combinator::cond[][src]

pub fn cond<I: Clone, O, E: ParseError<I>, F>(
    b: bool,
    f: F
) -> impl Fn(I) -> IResult<I, Option<O>, E> where
    F: Fn(I) -> IResult<I, O, E>, 

calls the parser if the condition is met

use nom::combinator::cond;
use nom::character::complete::alpha1;

fn parser(b: bool, i: &str) -> IResult<&str, Option<&str>> {
  cond(b, alpha1)(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(("123;", ErrorKind::Alpha))));
assert_eq!(parser(false, "123;"), Ok(("123;", None)));