oni_comb_parser/combinator/
map_res.rs1use crate::error::ParseError;
2use crate::fail::{Fail, PResult};
3use crate::input::Input;
4use crate::parser::Parser;
5
6pub struct MapRes<P, F> {
7 pub(crate) parser: P,
8 pub(crate) f: F,
9 pub(crate) label: &'static str,
10}
11
12impl<I, P, F, O2, E2> Parser<I> for MapRes<P, F>
13where
14 I: Input,
15 P: Parser<I, Error = ParseError>,
16 F: FnMut(P::Output) -> Result<O2, E2>,
17{
18 type Error = ParseError;
19 type Output = O2;
20
21 #[inline]
22 fn parse_next(&mut self, input: &mut I) -> PResult<Self::Output, ParseError> {
23 let pos = input.offset();
24 let v = self.parser.parse_next(input)?;
25 match (self.f)(v) {
26 Ok(o) => Ok(o),
27 Err(_) => Err(Fail::Backtrack(ParseError::expected_description(pos, self.label))),
28 }
29 }
30}