diff --git a/src/error.rs b/src/error.rs index 92427965..8fe7c276 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) -> Result { + match self { + ErrMode::Backtrack(e) | ErrMode::Cut(e) => Ok(e), + err @ ErrMode::Incomplete(_) => Err(err), + } + } } 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() } }