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, CountComparison, Error, ListComparison, ListCrossingError, ScalarComparison,
  SetComparison, TextComparison,
};
use crate::{
  At, ComplexComparison, FieldName, ListFilter, ScalarFilter, SetFilter, 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::Postgres> 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::Postgres 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 `$1` 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::Postgres> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::Postgres> = 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::Postgres> = track.bitrate.clone();
  let codec: SqlTextComparison<String, sqlx::Postgres> = track.codec.clone();

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

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

/// A crossing's whole signature comes from one module — the comparison and the refusal — and the
/// parameters that stay are the ones this module does not fix.
///
/// The collection SHAPE used to be one of those parameters. It is the comparison's NAME now, so the
/// shelf carries two of them and each is spelled without a marker: `ListComparison<i64, C>` for the
/// sequence and `SetComparison<i64, C>` for the set. `C` is the ELEMENT's comparison and stays,
/// because a module fixes a dialect and says nothing about what one member is asked.
///
/// The CARDINALITY's alias is the third, and the one whose parameter no collection ever varies:
/// `CountComparison<i64>` is what both kinds hold, because a length does not follow the element.
#[test]
fn a_crossing_names_its_comparison_and_its_refusal_from_one_module() {
  type Element = ScalarComparison<i64>;
  let crossed: Result<ListComparison<i64, Element>, ListCrossingError<core::convert::Infallible>> =
    ListComparison::<i64, Element>::try_from_filter(
      ListFilter::<i64>::new()
        .with_gt([1i64])
        .with_count(ScalarFilter::new().with_gte(2)),
    );

  let crossed = crossed.unwrap();
  assert_eq!(crossed.gt(), Some(&[1i64][..]));

  let count: &CountComparison<i64> = crossed.count().expect("the cardinality crossed");
  assert_eq!(count.gte(), Some(&2i64));

  let set: SetComparison<i64, Element> =
    SetComparison::<i64, Element>::try_from_filter(SetFilter::<i64>::new().with_overlaps([1i64]))
      .unwrap();
  assert_eq!(set.overlaps(), Some(&[1i64][..]));
}

/// 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, PgDuration};

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

  assert_eq!(
    aliased.lte().map(|stored| stored.get().microseconds),
    Some(90_000_000)
  );
  assert_eq!(
    render::<sqlx::Postgres, _>("len", &aliased),
    ("\"len\" <= $1".to_owned(), 1)
  );
}