From 194e23a5be24b6d2794df32512ec01102dd14722 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 15 Oct 2022 23:36:04 -0700 Subject: [PATCH] Commit --- src/lib.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..f411a62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,35 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +#![feature(assert_matches)] +#![allow(dead_code)] //TODO eventually turn this off + + +type ParseResult = Result<(O, I), E>; + +trait Parser { + fn parse(&self, input: I) -> ParseResult; +} + +impl Parser for F where F: Fn(I) -> ParseResult { + fn parse(&self, input: I) -> ParseResult { + self(input) + } +} + +fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, (), &str> { + move |input| match input.get(0..expected.len()) { + Some(next) if next == expected => + Ok(((), &input[expected.len()..])), + _ => Err(input) + } } #[cfg(test)] mod tests { use super::*; + use std::assert_matches::assert_matches; #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn parsing() { + let output = literal("a")("a yolo"); + assert_matches!(output.unwrap(), ((), " yolo")); } }