Skip to main content

SseDecoder

Struct SseDecoder 

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

An incremental SSE decoder: push bytes, take complete events.

SseStream drives one over a byte stream; use it directly when the bytes do not come as a Stream (a callback API, a test).

Decoding is linear: each byte is scanned for a line ending once, however the chunks split the lines. A size cap (max_event_size) bounds the line still waiting for its ending, where memory grows from chunk to chunk, and the data an event accumulates.

use allama_http::sse::SseDecoder;

let mut decoder = SseDecoder::new();
decoder.push(b"event: ping\r");
assert_eq!(decoder.next_event(), Ok(None));
decoder.push(b"\ndata: {}\r\n\r\n");
let event = decoder.next_event().unwrap().unwrap();
assert_eq!((event.event.as_str(), event.data.as_str()), ("ping", "{}"));
assert_eq!(decoder.next_event(), Ok(None));

Implementations§

Source§

impl SseDecoder

Source

pub const DEFAULT_MAX_EVENT_SIZE: usize

The default max_event_size: 64 MiB.

Generous on purpose: a Responses response.completed event repeats every output of the response, and streamed base64 images (OpenAI partial images, Gemini inline data) can pass 16 MiB.

Source

pub fn new() -> Self

A decoder at the start of a stream, with the default size cap.

Source

pub fn max_event_size(self, limit: usize) -> Self

Set the size cap, in bytes (default DEFAULT_MAX_EVENT_SIZE).

It bounds a line (its line ending excluded) and an event’s data (its data: values, joined). push checks the line still waiting for its ending before it buffers a chunk, so that line never grows past the cap, not even briefly while the buffer reallocates.

Past the cap, next_event fails with SseError::EventTooLarge, and decoding cannot go on: the decoder drops what it holds, ignores later bytes and keeps returning the error.

use allama_http::sse::{SseDecoder, SseError};

let mut decoder = SseDecoder::new().max_event_size(16);
decoder.push(b"data: 0123456789");
assert_eq!(decoder.next_event(), Ok(None));
// A 17th byte without a line ending.
decoder.push(b"a");
assert!(matches!(
    decoder.next_event(),
    Err(SseError::EventTooLarge { limit: 16, .. })
));
Source

pub fn push(&mut self, bytes: &[u8])

Append received bytes. Chunk boundaries may fall anywhere, inside a line, a CRLF or a UTF-8 sequence.

Bytes that would take the line still waiting for its ending past the size cap are not buffered: next_event returns the events completed before that line, then fails.

Source

pub fn next_event(&mut self) -> Result<Option<SseEvent>, SseError<Infallible>>

The next complete event, or None until more bytes arrive.

§Errors

A decoder fails with no other error: it reads no transport.

Trait Implementations§

Source§

impl Debug for SseDecoder

Source§

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

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

impl Default for SseDecoder

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more