Skip to main content

NdjsonDecoder

Struct NdjsonDecoder 

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

An incremental NDJSON framer: push bytes, take complete lines.

NdjsonStream drives one over a byte stream; use it directly when the bytes do not come as a Stream (a callback API, a test).

Framing is linear: each byte is scanned for a line ending once, however the chunks split the lines. A size cap (max_line_size) bounds the line still waiting for its ending, where memory grows from chunk to chunk.

use allama_http::ndjson::NdjsonDecoder;

let mut decoder = NdjsonDecoder::new();
decoder.push(b"{\"a\":1}\r");
assert_eq!(decoder.next_line(), Ok(None));
decoder.push(b"\n\n{\"b\":");
assert_eq!(decoder.next_line(), Ok(Some(r#"{"a":1}"#.to_owned())));
assert_eq!(decoder.next_line(), Ok(None));
decoder.push(b"2}");
// The body ended: the last line needs no line ending.
assert_eq!(decoder.finish(), Ok(Some(r#"{"b":2}"#.to_owned())));

Implementations§

Source§

impl NdjsonDecoder

Source

pub const DEFAULT_MAX_LINE_SIZE: usize

The default max_line_size: 64 MiB, as SseDecoder::DEFAULT_MAX_EVENT_SIZE, for the same reasons: a final line can repeat the whole response, and streamed base64 images are large.

Source

pub fn new() -> Self

A framer at the start of a stream, with the default size cap.

Source

pub fn max_line_size(self, limit: usize) -> Self

Set the size cap, in bytes (default DEFAULT_MAX_LINE_SIZE).

It bounds a line, its line ending excluded. push checks the line still waiting for its ending before it buffers a chunk, so that line never grows much past the cap.

Past the cap, next_line fails with NdjsonError::LineTooLarge, and framing cannot go on: the framer drops what it holds, ignores later bytes and keeps returning the error.

use allama_http::ndjson::{NdjsonDecoder, NdjsonError};

let mut decoder = NdjsonDecoder::new().max_line_size(8);
decoder.push(b"{\"a\":12}");
assert_eq!(decoder.next_line(), Ok(None));
// A 9th byte without a line ending.
decoder.push(b"3");
assert!(matches!(
    decoder.next_line(),
    Err(NdjsonError::LineTooLarge { limit: 8, .. })
));
Source

pub fn push(&mut self, bytes: &[u8])

Append received bytes. Chunk boundaries may fall anywhere, inside a line, a CRLF or a UTF-8 sequence.

Bytes that would take the line still waiting for its ending past the size cap are not buffered: next_line returns the lines completed before that line, then fails.

Source

pub fn next_line(&mut self) -> Result<Option<String>, NdjsonError<Infallible>>

The next complete, non-blank line, without its line ending, or None until more bytes arrive.

§Errors

A framer fails with no other error: it reads no transport.

Source

pub fn finish(&mut self) -> Result<Option<String>, NdjsonError<Infallible>>

The last line, once the body ended: what is left without a line ending, unless it is blank. Call it after next_line returned None; the framer is empty afterwards.

§Errors

As next_line.

Trait Implementations§

Source§

impl Debug for NdjsonDecoder

Source§

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

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

impl Default for NdjsonDecoder

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more