Skip to content

Commit 93fa688

Browse files
committed
Migrated AndThenParser to MapDecorator
1 parent 06e9d37 commit 93fa688

6 files changed

Lines changed: 28 additions & 17 deletions

File tree

rusty_pc/src/and_then.rs

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

34
pub struct AndThenParser<P, F> {
@@ -11,19 +12,25 @@ impl<P, F> AndThenParser<P, F> {
1112
}
1213
}
1314

14-
impl<I, C, P, F, U> Parser<I, C> for AndThenParser<P, F>
15+
impl<I, C, P, F, U> MapDecorator<I, C> for AndThenParser<P, F>
1516
where
1617
I: InputTrait,
1718
P: Parser<I, C>,
1819
F: Fn(P::Output) -> Result<U, P::Error>,
1920
{
21+
type OriginalOutput = P::Output;
2022
type Output = U;
2123
type Error = P::Error;
22-
fn parse(&mut self, tokenizer: &mut I) -> Result<Self::Output, Self::Error> {
23-
self.parser.parse(tokenizer).and_then(&self.mapper)
24+
25+
fn decorated(
26+
&mut self,
27+
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
28+
&mut self.parser
2429
}
2530

26-
fn set_context(&mut self, ctx: &C) {
27-
self.parser.set_context(ctx)
31+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
32+
(self.mapper)(ok)
2833
}
2934
}
35+
36+
impl<P, F> MapDecoratorMarker for AndThenParser<P, F> {}

rusty_pc/src/and_then_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ where
2828
&mut self.parser
2929
}
3030

31-
fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output {
32-
ok
31+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
32+
Ok(ok)
3333
}
3434

3535
fn map_soft_error(&self, err: Self::Error) -> Result<Self::Output, Self::Error> {

rusty_pc/src/map.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ where
2626
&mut self.parser
2727
}
2828

29-
fn map_ok(&self, ok: P::Output) -> U {
30-
(self.mapper)(ok)
29+
fn map_ok(&self, ok: P::Output) -> Result<Self::Output, Self::Error> {
30+
Ok((self.mapper)(ok))
3131
}
3232
}
3333

@@ -57,7 +57,9 @@ where
5757
&mut self.parser
5858
}
5959

60-
fn map_ok(&self, _ok: P::Output) {}
60+
fn map_ok(&self, _ok: P::Output) -> Result<Self::Output, Self::Error> {
61+
Ok(())
62+
}
6163
}
6264

6365
impl<P> MapDecoratorMarker for MapToUnitParser<P> {}

rusty_pc/src/map_decorator.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{InputTrait, Parser, ParserErrorTrait};
22

3-
/// A parser decorator that maps the result of the decorated parser.
3+
/// A parser decorator that maps the successful result
4+
/// and optionally the soft error
5+
/// of the decorated parser.
46
pub trait MapDecorator<I, C>
57
where
68
I: InputTrait,
@@ -15,7 +17,7 @@ where
1517
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error>;
1618

1719
/// Maps the successful result of the parser.
18-
fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output;
20+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error>;
1921

2022
/// Maps the soft error of the parser.
2123
/// By default, the error is returned as-is. However, it is possible to override this behavior.
@@ -38,7 +40,7 @@ where
3840

3941
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
4042
match self.decorated().parse(input) {
41-
Ok(ok) => Ok(self.map_ok(ok)),
43+
Ok(ok) => self.map_ok(ok),
4244
Err(err) if err.is_soft() => self.map_soft_error(err),
4345
Err(err) => Err(err),
4446
}

rusty_pc/src/or_default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ where
2727
&mut self.parser
2828
}
2929

30-
fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output {
31-
ok
30+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
31+
Ok(ok)
3232
}
3333

3434
fn map_soft_error(&self, _err: Self::Error) -> Result<Self::Output, Self::Error> {

rusty_pc/src/to_option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ where
2626
&mut self.parser
2727
}
2828

29-
fn map_ok(&self, ok: Self::OriginalOutput) -> Self::Output {
30-
Some(ok)
29+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
30+
Ok(Some(ok))
3131
}
3232

3333
fn map_soft_error(&self, _err: Self::Error) -> Result<Self::Output, Self::Error> {

0 commit comments

Comments
 (0)