Compare commits
No commits in common. "477fc50b65f3c8c8b444d65d07239d95aa8f0a53" and "bffaca4d683b768a1564be762e2cd89dd2982210" have entirely different histories.
477fc50b65
...
bffaca4d68
@ -16,13 +16,3 @@ where
|
||||
Ok((results, acc))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn optional<I, O, E>(parser: impl Parser<I, O, E>) -> impl Parser<I, Option<O>, E>
|
||||
where
|
||||
I: Clone,
|
||||
{
|
||||
move |input: I| match parser.parse(input.clone()) {
|
||||
Ok((output, rest)) => Ok((Some(output), rest)),
|
||||
Err(_e) => Ok((None, input)),
|
||||
}
|
||||
}
|
||||
|
13
src/lib.rs
13
src/lib.rs
@ -74,17 +74,4 @@ mod tests {
|
||||
let output = parser.parse("aaaaaaaabcd").unwrap();
|
||||
assert_eq! {((10, 'b'), "cd"), output};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional() {
|
||||
let parser = sequence(
|
||||
optional(literal("alpha")),
|
||||
sequence(repeated(literal(" ")), literal("beta")),
|
||||
);
|
||||
|
||||
let output1 = parser.parse(" beta").unwrap();
|
||||
assert_eq!(output1.0 .0, None);
|
||||
let output2 = parser.parse("alpha beta").unwrap();
|
||||
assert_eq!(output2.0 .0, Some("alpha"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user