Macro nom::map_opt [−][src]
macro_rules! map_opt {
(__impl $i : expr, $submac : ident! ($($args : tt) *), $submac2 : ident!
($($args2 : tt) *)) => { ... };
($i : expr, $submac : ident! ($($args : tt) *), $g : expr) => { ... };
($i : expr, $submac : ident! ($($args : tt) *), $submac2 : ident!
($($args2 : tt) *)) => { ... };
($i : expr, $f : expr, $g : expr) => { ... };
($i : expr, $f : expr, $submac : ident! ($($args : tt) *)) => { ... };
}
Expand description
map_opt!(I -> IResult<I, O>, O -> Option<P>) => I -> IResult<I, P>
maps a function returning an Option on the output of a parser
use nom::character::complete::digit1;
named!(parser<&str, u8>, map_opt!(digit1, |s: &str| s.parse::<u8>().ok()));
// the parser will convert the result of digit1 to a number
assert_eq!(parser("123"), Ok(("", 123)));
// this will fail if digit1 fails
assert_eq!(parser("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!(parser("123456"), Err(Err::Error(("123456", ErrorKind::MapOpt))));