parcos/combinators/
pred.rs1use crate::{error::Error, parser::Parser, stream::Streamable};
2
3pub struct Pred<F>(F);
6
7impl<I: Clone, F: Fn(&I) -> bool> Parser<I, I> for Pred<F> {
8 fn parse_impl<S: Streamable<I>>(
9 &self,
10 stream: &mut S,
11 _: &mut Vec<Error<I>>,
12 ) -> (usize, Result<I, Error<I>>)
13 where
14 Self: Sized,
15 {
16 match stream.peek() {
17 Some(i) if (self.0)(i) => (1, Ok(stream.next().unwrap())),
18 i => {
19 let i = i.cloned();
20 (0, Err(Error::Unexpected(stream.position(), vec![], i)))
21 }
22 }
23 }
24}
25
26pub fn pred<I, F: Fn(&I) -> bool>(f: F) -> Pred<F> {
37 Pred(f)
38}