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
impl SseParser
Sourcepub fn with_limits(self, max_line_bytes: usize, max_event_bytes: usize) -> Self
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.
Sourcepub fn decode(
&mut self,
input: &mut &[u8],
) -> Result<Option<SseEvent>, ModelInvokeError>
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.
Sourcepub fn finish(&mut self) -> Result<(), ModelInvokeError>
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.