Skip to main content

oni_comb_parser/combinator/
map_res.rs

1use crate::error::{ExpectError, Expected};
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>,
16  P::Error: ExpectError,
17  F: FnMut(P::Output) -> Result<O2, E2>,
18{
19  type Error = P::Error;
20  type Output = O2;
21
22  #[inline]
23  fn parse_next(&mut self, input: &mut I) -> PResult<Self::Output, Self::Error> {
24    let pos = input.offset();
25    let v = self.parser.parse_next(input)?;
26    match (self.f)(v) {
27      Ok(o) => Ok(o),
28      Err(_) => Err(Fail::Backtrack(Self::Error::from_expected(
29        pos,
30        Expected::Description(self.label),
31      ))),
32    }
33  }
34}