Skip to content

Commit

Permalink
feat(query): add limit parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSpeitel committed Jan 25, 2024
1 parent a4579c8 commit 89201fa
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod selectors;
use std::num::NonZeroUsize;

pub use selectors::*;

use crate::data::BoxedData;
Expand All @@ -13,14 +15,28 @@ pub mod prelude {

#[derive(Default, Debug)]
pub struct Query {
/// The selector to use to select links.
selector: LinkSelector,
/// The maximum number of results to return.
/// `None` means no limit.
limit: Option<NonZeroUsize>,
}

impl Query {
#[inline]
#[must_use]
pub fn new(selector: LinkSelector) -> Self {
Query { selector }
pub const fn new(selector: LinkSelector) -> Self {
Query {
selector,
limit: None,
}
}

#[inline]
#[must_use]
pub const fn with_limit(mut self, limit: usize) -> Self {
self.limit = NonZeroUsize::new(limit);
self
}

#[inline]
Expand All @@ -32,15 +48,24 @@ impl Query {

#[inline]
#[must_use]
pub fn build_unoptimized(self) -> Self {
pub const fn build_unoptimized(self) -> Self {
self
}

#[inline]
#[must_use]
pub fn selector(&self) -> &LinkSelector {
pub const fn selector(&self) -> &LinkSelector {
&self.selector
}

#[inline]
#[must_use]
pub const fn limit(&self) -> usize {
match self.limit {
Some(limit) => limit.get(),
None => usize::MAX,
}
}
}

impl<L: Link + ?Sized> Selector<L> for Query {
Expand All @@ -55,7 +80,15 @@ impl<L: Link + ?Sized> Selector<L> for Query {
}

impl From<LinkSelector> for Query {
#[inline]
fn from(value: LinkSelector) -> Self {
Self::new(value)
}
}

impl From<DataSelector> for Query {
#[inline]
fn from(value: DataSelector) -> Self {
Self::new(LinkSelector::Target(value))
}
}

0 comments on commit 89201fa

Please sign in to comment.