Skip to content

Commit c57f68f

Browse files
committed
feat(pc): Introducing err_supplier
1 parent f26b7aa commit c57f68f

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

rusty_parser/src/core/var_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn bare<T>()
190190
where
191191
T: Default + VarType,
192192
{
193-
supplier::supplier(|| T::default())
193+
supplier(T::default)
194194
}
195195

196196
fn name_with_opt_array<P>(

rusty_pc/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod or_default;
2020
mod parser;
2121
pub mod peek;
2222
mod seq;
23-
pub mod supplier;
23+
mod supplier;
2424
mod surround;
2525
pub mod text;
2626
mod then_with;
@@ -35,6 +35,7 @@ pub use lazy::*;
3535
pub use or::*;
3636
pub use parser::*;
3737
pub use seq::*;
38+
pub use supplier::*;
3839
pub use surround::*;
3940
pub use then_with::*;
4041
pub use then_with_left::*;

rusty_pc/src/supplier.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22

33
use crate::{InputTrait, Parser, ParserErrorTrait, SetContext};
44

5+
/// A parser that always succeeds, providing the value returned by the given function.
56
pub fn supplier<I, C, F, O, E>(f: F) -> impl Parser<I, C, Output = O, Error = E> + SetContext<C>
67
where
78
I: InputTrait,
@@ -27,6 +28,36 @@ where
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

Comments
 (0)