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::{BytesComparison, Conjunction, Error, ScalarComparison, TextComparison};
use crate::{
  At, ComplexComparison, FieldName, FixedBytesFilter, ScalarFilter, TextFilter,
  comparison::{
    TryFromFilter,
    sql::{
      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::MySql> 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::MySql 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::MySql> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::MySql> = 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::MySql> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::MySql> = track.codec.clone();

  assert_eq!(
    render::<sqlx::MySql, _>("track_bitrate", &bitrate),
    ("`track_bitrate` >= ?".to_owned(), 1)
  );
  assert_eq!(
    render::<sqlx::MySql, _>("track_codec", &codec),
    ("`track_codec` LIKE ? ESCAPE '\\\\'".to_owned(), 1)
  );

  assert_eq!(track.conjuncts(), 2);
  assert_eq!(
    threaded::<sqlx::MySql, _>("track", &track),
    "SELECT \"id\" FROM \"t\" WHERE `track_bitrate` >= ? AND `track_codec` LIKE ? \
     ESCAPE '\\\\'"
  );
}

/// A crossing's whole signature comes from one module, and what STAYS spellable outside it is the
/// operand type — the one thing a dialect module does not fix.
///
/// This test used to name a THIRD type from here, `BytesCrossingError`, and a second parameter on
/// the comparison. Both retired with the byte kinds' split: a fixed-width filter has no substring
/// field, so the family's one non-conversion refusal has nothing left to refuse and the crossing's
/// error is the converter's own. `Infallible` in the annotation is that statement made by the
/// compiler.
#[test]
fn a_crossing_names_its_comparison_from_one_module() {
  let crossed: Result<BytesComparison<Vec<u8>>, core::convert::Infallible> =
    BytesComparison::<Vec<u8>>::try_from_filter(
      FixedBytesFilter::<Vec<u8>, 4>::new().with_eq(vec![0x89u8]),
    );

  assert_eq!(crossed.unwrap().eq(), Some(&vec![0x89u8]));
}

/// The kind the three dialects do NOT share, which is why its alias is written per module instead
/// of by `dialect_aliases!`.
#[cfg(feature = "jiff")]
#[test]
fn the_duration_alias_is_the_kind_the_dialects_do_not_share() {
  use jiff::SignedDuration;

  use crate::{DurationFilter, comparison::sql::MySqlDuration};

  let aliased: super::DurationComparison<MySqlDuration> =
    super::DurationComparison::try_from_filter(
      DurationFilter::new().with_lte(SignedDuration::from_secs(90)),
    )
    .unwrap();

  assert_eq!(
    aliased.lte(),
    Some(&MySqlDuration::try_from(SignedDuration::from_secs(90)).unwrap())
  );
  assert_eq!(
    render::<sqlx::MySql, _>("len", &aliased),
    ("`len` <= ?".to_owned(), 1)
  );
}