summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-10-25 01:53:24 -0400
committerJohn Turner <jturner.usa@gmail.com>2025-10-25 01:53:24 -0400
commit6b7268a53df5404ab58223c13203eb873db61f1a (patch)
tree09f611e4c98886d0d7d91de3549cb8c7408f22c3
parentbcd42bd242a26646b31e472e2961312ed00a4774 (diff)
downloadmon-6b7268a53df5404ab58223c13203eb873db61f1a.tar.gz
create VerifyOutput combinator
-rw-r--r--src/lib.rs43
1 files changed, 39 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 33d5f34..d0b9cb3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -171,11 +171,21 @@ pub trait Parser<I: Input>: Sized {
}
}
- fn verify<C>(self, checker: C) -> impl Parser<I, Output = Self::Output>
+ fn verify_input<C>(self, checker: C) -> impl Parser<I, Output = Self::Output>
where
C: Parser<I>,
{
- Verify {
+ VerifyInput {
+ parser: self,
+ checker,
+ }
+ }
+
+ fn verify_output<F>(self, checker: F) -> impl Parser<I, Output = Self::Output>
+ where
+ F: Fn(&Self::Output) -> bool,
+ {
+ VerifyOutput {
parser: self,
checker,
}
@@ -827,12 +837,12 @@ where
Opt { parser }
}
-struct Verify<P, C> {
+struct VerifyInput<P, C> {
parser: P,
checker: C,
}
-impl<I, P, C> Parser<I> for Verify<P, C>
+impl<I, P, C> Parser<I> for VerifyInput<P, C>
where
I: Input,
P: Parser<I>,
@@ -854,6 +864,31 @@ where
}
}
+struct VerifyOutput<P, F> {
+ parser: P,
+ checker: F,
+}
+
+impl<I, P, F> Parser<I> for VerifyOutput<P, F>
+where
+ I: Input,
+ P: Parser<I>,
+ F: Fn(&P::Output) -> bool,
+{
+ type Output = P::Output;
+
+ fn run<OM: Mode, EM: Mode, Tracer: Trace<I>>(
+ &self,
+ it: InputIter<I>,
+ ) -> ParserResult<I, Self::Output, OM, EM> {
+ match self.parser.parse(it.clone()) {
+ Ok((rest, output)) if (self.checker)(&output) => Ok((rest, OM::bind(|| output))),
+ Ok(_) => Err(EM::bind(|| it)),
+ Err(rest) => Err(EM::bind(|| rest)),
+ }
+ }
+}
+
struct Ignore<P> {
parser: P,
}