[−][src]Macro nom::try_parse
A bit like std::try!
, this macro will return the remaining input and
parsed value if the child parser returned Ok
, and will do an early
return for the Err
side.
this can provide more flexibility than do_parse!
if needed
fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> { let (i1, sz) = try_parse!(input, nom::be_u8); let (i2, length) = try_parse!(i1, expr_opt!(size.checked_add(sz))); let (i3, data) = try_parse!(i2, take!(length)); return Ok((i3, 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))));