Function combine::satisfy_map

source ·
pub fn satisfy_map<I, P, R>(predicate: P) -> SatisfyMap<I, P>where
    I: Stream,
    P: FnMut(I::Item) -> Option<R>,
Expand description

Parses a token and passes it to predicate. If predicate returns Some the parser succeeds and returns the value inside the Option. If predicate returns None the parser fails without consuming any input.

#[derive(Debug, PartialEq)]
enum YesNo {
    Yes,
    No,
}
let mut parser = satisfy_map(|c| {
    match c {
        'Y' => Some(YesNo::Yes),
        'N' => Some(YesNo::No),
        _ => None,
    }
});
assert_eq!(parser.parse("Y").map(|x| x.0), Ok(YesNo::Yes));
assert!(parser.parse("A").map(|x| x.0).is_err());