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 aliases IN USE, which is the only thing that can prove one: a comparison hand-written the
//! way a caller outside this crate writes one, with every type in its signature coming from this
//! module.

use super::{Conjunction, Error, KeyIn, ScalarComparison, TextComparison};
use crate::{
  At, ComplexComparison, FieldName, ScalarFilter, TextFilter,
  comparison::{
    TryFromFilter,
    sql::{
      SqlKeyIn, SqlScalarComparison, SqlTextComparison,
      tests::{render, threaded},
    },
  },
};

/// Two columns of a caller's own row. The dialect is named in this module's path and nowhere in the
/// code below, which is the whole of what the module buys.
struct Track {
  bitrate: ScalarComparison<i64>,
  codec: TextComparison<String>,
}

impl ComplexComparison<sqlx::Sqlite> for Track {
  fn conjuncts(&self) -> usize {
    self.bitrate.conjuncts() + self.codec.conjuncts()
  }

  /// The accumulator and the refusal come from the module too, and they are the half a signature
  /// cannot fake: `Conjunction<'q>` has to BE `<sqlx::Sqlite as Backend>::Builder<'q>` and `Error`
  /// its `Error`, or this impl does not typecheck against the trait it claims.
  fn apply<'q>(&'q self, at: At<'_>, builder: Conjunction<'q>) -> Result<Conjunction<'q>, Error> {
    let bitrate = at.frame(FieldName::new("bitrate"));
    let builder = self.bitrate.apply(bitrate.at(), builder)?;
    let codec = at.frame(FieldName::new("codec"));
    self.codec.apply(codec.at(), builder)
  }
}

fn track() -> Track {
  Track {
    bitrate: SqlScalarComparison::try_from_filter(ScalarFilter::<i64>::new().with_gte(1_000i64))
      .unwrap(),
    codec: SqlTextComparison::try_from_filter(
      TextFilter::<String>::new().with_contains("av1".to_owned()),
    )
    .unwrap(),
  }
}

/// An alias is the type rather than a plausible spelling of it, and an assignment is the proof: a
/// struct does not coerce, so this compiles only if the two names denote one type.
///
/// The OPERANDS are asserted beside it, because they are what the SQL cannot show — a `?` says
/// nothing about what went into it, so an alias resolving to some other `V` would render the same
/// text.
#[test]
fn the_two_spellings_are_one_type() {
  let track = track();

  let bitrate: SqlScalarComparison<i64, sqlx::Sqlite> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::Sqlite> = track.codec.clone();

  assert_eq!(bitrate.gte(), Some(&1_000));
  assert_eq!(codec.contains().map(String::as_str), Some("av1"));
  assert_eq!(bitrate, track.bitrate);
  assert_eq!(codec, track.codec);

  // And back, which is the direction a caller writes.
  let _: ScalarComparison<i64> = bitrate;
  let _: TextComparison<String> = codec;
}

/// The hand-written impl lowers exactly what the full spelling does: each half rendered on its own
/// through the family's harness, then the two joined by the accumulator the alias named.
#[test]
fn the_aliased_impl_lowers_what_the_full_form_lowers() {
  let track = track();
  let bitrate: SqlScalarComparison<i64, sqlx::Sqlite> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::Sqlite> = track.codec.clone();

  assert_eq!(
    render::<sqlx::Sqlite, _>("track_bitrate", &bitrate),
    ("\"track_bitrate\" >= ?".to_owned(), 1)
  );
  assert_eq!(
    render::<sqlx::Sqlite, _>("track_codec", &codec),
    ("instr(\"track_codec\", ?) > 0".to_owned(), 1)
  );

  assert_eq!(track.conjuncts(), 2);
  assert_eq!(
    threaded::<sqlx::Sqlite, _>("track", &track),
    "SELECT \"id\" FROM \"t\" WHERE \"track_bitrate\" >= ? AND instr(\"track_codec\", ?) > 0"
  );
}

/// `N` survives the alias, and it has to: the module fixes a dialect, and a key's WIDTH is the
/// key's own — an alias that dropped it could not be written at all, since there would be nothing
/// left to say how wide the set is.
#[test]
fn the_key_set_alias_keeps_the_width_the_module_does_not_fix() {
  let full = SqlKeyIn::<i64, sqlx::Sqlite, 2>::new(["media", "segment"], [[1i64, 10], [2, 20]]);
  let aliased: KeyIn<i64, 2> = full;

  assert_eq!(aliased.parts(), &["media", "segment"]);
  assert_eq!(aliased.keys(), [[1, 10], [2, 20]]);
  assert_eq!(
    render::<sqlx::Sqlite, _>("k", &aliased),
    (
      "(\"k_media\", \"k_segment\") IN ((?, ?), (?, ?))".to_owned(),
      4
    )
  );
}