pub struct StreamParser { /* private fields */ }Expand description
Incrementally emits complete top-level Links Notation records.
A record can acquire indented children, so a terminating newline is not by itself a safe boundary. A record is committed when the next non-indented content line begins, and the canonical parser validates every committed segment. Set collection off for bounded-memory callback or iterator use.
Implementations§
Source§impl StreamParser
impl StreamParser
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a parser with the canonical default options.
Examples found in repository?
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}Sourcepub fn with_config(config: ParserConfig) -> Self
pub fn with_config(config: ParserConfig) -> Self
Create a parser with explicit canonical parser options.
Sourcepub fn on_link<F>(&mut self, callback: F) -> &mut Self
pub fn on_link<F>(&mut self, callback: F) -> &mut Self
Register a callback invoked once for every completed link.
Examples found in repository?
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}Sourcepub fn on_error<F>(&mut self, callback: F) -> &mut Self
pub fn on_error<F>(&mut self, callback: F) -> &mut Self
Register a callback invoked when parsing fails.
Sourcepub fn set_collect(&mut self, collect: bool) -> &mut Self
pub fn set_collect(&mut self, collect: bool) -> &mut Self
Examples found in repository?
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}Sourcepub fn set_max_buffer_size(
&mut self,
max_buffer_size: usize,
) -> Result<&mut Self, StreamParseError>
pub fn set_max_buffer_size( &mut self, max_buffer_size: usize, ) -> Result<&mut Self, StreamParseError>
Set the largest unresolved record accepted by the stream.
Sourcepub fn write(
&mut self,
chunk: &str,
) -> Result<Vec<LiNo<String>>, StreamParseError>
pub fn write( &mut self, chunk: &str, ) -> Result<Vec<LiNo<String>>, StreamParseError>
Consume a string chunk and return links made complete by it.
Examples found in repository?
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}Sourcepub fn finish(&mut self) -> Result<Vec<LiNo<String>>, StreamParseError>
pub fn finish(&mut self) -> Result<Vec<LiNo<String>>, StreamParseError>
Finish the stream and return all undrained or newly emitted links.
Examples found in repository?
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}Sourcepub fn reset(&mut self) -> &mut Self
pub fn reset(&mut self) -> &mut Self
Reuse this parser while preserving its configuration and callbacks.
Sourcepub fn position(&self) -> StreamPosition
pub fn position(&self) -> StreamPosition
Return the absolute stream position and unresolved buffer size.
Sourcepub fn parse_chunks<I, S>(chunks: I) -> StreamIterator<I::IntoIter> ⓘ
pub fn parse_chunks<I, S>(chunks: I) -> StreamIterator<I::IntoIter> ⓘ
Lazily parse any iterator of string-like chunks.
Examples found in repository?
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}