Commit
This commit is contained in:
parent
25adedfec2
commit
194e23a5be
31
src/lib.rs
31
src/lib.rs
@ -1,14 +1,35 @@
|
|||||||
pub fn add(left: usize, right: usize) -> usize {
|
#![feature(assert_matches)]
|
||||||
left + right
|
#![allow(dead_code)] //TODO eventually turn this off
|
||||||
|
|
||||||
|
|
||||||
|
type ParseResult<I, O, E> = Result<(O, I), E>;
|
||||||
|
|
||||||
|
trait Parser<I, O, E> {
|
||||||
|
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I, O, E, F> Parser<I, O, E> for F where F: Fn(I) -> ParseResult<I, O, E> {
|
||||||
|
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::assert_matches::assert_matches;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn parsing() {
|
||||||
let result = add(2, 2);
|
let output = literal("a")("a yolo");
|
||||||
assert_eq!(result, 4);
|
assert_matches!(output.unwrap(), ((), " yolo"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user