Skip to main content

SseParser

Struct SseParser 

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

A bounded byte decoder with no event queue or input history.

UTF-8 is validated as lines become complete, so a chunk may end inside a character. LF, CRLF and CR terminate lines; a leading UTF-8 BOM is ignored. Comments and unknown fields are ignored but count against the event limit. Errors latch: after an error, discard the parser. There is no lossy decode, oversized-event truncation, or implicit EOF dispatch.

use causa_protocol::SseParser;
let mut parser = SseParser::new();
let mut bytes = b"data: hello\n\ndata: world\n\n".as_slice();
assert_eq!(parser.decode(&mut bytes)?.unwrap().data, "hello");
assert_eq!(parser.decode(&mut bytes)?.unwrap().data, "world");
assert!(parser.decode(&mut bytes)?.is_none());
parser.finish()?;

Implementations§

Source§

impl SseParser

Source

pub fn new() -> Self

Create a decoder with 32 MiB line and event limits.

Source

pub fn with_limits(self, max_line_bytes: usize, max_event_bytes: usize) -> Self

Set raw line and event byte limits before decoding.

A line excludes its terminator. An event includes its lines, ignored fields and delimiters. A CRLF counts as CR plus a skipped LF; the LF does not belong to the next event. Zero limits reject nonempty input.

Source

pub fn decode( &mut self, input: &mut &[u8], ) -> Result<Option<SseEvent>, ModelInvokeError>

Consume bytes through at most one complete event.

The caller retains the remaining slice and should call again, including when multiple events arrived in one HTTP chunk. None means all supplied bytes were consumed without completing a data event. Completed comments and unknown fields do not emit events. Calls after successful finish reject nonempty input.

Source

pub fn finish(&mut self) -> Result<(), ModelInvokeError>

Validate EOF without emitting an unfinished event.

Invalid or truncated UTF-8 is Undecodable; a valid unterminated line or event is UnknownOutcome. A clean SSE boundary succeeds, but does not prove that the enclosing model protocol supplied its terminal event.

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() -> 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.