Function nom::combinator::map_res
source · pub fn map_res<I: Clone, O1, O2, E: FromExternalError<I, E2>, E2, F, G>(
parser: F,
f: G
) -> impl FnMut(I) -> IResult<I, O2, E>where
F: Parser<I, O1, E>,
G: FnMut(O1) -> Result<O2, E2>,
Expand description
Applies a function returning a Result
over the result of a parser.
use nom::character::complete::digit1;
use nom::combinator::map_res;
let mut parse = map_res(digit1, |s: &str| s.parse::<u8>());
// the parser will convert the result of digit1 to a number
assert_eq!(parse("123"), Ok(("", 123)));
// this will fail if digit1 fails
assert_eq!(parse("abc"), Err(Err::Error(("abc", ErrorKind::Digit))));
// this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
assert_eq!(parse("123456"), Err(Err::Error(("123456", ErrorKind::MapRes))));