cat_gateway/db/event/common/
query_limits.rsuse std::fmt::Display;
use crate::service::common::types::generic::query::pagination::{Limit, Page};
pub(crate) struct QueryLimits(QueryLimitsInner);
enum QueryLimitsInner {
All,
Limit(u64),
LimitAndOffset(u64, u64),
}
impl Display for QueryLimits {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
QueryLimitsInner::All => write!(f, ""),
QueryLimitsInner::Limit(limit) => write!(f, "LIMIT {limit}"),
QueryLimitsInner::LimitAndOffset(limit, offset) => {
write!(f, "LIMIT {limit} OFFSET {offset}")
},
}
}
}
impl QueryLimits {
#[allow(dead_code)]
pub(crate) const ALL: QueryLimits = Self(QueryLimitsInner::All);
#[allow(dead_code)]
pub(crate) const ONE: QueryLimits = Self(QueryLimitsInner::Limit(1));
pub(crate) fn new(limit: Option<Limit>, page: Option<Page>) -> Self {
match (limit, page) {
(Some(limit), Some(page)) => {
Self(QueryLimitsInner::LimitAndOffset(limit.into(), page.into()))
},
(Some(limit), None) => {
Self(QueryLimitsInner::LimitAndOffset(
limit.into(),
Page::default().into(),
))
},
(None, Some(page)) => {
Self(QueryLimitsInner::LimitAndOffset(
Limit::default().into(),
page.into(),
))
},
(None, None) => {
Self(QueryLimitsInner::LimitAndOffset(
Limit::default().into(),
Page::default().into(),
))
},
}
}
}