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 boolean comparison: [`MongoBooleanComparison`].

use super::{Mongo, MongoCarrier, MongoOperator, field_path, membership};
use crate::{
  BooleanFilter,
  comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
  graphql::filter::{map_operand, map_operands},
};

mongo_comparison_kind! {
  /// A [`BooleanFilter`]'s storage-side twin: equality and membership, over the type this database
  /// stores the field as.
  ///
  /// # It has a `V` where the filter has none
  ///
  /// [`BooleanFilter`] guards exactly one Rust type, so it needs no parameter. What a field STORES
  /// is a different question — a BSON `Boolean`, an `Int32` of `0`/`1`, a `String` of `"Y"`/`"N"` —
  /// and the storage form is a declaration here as everywhere else.
  ///
  /// That asymmetry is also why this kind has no operand-level `try_map` on the filter side:
  /// mapping `bool` to some `V` has no [`BooleanFilter`] to land in, and this type IS the landing
  /// place. So [`try_from_filter_with`](Self::try_from_filter_with) is the crossing's one entry
  /// point.
  MongoBooleanComparison {
    one { eq, ne }
    many { in_, not_in }
  }
}

impl<V> MongoBooleanComparison<V> {
  /// Cross a [`BooleanFilter`] into this comparison, converting each operand with `f`.
  ///
  /// The entry point a DECLARED converter uses, and — see the type's own note — the only place a
  /// boolean's operands can be mapped at all.
  pub fn try_from_filter_with<E, F>(filter: BooleanFilter, mut f: F) -> Result<Self, E>
  where
    F: FnMut(bool) -> Result<V, E>,
  {
    Ok(Self {
      eq: map_operand(filter.eq, &mut f)?,
      ne: map_operand(filter.ne, &mut f)?,
      in_: map_operands(filter.in_, &mut f)?,
      not_in: map_operands(filter.not_in, &mut f)?,
    })
  }
}

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

/// The crossing with no declared converter. `bool` stored as `bool` is the identity and cannot
/// fail; `bool` stored as an `i32` is a widening [`From`] and cannot fail either — so the common
/// boolean field reaches its comparison with an [`Infallible`](core::convert::Infallible) error
/// type.
impl<V: TryFrom<bool>> TryFromFilter<BooleanFilter> for MongoBooleanComparison<V> {
  type Error = V::Error;

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

/// The same four operators the enum kind carries, lowered 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 MongoBooleanComparison<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;