use crate::{FirestoreQueryDirection, FirestoreQueryOrder};
pub struct FirestoreQueryOrderBuilder {}
impl FirestoreQueryOrderBuilder {
pub(crate) fn new() -> Self {
Self {}
}
#[inline]
pub fn fields<I>(&self, order_field_expr: I) -> Vec<FirestoreQueryOrder>
where
I: IntoIterator,
I::Item: FirestoreQueryOrderExpr,
{
order_field_expr
.into_iter()
.filter_map(|order| order.build_order())
.collect()
}
#[inline]
pub fn field<S>(&self, field_name: S) -> FirestoreQueryOrderFieldExpr
where
S: AsRef<str>,
{
FirestoreQueryOrderFieldExpr::new(field_name.as_ref().to_string())
}
}
pub trait FirestoreQueryOrderExpr {
fn build_order(self) -> Option<FirestoreQueryOrder>;
}
pub struct FirestoreQueryOrderFieldExpr {
field_name: String,
}
impl FirestoreQueryOrderFieldExpr {
pub(crate) fn new(field_name: String) -> Self {
Self { field_name }
}
#[inline]
pub fn asc(self) -> Option<FirestoreQueryOrder> {
self.ascending()
}
#[inline]
pub fn desc(self) -> Option<FirestoreQueryOrder> {
self.descending()
}
#[inline]
pub fn ascending(self) -> Option<FirestoreQueryOrder> {
self.direction(FirestoreQueryDirection::Ascending)
}
#[inline]
pub fn descending(self) -> Option<FirestoreQueryOrder> {
self.direction(FirestoreQueryDirection::Descending)
}
#[inline]
pub fn direction(self, direction: FirestoreQueryDirection) -> Option<FirestoreQueryOrder> {
Some(FirestoreQueryOrder::new(self.field_name, direction))
}
}
impl FirestoreQueryOrderExpr for FirestoreQueryOrder {
#[inline]
fn build_order(self) -> Option<FirestoreQueryOrder> {
Some(self)
}
}
impl<F> FirestoreQueryOrderExpr for Option<F>
where
F: FirestoreQueryOrderExpr,
{
#[inline]
fn build_order(self) -> Option<FirestoreQueryOrder> {
self.and_then(|expr| expr.build_order())
}
}