use core::convert::Infallible;
use super::{Mongo, MongoCarrier, MongoOperator, field_path, membership};
use crate::{
BytesFilter, FixedBytesFilter, TryMapFilter,
comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
graphql::filter::{ByteOperands, ByteSlots},
};
pub(crate) trait MongoByteRefusal {
fn refused<E>(&self) -> Option<MongoBytesCrossingError<E>>;
}
impl<B, I> MongoByteRefusal for BytesFilter<B, I> {
fn refused<E>(&self) -> Option<MongoBytesCrossingError<E>> {
for (operand, operator) in [
(self.contains(), "contains"),
(self.not_contains(), "not_contains"),
] {
if operand.is_some() {
return Some(MongoBytesCrossingError::Substring(operator));
}
}
for (operand, operator) in [
(self.gt(), "gt"),
(self.gte(), "gte"),
(self.lt(), "lt"),
(self.lte(), "lte"),
] {
if operand.is_some() {
return Some(MongoBytesCrossingError::VariableOrdering(operator));
}
}
None
}
}
impl<B, const N: usize, I> MongoByteRefusal for FixedBytesFilter<B, N, I> {
#[inline]
fn refused<E>(&self) -> Option<MongoBytesCrossingError<E>> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MongoBytesCrossingError<E> {
Operand(E),
Substring(&'static str),
VariableOrdering(&'static str),
}
impl<E: core::fmt::Display> core::fmt::Display for MongoBytesCrossingError<E> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Operand(error) => error.fmt(f),
Self::Substring(operator) => write!(
f,
"operator {operator:?} is a byte-substring match, and MongoDB's only substring form reads a \
string rather than binary data"
),
Self::VariableOrdering(operator) => write!(
f,
"operator {operator:?} orders a variable-width binary column, which MongoDB compares by \
length before bytes and this crate will not answer approximately"
),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for MongoBytesCrossingError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Operand(error) => Some(error),
Self::Substring(_) | Self::VariableOrdering(_) => None,
}
}
}
mongo_comparison_kind! {
MongoBytesComparison {
one { eq, ne, gt, gte, lt, lte }
many { in_, not_in }
}
}
impl<V> MongoBytesComparison<V> {
#[allow(private_bounds)]
pub fn try_from_filter_with<D, E, F, X, M>(
filter: X,
f: F,
) -> Result<Self, MongoBytesCrossingError<E>>
where
X: TryMapFilter<D, D, Mapped = M>,
M: MongoByteRefusal + TryMapFilter<D, V, Mapped: ByteSlots<V>>,
F: FnMut(D) -> Result<V, E>,
{
let filter = match filter.try_map_filter(Ok::<D, Infallible>) {
Ok(filter) => filter,
Err(never) => match never {},
};
Self::crossed(filter, f)
}
fn crossed<D, E, F, M>(filter: M, f: F) -> Result<Self, MongoBytesCrossingError<E>>
where
M: MongoByteRefusal + TryMapFilter<D, V, Mapped: ByteSlots<V>>,
F: FnMut(D) -> Result<V, E>,
{
if let Some(refusal) = filter.refused() {
return Err(refusal);
}
let stored = filter
.try_map_filter(f)
.map_err(MongoBytesCrossingError::Operand)?
.byte_operands();
Ok(Self::from_operands(stored))
}
fn from_operands(operands: ByteOperands<V>) -> Self {
Self {
eq: operands.eq,
ne: operands.ne,
gt: operands.gt,
gte: operands.gte,
lt: operands.lt,
lte: operands.lte,
in_: operands.in_,
not_in: operands.not_in,
}
}
}
impl<D, V: MongoCarrier, I> ToDatabaseComparison<V, Mongo> for BytesFilter<D, I> {
type Comparison = MongoBytesComparison<V>;
}
impl<D, V: MongoCarrier, const N: usize, I> ToDatabaseComparison<V, Mongo>
for FixedBytesFilter<D, N, I>
{
type Comparison = MongoBytesComparison<V>;
}
impl<D, V: TryFrom<D>, I> TryFromFilter<BytesFilter<D, I>> for MongoBytesComparison<V> {
type Error = MongoBytesCrossingError<V::Error>;
fn try_from_filter(filter: BytesFilter<D, I>) -> Result<Self, Self::Error> {
Self::crossed(filter, V::try_from)
}
}
impl<D, V: TryFrom<D>, const N: usize, I> TryFromFilter<FixedBytesFilter<D, N, I>>
for MongoBytesComparison<V>
{
type Error = V::Error;
fn try_from_filter(filter: FixedBytesFilter<D, N, I>) -> Result<Self, Self::Error> {
Ok(Self::from_operands(
filter.try_map(V::try_from)?.byte_operands(),
))
}
}
impl<V> crate::comparison::ReferentialComparison<Mongo> for MongoBytesComparison<V> {
#[inline]
fn is_referential(&self) -> bool {
Self::conjuncts(self) == 1 && (self.eq.is_some() || self.in_.is_some())
}
}
impl<V: MongoCarrier> ComplexComparison<Mongo> for MongoBytesComparison<V> {
#[inline]
fn conjuncts(&self) -> usize {
Self::conjuncts(self)
}
fn apply<'q>(
&'q self,
at: At<'_>,
mut builder: <Mongo as Backend>::Builder<'q>,
) -> Result<<Mongo as Backend>::Builder<'q>, <Mongo as Backend>::Error> {
let field = field_path(at);
for (slot, op) in [
(&self.eq, MongoOperator::Eq),
(&self.ne, MongoOperator::Ne),
(&self.gt, MongoOperator::Gt),
(&self.gte, MongoOperator::Gte),
(&self.lt, MongoOperator::Lt),
(&self.lte, MongoOperator::Lte),
] {
if let Some(value) = slot {
builder.merge(&field, op, value.to_bson());
}
}
for (slot, op) in [
(&self.in_, MongoOperator::In),
(&self.not_in, MongoOperator::Nin),
] {
if let Some(values) = slot {
builder.merge(&field, op, membership(values));
}
}
Ok(builder)
}
}
#[cfg(test)]
mod tests;