[−][src]Macro nom::cond
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!
.
let b = true; let f: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], cond!( b, tag!("abcd") )) ); let a = b"abcdef"; assert_eq!(f(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..])))); let b2 = false; let f2:Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8], cond!( b2, tag!("abcd") )) ); assert_eq!(f2(&a[..]), Ok((&b"abcdef"[..], None)));