pub struct EarlyStopParser { /* private fields */ }Expand description
A streaming parser that stops as soon as a predicate matches a node.
Useful for extracting a single element from a large HTML document without parsing the entire thing.
§Example
use fhp_tree::streaming::EarlyStopParser;
use fhp_tree::streaming::ParseStatus;
use fhp_core::tag::Tag;
let mut parser = EarlyStopParser::stop_when(|node| node.tag == Tag::A);
let html = b"<div><p>text</p><a href=\"#\">link</a><span>more</span></div>";
match parser.feed(html) {
ParseStatus::Found(id) => {
// Found the <a> tag — no need to parse <span>more</span>.
}
_ => panic!("expected Found"),
}Implementations§
Source§impl EarlyStopParser
impl EarlyStopParser
Sourcepub fn stop_when(predicate: impl Fn(&Node) -> bool + 'static) -> Self
pub fn stop_when(predicate: impl Fn(&Node) -> bool + 'static) -> Self
Create a parser that stops when predicate returns true for a node.
Sourcepub fn feed(&mut self, chunk: &[u8]) -> ParseStatus
pub fn feed(&mut self, chunk: &[u8]) -> ParseStatus
Feed a chunk of raw bytes and check the predicate against new nodes.
Returns ParseStatus::Found as soon as a matching node is created,
or ParseStatus::NeedMore if the predicate hasn’t matched yet.
Encoding is detected eagerly from the first non-empty chunk to
minimise latency. For accurate meta-charset detection with small
chunks, use StreamParser instead.
Sourcepub fn finish(self) -> ParseStatus
pub fn finish(self) -> ParseStatus
Finish parsing and return the document.
If the predicate was already matched, returns ParseStatus::Found.
Otherwise returns ParseStatus::Done with the complete document.