pub fn cond<Input, Output, Error, ParseNext>(
cond: bool,
parser: ParseNext,
) -> impl Parser<Input, Option<Output>, Error>
Expand description
Calls the parser if the condition is met.
§Example
use winnow::combinator::cond;
use winnow::ascii::alpha1;
fn parser<'i>(i: &mut &'i str) -> ModalResult<Option<&'i str>> {
let prefix = opt("-").parse_next(i)?;
let condition = prefix.is_some();
cond(condition, alpha1).parse_next(i)
}
assert_eq!(parser.parse_peek("-abcd;"), Ok((";", Some("abcd"))));
assert_eq!(parser.parse_peek("abcd;"), Ok(("abcd;", None)));
assert!(parser.parse_peek("-123;").is_err());
assert_eq!(parser.parse_peek("123;"), Ok(("123;", None)));