Skip to main content

Query

Struct Query 

Source
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 Transition struct, each edge consists of a guard (a topological Combinator dictating depth requirements like > or ) and a predicate (an ElementPredicate matching tags, classes, etc.).
  • Branches: A QuerySection represents a linear sequence of these transitions (usually representing a single string selector). Branching your query with QueryBuilder::then creates 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>

Source

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.

Source

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();

Trait Implementations§

Source§

impl<'query> Clone for Query<'query>

Source§

fn clone(&self) -> Query<'query>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'query> Debug for Query<'query>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'query> PartialEq for Query<'query>

Source§

fn eq(&self, other: &Query<'query>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'query> StructuralPartialEq for Query<'query>

Auto Trait Implementations§

§

impl<'query> Freeze for Query<'query>

§

impl<'query> RefUnwindSafe for Query<'query>

§

impl<'query> Send for Query<'query>

§

impl<'query> Sync for Query<'query>

§

impl<'query> Unpin for Query<'query>

§

impl<'query> UnsafeUnpin for Query<'query>

§

impl<'query> UnwindSafe for Query<'query>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.