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