From 8b010811e30c3a67987c40d31cdcb06bccf7781a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 17 Oct 2022 00:37:55 -0700 Subject: [PATCH] one_of combinator --- src/primitives.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/primitives.rs b/src/primitives.rs index df5c25f..b322b81 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -21,6 +21,18 @@ pub fn any_char(input: &str) -> ParseResult<&str, char, &str> { } } +pub fn one_of<'a>(items: &'static str) -> impl Parser<&'a str, &'a str, &'a str> { + move |input: &'a str| { + if let Some(ch) = input.chars().nth(0) { + if items.contains(ch) { + let (first, rest) = input.split_at(1); + return Ok((first, rest)); + } + } + Err(input) + } +} + pub fn pred(parser: P, pred_fn: F) -> impl Parser where P: Parser,