nom::expr_opt! [] [src]

macro_rules! expr_opt {
    ($i:expr, $e:expr) => { ... };
}

expr_opt!(Option<O>) => I -> IResult<I, O> evaluate an expression that returns a Option and returns a IResult::Done(I,T) if Ok

Useful when doing computations in a chain


 fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> {
   chain!(input,
     sz:     be_u8                             ~
     length: expr_opt!(size.checked_add(sz))   ~ // checking for integer overflow (returns an Option)
     data:   take!(length)                     ,
     ||{ data }
   )
 }
let arr1 = [1, 2, 3, 4, 5];
let r1 = take_add(&arr1[..], 1);
assert_eq!(r1, Done(&[4,5][..], &[2,3][..]));

let arr2 = [0xFE, 2, 3, 4, 5];
// size is overflowing
let r1 = take_add(&arr2[..], 42);
assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..])));