Query

Struct Query 

Source
pub struct Query<'a, T: 'static> { /* private fields */ }
Expand description

A query builder for filtering, selecting, ordering, grouping, and aggregating data.

§Type Parameters

  • 'a - The lifetime of the data being queried
  • T - The type of items in the collection

§Example

let products = vec![/* ... */];
let query = Query::new(&products)
    .where_(Product::price_r(), |&price| price < 100.0)
    .order_by_float(Product::price_r());

Implementations§

Source§

impl<'a, T: 'static> Query<'a, T>

Source

pub fn new(data: &'a [T]) -> Self

Creates a new query from a slice of data.

§Arguments
  • data - A slice of items to query
§Example
let query = Query::new(&products);
Source

pub fn where_<F>( self, path: KeyPaths<T, F>, predicate: impl Fn(&F) -> bool + 'static, ) -> Self
where F: 'static,

Adds a filter predicate using a key-path.

§Arguments
  • path - The key-path to the field to filter on
  • predicate - A function that returns true for items to keep
§Example
let query = Query::new(&products)
    .where_(Product::category_r(), |cat| cat == "Electronics");
Source

pub fn all(&self) -> Vec<&T>

Returns all items matching the query filters.

§Example
let results = query.all();
Source

pub fn first(&self) -> Option<&T>

Returns the first item matching the query filters.

§Example
let first = query.first();
Source

pub fn count(&self) -> usize

Returns the count of items matching the query filters.

§Example
let count = query.count();
Source

pub fn limit(&self, n: usize) -> Vec<&T>

Returns the first n items matching the query filters.

§Arguments
  • n - The maximum number of items to return
§Example
let first_10 = query.limit(10);
Source

pub fn skip<'b>(&'b self, offset: usize) -> QueryWithSkip<'a, 'b, T>

Skips the first offset items for pagination.

§Arguments
  • offset - The number of items to skip
§Example
let page_2 = query.skip(20).limit(10);
Source

pub fn select<F>(&self, path: KeyPaths<T, F>) -> Vec<F>
where F: Clone + 'static,

Projects/selects a single field from results.

§Arguments
  • path - The key-path to the field to select
§Example
let names = query.select(Product::name_r());
Source

pub fn sum<F>(&self, path: KeyPaths<T, F>) -> F
where F: Clone + Add<Output = F> + Default + 'static,

Computes the sum of a numeric field.

§Arguments
  • path - The key-path to the numeric field
§Example
let total_price = query.sum(Product::price_r());
Source

pub fn avg(&self, path: KeyPaths<T, f64>) -> Option<f64>

Computes the average of a float field.

§Arguments
  • path - The key-path to the f64 field
§Example
let avg_price = query.avg(Product::price_r()).unwrap_or(0.0);
Source

pub fn min<F>(&self, path: KeyPaths<T, F>) -> Option<F>
where F: Ord + Clone + 'static,

Finds the minimum value of a field.

§Arguments
  • path - The key-path to the field
§Example
let min_stock = query.min(Product::stock_r());
Source

pub fn max<F>(&self, path: KeyPaths<T, F>) -> Option<F>
where F: Ord + Clone + 'static,

Finds the maximum value of a field.

§Arguments
  • path - The key-path to the field
§Example
let max_stock = query.max(Product::stock_r());
Source

pub fn min_float(&self, path: KeyPaths<T, f64>) -> Option<f64>

Finds the minimum value of a float field.

§Arguments
  • path - The key-path to the f64 field
§Example
let min_price = query.min_float(Product::price_r());
Source

pub fn max_float(&self, path: KeyPaths<T, f64>) -> Option<f64>

Finds the maximum value of a float field.

§Arguments
  • path - The key-path to the f64 field
§Example
let max_price = query.max_float(Product::price_r());
Source

pub fn exists(&self) -> bool

Checks if any items match the query filters.

§Example
let has_results = query.exists();
Source

pub fn where_after_systemtime( self, path: KeyPaths<T, SystemTime>, reference: SystemTime, ) -> Self

Filter by SystemTime being after a reference time.

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let recent = query.where_after_systemtime(Event::timestamp_r(), &cutoff_time);
Source

pub fn where_before_systemtime( self, path: KeyPaths<T, SystemTime>, reference: SystemTime, ) -> Self

Filter by SystemTime being before a reference time.

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let old = query.where_before_systemtime(Event::timestamp_r(), &cutoff_time);
Source

pub fn where_between_systemtime( self, path: KeyPaths<T, SystemTime>, start: SystemTime, end: SystemTime, ) -> Self

Filter by SystemTime being between two times (inclusive).

§Arguments
  • path - The key-path to the SystemTime field
  • start - The start time
  • end - The end time
§Example
let range = query.where_between_systemtime(Event::timestamp_r(), &start, &end);
Source§

impl<'a, T: 'static + Clone> Query<'a, T>

Source

pub fn order_by<F>(&self, path: KeyPaths<T, F>) -> Vec<T>
where F: Ord + Clone + 'static,

Orders results by a field in ascending order.

Note: This method requires T: Clone as it creates owned sorted copies.

§Arguments
  • path - The key-path to the field to order by
§Example
let sorted = query.order_by(Product::name_r());
Source

pub fn order_by_desc<F>(&self, path: KeyPaths<T, F>) -> Vec<T>
where F: Ord + Clone + 'static,

Orders results by a field in descending order.

Note: This method requires T: Clone as it creates owned sorted copies.

§Arguments
  • path - The key-path to the field to order by
§Example
let sorted = query.order_by_desc(Product::stock_r());
Source

pub fn order_by_float(&self, path: KeyPaths<T, f64>) -> Vec<T>

Orders results by a float field in ascending order.

Note: This method requires T: Clone as it creates owned sorted copies.

§Arguments
  • path - The key-path to the f64 field to order by
§Example
let sorted = query.order_by_float(Product::price_r());
Source

pub fn order_by_float_desc(&self, path: KeyPaths<T, f64>) -> Vec<T>

Orders results by a float field in descending order.

Note: This method requires T: Clone as it creates owned sorted copies.

§Arguments
  • path - The key-path to the f64 field to order by
§Example
let sorted = query.order_by_float_desc(Product::rating_r());
Source

pub fn group_by<F>(&self, path: KeyPaths<T, F>) -> HashMap<F, Vec<T>>
where F: Eq + Hash + Clone + 'static,

Groups results by a field value.

Note: This method requires T: Clone as it creates owned copies in groups.

§Arguments
  • path - The key-path to the field to group by
§Example
let by_category = query.group_by(Product::category_r());

Auto Trait Implementations§

§

impl<'a, T> Freeze for Query<'a, T>

§

impl<'a, T> !RefUnwindSafe for Query<'a, T>

§

impl<'a, T> !Send for Query<'a, T>

§

impl<'a, T> !Sync for Query<'a, T>

§

impl<'a, T> Unpin for Query<'a, T>

§

impl<'a, T> !UnwindSafe for Query<'a, T>

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.