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(), |&price| price < 100.0)
    .order_by_float(Product::price());

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(), |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());
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());
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()).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());
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());
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());
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());
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(), &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(), &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(), &start, &end);
Source§

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

Source

pub fn where_after<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, reference: DateTime<Tz>, ) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being after a reference time.

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

pub fn where_before<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, reference: DateTime<Tz>, ) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being before a reference time.

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

pub fn where_between<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, start: DateTime<Tz>, end: DateTime<Tz>, ) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being between two times (inclusive).

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

pub fn where_today<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, now: DateTime<Tz>, ) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being today.

§Arguments
  • path - The key-path to the DateTime field
  • now - The current DateTime to compare against
§Example
let today = query.where_today(Event::timestamp(), &Utc::now());
Source

pub fn where_year<Tz>(self, path: KeyPaths<T, DateTime<Tz>>, year: i32) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime year.

§Arguments
  • path - The key-path to the DateTime field
  • year - The year to filter by
§Example
let this_year = query.where_year(Event::timestamp(), 2024);
Source

pub fn where_month<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, month: u32, ) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime month.

§Arguments
  • path - The key-path to the DateTime field
  • month - The month to filter by (1-12)
§Example
let december = query.where_month(Event::timestamp(), 12);
Source

pub fn where_day<Tz>(self, path: KeyPaths<T, DateTime<Tz>>, day: u32) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime day.

§Arguments
  • path - The key-path to the DateTime field
  • day - The day to filter by (1-31)
§Example
let first = query.where_day(Event::timestamp(), 1);
Source

pub fn where_weekend<Tz>(self, path: KeyPaths<T, DateTime<Tz>>) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by weekend dates (Saturday and Sunday).

§Arguments
  • path - The key-path to the DateTime field
§Example
let weekend_events = query.where_weekend(Event::timestamp());
Source

pub fn where_weekday<Tz>(self, path: KeyPaths<T, DateTime<Tz>>) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by weekday dates (Monday through Friday).

§Arguments
  • path - The key-path to the DateTime field
§Example
let weekday_events = query.where_weekday(Event::timestamp());
Source

pub fn where_business_hours<Tz>(self, path: KeyPaths<T, DateTime<Tz>>) -> Self
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by business hours (9 AM - 5 PM).

§Arguments
  • path - The key-path to the DateTime field
§Example
let business_hours = query.where_business_hours(Event::timestamp());
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());
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());
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());
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());
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());
Source

pub fn min_timestamp(&self, path: KeyPaths<T, i64>) -> Option<i64>

Finds the minimum i64 timestamp value.

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let earliest = query.min_timestamp(Event::created_at());
Source

pub fn max_timestamp(&self, path: KeyPaths<T, i64>) -> Option<i64>

Finds the maximum i64 timestamp value.

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let latest = query.max_timestamp(Event::created_at());
Source

pub fn avg_timestamp(&self, path: KeyPaths<T, i64>) -> Option<i64>

Calculates the average of i64 timestamp values.

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let avg_timestamp = query.avg_timestamp(Event::created_at()).unwrap_or(0);
Source

pub fn sum_timestamp(&self, path: KeyPaths<T, i64>) -> i64

Calculates the sum of i64 timestamp values.

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let total_timestamp = query.sum_timestamp(Event::created_at());
Source

pub fn count_timestamp(&self, path: KeyPaths<T, i64>) -> usize

Counts the number of non-null i64 timestamp values.

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let timestamp_count = query.count_timestamp(Event::created_at());
Source

pub fn where_after_timestamp( self, path: KeyPaths<T, i64>, reference: i64, ) -> Self

Filters by i64 timestamp being after a reference timestamp.

§Arguments
  • path - The key-path to the i64 timestamp field
  • reference - The reference timestamp to compare against
§Example
let recent = query.where_after_timestamp(Event::created_at(), cutoff_timestamp);
Source

pub fn where_before_timestamp( self, path: KeyPaths<T, i64>, reference: i64, ) -> Self

Filters by i64 timestamp being before a reference timestamp.

§Arguments
  • path - The key-path to the i64 timestamp field
  • reference - The reference timestamp to compare against
§Example
let old = query.where_before_timestamp(Event::created_at(), cutoff_timestamp);
Source

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

Filters by i64 timestamp being between two timestamps (inclusive).

§Arguments
  • path - The key-path to the i64 timestamp field
  • start - The start timestamp
  • end - The end timestamp
§Example
let range = query.where_between_timestamp(Event::created_at(), start_ts, end_ts);
Source

pub fn where_last_days_timestamp( self, path: KeyPaths<T, i64>, days: i64, ) -> Self

Filters by i64 timestamp being within the last N days from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • days - Number of days to look back
§Example
let recent = query.where_last_days_timestamp(Event::created_at(), 30);
Source

pub fn where_next_days_timestamp( self, path: KeyPaths<T, i64>, days: i64, ) -> Self

Filters by i64 timestamp being within the next N days from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • days - Number of days to look ahead
§Example
let upcoming = query.where_next_days_timestamp(Event::scheduled_at(), 7);
Source

pub fn where_last_hours_timestamp( self, path: KeyPaths<T, i64>, hours: i64, ) -> Self

Filters by i64 timestamp being within the last N hours from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • hours - Number of hours to look back
§Example
let recent = query.where_last_hours_timestamp(Event::created_at(), 24);
Source

pub fn where_next_hours_timestamp( self, path: KeyPaths<T, i64>, hours: i64, ) -> Self

Filters by i64 timestamp being within the next N hours from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • hours - Number of hours to look ahead
§Example
let upcoming = query.where_next_hours_timestamp(Event::scheduled_at(), 2);
Source

pub fn where_last_minutes_timestamp( self, path: KeyPaths<T, i64>, minutes: i64, ) -> Self

Filters by i64 timestamp being within the last N minutes from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • minutes - Number of minutes to look back
§Example
let recent = query.where_last_minutes_timestamp(Event::created_at(), 60);
Source

pub fn where_next_minutes_timestamp( self, path: KeyPaths<T, i64>, minutes: i64, ) -> Self

Filters by i64 timestamp being within the next N minutes from now.

§Arguments
  • path - The key-path to the i64 timestamp field
  • minutes - Number of minutes to look ahead
§Example
let upcoming = query.where_next_minutes_timestamp(Event::scheduled_at(), 30);
Source

pub fn order_by_timestamp(&self, path: KeyPaths<T, i64>) -> Vec<T>

Orders results by i64 timestamp in ascending order (oldest first).

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let sorted = query.order_by_timestamp(Event::created_at());
Source

pub fn order_by_timestamp_desc(&self, path: KeyPaths<T, i64>) -> Vec<T>

Orders results by i64 timestamp in descending order (newest first).

§Arguments
  • path - The key-path to the i64 timestamp field
§Example
let sorted = query.order_by_timestamp_desc(Event::created_at());

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.