#![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 parsing() { let output = literal("a")("a yolo"); assert_matches!(output.unwrap(), ((), " yolo")); } }