changeset 11:11802adb59ae

Implement Parser::apply
author Lewin Bormann <lewin@lewin-bormann.info>
date Sun, 02 Jun 2019 13:48:26 +0200
parents 61a0e5bc6f6c
children a37d7c2aa256
files src/parser.rs src/primitives.rs
diffstat 2 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/parser.rs	Sun Jun 02 13:47:52 2019 +0200
+++ b/src/parser.rs	Sun Jun 02 13:48:26 2019 +0200
@@ -1,5 +1,6 @@
 use std::fmt;
 
+use crate::combinators;
 use crate::state::ParseState;
 
 #[derive(Debug, PartialEq)]
@@ -28,4 +29,15 @@
         &mut self,
         st: &mut ParseState<impl Iterator<Item = char>>,
     ) -> ParseResult<Self::Result>;
+
+    /// apply transforms the result of this parser using a Transform combinator.
+    fn apply<R2, F: Fn(Self::Result) -> ParseResult<R2>>(
+        self,
+        f: F,
+    ) -> combinators::Transform<Self::Result, R2, Self, F>
+    where
+        Self: std::marker::Sized,
+    {
+        combinators::Transform::new(self, f)
+    }
 }
--- a/src/primitives.rs	Sun Jun 02 13:47:52 2019 +0200
+++ b/src/primitives.rs	Sun Jun 02 13:48:26 2019 +0200
@@ -1,4 +1,4 @@
-use crate::combinators::{Repeat, RepeatSpec, Transform};
+use crate::combinators::{Repeat, RepeatSpec};
 use crate::parser::{ParseError, ParseResult, Parser};
 use crate::state::ParseState;
 
@@ -108,7 +108,7 @@
     let oo = OneOf::new(chars);
     let rp = Repeat::new(oo, rp);
     let make_string = |charvec: Vec<char>| Ok(String::from_iter(charvec.into_iter()));
-    Transform::new(rp, make_string)
+    rp.apply(make_string)
 }
 
 #[cfg(test)]