Skip to content

Commit

Permalink
Merge pull request #270 from epage/inner
Browse files Browse the repository at this point in the history
fix(error): Expose ErrMode::into_inner
  • Loading branch information
epage authored Jun 30, 2023
2 parents b2b77ee + 6534c97 commit 827de0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ impl<E> ErrMode<E> {
self.map(ErrorConvert::convert)
}

/// Unwrap the mode, returning the underlying error
///
/// Returns `None` for [`ErrMode::Incomplete`]
#[cfg_attr(debug_assertions, track_caller)]
pub(crate) fn into_inner(self) -> E {
pub fn into_inner(self) -> Option<E> {
match self {
ErrMode::Backtrack(e) | ErrMode::Cut(e) => e,
ErrMode::Incomplete(_) => {
panic!("complete parsers should not report `ErrMode::Incomplete(_)`")
}
ErrMode::Backtrack(e) | ErrMode::Cut(e) => Some(e),
ErrMode::Incomplete(_) => None,
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ pub trait Parser<I, O, E> {
"partial streams need to handle `ErrMode::Incomplete`"
);

let (i, o) = self.parse_next(input).map_err(|e| e.into_inner())?;
let _ = crate::combinator::eof(i).map_err(|e| e.into_inner())?;
let (i, o) = self.parse_next(input).map_err(|e| {
e.into_inner()
.expect("complete parsers should not report `ErrMode::Incomplete(_)`")
})?;
let _ = crate::combinator::eof(i).map_err(|e| {
e.into_inner()
.expect("complete parsers should not report `ErrMode::Incomplete(_)`")
})?;
Ok(o)
}

Expand Down

0 comments on commit 827de0a

Please sign in to comment.