Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<'query> {
    pub states: Vec<Transition<'query>>,
    pub selection: Vec<QuerySection<'query>>,
}
Expand description

An in-progress query being assembled via a builder pattern.

You typically don’t construct a QueryBuilder directly; instead, start with Query::all or Query::first and then chain further selectors with .all(), .first(), or .then(). Finalise with .build() to produce a Query.

§Example

use scah::{Query, Save};

let query = Query::all("main > section", Save::all())
    .then(|ctx| [
        ctx.all("> a[href]", Save::all()),
        ctx.all("div a",    Save::only_text_content()),
    ])
    .build();

Fields§

§states: Vec<Transition<'query>>

Internal automaton transitions (compiled selector segments).

§selection: Vec<QuerySection<'query>>

Internal ordered list of query sections.

Implementations§

Source§

impl<'query> QueryBuilder<'query>

Source

pub fn all(self, query: &'query str, save: Save) -> Self

Append a child selector that matches all occurrences.

The new selector is scoped to elements that already matched the previous selector in the chain.

Source

pub fn first(self, query: &'query str, save: Save) -> Self

Append a child selector that matches only the first occurrence.

Enables early-exit optimisation for this branch of the query tree.

Source

pub fn append(&mut self, parent: usize, other: Self)

Source

pub fn then<F, I>(self, func: F) -> Self
where F: FnOnce(QueryFactory) -> I, I: IntoIterator<Item = Self>,

Branch into multiple child queries using a closure.

The closure receives a QueryFactory that can create new sub-queries. Each sub-query is scoped to run only within elements matched by the current (most recently added) selector.

This is the key mechanism for structured querying; extracting hierarchical data relationships in a single streaming pass.

§Example
use scah::{Query, Save};

let query = Query::all("article", Save::none())
    .then(|article| [
        article.first("h1",      Save::only_text_content()),
        article.all("a[href]",   Save::all()),
    ])
    .build();
Source§

impl<'query> QueryBuilder<'query>

Source

pub fn build(self) -> Query<'query>

Finalise the builder and produce a compiled Query.

This computes early-exit optimisation metadata and converts the internal vectors into boxed slices. After calling build, pass the resulting Query to parse.

Trait Implementations§

Source§

impl<'query> Clone for QueryBuilder<'query>

Source§

fn clone(&self) -> QueryBuilder<'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 QueryBuilder<'query>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'query> Freeze for QueryBuilder<'query>

§

impl<'query> RefUnwindSafe for QueryBuilder<'query>

§

impl<'query> Send for QueryBuilder<'query>

§

impl<'query> Sync for QueryBuilder<'query>

§

impl<'query> Unpin for QueryBuilder<'query>

§

impl<'query> UnsafeUnpin for QueryBuilder<'query>

§

impl<'query> UnwindSafe for QueryBuilder<'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.