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
use core::convert::Infallible;

use super::SqlEnumComparison;
use crate::EnumFilter;

/// A downstream-shaped enum: the field type a declaration names, with a stored form of its own.
#[derive(Debug, Clone, Copy, PartialEq)]
enum MediaKind {
  Audio,
  Video,
}

/// The enum's stored form is TEXT — the case an enum reaches for most, and one no blanket `TryFrom`
/// could supply.
fn name(kind: MediaKind) -> Result<String, Infallible> {
  Ok(
    match kind {
      MediaKind::Audio => "audio",
      MediaKind::Video => "video",
    }
    .to_owned(),
  )
}

#[test]
fn a_crossing_replaces_the_variant_with_its_stored_name() {
  let filter = EnumFilter::new()
    .with_eq(MediaKind::Audio)
    .with_not_in([MediaKind::Video]);

  let crossed = SqlEnumComparison::<String, ()>::try_from_filter_with(filter, name).unwrap();

  assert_eq!(crossed.eq().map(String::as_str), Some("audio"));
  assert_eq!(crossed.not_in().unwrap(), ["video".to_owned()]);
  assert_eq!(crossed.conjuncts(), 2);
}

/// The same enum stored as a discriminant instead — the storage form is a declaration, and the
/// comparison type is the same one either way.
#[test]
fn the_same_filter_crosses_into_whatever_the_column_stores() {
  let discriminant = |kind: MediaKind| -> Result<i16, Infallible> {
    Ok(match kind {
      MediaKind::Audio => 0,
      MediaKind::Video => 1,
    })
  };

  let crossed = SqlEnumComparison::<i16, ()>::try_from_filter_with(
    EnumFilter::new().with_eq(MediaKind::Video),
    discriminant,
  )
  .unwrap();

  assert_eq!(crossed.eq(), Some(&1));
}

#[cfg(feature = "sqlite")]
mod sqlite {
  use core::{cell::RefCell, convert::Infallible};

  use super::SqlEnumComparison;
  use crate::{
    EnumFilter,
    comparison::{
      TryFromFilter,
      sql::tests::{Probe, render, threaded},
    },
  };

  /// The four operators this kind has, and the four it does not: nothing here renders an ordering
  /// comparison, because a variant set has no magnitude.
  fn all_four() -> SqlEnumComparison<i16, sqlx::Sqlite> {
    SqlEnumComparison::try_from_filter(
      EnumFilter::new()
        .with_eq(1i16)
        .with_ne(2i16)
        .with_in_([3i16, 4])
        .with_not_in([5i16]),
    )
    .unwrap()
  }

  #[test]
  fn only_equality_and_membership_render() {
    let crossed = all_four();

    assert_eq!(crossed.conjuncts(), 4);
    assert_eq!(
      render::<sqlx::Sqlite, _>("k", &crossed),
      (
        "\"k\" = ? AND NOT ( \"k\" = ? ) AND \"k\" IN (?, ?) AND NOT ( \"k\" IN (?) )".to_owned(),
        5
      )
    );
  }

  /// The threaded lowering emits the borrowed one's conjuncts, joined to the clause keyword the
  /// accumulator opened with — the first straight to it and every later one with its own ` AND `.
  #[test]
  fn the_threaded_lowering_renders_the_same_conjuncts() {
    assert_eq!(
      threaded::<sqlx::Sqlite, _>("k", &all_four()),
      "SELECT \"id\" FROM \"t\" WHERE \"k\" = ? AND NOT ( \"k\" = ? ) AND \"k\" IN (?, ?) \
       AND NOT ( \"k\" IN (?) )"
    );
  }

  /// Every operand the comparison holds reaches the builder, in slot order — the assertion the SQL
  /// cannot make, because a `?` says nothing about what went into it.
  #[test]
  fn every_operand_the_comparison_holds_is_bound() {
    let bound = RefCell::new(Vec::new());
    let crossed = SqlEnumComparison::<Probe<'_>, sqlx::Sqlite>::try_from_filter_with(
      EnumFilter::new()
        .with_eq(1i64)
        .with_ne(2i64)
        .with_in_([3i64, 4])
        .with_not_in([5i64]),
      |value| {
        Ok::<_, Infallible>(Probe {
          value,
          bound: &bound,
        })
      },
    )
    .unwrap();

    threaded::<sqlx::Sqlite, _>("k", &crossed);

    assert_eq!(*bound.borrow(), [1, 2, 3, 4, 5]);
  }
}