ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! The flags comparison: [`MongoFlagsComparison`].

use super::{Mongo, MongoCarrier, MongoOperator, field_path, membership};
use crate::{
  FlagsFilter, TryMapFilter,
  comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
};

mongo_comparison_kind! {
  /// A [`FlagsFilter`]'s storage-side twin on MongoDB: the whole value compared or enumerated, and
  /// three questions about its BITS, over the integer this database stores the field as.
  ///
  /// SEVEN operators and no sugar, for the reason the SQL twin has seven: the sentences and the
  /// per-flag fan are folded at the wire, so a lowering sees one set of words whatever a document
  /// wrote.
  ///
  /// No ordering, as the enum kind has none.
  MongoFlagsComparison {
    one { eq, ne, has_all, has_any, has_none }
    many { in_, not_in }
  }
}

impl<V> MongoFlagsComparison<V> {
  /// Cross a [`FlagsFilter`] into this comparison, converting each operand with `f`.
  ///
  /// The entry point a DECLARED converter uses — see
  /// [`SqlFlagsComparison::try_from_filter_with`](crate::comparison::sql::SqlFlagsComparison::try_from_filter_with).
  pub fn try_from_filter_with<D, E, F, X>(filter: X, f: F) -> Result<Self, E>
  where
    X: TryMapFilter<D, V, Mapped = FlagsFilter<V>>,
    F: FnMut(D) -> Result<V, E>,
  {
    let stored = filter.try_map_filter(f)?;
    Ok(Self {
      eq: stored.eq,
      ne: stored.ne,
      in_: stored.in_,
      not_in: stored.not_in,
      has_all: stored.has_all,
      has_any: stored.has_any,
      has_none: stored.has_none,
    })
  }
}

/// One backend, so one row — and the header is concrete for the coherence reason
/// [`MongoScalarComparison`](super::MongoScalarComparison)'s records.
impl<D, V: MongoCarrier> ToDatabaseComparison<V, Mongo> for FlagsFilter<D> {
  type Comparison = MongoFlagsComparison<V>;
}

/// The crossing with no declared converter — see
/// [`MongoScalarComparison`](super::MongoScalarComparison)'s for what the identity case buys.
impl<D, V: TryFrom<D>> TryFromFilter<FlagsFilter<D>> for MongoFlagsComparison<V> {
  type Error = V::Error;

  fn try_from_filter(filter: FlagsFilter<D>) -> Result<Self, Self::Error> {
    Self::try_from_filter_with(filter, V::try_from)
  }
}

/// The flags kind answers as its SQL twin does, and narrows for the same reason: a mask word names
/// a SET of stored values, so no rewrite onto a parent's foreign key exists for it.
impl<V> crate::comparison::ReferentialComparison<Mongo> for MongoFlagsComparison<V> {
  #[inline]
  fn is_referential(&self) -> bool {
    Self::conjuncts(self) == 1 && (self.eq.is_some() || self.in_.is_some())
  }
}

/// **The three mask words are NATIVE here**, and that is the one place this kind's two lowerings
/// genuinely differ.
///
/// SQL has no bitwise PREDICATE, so its twin builds an arithmetic subject — `(col & ?) = ?` — and
/// carries a parenthesis to survive two dialects' precedence. MongoDB's query language carries the
/// predicates themselves, so `hasAll`/`hasAny`/`hasNone` are `$bitsAllSet`, `$bitsAnySet` and
/// `$bitsAllClear` written straight onto the field, with the mask as their operand.
///
/// The vacuous cases agree with the SQL's and with `bitflags`' own reading, which is what makes the
/// two lowerings one semantics rather than two that happen to look alike: over an empty mask
/// `$bitsAllSet` matches every document, `$bitsAnySet` matches none, and `$bitsAllClear` matches
/// every one — the same TRUE, FALSE, TRUE the SQL renders.
///
/// The four remaining words are four of the scalar kind's eight and lower the same way.
impl<V: MongoCarrier> ComplexComparison<Mongo> for MongoFlagsComparison<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.has_all, MongoOperator::BitsAllSet),
      (&self.has_any, MongoOperator::BitsAnySet),
      (&self.has_none, MongoOperator::BitsAllClear),
    ] {
      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;