pub fn one_of<I, T, Error: ParseError<I>, const STREAMING: bool>(
list: T
) -> impl Fn(I) -> IResult<I, <I as InputIter>::Item, Error>where
I: Slice<RangeFrom<usize>> + InputIter + InputLength + InputIsStreaming<STREAMING>,
<I as InputIter>::Item: Copy,
T: FindToken<<I as InputIter>::Item>,
Expand description
Returns a token that matches the pattern
Note: Parser
is implemented as a convenience (complete
only) for
u8
char
Complete version: Will return an error if there’s not enough input data.
Streaming version: Will return Err(nom8::Err::Incomplete(_))
if there’s not enough input data.
Example
assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("abc")("b"), Ok(("", 'b')));
assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf))));
assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("a")(""), Err(Err::Error(("", ErrorKind::OneOf))));
fn parser_fn(i: &str) -> IResult<&str, char> {
one_of(|c| c == 'a' || c == 'b')(i)
}
assert_eq!(parser_fn("abc"), Ok(("bc", 'a')));
assert_eq!(parser_fn("cd"), Err(Err::Error(Error::new("cd", ErrorKind::OneOf))));
assert_eq!(parser_fn(""), Err(Err::Error(Error::new("", ErrorKind::OneOf))));
assert_eq!(one_of::<_, _, (_, ErrorKind), true>("abc")(Streaming("b")), Ok((Streaming(""), 'b')));
assert_eq!(one_of::<_, _, (_, ErrorKind), true>("a")(Streaming("bc")), Err(Err::Error((Streaming("bc"), ErrorKind::OneOf))));
assert_eq!(one_of::<_, _, (_, ErrorKind), true>("a")(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
fn parser_fn(i: Streaming<&str>) -> IResult<Streaming<&str>, char> {
one_of(|c| c == 'a' || c == 'b')(i)
}
assert_eq!(parser_fn(Streaming("abc")), Ok((Streaming("bc"), 'a')));
assert_eq!(parser_fn(Streaming("cd")), Err(Err::Error(Error::new(Streaming("cd"), ErrorKind::OneOf))));
assert_eq!(parser_fn(Streaming("")), Err(Err::Incomplete(Needed::new(1))));