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: [`SqlEnumComparison`].

use core::{fmt::Display, marker::PhantomData};

use super::{Carrier, SqlConjunction, push_cmp, push_in};
use crate::{
  EnumFilter, TryMapFilter,
  comparison::{At, Backend, ComplexComparison, ToDatabaseComparison, TryFromFilter},
};

comparison_kind! {
  /// An [`EnumFilter`]'s storage-side twin: equality and membership, over the type this database
  /// stores the column 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.
  SqlEnumComparison {
    one { eq, ne }
    many { in_, not_in }
  }
}

impl<V, Db> SqlEnumComparison<V, Db> {
  /// 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`](super::SqlScalarComparison::try_from_filter_with),
  /// where the wall that closes is written up. This kind is where it was MEASURED: an element
  /// door's enum family is the one `ingraph/tests/flat_crossings.rs` declares a column of.
  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,
      db: PhantomData,
    })
  }
}

/// The bounds are the LOWERING's, restated — see
/// [`SqlBooleanComparison`](super::SqlBooleanComparison)'s row for what the crossing's item bound
/// asks of a kind whose lowering is one generic impl.
impl<D, V, Db> ToDatabaseComparison<V, Db> for EnumFilter<D>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
  V: Carrier<Db>,
{
  type Comparison = SqlEnumComparison<V, Db>;
}

/// The crossing with no declared converter — see
/// [`SqlScalarComparison`](super::SqlScalarComparison)'s for what the identity case buys.
impl<D, V, Db> TryFromFilter<EnumFilter<D>> for SqlEnumComparison<V, Db>
where
  V: TryFrom<D>,
{
  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: a variant compared under `eq` names one stored form, which is one child
/// row wherever that column is a key. Its operator set is the scalar's minus the ordering four, so
/// the count is doing strictly less work here and the spelling is deliberately the same one.
impl<V, Db: crate::comparison::Backend> crate::comparison::ReferentialComparison<Db>
  for SqlEnumComparison<V, Db>
{
  #[inline]
  fn is_referential(&self) -> bool {
    Self::conjuncts(self) == 1 && (self.eq.is_some() || self.in_.is_some())
  }
}

/// ONE impl for every dialect — [`SqlScalarComparison`](super::SqlScalarComparison)'s finding over a
/// strictly smaller operator set, and true for the same two reasons.
///
/// - **The SQL is identical.** This kind emits equality and membership only, and `"col" = ?` /
///   `"col" IN (?, …)` read the same in SQLite, Postgres and MySQL.
/// - **The bounds are identical, and admit the same types.** `V: Carrier<Db>`, and nothing else.
///
/// A native Postgres `ENUM` column is the case worth naming, because it looks like a difference and
/// is not one: comparing such a column against a `TEXT` bind needs a cast, and a cast is something a
/// [`Carrier`] row RENDERS (the trait is a push for exactly that reason). So it is answered one
/// layer down, at (storage type × dialect), and never reaches this impl — which is the same place
/// `u64`'s missing SQLite row is answered.
impl<V, Db> ComplexComparison<Db> for SqlEnumComparison<V, Db>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Buffer: Display>,
  V: Carrier<Db>,
{
  #[inline]
  fn conjuncts(&self) -> usize {
    Self::conjuncts(self)
  }

  fn apply<'q>(
    &'q self,
    at: At<'_>,
    mut builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    if let Some(value) = &self.eq {
      push_cmp(builder.open(), at, " = ", value, false);
    }
    if let Some(value) = &self.ne {
      push_cmp(builder.open(), at, " = ", value, true);
    }
    if let Some(values) = &self.in_ {
      push_in(builder.open(), at, values, false);
    }
    if let Some(values) = &self.not_in {
      push_in(builder.open(), at, values, true);
    }
    Ok(builder)
  }
}

#[cfg(test)]
mod tests;