Make map a method

This commit is contained in:
Greg Shuflin 2022-10-16 00:54:41 -07:00
parent d707249904
commit 981b5c08d0
1 changed files with 18 additions and 6 deletions

View File

@ -6,16 +6,28 @@ type ParseResult<I, O, E> = Result<(O, I), E>;
trait Parser<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn map<'a, F, O2>(self, map_fn: F) -> BoxedParser<'a, I, O2, E>
where
Self: Sized + 'a,
I: 'a,
E: 'a,
O: 'a,
O2: 'a,
F: Fn(O) -> O2 + 'a,
{
BoxedParser::new(map(self, map_fn))
}
}
struct BoxedParser<I, O, E> {
inner: Box<dyn Parser<I, O, E>>,
struct BoxedParser<'a, I, O, E> {
inner: Box<dyn Parser<I, O, E> + 'a>,
}
impl<I, O, E> BoxedParser<I, O, E> {
impl<'a, I, O, E> BoxedParser<'a, I, O, E> {
fn new<P>(inner: P) -> Self
where
P: Parser<I, O, E> + 'static,
P: Parser<I, O, E> + 'a,
{
BoxedParser {
inner: Box::new(inner),
@ -23,7 +35,7 @@ impl<I, O, E> BoxedParser<I, O, E> {
}
}
impl<I, O, E> Parser<I, O, E> for BoxedParser<I, O, E> {
impl<'a, I, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)
}
@ -178,7 +190,7 @@ mod tests {
#[test]
fn test_map() {
let lit_a = literal("a");
let output = map(lit_a, |s| s.to_uppercase()).parse("a yolo");
let output = lit_a.map(|s| s.to_uppercase()).parse("a yolo");
assert_matches!(output.unwrap(), (s, " yolo") if s == "A");
}