Skip to main content

streaming_parser/
streaming_parser.rs

1//! Incremental parsing with callbacks and the native iterator adapter.
2
3use links_notation::StreamParser;
4use std::sync::{
5    atomic::{AtomicUsize, Ordering},
6    Arc,
7};
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let document = "first loves data\nprofile:\n  name Ada\nlast sees first";
11
12    // Disabling collection retains only the unresolved top-level record.
13    let count = Arc::new(AtomicUsize::new(0));
14    let callback_count = Arc::clone(&count);
15    let mut stream = StreamParser::new();
16    stream.set_collect(false).on_link(move |link| {
17        callback_count.fetch_add(1, Ordering::Relaxed);
18        println!("callback: {link}");
19    });
20
21    // Chunk boundaries are arbitrary: this writes one Unicode scalar at a time.
22    let mut encoded = [0; 4];
23    for symbol in document.chars() {
24        stream.write(symbol.encode_utf8(&mut encoded))?;
25    }
26    stream.finish()?;
27    println!("parsed {} links", count.load(Ordering::Relaxed));
28
29    // Iteration is lazy and automatically disables collection.
30    let chunks = ["one link\npro", "file:\n  name Ada\n", "two link"];
31    for link in StreamParser::parse_chunks(chunks) {
32        println!("iterator: {}", link?);
33    }
34
35    Ok(())
36}