pub struct StreamParser<'src> { /* private fields */ }Expand description
Pull-based streaming parser that yields arena-allocated subtrees.
Implementations§
Source§impl<'src> StreamParser<'src>
impl<'src> StreamParser<'src>
pub fn from_str(input: &'src str) -> Self
pub fn from_bytes(input: &'src [u8]) -> Result<Self>
Sourcepub fn with_options(self, opts: &ParseOptions) -> Self
pub fn with_options(self, opts: &ParseOptions) -> Self
Replace ParseOptions on the underlying reader. Takes a reference
(the reader internally clones once — amortized over the whole stream).
Sourcepub fn emit_at_depth(self, depth: u32) -> Self
pub fn emit_at_depth(self, depth: u32) -> Self
Emit elements at the given depth. depth = 1 matches direct children
of the root. Memory-bounded.
Sourcepub fn emit_at_path(self, path: &[&str]) -> Self
pub fn emit_at_path(self, path: &[&str]) -> Self
Emit elements whose ancestor chain (from root) matches path exactly.
path[0] is the root’s name, path[1] its child, …, path[N-1] the
element to emit. Memory-bounded.
Sourcepub fn emit_when<F>(self, predicate: F) -> Self
pub fn emit_when<F>(self, predicate: F) -> Self
Emit elements whose ancestor chain satisfies predicate. The
predicate receives a slice of QNames from root to the
just-opened element (inclusive at the end); return true to
emit that element’s subtree.
More flexible than emit_at_path /
emit_at_depth: handy when the
emission criterion is “any <item> regardless of where it
appears in the tree” or “any element under <rss> that is
itself named entry”.
Memory profile matches the fixed-depth modes — elements above the matching boundary cost only the ancestor stack.
§Example
use sup_xml_core::StreamParser;
let xml = r#"<root>
<section><item>a</item></section>
<other><item>b</item></other>
</root>"#;
let mut sp = StreamParser::from_str(xml)
.emit_when(|chain| chain.last().map(String::as_str) == Some("item"));
while sp.next().unwrap().is_some() {}