Struct ParsingError

Source
pub struct ParsingError<In, Reason = Infallible> {
    pub rest: In,
    pub reason: Option<Reason>,
}
Expand description

Error returned by a parser.

A parsing error may be either recoverable or fatal, parser methods such as [Parser::or] allow trying different paths if a recoverable error occurs, whereas a fatal error is not intended to be recovered from and should just be propagated.

To make the error more useful, consider the following options:

Fields§

§rest: In

The rest of the input that could not be processed.

§reason: Option<Reason>

What the parser expected, the reason for the error. None means that the error is recoverable.

Implementations§

Source§

impl<In, Reason> ParsingError<In, Reason>

Source

pub const fn new(rest: In, reason: Reason) -> Self

Create a new fatal parsing error.

Source

pub const fn new_recoverable(rest: In) -> Self

Create a new recoverable parsing error.

Source

pub const fn is_recoverable(&self) -> bool

Returns a boolean indicating whether the error is recoverable.

Source

pub fn reason<NewReason>(self, reason: NewReason) -> ParsingError<In, NewReason>

Changes the reason associated with the error, making the error fatal.

Source

pub fn or_reason(self, reason: Reason) -> Self

Makes a recoverable error fatal by giving it a reason, if it’s already fatal, does nothing

Source

pub fn or_reason_if_nonempty(self, reason: Reason) -> Self
where In: Input,

Like ParsingError::or_reason but does nothing if the rest of the input is empty

Source

pub fn map_reason<NewReason>( self, f: impl FnOnce(Reason) -> NewReason, ) -> ParsingError<In, NewReason>

Transforms the reason by calling f, except if it’s a recoverable error, in which case it remains recoverable.

Source

pub fn adapt_reason<NewReason>(self) -> ParsingError<In, NewReason>
where Infallible: From<Reason>,

Convert the reason of an always recoverable error to another type. This will be a no-op since it’s statically guaranteed that the reason doesn’t exist.

Source

pub fn with_src_loc<'a>( self, path: impl PathLike<'a>, src: &'a str, ) -> FullParsingError<'a, Reason>
where In: Input,

Turns the error into a FullParsingError for a more informative report.

The error will point to the provided source code. The provided path will only be used for display purposes, this method won’t access the file system.

Examples found in repository?
examples/xml.rs (line 110)
105fn main() {
106    let input = args().nth(1).expect("XML input");
107    for fragment in xml_fragments(&*input) {
108        match fragment {
109            Ok(fragment) => println!("{fragment:?}"),
110            Err(e) => eprintln!("{}", e.with_src_loc("<input>", &input)),
111        }
112    }
113}
Source

pub fn with_file_loc<'a>( self, path: impl PathLike<'a>, ) -> Result<FullParsingError<'a, Reason>>
where In: Input,

Available on crate feature std only.

Turns this error into a FullParsingError that points to a file on the machine, for a more informative report.

§Errors

Returns an error if std::fs::read_to_string does.

Trait Implementations§

Source§

impl<In: Clone, Reason: Clone> Clone for ParsingError<In, Reason>

Source§

fn clone(&self) -> ParsingError<In, Reason>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<In: Debug, Reason: Debug> Debug for ParsingError<In, Reason>

Source§

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

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

impl<In: Input, Reason: Display> Display for ParsingError<In, Reason>

Source§

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

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

impl<In: Input, Reason: Error> Error for ParsingError<In, Reason>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<In: PartialEq, Reason: PartialEq> PartialEq for ParsingError<In, Reason>

Source§

fn eq(&self, other: &ParsingError<In, Reason>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<In: Copy, Reason: Copy> Copy for ParsingError<In, Reason>

Source§

impl<In: Eq, Reason: Eq> Eq for ParsingError<In, Reason>

Source§

impl<In, Reason> StructuralPartialEq for ParsingError<In, Reason>

Auto Trait Implementations§

§

impl<In, Reason> Freeze for ParsingError<In, Reason>
where In: Freeze, Reason: Freeze,

§

impl<In, Reason> RefUnwindSafe for ParsingError<In, Reason>
where In: RefUnwindSafe, Reason: RefUnwindSafe,

§

impl<In, Reason> Send for ParsingError<In, Reason>
where In: Send, Reason: Send,

§

impl<In, Reason> Sync for ParsingError<In, Reason>
where In: Sync, Reason: Sync,

§

impl<In, Reason> Unpin for ParsingError<In, Reason>
where In: Unpin, Reason: Unpin,

§

impl<In, Reason> UnwindSafe for ParsingError<In, Reason>
where In: UnwindSafe, Reason: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.