Enum combine::error::Consumed

source ·
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§

Returns true if self is empty.

Extracts the contained value.

Converts self into the Consumed state.

Converts self into the Empty state.

Maps over the contained value without changing the consumed state.

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(), "")));
}

Trait Implementations§

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.