Skip to main content

Decoder

Struct Decoder 

Source
pub struct Decoder { /* private fields */ }
Expand description

Bounded OSC scanner for chunks of terminal output.

Holds at most one 4096-byte frame, never allocates, and skips other OSCs through their terminator. Each callback borrows this decoder’s buffer only for that callback; decode or copy what you need before it returns. Oversized target frames produce one error and are drained through ST. Only seven-bit OSC/ST and BEL are recognized, as specified by revision 0.4. This scanner does not track full VT context or forward unrelated bytes. See the crate’s terminal-stream example for that integration.

Implementations§

Source§

impl Decoder

Source

pub const fn new() -> Self

Creates an empty decoder with a fixed-size inline buffer.

Source

pub fn reset(&mut self)

Discards a partial frame, for example after a terminal reset.

Source

pub fn feed( &mut self, input: &[u8], on_event: impl FnMut(Result<Event<'_>, ParserError>), )

Consumes bytes and invokes the callback for accepted events or rejected target frames. Ignored reports and unrelated output produce no callback.

Source

pub fn try_feed<E>( &mut self, input: &[u8], on_event: impl FnMut(Result<Event<'_>, ParserError>) -> Result<(), E>, ) -> Result<(), Error<E>>

Consumes bytes until the input ends or the callback returns an error. Ignored reports and unrelated output produce no callback.

On success, all input was consumed. On callback failure, the delivered event stays consumed and the remaining input is untouched. Resume with &input[failure.consumed()..] on this decoder. Callback effects are not rolled back. The callback must copy any event data it needs to retain.

An oversized frame produces one parser error when it exceeds the cap. If the callback stops before the terminator, resuming drains the rest of that frame without delivering the error again.

§Errors

Returns the callback’s error and the consumed byte count in Error. Parser errors are passed to the callback; they do not stop feeding unless the callback returns an error.

§Examples

Record one event before pausing, then resume with the remaining input. The recorded text owns its data because events borrow the decoder.

use osc7501_parser::{Decoder, Error, Event};

let mut decoder = Decoder::new();
let input = b"\x1b]7501;?\x07\x1b]7501;state=done\x07";
let mut events = Vec::new();
let mut record = |event: Result<Event<'_>, Error>| {
    events.push(event.map(|event| format!("{event:?}")));
};

let result = decoder.try_feed(input, |event| {
    record(event);
    Err(())
});
if let Err(stopped) = result {
    decoder.feed(&input[stopped.consumed()..], record);
}
assert_eq!(events.len(), 2);

Trait Implementations§

Source§

impl Default for Decoder

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.