Skip to content

Commit eac75c7

Browse files
committed
Migrated ToOptionParser to MapDecorator
1 parent 3d02d96 commit eac75c7

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

rusty_pc/src/to_option.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
use crate::{InputTrait, Parser, ParserErrorTrait};
1+
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
2+
use crate::{InputTrait, Parser};
23

34
pub struct ToOptionParser<P> {
45
parser: P,
56
}
7+
68
impl<P> ToOptionParser<P> {
79
pub(crate) fn new(parser: P) -> Self {
810
Self { parser }
911
}
1012
}
11-
impl<I, C, P> Parser<I, C> for ToOptionParser<P>
13+
14+
impl<I, C, P> MapDecorator<I, C> for ToOptionParser<P>
1215
where
1316
I: InputTrait,
1417
P: Parser<I, C>,
1518
{
19+
type OriginalOutput = P::Output;
1620
type Output = Option<P::Output>;
1721
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
2427
}
2528

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)
2835
}
2936
}
37+
38+
impl<P> MapDecoratorMarker for ToOptionParser<P> {}

0 commit comments

Comments
 (0)