|
1 | | -use crate::{InputTrait, Parser, ParserErrorTrait}; |
| 1 | +use crate::map_decorator::{MapDecorator, MapDecoratorMarker}; |
| 2 | +use crate::{InputTrait, Parser}; |
2 | 3 |
|
3 | 4 | pub struct OrDefaultParser<P> { |
4 | 5 | parser: P, |
5 | 6 | } |
| 7 | + |
6 | 8 | impl<P> OrDefaultParser<P> { |
7 | 9 | pub(crate) fn new(parser: P) -> Self { |
8 | 10 | Self { parser } |
9 | 11 | } |
10 | 12 | } |
11 | | -impl<I, C, P> Parser<I, C> for OrDefaultParser<P> |
| 13 | + |
| 14 | +impl<I, C, P> MapDecorator<I, C> for OrDefaultParser<P> |
12 | 15 | where |
13 | 16 | I: InputTrait, |
14 | 17 | P: Parser<I, C>, |
15 | 18 | P::Output: Default, |
16 | 19 | { |
| 20 | + type OriginalOutput = P::Output; |
17 | 21 | type Output = P::Output; |
18 | 22 | type Error = P::Error; |
19 | | - fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> { |
20 | | - match self.parser.parse(input) { |
21 | | - Ok(value) => Ok(value), |
22 | | - Err(err) if err.is_soft() => Ok(P::Output::default()), |
23 | | - Err(err) => Err(err), |
24 | | - } |
| 23 | + |
| 24 | + fn decorated( |
| 25 | + &mut self, |
| 26 | + ) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> { |
| 27 | + &mut self.parser |
25 | 28 | } |
26 | 29 |
|
27 | | - fn set_context(&mut self, ctx: &C) { |
28 | | - self.parser.set_context(ctx) |
| 30 | + fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output { |
| 31 | + ok |
| 32 | + } |
| 33 | + |
| 34 | + fn map_soft_error(&self, _err: Self::Error) -> Result<Self::Output, Self::Error> { |
| 35 | + Ok(P::Output::default()) |
29 | 36 | } |
30 | 37 | } |
| 38 | + |
| 39 | +impl<P> MapDecoratorMarker for OrDefaultParser<P> {} |
0 commit comments