pub struct UnescapeStream { /* private fields */ }
Expand description
A streaming JSON string unescaper that operates over byte slices.
This struct is the main entry point for streaming unescaping. It maintains a small internal buffer to handle escape sequences that are split across slice boundaries without requiring heap allocations.
See the module-level documentation for examples and more details.
Implementations§
Source§impl UnescapeStream
impl UnescapeStream
Sourcepub fn try_unescape_next<'a, 'b, I: AsRef<[u8]> + ?Sized>(
&'a mut self,
next_part: &'b I,
) -> Result<(Option<char>, UnescapeNext<'a, 'b>), UnescapeError>
pub fn try_unescape_next<'a, 'b, I: AsRef<[u8]> + ?Sized>( &'a mut self, next_part: &'b I, ) -> Result<(Option<char>, UnescapeNext<'a, 'b>), UnescapeError>
Processes the next byte slice, returning a fallible result.
This is a convenience wrapper around UnescapeStream::unescape_next
. Instead of returning
an Option<Result<...>>
, it “hoists” a potential boundary error into
the main Result
.
This simplifies error handling, as you can use the ?
operator to handle
errors from both the boundary character and the rest of the stream.
§Returns
Ok((Option<char>, UnescapeNext))
on success. TheOption<char>
contains the successfully unescaped character from a boundary-spanning sequence.Err(UnescapeError)
if completing a boundary-spanning sequence results in a parsing error.
Sourcepub fn unescape_next<'a, 'b, I: AsRef<[u8]> + ?Sized>(
&'a mut self,
next_part: &'b I,
) -> (Option<Result<char, UnescapeError>>, UnescapeNext<'a, 'b>)
pub fn unescape_next<'a, 'b, I: AsRef<[u8]> + ?Sized>( &'a mut self, next_part: &'b I, ) -> (Option<Result<char, UnescapeError>>, UnescapeNext<'a, 'b>)
Processes the next byte slice in the stream.
This is the primary method for feeding data to the unescaper. It returns a tuple:
- An
Option<Result<char, UnescapeError>>
for the character that may have spanned the boundary from the previous slice.None
if the previous slice ended cleanly. - An
UnescapeNext
iterator for the remainder of the current slice.
If the current slice ends with an incomplete escape sequence, that partial data is
saved internally. It will be resolved on the next call to unescape_next
again with
the subsequent slice or reported as an error by finish
.
Sourcepub fn finish(self) -> Result<(), UnescapeError>
pub fn finish(self) -> Result<(), UnescapeError>
Finalizes the unescaping process, checking for leftover incomplete data.
This method must be called after all slices have been processed. It checks
if there is an incomplete escape sequence stored in its internal buffer. If so,
it means the stream ended unexpectedly, and an Err(UnescapeError)
is returned.
This method consumes the UnescapeStream
, preventing further use.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears any partial escape sequence data from the internal buffer.
You might call this after encountering a non-fatal error if you want to discard the invalid partial data and continue processing the stream from a fresh state.
Warning: If you clear()
the state after an error and then call
finish()
without processing more data, finish()
will return Ok(())
because the partial state that caused the original error has been erased.
Sourcepub fn unescape_from_fn<Src, Dst, SrcError, DstError, B>(
self,
src: Src,
dst: Dst,
) -> Result<(), UnescapeFnError<SrcError, DstError>>
pub fn unescape_from_fn<Src, Dst, SrcError, DstError, B>( self, src: Src, dst: Dst, ) -> Result<(), UnescapeFnError<SrcError, DstError>>
Unescapes a stream of byte chunks from a source function to a destination function.
This function acts as a driver for the UnescapeStream
. It repeatedly calls a
source function (src
) to get the next chunk of data, unescapes it, and then
calls a destination function (dst
) for each resulting UnescapedToken
.
This provides a flexible way to connect any data source (like a file reader or network socket) to any data sink without manually iterating.
§Parameters
self
: TheUnescapeStream
instance, which will be consumed.src
: A closure or function that, when called, returns the next chunk of data asOption<Result<B, SrcError>>
. It should returnSome(Ok(chunk))
for data,Some(Err(e))
for a source error, andNone
to signal the end of the stream.dst
: A closure or function that receives anUnescapedToken
. It should returnOk(())
on success orErr(DstError)
on failure.
§Errors
Returns an UnescapeFnError
if an error occurs at any point:
UnescapeFnError::Src
if thesrc
function returns an error.UnescapeFnError::Unescape
if the JSON string is malformed (e.g., an invalid escape sequence or incomplete data at the end of the stream).UnescapeFnError::Dst
if thedst
function returns an error.
Sourcepub fn unescape_from_source<Src, Dst, SrcError, DstError>(
self,
src: Src,
dst: Dst,
) -> Result<(), UnescapeFnError<SrcError, DstError>>
pub fn unescape_from_source<Src, Dst, SrcError, DstError>( self, src: Src, dst: Dst, ) -> Result<(), UnescapeFnError<SrcError, DstError>>
Processes a stream of byte chunks from a source and unescapes them to a destination.
This function drives the unescaping process by repeatedly calling a src
(source) to get a chunk of bytes, unescaping the data, and then passing
the resulting UnescapedToken
s to a dst
(destination).
This provides a flexible pipeline for connecting any byte source (e.g., a file or network stream) to any byte sink without manual iteration.
§Parameters
src
: AChunkSource
that provides the raw, escaped byte chunks.dst
: A closure that receives eachUnescapedToken
and processes it.
§Errors
This function returns an UnescapeFnError
if an error occurs at any stage:
UnescapeFnError::Src
: An error from the source (src
).UnescapeFnError::Unescape
: The data is malformed (e.g., an invalid escape sequence).UnescapeFnError::Dst
: An error from the destination (dst
).
Trait Implementations§
Source§impl Clone for UnescapeStream
impl Clone for UnescapeStream
Source§fn clone(&self) -> UnescapeStream
fn clone(&self) -> UnescapeStream
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more