1+ use crate :: map_decorator:: { MapDecorator , MapDecoratorMarker } ;
12use crate :: { InputTrait , Parser } ;
23
34pub 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 >
2122where
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