Skip to main content

NdjsonStream

Struct NdjsonStream 

Source
pub struct NdjsonStream;
Expand description

NDJSON streaming utilities.

Provides methods for parsing NDJSON streams into typed structs. Works with any type implementing DeserializeOwned.

§Type Safety

Parsing is type-safe - invalid JSON or incompatible types result in StreamError::JsonParse rather than panics.

Implementations§

Source§

impl NdjsonStream

Source

pub fn parse<T>( lines: impl Stream<Item = Result<String, Error>>, ) -> impl Stream<Item = Result<T, StreamError>>

Parse stream of lines into typed structs.

Each line should contain a valid JSON object matching type T. Invalid lines result in StreamError::JsonParse.

§Type Parameters
  • T - Target type, must implement DeserializeOwned
§Example
use wme_stream::NdjsonStream;
use wme_models::Article;
use futures::StreamExt;
use std::pin::pin;

let articles = NdjsonStream::parse::<Article>(lines);
let mut pinned = pin!(articles);
while let Some(result) = pinned.next().await {
    match result {
        Ok(article) => println!("{}", article.name),
        Err(e) => eprintln!("Parse error: {}", e),
    }
}
Source

pub fn parse_articles( lines: impl Stream<Item = Result<String, Error>>, ) -> impl Stream<Item = Result<Article, StreamError>>

Parse stream specifically for Articles.

Convenience method equivalent to parse::<Article>(lines).

§Example
use wme_stream::NdjsonStream;
use futures::StreamExt;
use std::pin::pin;

let articles = NdjsonStream::parse_articles(lines);
let mut pinned = pin!(articles);
while let Some(result) = pinned.next().await {
    if let Ok(article) = result {
        println!("{}: {}", article.identifier, article.name);
    }
}
Source

pub fn from_reader<R: BufRead + Send + 'static>( reader: R, ) -> impl Stream<Item = Result<String, Error>>

Read lines from a buffered reader.

Creates an async stream from any BufRead implementor. Useful for reading NDJSON from files or network streams.

§Performance

The reader is read synchronously but yields to the async runtime between lines. For large files, this provides good throughput without blocking the runtime.

§Example
use wme_stream::NdjsonStream;
use std::io::BufReader;
use std::fs::File;

let file = File::open("data.ndjson").unwrap();
let reader = BufReader::new(file);
let lines = NdjsonStream::from_reader(reader);

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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.