Skip to main content

oni_comb_parser/combinator/
flat_map.rs

1use crate::fail::PResult;
2use crate::input::Input;
3use crate::parser::Parser;
4
5pub struct FlatMap<P, F> {
6  pub(crate) parser: P,
7  pub(crate) f: F,
8}
9
10impl<I, P, F, P2> Parser<I> for FlatMap<P, F>
11where
12  I: Input,
13  P: Parser<I>,
14  P2: Parser<I, Error = P::Error>,
15  F: FnMut(P::Output) -> P2,
16{
17  type Error = P::Error;
18  type Output = P2::Output;
19
20  #[inline]
21  fn parse_next(&mut self, input: &mut I) -> PResult<Self::Output, Self::Error> {
22    let v = self.parser.parse_next(input)?;
23    let mut p2 = (self.f)(v);
24    p2.parse_next(input)
25  }
26}