pub struct LineDecoder { /* private fields */ }Expand description
Incremental newline framer.
Accumulates pushed bytes and yields complete lines split on \n, stripping
a single trailing \r from each (so both \n and \r\n framing work). A
partial final line with no terminating \n stays buffered across pushes
until either a later push completes it or flush is
called.
new uses DEFAULT_MAX_LINE_BYTES. Untrusted streams
should call push_checked so over-long lines
are reported as NetError::LineTooLong. unbounded
is the explicit opt-out for trusted in-memory inputs.
This is the framing half of sim-lib-agent-runner-http’s old per-decoder
line_buffer loop, lifted into one shared, tested type.
Implementations§
Source§impl LineDecoder
impl LineDecoder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty decoder using DEFAULT_MAX_LINE_BYTES.
Sourcepub fn unbounded() -> Self
pub fn unbounded() -> Self
Create an empty decoder with no line limit.
Use this only for trusted, already-bounded inputs.
Sourcepub fn with_max_line_bytes(max_line_bytes: usize) -> Self
pub fn with_max_line_bytes(max_line_bytes: usize) -> Self
Create an empty decoder that rejects any line longer than
max_line_bytes.
Sourcepub fn push(&mut self, bytes: &[u8]) -> Vec<Vec<u8>>
pub fn push(&mut self, bytes: &[u8]) -> Vec<Vec<u8>>
Append bytes and return every line completed by a \n. The trailing
\r of a \r\n pair is stripped. Bytes after the last \n remain
buffered.
This compatibility convenience panics if the configured line cap is
exceeded. Use push_checked for untrusted
input so the caller can map the error into its own domain.
Sourcepub fn push_checked(&mut self, bytes: &[u8]) -> Result<Vec<Vec<u8>>, NetError>
pub fn push_checked(&mut self, bytes: &[u8]) -> Result<Vec<Vec<u8>>, NetError>
Append bytes and return every completed line.
The configured cap is checked before bytes are appended, so an over-long completed line or still-unterminated line leaves the existing buffered state unchanged.
Sourcepub fn flush(&mut self) -> Option<Vec<u8>>
pub fn flush(&mut self) -> Option<Vec<u8>>
Take any buffered remainder (an unterminated final line). Returns None
when nothing is buffered. A single trailing \r is stripped to match
push.
This compatibility convenience panics if the configured line cap is
exceeded. Use flush_checked for
untrusted input.
Sourcepub fn flush_checked(&mut self) -> Result<Option<Vec<u8>>, NetError>
pub fn flush_checked(&mut self) -> Result<Option<Vec<u8>>, NetError>
Take any buffered remainder, checking the configured cap first.
Sourcepub fn buffered_len(&self) -> usize
pub fn buffered_len(&self) -> usize
Number of bytes currently buffered as a partial line.
Sourcepub fn buffered(&self) -> &[u8] ⓘ
pub fn buffered(&self) -> &[u8] ⓘ
Borrow the bytes currently buffered as a partial (unterminated) line.
Sourcepub fn has_buffered(&self) -> bool
pub fn has_buffered(&self) -> bool
Whether any partial line is currently buffered.