Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding meta queries to support combinations of queries as queries. #291

Merged
merged 6 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/x/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,51 @@ use std::fmt;
pub trait Query<X: XConn> {
/// Run this query for a given window ID.
fn run(&self, id: Xid, x: &X) -> Result<bool>;

/// Combine this query with another query using a logical AND.
sminez marked this conversation as resolved.
Show resolved Hide resolved
///
/// This follows typical short-circuiting behavior, i.e. if the first query
/// returns false, the second query will not be run.
fn and<Other>(self, other: Other) -> AndQuery<X>
where
Self: Sized + 'static,
Other: Query<X> + 'static,
sminez marked this conversation as resolved.
Show resolved Hide resolved
{
AndQuery {
first: Box::new(self),
second: Box::new(other),
_phantom: std::marker::PhantomData,
}
}

/// Combine this query with another query using a logical OR.
///
/// This follows typical short-circuiting behavior, i.e. if the first query
/// returns true, the second query will not be run.
fn or<Other>(self, other: Other) -> OrQuery<X>
where
Self: Sized + 'static,
Other: Query<X> + 'static,
{
OrQuery {
first: Box::new(self),
second: Box::new(other),
_phantom: std::marker::PhantomData,
}
}

/// Apply a logical NOT to this query.
///
/// This will invert the result of the query.
fn not(self) -> NotQuery<X>
where
Self: Sized + 'static,
{
NotQuery {
inner: Box::new(self),
_phantom: std::marker::PhantomData,
}
}
}

impl<X: XConn> fmt::Debug for Box<dyn Query<X>> {
Expand Down Expand Up @@ -98,3 +143,44 @@ where
}
}
}

/// A meta [Query] for combining two queries with a logical AND.
#[derive(Debug)]
pub struct AndQuery<X: XConn> {
first: Box<dyn Query<X>>,
second: Box<dyn Query<X>>,
_phantom: std::marker::PhantomData<X>,
}

impl<X: XConn> Query<X> for AndQuery<X> {
fn run(&self, id: Xid, x: &X) -> Result<bool> {
Ok(self.first.run(id, x)? && self.second.run(id, x)?)
}
}

/// A meta [Query] for combining two queries with a logical OR.
#[derive(Debug)]
pub struct OrQuery<X: XConn> {
first: Box<dyn Query<X>>,
second: Box<dyn Query<X>>,
_phantom: std::marker::PhantomData<X>,
}

impl<X: XConn> Query<X> for OrQuery<X> {
fn run(&self, id: Xid, x: &X) -> Result<bool> {
Ok(self.first.run(id, x)? || self.second.run(id, x)?)
}
}

/// A meta [Query] for applying a logical NOT to a query.
#[derive(Debug)]
pub struct NotQuery<X: XConn> {
inner: Box<dyn Query<X>>,
_phantom: std::marker::PhantomData<X>,
}

impl<X: XConn> Query<X> for NotQuery<X> {
fn run(&self, id: Xid, x: &X) -> Result<bool> {
Ok(!self.inner.run(id, x)?)
}
}
Loading