Struct EventDecoder
pub struct EventDecoder<T = String>where
T: EventDataRead,{ /* private fields */ }http and std only.Expand description
Push-driven decoder turning raw SSE bytes into Events.
Push borrowed chunks to inspect an SSE body while forwarding it unchanged.
Use EventStream to decode a stream of chunks instead.
Push a chunk, drain what it completed, repeat, and call
finish once the body ends:
use rama_http_types::sse::EventDecoder;
let mut decoder = EventDecoder::<String>::new();
for chunk in [&b"data: hello\n"[..], b"\ndata: wor", b"ld\n\n"] {
decoder.push(chunk)?;
for event in decoder.events() {
let event = event?;
assert!(matches!(event.data(), Some(data) if data == "hello" || data == "world"));
}
}
decoder.finish()?;Chunks may split lines, UTF-8 sequences and CRLF pairs anywhere. Partial lines and undecoded chunk tails are buffered.
Events dispatch on blank lines. finish is optional;
it checks trailing UTF-8 without dispatching an event.
on_incomplete receives the buffered partial
line on finish or drop.
Input is unlimited by default. For untrusted input, set
max_line_len and
max_event_len. These limits do not cap the
undecoded backlog; drain events between pushes.
A decode error is fatal by default. lenient mode
instead recovers to the next event boundary, losing only the events around
the fault — useful when observing a stream that must not be blinded by one
bad event.
Implementations§
§impl<T> EventDecoder<T>where
T: EventDataRead,
impl<T> EventDecoder<T>where
T: EventDataRead,
pub fn new() -> EventDecoder<T>
pub fn new() -> EventDecoder<T>
Create a new EventDecoder.
pub fn maybe_with_max_line_len(self, max: Option<usize>) -> EventDecoder<T>
pub fn maybe_with_max_line_len(self, max: Option<usize>) -> EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn maybe_set_max_line_len(
&mut self,
max: Option<usize>,
) -> &mut EventDecoder<T>
pub fn maybe_set_max_line_len( &mut self, max: Option<usize>, ) -> &mut EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn with_max_line_len(self, max: usize) -> EventDecoder<T>
pub fn with_max_line_len(self, max: usize) -> EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn set_max_line_len(&mut self, max: usize) -> &mut EventDecoder<T>
pub fn set_max_line_len(&mut self, max: usize) -> &mut EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn without_max_line_len(self) -> EventDecoder<T>
pub fn without_max_line_len(self) -> EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn unset_max_line_len(&mut self) -> &mut EventDecoder<T>
pub fn unset_max_line_len(&mut self) -> &mut EventDecoder<T>
Fail decoding once a single line grows past max bytes,
terminator excluded.
pub fn maybe_with_max_event_len(self, max: Option<usize>) -> EventDecoder<T>
pub fn maybe_with_max_event_len(self, max: Option<usize>) -> EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn maybe_set_max_event_len(
&mut self,
max: Option<usize>,
) -> &mut EventDecoder<T>
pub fn maybe_set_max_event_len( &mut self, max: Option<usize>, ) -> &mut EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn with_max_event_len(self, max: usize) -> EventDecoder<T>
pub fn with_max_event_len(self, max: usize) -> EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn set_max_event_len(&mut self, max: usize) -> &mut EventDecoder<T>
pub fn set_max_event_len(&mut self, max: usize) -> &mut EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn without_max_event_len(self) -> EventDecoder<T>
pub fn without_max_event_len(self) -> EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn unset_max_event_len(&mut self) -> &mut EventDecoder<T>
pub fn unset_max_event_len(&mut self) -> &mut EventDecoder<T>
Fail decoding once an event exceeds max raw line bytes.
Includes field names, separators, comments, unknown fields and
partial lines; excludes line terminators. Resets per event.
pub fn maybe_with_on_incomplete(
self,
cb: Option<Box<dyn FnOnce(Vec<u8>) + Sync + Send>>,
) -> EventDecoder<T>
pub fn maybe_with_on_incomplete( self, cb: Option<Box<dyn FnOnce(Vec<u8>) + Sync + Send>>, ) -> EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn maybe_set_on_incomplete(
&mut self,
cb: Option<Box<dyn FnOnce(Vec<u8>) + Sync + Send>>,
) -> &mut EventDecoder<T>
pub fn maybe_set_on_incomplete( &mut self, cb: Option<Box<dyn FnOnce(Vec<u8>) + Sync + Send>>, ) -> &mut EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn with_on_incomplete(
self,
cb: Box<dyn FnOnce(Vec<u8>) + Sync + Send>,
) -> EventDecoder<T>
pub fn with_on_incomplete( self, cb: Box<dyn FnOnce(Vec<u8>) + Sync + Send>, ) -> EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn set_on_incomplete(
&mut self,
cb: Box<dyn FnOnce(Vec<u8>) + Sync + Send>,
) -> &mut EventDecoder<T>
pub fn set_on_incomplete( &mut self, cb: Box<dyn FnOnce(Vec<u8>) + Sync + Send>, ) -> &mut EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn without_on_incomplete(self) -> EventDecoder<T>
pub fn without_on_incomplete(self) -> EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn unset_on_incomplete(&mut self) -> &mut EventDecoder<T>
pub fn unset_on_incomplete(&mut self) -> &mut EventDecoder<T>
Pass a nonempty buffered partial line to cb at most once,
on finish or drop.
Bytes may contain invalid UTF-8. Completed lines from an unfinished event and undrained input are excluded.
pub fn with_lenient(self, lenient: bool) -> EventDecoder<T>
pub fn with_lenient(self, lenient: bool) -> EventDecoder<T>
Recover from a decode fault instead of failing the whole stream.
When on, a fault drops the event being built and skips to the next
blank line, then resumes — losing only the events around it, never
the rest of the stream. Off by default. See
resync_count and
with_on_resync to observe recovery.
pub fn set_lenient(&mut self, lenient: bool) -> &mut EventDecoder<T>
pub fn set_lenient(&mut self, lenient: bool) -> &mut EventDecoder<T>
Recover from a decode fault instead of failing the whole stream.
When on, a fault drops the event being built and skips to the next
blank line, then resumes — losing only the events around it, never
the rest of the stream. Off by default. See
resync_count and
with_on_resync to observe recovery.
pub fn maybe_with_on_resync(
self,
sink: Option<Arc<dyn ErrorSink>>,
) -> EventDecoder<T>
pub fn maybe_with_on_resync( self, sink: Option<Arc<dyn ErrorSink>>, ) -> EventDecoder<T>
pub fn maybe_set_on_resync(
&mut self,
sink: Option<Arc<dyn ErrorSink>>,
) -> &mut EventDecoder<T>
pub fn maybe_set_on_resync( &mut self, sink: Option<Arc<dyn ErrorSink>>, ) -> &mut EventDecoder<T>
pub fn with_on_resync(self, sink: Arc<dyn ErrorSink>) -> EventDecoder<T>
pub fn with_on_resync(self, sink: Arc<dyn ErrorSink>) -> EventDecoder<T>
pub fn set_on_resync(
&mut self,
sink: Arc<dyn ErrorSink>,
) -> &mut EventDecoder<T>
pub fn set_on_resync( &mut self, sink: Arc<dyn ErrorSink>, ) -> &mut EventDecoder<T>
pub fn without_on_resync(self) -> EventDecoder<T>
pub fn without_on_resync(self) -> EventDecoder<T>
pub fn unset_on_resync(&mut self) -> &mut EventDecoder<T>
pub fn unset_on_resync(&mut self) -> &mut EventDecoder<T>
pub fn resync_count(&self) -> usize
pub fn resync_count(&self) -> usize
How many resyncs have happened in lenient mode.
Zero when off, and nonzero exactly when recovery occurred — a health
signal for an otherwise silent stream. Best-effort: the tally can shift
slightly with how the input is chunked, though the surviving events do
not.
pub fn try_set_last_event_id(
&mut self,
id: impl Into<SmolStr>,
) -> Result<(), Box<dyn Error + Sync + Send>>
pub fn try_set_last_event_id( &mut self, id: impl Into<SmolStr>, ) -> Result<(), Box<dyn Error + Sync + Send>>
Set the last event ID, e.g. to initialize the decoder with the ID a previous connection ended on.
pub fn last_event_id(&self) -> Option<&str>
pub fn last_event_id(&self) -> Option<&str>
The ID of the last event yielded that carried one.
pub fn push(&mut self, chunk: &[u8]) -> Result<(), Box<dyn Error + Sync + Send>>
pub fn push(&mut self, chunk: &[u8]) -> Result<(), Box<dyn Error + Sync + Send>>
Push one chunk of the event stream into the decoder.
Undecoded bytes are buffered. Drain with events
between pushes to keep the backlog empty.
Decode errors surface through next_event after
preceding events. This method returns an error if already finished or failed.
pub fn next_event(
&mut self,
) -> Result<Option<Event<T>>, Box<dyn Error + Sync + Send>>
pub fn next_event( &mut self, ) -> Result<Option<Event<T>>, Box<dyn Error + Sync + Send>>
Yield the next decoded event, or None once everything pushed so far
has been decoded.
An error is fatal: the byte stream can no longer be interpreted reliably, so the decoder yields nothing further.
Trait Implementations§
§impl<T> Debug for EventDecoder<T>
impl<T> Debug for EventDecoder<T>
§impl<T> Default for EventDecoder<T>where
T: EventDataRead,
impl<T> Default for EventDecoder<T>where
T: EventDataRead,
§fn default() -> EventDecoder<T>
fn default() -> EventDecoder<T>
§impl<T> Drop for EventDecoder<T>where
T: EventDataRead,
impl<T> Drop for EventDecoder<T>where
T: EventDataRead,
Auto Trait Implementations§
impl<T = String> !RefUnwindSafe for EventDecoder<T>
impl<T = String> !UnwindSafe for EventDecoder<T>
impl<T> Freeze for EventDecoder<T>where
DecodeState<T>: Freeze,
impl<T> Send for EventDecoder<T>where
DecodeState<T>: Send,
impl<T> Sync for EventDecoder<T>where
DecodeState<T>: Sync,
impl<T> Unpin for EventDecoder<T>where
DecodeState<T>: Unpin,
impl<T> UnsafeUnpin for EventDecoder<T>where
DecodeState<T>: UnsafeUnpin,
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more