From 24aa8b31b9a0e21d7e213c6d6634700327e82713 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 29 Jan 2025 14:56:27 -0600 Subject: [PATCH 1/2] fix(error): Add back ModalError functions for easier transition --- src/error.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/error.rs b/src/error.rs index 92427965..37a7a55f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -137,6 +137,22 @@ impl ErrMode { 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(self, f: F) -> ErrMode where @@ -149,14 +165,24 @@ impl ErrMode { } } - /// 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(self) -> ErrMode where E: ErrorConvert, { ErrorConvert::convert(self) } + + /// Unwrap the mode, returning the underlying error + /// + /// Returns `None` for [`ErrMode::Incomplete`] + #[inline(always)] + pub fn into_inner(self) -> Option { + match self { + ErrMode::Backtrack(e) | ErrMode::Cut(e) => Some(e), + ErrMode::Incomplete(_) => None, + } + } } impl> ParserError for ErrMode { @@ -228,17 +254,11 @@ impl> ParserError for ErrMode { impl ModalError for ErrMode { 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() } } From b2beb6fcef11ae0edd43b7ab805fa5d4345465b9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 29 Jan 2025 14:57:13 -0600 Subject: [PATCH 2/2] fix(error)!: Update ErrMode::into_inner to match trait --- src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index 37a7a55f..8fe7c276 100644 --- a/src/error.rs +++ b/src/error.rs @@ -177,10 +177,10 @@ impl ErrMode { /// /// Returns `None` for [`ErrMode::Incomplete`] #[inline(always)] - pub fn into_inner(self) -> Option { + pub fn into_inner(self) -> Result { match self { - ErrMode::Backtrack(e) | ErrMode::Cut(e) => Some(e), - ErrMode::Incomplete(_) => None, + ErrMode::Backtrack(e) | ErrMode::Cut(e) => Ok(e), + err @ ErrMode::Incomplete(_) => Err(err), } } }