pub enum ErrorTree {
    Or {
        pos: usize,
        left: Box<ErrorTree>,
        right: Box<ErrorTree>,
    },
    Predicate {
        pos: usize,
    },
    Repeat {
        pos: usize,
        child: Box<ErrorTree>,
        matched: usize,
        lower_bound: usize,
        upper_bound: Option<usize>,
    },
    Peeked {
        pos: usize,
        child: Box<ErrorTree>,
    },
    NotPeeked {
        pos: usize,
    },
    AndThen {
        pos: usize,
        callback_error: Box<dyn Error>,
    },
    Sequence {
        pos: usize,
        position_in_sequence: usize,
        child: Box<ErrorTree>,
    },
    Label {
        pos: usize,
        label: &'static str,
        child: Box<ErrorTree>,
    },
    Mismatch {
        expected: String,
        pos: usize,
    },
    Custom {
        pos: usize,
        child: Box<ErrorTree>,
        err: Box<dyn Error>,
    },
}
Expand description

The error type returned when parsers fail.

This is a tree-like structure where nodes represent specific parsers or combinators that failed.

Here is an example:

use parser_compose::Parser;

let msg = "ABCD";

let parser = "B".or("C").or("A");

let result = parser.repeated(4).try_parse(msg.into());

assert!(result.is_err());

println!("{}", result.err().unwrap());
// stdout:
// expected 4 match(es). matched 3 time(s)
//    both branches failed:
//       both branches failed:
//          expected 'B' at position 3
//          expected 'C' at position 3
//       expected 'A' at position 3

In this case, the parser failed because it could not match “A”, “B,” or “C” at position 3 in the input.

You can augment the information in this tree by adding custom errors using err() or labelling a parser using label(). Both those methods insert additional nodes into the tree.

use parser_compose::Parser;

let msg = "A";

let parser = "B".or("C").label("failed to parse C or B");

let result = parser.try_parse(msg.into());

assert!(result.is_err());
println!("{}", result.err().unwrap());

// stdout:
// [failed to parse C or B]
//    both branches failed:
//       expected 'B' at position 0
//       expected 'C' at position 0

Variants§

§

Or

Fields

§pos: usize
§

Predicate

Fields

§pos: usize
§

Repeat

Fields

§pos: usize
§matched: usize
§lower_bound: usize
§upper_bound: Option<usize>
§

Peeked

Fields

§pos: usize
§

NotPeeked

Fields

§pos: usize
§

AndThen

Fields

§pos: usize
§callback_error: Box<dyn Error>
§

Sequence

Fields

§pos: usize
§position_in_sequence: usize
§

Label

Fields

§pos: usize
§label: &'static str
§

Mismatch

Fields

§expected: String
§pos: usize
§

Custom

Fields

§pos: usize
§err: Box<dyn Error>

Trait Implementations§

source§

impl Debug for ErrorTree

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for ErrorTree

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.