Enum flussab::Parsed

source ·
pub enum Parsed<T, E> {
    Res(Result<T, E>),
    Fallthrough,
}
Expand description

Return type for recursive descent parser that can fall through to another parser on unexpected input.

If you have a parser that only fails irrecoverably, by returning a Result, you can use into to convert it to a Parsed value. Conversely, Parsed values can be converted into plain Result using or_give_up, making the Parsed value fall through to an unrecoverable error, or using optional making the parsed value optional.

Note that some of the methods of Parsed already have equivalent methods of the Result type in std. Some more methods are also implemented for Result by this crate, using the ResultExt trait. Finally some of the methods are only intended for parser that can fall through, and as such, they are not implemented for Result.

Variants§

§

Res(Result<T, E>)

A successfully parsed value or an irrecoverable error.

When Res(Ok(v)) is returned, the value was parsed successfully and the input stream should be advanced past the parsed input.

When Res(Err(err)) is returned, either the initial input matched, but continued in an unexpected way or some other kind of runtime error occured. In this case the input stream points at the position of the unexpected input or the input causing an error.

§

Fallthrough

The input does not match the expected input of the parser.

This leaves the input stream unchanged, so that another parser can be tried.

Implementations§

source§

impl<T, E> Parsed<T, E>

source

pub fn err_into<E2: From<E>>(self) -> Parsed<T, E2>

Equivalent to map_err(From::from).

source

pub fn or_give_up(self, err: impl FnOnce() -> E) -> Result<T, E>

Makes a parser fail irrecoverably when the current input did not match the expected input of the returning parser.

This makes the expected input of the parser returning this Parsed value a mandatory part of the input.

source

pub fn optional(self) -> Result<Option<T>, E>

Returns None when the current input did not match the expected input of the returning parser.

This makes the expected input of the parser returning this Parsed value optional.

source

pub fn matches(self) -> Result<bool, E>

Returns whether the current input did match the expected input of the returning parser.

This makes the expected input of the parser returning this Parsed value optional.

source

pub fn or_parse(self, parse: impl FnOnce() -> Parsed<T, E>) -> Parsed<T, E>

Tries a different parser when the current input did not match the expected input of the returning parser.

This adds another parser to the expected choices for the current input.

This is intentionally not called or_else as calling Result::or_else on an irrecoverably failing parser cannot be used to implement choice, as such a parser can leave the input stream in a modified state when it returns an Err value.

source

pub fn or_always_parse( self, parse: impl FnOnce() -> Result<T, E> ) -> Result<T, E>

Variant of or_parse for when the alternative parser is guaranteed to not fall through.

source

pub fn and_then<U>(self, parse: impl FnOnce(T) -> Result<U, E>) -> Parsed<U, E>

Locks in the choice when the input matches the expected input of the returning parser.

When the returning parser succeeds, but the passed parser fails, further alternatives will not be tried.

source

pub fn and_also( self, parse: impl FnOnce(&mut T) -> Result<(), E> ) -> Parsed<T, E>

Variant of and_then which always returns the (possibly modified) original value on success.

This is useful to parse delimiters which do not contain any parsed information.

source

pub fn and_do(self, action: impl FnOnce(&mut T)) -> Parsed<T, E>

Variant of and_also where the action cannot fail.

This is useful to modify a value or to perform side-effects on successful parses.

source

pub fn map<T2>(self, f: impl FnOnce(T) -> T2) -> Parsed<T2, E>

Replaces a successfully parsed value with the value returned when applying the function f to it.

If the parser was not successful, the error or fallthrough is returned unchanged.

source

pub fn map_err<E2>(self, f: impl FnOnce(E) -> E2) -> Parsed<T, E2>

Replaces an occured error with the error returned when applying the function f to it.

Trait Implementations§

source§

impl<T, E> From<Result<T, E>> for Parsed<T, E>

source§

fn from(res: Result<T, E>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, E> RefUnwindSafe for Parsed<T, E>where E: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, E> Send for Parsed<T, E>where E: Send, T: Send,

§

impl<T, E> Sync for Parsed<T, E>where E: Sync, T: Sync,

§

impl<T, E> Unpin for Parsed<T, E>where E: Unpin, T: Unpin,

§

impl<T, E> UnwindSafe for Parsed<T, E>where E: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.