Skip to content

Commit

Permalink
Merge pull request #725 from epage/error
Browse files Browse the repository at this point in the history
fix(error): Add back ModalError functions for easier transition
  • Loading branch information
epage authored Jan 29, 2025
2 parents f2e15cd + b2beb6f commit a259438
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ impl<E> ErrMode<E> {
matches!(self, ErrMode::Incomplete(_))
}

/// Prevent backtracking, bubbling the error up to the top
pub fn cut(self) -> Self {
match self {
ErrMode::Backtrack(e) => ErrMode::Cut(e),
rest => rest,
}
}

/// Enable backtracking support
pub fn backtrack(self) -> Self {
match self {
ErrMode::Cut(e) => ErrMode::Backtrack(e),
rest => rest,
}
}

/// Applies the given function to the inner error
pub fn map<E2, F>(self, f: F) -> ErrMode<E2>
where
Expand All @@ -149,14 +165,24 @@ impl<E> ErrMode<E> {
}
}

/// Deprecated, replaced with [`ErrorConvert`]
#[deprecated(since = "0.6.23", note = "Replaced with `ErrorConvert`")]
/// Automatically converts between errors if the underlying type supports it
pub fn convert<F>(self) -> ErrMode<F>
where
E: ErrorConvert<F>,
{
ErrorConvert::convert(self)
}

/// Unwrap the mode, returning the underlying error
///
/// Returns `None` for [`ErrMode::Incomplete`]
#[inline(always)]
pub fn into_inner(self) -> Result<E, Self> {
match self {
ErrMode::Backtrack(e) | ErrMode::Cut(e) => Ok(e),
err @ ErrMode::Incomplete(_) => Err(err),
}
}
}

impl<I: Stream, E: ParserError<I>> ParserError<I> for ErrMode<E> {
Expand Down Expand Up @@ -228,17 +254,11 @@ impl<I: Stream, E: ParserError<I>> ParserError<I> for ErrMode<E> {

impl<E> ModalError for ErrMode<E> {
fn cut(self) -> Self {
match self {
ErrMode::Backtrack(e) => ErrMode::Cut(e),
rest => rest,
}
self.cut()
}

fn backtrack(self) -> Self {
match self {
ErrMode::Cut(e) => ErrMode::Backtrack(e),
rest => rest,
}
self.backtrack()
}
}

Expand Down

0 comments on commit a259438

Please sign in to comment.