pub struct Filter { /* private fields */ }Expand description
Filter.
Filters efficiently match identifiers against a set of expressions, which are composed of conditions and logical operators, and compiled into a set of optimized instructions. This makes it possible to evaluate arbitrarily complex logical expressions against identifiers extremely fast.
Each Filter manages a Matcher, used to identify matching terms in
expressions in nanoseconds, and a set of conditions built from expressions,
which are checked for satisfiability by using a bitwise stack-based virtual
machine with an optimized set of instructions. Thus, the Matcher can be
thought of as the first stage, eliminating non-matching expressions, while
the condition set is the second stage, which checks whether the remaining
expressions are actually satisfied by the identifier.
§Examples
use zrx_id::{selector, Expression, Filter, Id};
// Create filter builder and insert expression
let mut builder = Filter::builder();
builder.insert(Expression::all(|expr| {
expr.with(selector!(location = "**/*.md")?)?
.with(selector!(provider = "file")?)
})?);
// Create filter from builder
let filter = builder.build()?;
// Create identifier and obtain candidate expressions
let id: Id = "zri:file:::docs:index.md:".parse()?;
for index in filter.candidates(&id)? {
println!("{index:?}");
}Implementations§
Source§impl Filter
impl Filter
Sourcepub fn builder() -> Builder
pub fn builder() -> Builder
Creates a filter builder.
§Examples
use zrx_id::Filter;
// Create filter builder
let mut builder = Filter::builder();Sourcepub fn into_builder(self) -> Builder
pub fn into_builder(self) -> Builder
Creates a filter builder from the filter.
This method allows to modify an existing Filter by converting it
back into a filter builder to insert or remove expressions.
§Examples
use zrx_id::Filter;
// Create filter
let filter = Filter::default();
// Create filter builder
let mut builder = filter.into_builder();Source§impl Filter
impl Filter
Sourcepub fn candidates<T>(&self, id: &T) -> Result<Candidates<'_>>where
T: TryToId,
pub fn candidates<T>(&self, id: &T) -> Result<Candidates<'_>>where
T: TryToId,
Returns the indices of expressions that match the identifier.
This method compares expressions part of the filter against the given identifier, and returns an iterator over the indices of the expressions that match. Note that the order of the returned indices corresponds to the order in which the expressions were added to the filter.
§Errors
Returns Error::Matcher if the identifier is invalid.
§Examples
use zrx_id::{selector, Expression, Filter, Id};
// Create filter builder and insert expression
let mut builder = Filter::builder();
builder.insert(Expression::all(|expr| {
expr.with(selector!(location = "**/*.md")?)?
.with(selector!(provider = "file")?)
})?);
// Create filter from builder
let filter = builder.build()?;
// Create identifier and obtain candidate expressions
let id: Id = "zri:file:::docs:index.md:".parse()?;
for index in filter.candidates(&id)? {
println!("{index:?}");
}Source§impl Filter
impl Filter
Sourcepub fn terms(&self) -> Terms<'_> ⓘ
pub fn terms(&self) -> Terms<'_> ⓘ
Creates an iterator over the terms.
§Examples
use zrx_id::{selector, Expression, Filter};
// Create filter builder and insert expression
let mut builder = Filter::builder();
builder.insert(Expression::any(|expr| {
expr.with(selector!(location = "**/*.jpg")?)?
.with(selector!(location = "**/*.png")?)
})?);
// Create filter from builder
let filter = builder.build()?;
// Create iterator over terms
for term in filter.terms() {
println!("{term:?}");
}