Skip to main content

SseParser

Struct SseParser 

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

Incremental Server-Sent Events parser.

§Example

use stream_rs::sse::SseParser;

let mut p = SseParser::new();
let mut events = Vec::new();
// Note the chunk boundary splitting the CRLF and a multi-line data field.
p.feed(b"event: greeting\r\ndata: hello\r", &mut events);
p.feed(b"\ndata: world\r\n\r\n", &mut events);

assert_eq!(events.len(), 1);
assert_eq!(events[0].event.as_deref(), Some("greeting"));
assert_eq!(events[0].data, "hello\nworld");

Implementations§

Source§

impl SseParser

Source

pub fn new() -> Self

Create a fresh parser.

Source

pub fn last_id(&self) -> Option<&str>

The persistent last event id.

Per the WHATWG spec this survives across dispatches: once an id field is seen it is reported on every subsequent event until replaced. This getter exposes the current value so a caller can send it as the Last-Event-ID header when reconnecting. Note that finish resets the parser, clearing this — read it before calling finish if you need it for reconnection.

Source

pub fn reconnection_time(&self) -> Option<u64>

The current reconnection time in milliseconds, from the most recent valid retry field.

Unlike the per-event SseEvent::retry, this is connection-level state that persists across dispatched events (and across events that carried no data and were therefore never surfaced), matching the WHATWG semantics.

Source

pub fn feed(&mut self, chunk: &[u8], out: &mut Vec<SseEvent>)

Feed a chunk of bytes. Completed events are pushed onto out.

Bytes that do not yet form a complete line are buffered internally until the next call.

Source

pub fn finish(&mut self, out: &mut Vec<SseEvent>)

Signal end of stream.

Per the WHATWG algorithm, once the stream ends the final line (bytes received after the last terminator) is processed as a field, but a pending event is only dispatched by a blank line — so an event that was never terminated by a blank line is discarded. This call therefore applies any trailing unterminated field, then drops the in-progress event buffers without emitting them.

out exists for API symmetry with feed; by spec nothing is ever pushed onto it here, but the signature lets callers funnel both calls through the same sink.

§Reset semantics

finish fully resets the parser so it can be reused for a fresh stream. This clears the persistent state too — the last event id (last_id) and the reconnection time (reconnection_time). If you need either for a reconnection, read it before calling finish.

Trait Implementations§

Source§

impl Debug for SseParser

Source§

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

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

impl Default for SseParser

Source§

fn default() -> SseParser

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 = 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.