Skip to main content

Module stream_parser

Module stream_parser 

Source
Expand description

Streaming pull parser that yields arena-allocated subtrees.

The arena counterpart of crate::stream_parser. Each emitted subtree is its own Document with its own bumpalo::Bump arena — drop the Document and that subtree’s memory is freed. Memory is bounded by the largest single emitted subtree, regardless of overall document size.

§API surface

  • emit_at_depth(d) — emit elements at a fixed depth from the root.
  • emit_at_path(&[…]) — emit elements whose root-anchored ancestor chain matches path exactly.
  • emit_when(predicate) — emit elements whose ancestor chain satisfies an arbitrary closure. Useful for “any <item> regardless of nesting” / “anything two levels under <rss>” patterns the fixed-depth modes can’t express.

All three are ancestor-bounded modes: the parser inspects the freshly-opened element’s name + ancestor chain to decide whether to materialize its subtree. Elements above the emit boundary cost just a depth counter + name on a Vec<String> ancestor stack — independent of total document size.

Namespace resolution runs per emission: each emitted Document’s nodes carry namespace set to the in-scope binding at the time the element was opened, inherited correctly through ancestor xmlns:* declarations.

§Example

use sup_xml_core::StreamParser;

let xml = r#"<rss><channel>
  <item><title>a</title></item>
  <item><title>b</title></item>
</channel></rss>"#;

let mut sp = StreamParser::from_str(xml)
    .emit_at_path(&["rss", "channel", "item"]);

let mut titles = Vec::new();
while let Some(item) = sp.next().unwrap() {
    let t = item.root().find_child("title").unwrap();
    titles.push(t.text_content().unwrap().to_owned());
    // `item` (and its arena) drops at the end of this iteration.
}
assert_eq!(titles, vec!["a", "b"]);

Structs§

StreamParser
Pull-based streaming parser that yields arena-allocated subtrees.

Type Aliases§

EmitPredicate
Predicate evaluated against the ancestor-name chain of the just-opened element. Receives a slice whose last entry is the just-opened element’s QName and earlier entries are its ancestors in order from the root. Return true to emit that element’s subtree.