pub enum Consumed<T> {
Consumed(T),
Empty(T),
}
Expand description
Enum used to indicate if a parser consumed 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 consume any tokens.
Variants§
Consumed(T)
Constructor indicating that the parser has consumed elements
Empty(T)
Constructor indicating that the parser did not consume any elements
Implementations§
Source§impl<T> Consumed<T>
impl<T> Consumed<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Extracts the contained value.
Sourcepub fn into_consumed(self) -> Consumed<T>
pub fn into_consumed(self) -> Consumed<T>
Converts self
into the Consumed
state.
Sourcepub fn into_empty(self) -> Consumed<T>
pub fn into_empty(self) -> Consumed<T>
Converts self
into the Empty
state.
Sourcepub fn map<F, U>(self, f: F) -> Consumed<U>where
F: FnOnce(T) -> U,
pub fn map<F, U>(self, f: F) -> Consumed<U>where
F: FnOnce(T) -> U,
Maps over the contained value without changing the consumed state.
pub fn merge(&self, current: Consumed<T>) -> Consumed<T>
Sourcepub fn combine<F, U, E>(self, f: F) -> ParseResult2<U, E>where
F: FnOnce(T) -> ParseResult2<U, E>,
pub fn combine<F, U, E>(self, f: F) -> ParseResult2<U, E>where
F: FnOnce(T) -> ParseResult2<U, E>,
Combines the Consumed
flags from self
and the result of f
.
Empty <> Empty -> Empty
Consumed <> Empty -> Consumed
Empty <> Consumed -> Consumed
Consumed <> Consumed -> Consumed
//Parses a character of string literal and handles the escaped characters \\ and \" as \
//and " respectively
fn char<I>(input: &mut I) -> ParseResult<char, I>
where I: Stream<Item = char>,
I::Error: ParseError<I::Item, I::Range, I::Position>,
{
let (c, consumed) = try!(satisfy(|c| c != '"').parse_stream(input));
match c {
//Since the `char` parser has already consumed some of the input `combine` is used
//propagate the consumed state to the next part of the parser
'\\' => consumed.combine(|_| {
satisfy(|c| c == '"' || c == '\\')
.map(|c| {
match c {
'"' => '"',
'\\' => '\\',
c => c
}
})
.parse_stream(input)
}),
_ => Ok((c, consumed))
}
}
let result = many(parser(char))
.easy_parse(r#"abc\"\\"#);
assert_eq!(result, Ok((r#"abc"\"#.to_string(), "")));
}
pub fn combine_consumed<F, U, E>(self, f: F) -> FastResult<U, E>where
F: FnOnce(T) -> FastResult<U, E>,
Trait Implementations§
impl<T: Copy> Copy for Consumed<T>
impl<T> StructuralPartialEq for Consumed<T>
Auto Trait Implementations§
impl<T> Freeze for Consumed<T>where
T: Freeze,
impl<T> RefUnwindSafe for Consumed<T>where
T: RefUnwindSafe,
impl<T> Send for Consumed<T>where
T: Send,
impl<T> Sync for Consumed<T>where
T: Sync,
impl<T> Unpin for Consumed<T>where
T: Unpin,
impl<T> UnwindSafe for Consumed<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more