Skip to content

Commit 1e84fdd

Browse files
committed
Migrated LazyParser to MapDecorator
1 parent 93fa688 commit 1e84fdd

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

rusty_pc/src/lazy.rs

Lines changed: 14 additions & 12 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 fn lazy<I, C, F, P>(factory: F) -> impl Parser<I, C, Output = P::Output, Error = P::Error>
@@ -17,28 +18,29 @@ struct LazyParser<F, P> {
1718
parser_holder: Option<P>,
1819
}
1920

20-
impl<I, C, F, P> Parser<I, C> for LazyParser<F, P>
21+
impl<I, C, F, P> MapDecorator<I, C> for LazyParser<F, P>
2122
where
2223
F: Fn() -> P,
2324
I: InputTrait,
2425
P: Parser<I, C>,
2526
{
27+
type OriginalOutput = P::Output;
2628
type Output = P::Output;
2729
type Error = P::Error;
2830

29-
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
30-
match self.parser_holder.as_mut() {
31-
Some(parser) => parser.parse(input),
32-
None => {
33-
let mut parser = (self.factory)();
34-
let result = parser.parse(input);
35-
self.parser_holder = Some(parser);
36-
result
37-
}
31+
fn decorated(
32+
&mut self,
33+
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
34+
if self.parser_holder.is_none() {
35+
let parser = (self.factory)();
36+
self.parser_holder = Some(parser);
3837
}
38+
self.parser_holder.as_mut().unwrap()
3939
}
4040

41-
fn set_context(&mut self, _ctx: &C) {
42-
unimplemented!()
41+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
42+
Ok(ok)
4343
}
4444
}
45+
46+
impl<F, P> MapDecoratorMarker for LazyParser<F, P> {}

0 commit comments

Comments
 (0)