From c9c92cb8c19ab7956bf58aba993aabcf8963b9e0 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 14 Oct 2024 12:48:55 -0700 Subject: [PATCH] feat: ParseError::with_input This lets you replace the input field in a `ParseError`, e.g. to transform a `ParseError<&str, E>` into a `ParseError`. --- src/error.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index e7380f87..058916f4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1164,7 +1164,8 @@ pub struct ParseError { } impl> ParseError { - pub(crate) fn new(mut input: I, start: I::Checkpoint, inner: E) -> Self { + /// Construct a new [`ParseError`] wrapping the given error and input. + pub fn new(mut input: I, start: I::Checkpoint, inner: E) -> Self { let offset = input.offset_from(&start); input.reset(&start); Self { @@ -1176,6 +1177,21 @@ impl> ParseError { } impl ParseError { + /// Replace the [`input`][`ParseError::input`] in this [`ParseError`], returning a new + /// [`ParseError`]. + /// + /// **Note:** To replace the [`input`][`ParseError::input`] and + /// [`offset`][`ParseError::offset`], use [`ParseError::new`] and + /// [`ParseError::into_inner`]. + #[inline] + pub fn with_input(self, input: I2) -> ParseError { + ParseError { + input, + offset: self.offset, + inner: self.inner, + } + } + /// The [`Stream`] at the initial location when parsing started #[inline] pub fn input(&self) -> &I {