Skip to main content

EarlyStopParser

Struct EarlyStopParser 

Source
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

Source

pub fn stop_when(predicate: impl Fn(&Node) -> bool + 'static) -> EarlyStopParser

Create a parser that stops when predicate returns true for a node.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.