Function chumsky::primitive::filter_map
source · pub fn filter_map<I, O, F: Fn(E::Span, I) -> Result<O, E>, E: Error<I>>(
f: F
) -> FilterMap<F, E>
Expand description
A parser that accepts a input and tests it against the given fallible function.
This function allows integration with custom error types to allow for custom parser errors.
Before using this function, consider whether the select
macro would serve you better.
The output type of this parser is I
, the input that was found.
Examples
let numeral = filter_map(|span, c: char| match c.to_digit(10) {
Some(x) => Ok(x),
None => Err(Simple::custom(span, format!("'{}' is not a digit", c))),
});
assert_eq!(numeral.parse("3"), Ok(3));
assert_eq!(numeral.parse("7"), Ok(7));
assert_eq!(numeral.parse("f"), Err(vec![Simple::custom(0..1, "'f' is not a digit")]));