Function chomp::combinators::either
[−]
[src]
pub fn either<I, T, U, E, F, G>(
i: I,
f: F,
g: G
) -> ParseResult<I, Either<T, U>, E> where
I: Input,
F: FnOnce(I) -> ParseResult<I, T, E>,
G: FnOnce(I) -> ParseResult<I, U, E>,
Attempts the left parser first and then the right parser if the first parser fails. Result is
returned as an Either<T, U>
depending on which parser succeeded.
NOTE: If both parsers have the same return-type, use or
instead.
use chomp::prelude::{Error, parse_only, either, token, Left, Right}; let p = |i| either(i, |i| token(i, b'a'), |i| token(i, b'b')); assert_eq!(parse_only(&p, b"a"), Ok(Left(b'a'))); assert_eq!(parse_only(&p, b"b"), Ok(Right(b'b'))); assert_eq!(parse_only(&p, b"c"), Err((&b"c"[..], Error::expected(b'b'))));