UnescapeStream

Struct UnescapeStream 

Source
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

Source

pub fn new() -> Self

Creates a new, empty UnescapeStream.

Source

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. The Option<char> contains the successfully unescaped character from a boundary-spanning sequence.
  • Err(UnescapeError) if completing a boundary-spanning sequence results in a parsing error.
Source

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:

  1. 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.
  2. 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.

Source

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.

Source

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.

Source

pub fn unescape_from_fn<Src, Dst, SrcError, DstError, B>( self, src: Src, dst: Dst, ) -> Result<(), UnescapeFnError<SrcError, DstError>>
where Src: FnMut() -> Option<Result<B, SrcError>>, Dst: FnMut(UnescapedToken<'_>) -> Result<(), DstError>, B: AsRef<[u8]>,

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: The UnescapeStream instance, which will be consumed.
  • src: A closure or function that, when called, returns the next chunk of data as Option<Result<B, SrcError>>. It should return Some(Ok(chunk)) for data, Some(Err(e)) for a source error, and None to signal the end of the stream.
  • dst: A closure or function that receives an UnescapedToken. It should return Ok(()) on success or Err(DstError) on failure.
§Errors

Returns an UnescapeFnError if an error occurs at any point:

Source

pub fn unescape_from_source<Src, Dst, SrcError, DstError>( self, src: Src, dst: Dst, ) -> Result<(), UnescapeFnError<SrcError, DstError>>
where Src: ChunkSource<Error = SrcError>, Dst: FnMut(UnescapedToken<'_>) -> Result<(), 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 UnescapedTokens 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: A ChunkSource that provides the raw, escaped byte chunks.
  • dst: A closure that receives each UnescapedToken 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

Source§

fn clone(&self) -> UnescapeStream

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnescapeStream

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UnescapeStream

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.