Macro nom::parse_to

source ·
macro_rules! parse_to {
    ($i:expr, $t:ty ) => { ... };
}
Expand description

parse_to!(O) => I -> IResult<I, O> Uses the parse method from std::str::FromStr to convert the current input to the specified type.

This will completely consume the input.

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

named!(parser<&str, u8>, parse_to!(u8));

assert_eq!(parser("123"), Ok(("", 123)));

assert_eq!(parser("abc"), Err(Err::Error(Error::new("abc", ErrorKind::ParseTo))));

// this will fail if the mapped function fails (a `u8` is too small to hold `123456`)
assert_eq!(parser("123456"), Err(Err::Error(Error::new("123456", ErrorKind::ParseTo))));