Macro nom::map

source ·
macro_rules! map {
    (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { ... };
    ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { ... };
    ($i:expr, $f:expr, $g:expr) => { ... };
}
Expand description

map!(I -> IResult<I, O>, O -> P) => I -> IResult<I, P>

maps a function on the result of a parser

use nom::character::complete::digit1;

named!(parse<&str, usize>, map!(digit1, |s| s.len()));

// the parser will count how many characters were returned by digit1
assert_eq!(parse("123456"), Ok(("", 6)));

// this will fail if digit1 fails
assert_eq!(parse("abc"), Err(Err::Error(error_position!("abc", ErrorKind::Digit))));