Function from_stream_with_config

Source
pub fn from_stream_with_config<T, S>(
    bytes_stream: S,
    config: NdjsonConfig,
) -> NdjsonStream<T, S>
Available on crate feature stream only.
Expand description

Wraps a Stream of data blocks, i.e. types implementing AsBytes, and offers a Stream implementation over parsed NDJSON-records according to Deserialize. The parser is configured with the given NdjsonConfig.

ยงExample

use futures::stream::{self, StreamExt};
use ndjson_stream::config::{EmptyLineHandling, NdjsonConfig};

let data_blocks = vec![
    "123\n",
    "456\n   \n789\n"
];
let config = NdjsonConfig::default().with_empty_line_handling(EmptyLineHandling::IgnoreBlank);

let mut ndjson_stream =
    ndjson_stream::from_stream_with_config::<u32, _>(stream::iter(data_blocks), config);

tokio_test::block_on(async {
    assert!(matches!(ndjson_stream.next().await, Some(Ok(123))));
    assert!(matches!(ndjson_stream.next().await, Some(Ok(456))));
    assert!(matches!(ndjson_stream.next().await, Some(Ok(789))));
    assert!(ndjson_stream.next().await.is_none());
});