Skip to content

Commit 06e9d37

Browse files
committed
Migrated AndThenErrParser to MapDecorator
1 parent eac75c7 commit 06e9d37

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

rusty_pc/src/and_then_err.rs

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

34
pub struct AndThenErrParser<P, F> {
45
parser: P,
@@ -11,23 +12,29 @@ impl<P, F> AndThenErrParser<P, F> {
1112
}
1213
}
1314

14-
impl<I, C, P, F> Parser<I, C> for AndThenErrParser<P, F>
15+
impl<I, C, P, F> MapDecorator<I, C> for AndThenErrParser<P, F>
1516
where
1617
I: InputTrait,
1718
P: Parser<I, C>,
1819
F: Fn(P::Error) -> Result<P::Output, P::Error>,
1920
{
21+
type OriginalOutput = P::Output;
2022
type Output = P::Output;
2123
type Error = P::Error;
22-
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
23-
match self.parser.parse(input) {
24-
Ok(value) => Ok(value),
25-
Err(err) if err.is_soft() => (self.mapper)(err),
26-
Err(err) => Err(err),
27-
}
24+
25+
fn decorated(
26+
&mut self,
27+
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
28+
&mut self.parser
29+
}
30+
31+
fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output {
32+
ok
2833
}
2934

30-
fn set_context(&mut self, ctx: &C) {
31-
self.parser.set_context(ctx)
35+
fn map_soft_error(&self, err: Self::Error) -> Result<Self::Output, Self::Error> {
36+
(self.mapper)(err)
3237
}
3338
}
39+
40+
impl<P, F> MapDecoratorMarker for AndThenErrParser<P, F> {}

0 commit comments

Comments
 (0)