Macro 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
Useful when doing computations in a chain
fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> { do_parse!(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, Ok((&[4,5][..], &[2,3][..]))); let arr2 = [0xFE, 2, 3, 4, 5]; // size is overflowing let r1 = take_add(&arr2[..], 42); assert_eq!(r1, Err(Err::Error(error_position!(&[2,3,4,5][..], ErrorKind::ExprOpt))));