@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22
33use crate :: { InputTrait , Parser , ParserErrorTrait , SetContext } ;
44
5+ /// A parser that always succeeds, providing the value returned by the given function.
56pub fn supplier < I , C , F , O , E > ( f : F ) -> impl Parser < I , C , Output = O , Error = E > + SetContext < C >
67where
78 I : InputTrait ,
2728 }
2829}
2930
30- impl < C , P , F > SetContext < C > for SupplierParser < P , F > {
31+ impl < C , F , E > SetContext < C > for SupplierParser < F , E > {
32+ fn set_context ( & mut self , _ctx : C ) { }
33+ }
34+
35+ /// A parser that always fails, providing the value returned by the given function.
36+ pub fn err_supplier < I , C , F , O , E > ( f : F ) -> impl Parser < I , C , Output = O , Error = E > + SetContext < C >
37+ where
38+ I : InputTrait ,
39+ F : Fn ( ) -> E ,
40+ E : ParserErrorTrait ,
41+ {
42+ ErrSupplierParser ( f, PhantomData )
43+ }
44+
45+ struct ErrSupplierParser < F , O > ( F , PhantomData < O > ) ;
46+
47+ impl < I , C , F , O , E > Parser < I , C > for ErrSupplierParser < F , O >
48+ where
49+ I : InputTrait ,
50+ F : Fn ( ) -> E ,
51+ E : ParserErrorTrait ,
52+ {
53+ type Output = O ;
54+ type Error = E ;
55+
56+ fn parse ( & mut self , _input : & mut I ) -> Result < O , E > {
57+ Err ( ( self . 0 ) ( ) )
58+ }
59+ }
60+
61+ impl < C , F , O > SetContext < C > for ErrSupplierParser < F , O > {
3162 fn set_context ( & mut self , _ctx : C ) { }
3263}
0 commit comments