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 enum comparison: [`MongoEnumComparison`].

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

mongo_comparison_kind! {
  /// An [`EnumFilter`]'s storage-side twin: equality and membership, over the type this database
  /// stores the field as — a text name or an integer discriminant, whichever the field declared.
  ///
  /// No ordering, for the reason the filter has none: a variant set has no magnitude, and the order
  /// a discriminant would impose is an artefact of the declaration rather than a fact about the
  /// values.
  MongoEnumComparison {
    one { eq, ne }
    many { in_, not_in }
  }
}

impl<V> MongoEnumComparison<V> {
  /// Cross an [`EnumFilter`] into this comparison, converting each operand with `f`.
  ///
  /// The entry point a DECLARED converter uses, and the one an enum reaches for most: a variant's
  /// stored form is almost always something the enum's own code writes.
  ///
  /// The filter is a BOUND rather than a spelling — see
  /// [`SqlScalarComparison::try_from_filter_with`](crate::comparison::sql::SqlScalarComparison::try_from_filter_with),
  /// where the wall that closes is written up.
  pub fn try_from_filter_with<D, E, F, X>(filter: X, f: F) -> Result<Self, E>
  where
    X: TryMapFilter<D, V, Mapped = EnumFilter<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,
    })
  }
}

/// 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 EnumFilter<D> {
  type Comparison = MongoEnumComparison<V>;
}

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

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

/// The enum kind answers, as its SQL twin does and over the same four operators.
impl<V> crate::comparison::ReferentialComparison<Mongo> for MongoEnumComparison<V> {
  #[inline]
  fn is_referential(&self) -> bool {
    Self::conjuncts(self) == 1 && (self.eq.is_some() || self.in_.is_some())
  }
}

/// The four operators this kind carries are four of the scalar kind's eight, and they lower the
/// same way — see [`MongoScalarComparison`](super::MongoScalarComparison) for the two places the
/// one-to-one mapping is not the identity it looks like.
impl<V: MongoCarrier> ComplexComparison<Mongo> for MongoEnumComparison<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)] {
      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;