pub fn digit1<I, E: ParseError<I>>(
input: I
) -> IResult<I, <I as Stream>::Slice, E>where
I: StreamIsPartial + Stream,
<I as Stream>::Token: AsChar,
Expand description
Recognizes one or more ASCII numerical characters: 0-9
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non digit character).
Partial version: Will return Err(winnow::error::ErrMode::Incomplete(_))
if there’s not enough input data,
or if no terminating token is found (a non digit character).
Example
fn parser(input: &str) -> IResult<&str, &str> {
digit1(input)
}
assert_eq!(parser("21c"), Ok(("c", "21")));
assert_eq!(parser("c1"), Err(ErrMode::Backtrack(Error::new("c1", ErrorKind::Slice))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Slice))));
assert_eq!(digit1::<_, Error<_>>(Partial::new("21c")), Ok((Partial::new("c"), "21")));
assert_eq!(digit1::<_, Error<_>>(Partial::new("c1")), Err(ErrMode::Backtrack(Error::new(Partial::new("c1"), ErrorKind::Slice))));
assert_eq!(digit1::<_, Error<_>>(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1))));
Parsing an integer
You can use digit1
in combination with Parser::map_res
to parse an integer:
fn parser(input: &str) -> IResult<&str, u32> {
digit1.map_res(str::parse).parse_next(input)
}
assert_eq!(parser("416"), Ok(("", 416)));
assert_eq!(parser("12b"), Ok(("b", 12)));
assert!(parser("b").is_err());