nom::cond_reduce! [] [src]

macro_rules! cond_reduce(
  ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => (
    {
      if $cond {
        match $submac!($i, $($args)*) {
          $crate::IResult::Done(i,o)     => $crate::IResult::Done(i, o),
          $crate::IResult::Error(e)      => $crate::IResult::Error(e),
          $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
        }
      } else {
        $crate::IResult::Error($crate::Err::Position($crate::ErrorCode::CondReduce as u32, $i))
      }
    }
  );
  ($i:expr, $cond:expr, $f:expr) => (
    cond_reduce!($i, $cond, call!($f));
  );
);

cond_reduce!(bool, I -> IResult<I,O>) => I -> IResult<I, O> Conditional combinator with error

Wraps another parser and calls it if the condition is met. This combinator returns an error if the condition is false

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

 let b = true;
 let f = closure!(&'static[u8],
   cond_reduce!( b, tag!("abcd") )
 );

 let a = b"abcdef";
 assert_eq!(f(&a[..]), Done(&b"ef"[..], &b"abcd"[..]));

 let b2 = false;
 let f2 = closure!(&'static[u8],
   cond_reduce!( b2, tag!("abcd") )
 );
 assert_eq!(f2(&a[..]), Error(Err::Position(ErrorCode::CondReduce as u32, &a[..])));