pub enum Commit<T> {
Commit(T),
Peek(T),
}
Expand description
Enum used to indicate if a parser committed any items of the stream it was given as an input.
This is used by parsers such as or
and choice
to determine if they should try to parse
with another parser as they will only be able to provide good error reporting if the preceding
parser did not commit to the parse.
Variants
Commit(T)
Constructor indicating that the parser has committed to this parse. If a parser after this fails,
other parser alternatives will not be attempted (CommitErr
will be returned)
Peek(T)
Constructor indicating that the parser has not committed to this parse. If a parser after this fails,
other parser alternatives will be attempted (EmptyErr
will be returned)
Implementations
sourceimpl<T> Commit<T>
impl<T> Commit<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Extracts the contained value.
sourcepub fn into_commit(self) -> Commit<T>
pub fn into_commit(self) -> Commit<T>
Converts self
into the Commit
state.
sourcepub fn map<F, U>(self, f: F) -> Commit<U> where
F: FnOnce(T) -> U,
pub fn map<F, U>(self, f: F) -> Commit<U> where
F: FnOnce(T) -> U,
Maps over the contained value without changing the committed state.
pub fn merge(&self, current: Commit<T>) -> Commit<T>
sourcepub fn combine<F, U, E>(self, f: F) -> StdParseResult2<U, E> where
F: FnOnce(T) -> StdParseResult2<U, E>,
pub fn combine<F, U, E>(self, f: F) -> StdParseResult2<U, E> where
F: FnOnce(T) -> StdParseResult2<U, E>,
Combines the Commit
flags from self
and the result of f
.
Peek <> Peek -> Peek
Commit <> Peek -> Commit
Peek <> Commit -> Commit
Commit <> Commit -> Commit
//Parses a character of string literal and handles the escaped characters \\ and \" as \
//and " respectively
fn char<Input>(input: &mut Input) -> StdParseResult<char, Input>
where Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
let (c, committed) = satisfy(|c| c != '"').parse_stream(input).into_result()?;
match c {
//Since the `char` parser has already committed some of the input `combine` is used
//propagate the committed state to the next part of the parser
'\\' => committed.combine(|_| {
satisfy(|c| c == '"' || c == '\\')
.map(|c| {
match c {
'"' => '"',
'\\' => '\\',
c => c
}
})
.parse_stream(input)
.into_result()
}),
_ => Ok((c, committed))
}
}
let result = many(parser(char))
.easy_parse(r#"abc\"\\"#);
assert_eq!(result, Ok((r#"abc"\"#.to_string(), "")));
}
pub fn combine_commit<F, U, E>(self, f: F) -> ParseResult<U, E> where
F: FnOnce(T) -> ParseResult<U, E>,
Trait Implementations
impl<T: Copy> Copy for Commit<T>
impl<T> StructuralPartialEq for Commit<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Commit<T> where
T: RefUnwindSafe,
impl<T> Send for Commit<T> where
T: Send,
impl<T> Sync for Commit<T> where
T: Sync,
impl<T> Unpin for Commit<T> where
T: Unpin,
impl<T> UnwindSafe for Commit<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more