nom::opt_res! [] [src]

macro_rules! opt_res (
  ($i:expr, $submac:ident!( $($args:tt)* )) => (
    {
      match $submac!($i, $($args)*) {
        $crate::IResult::Done(i,o)     => $crate::IResult::Done(i,  Ok(o)),
        $crate::IResult::Error(e)      => $crate::IResult::Done($i, Err(e)),
        $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i)
      }
    }
  );
  ($i:expr, $f:expr) => (
    opt_res!($i, call!($f));
  );
);

opt_res!(I -> IResult<I,O>) => I -> IResult<I, Result<nom::Err,O>> make the underlying parser optional

returns a Result, with Err containing the parsing error

 named!( o<&[u8], Result<&[u8], nom::Err> >, opt_res!( tag!( "abcd" ) ) );

 let a = b"abcdef";
 let b = b"bcdefg";
 assert_eq!(o(&a[..]), Done(&b"ef"[..], Ok(&b"abcd"[..])));
 assert_eq!(o(b), Done(&b"bcdefg"[..], Err(Position(0, b))));