pub struct Query<'query> { /* private fields */ }Expand description
A compiled CSS query ready to be executed against an HTML document.
A Query encapsulates a tree of QuerySections, each representing
one CSS selector, compiled into an automaton of internal transitions.
The automaton is evaluated during streaming parsing to match elements
efficiently in a single pass.
§NFA Execution Model
Under the hood, a Query is compiled into a Non-Deterministic Finite Automaton (NFA).
- Fictitious States: The NFA states themselves are implicit. They simply represent the position (the integer index) between sequential transitions within the automaton’s evaluation path.
- Transitions: Defined by the internal
Transitionstruct, each edge consists of aguard(a topologicalCombinatordictating depth requirements like>or) and apredicate(anElementPredicatematching tags, classes, etc.). - Branches: A
QuerySectionrepresents a linear sequence of these transitions (usually representing a single string selector). Branching your query withQueryBuilder::thencreates new sections that form a directed tree of sub-automata.
§Building a Query
Use Query::all or Query::first as entry points, then chain with
QueryBuilder::all, QueryBuilder::first, or QueryBuilder::then,
and finalise with QueryBuilder::build.
use scah::{Query, Save};
// Simple: find all <a> tags
let q1 = Query::all("a", Save::all()).build();
// Compound: find sections, then extract links and text within them
let q2 = Query::all("section", Save::none())
.then(|s| [
s.all("a[href]", Save::all()),
s.first("p", Save::only_text_content()),
])
.build();Implementations§
Source§impl<'query> Query<'query>
impl<'query> Query<'query>
Sourcepub fn first(query: &'query str, save: Save) -> QueryBuilder<'query>
pub fn first(query: &'query str, save: Save) -> QueryBuilder<'query>
Start building a query that matches only the first element satisfying the given CSS selector.
Using first enables an early-exit optimisation: once the
match is found and its content captured, parsing of this branch
can stop early.
Sourcepub fn all(query: &'query str, save: Save) -> QueryBuilder<'query>
pub fn all(query: &'query str, save: Save) -> QueryBuilder<'query>
Start building a query that matches all elements satisfying the given CSS selector.
This is the most common entry point. The returned QueryBuilder
can be chained with .all(), .first(), .then(), and finally
.build() to produce a Query.
§Example
use scah::{Query, Save};
let query = Query::all("a[href]", Save::all()).build();