Map
This commit is contained in:
parent
cbb30d3e9f
commit
41829019b6
12
src/lib.rs
12
src/lib.rs
@ -1,9 +1,11 @@
|
||||
#![allow(dead_code)] //TODO eventually turn this off
|
||||
mod choice;
|
||||
mod map;
|
||||
mod primitives;
|
||||
mod sequence;
|
||||
|
||||
pub use choice::*;
|
||||
pub use map::*;
|
||||
pub use primitives::*;
|
||||
pub use sequence::*;
|
||||
|
||||
@ -53,4 +55,14 @@ mod tests {
|
||||
let output = parser.parse("ara hajimete").unwrap();
|
||||
assert_eq!(("ara", " hajimete"), output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map() {
|
||||
let parser = map(
|
||||
sequence(literal("a"), literal("b")),
|
||||
|(_a, _b): (&str, &str)| 59,
|
||||
);
|
||||
let output = parser.parse("abcd").unwrap();
|
||||
assert_eq!((59, "cd"), output);
|
||||
}
|
||||
}
|
||||
|
13
src/map.rs
Normal file
13
src/map.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use crate::Parser;
|
||||
|
||||
pub fn map<P, F, I, O1, O2, E>(parser: P, map_fn: F) -> impl Parser<I, O2, E>
|
||||
where
|
||||
P: Parser<I, O1, E>,
|
||||
F: Fn(O1) -> O2,
|
||||
{
|
||||
move |input| {
|
||||
parser
|
||||
.parse(input)
|
||||
.map(|(result, rest)| (map_fn(result), rest))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user