[][src]Macro nom::cond

macro_rules! cond {
    ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { ... };
    ($i:expr, $cond:expr, $f:expr) => { ... };

cond!(bool, I -> IResult<I,O>) => I -> IResult<I, Option<O>> Conditional combinator

Wraps another parser and calls it if the condition is met. This combinator returns an Option of the return type of the child parser.

This is especially useful if a parser depends on the value returned by a preceding parser in a do_parse!.

 fn f_true(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> {
   cond!(i, true, tag!("abcd"))

 fn f_false(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> {
   cond!(i, false, tag!("abcd"))

 let a = b"abcdef";
 assert_eq!(f_true(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..]))));

 assert_eq!(f_false(&a[..]), Ok((&b"abcdef"[..], None)));