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
impl Decoder
Sourcepub fn feed(
&mut self,
input: &[u8],
on_event: impl FnMut(Result<Event<'_>, ParserError>),
)
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.
Sourcepub fn try_feed<E>(
&mut self,
input: &[u8],
on_event: impl FnMut(Result<Event<'_>, ParserError>) -> Result<(), E>,
) -> Result<(), Error<E>>
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);