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 CROSSING — and it is almost entirely somebody else's, which is what this page is for.

use crate::{
  ComplexComparison, ConfidenceFilter, ConfidenceOperand, Nullable, ScalarFilter, Score,
  TryFromFilter,
};

/// A filter's THRESHOLD — widthless, and read at the column's own carrier by the crossing.
fn threshold(value: f64) -> ConfidenceOperand {
  ConfidenceOperand::number(value).expect("a threshold in [0, 1]")
}
use sqlx::Sqlite;

use super::SqlDetectedLanguageComparison;
use crate::primitives::lang::{
  DetectedLanguageFilter, Language, LanguageId, LanguageIdCrossingError, LanguageIdFilter,
};

/// The comparison a filter crosses into, for the one dialect this crate compiles a driver for.
type Crossed = SqlDetectedLanguageComparison<Sqlite>;

/// One tag the door must take, unwrapped.
fn tag(sent: &str) -> LanguageId {
  LanguageId::new(sent).unwrap_or_else(|refused| panic!("`{sent}` is a language tag: {refused}"))
}

/// One filter through the crossing, unwrapped.
fn crossed(filter: DetectedLanguageFilter) -> Crossed {
  Crossed::try_from_filter(filter).expect("the crossing")
}

/// **AN EMPTY FILTER OPENS NO CONJUNCT.**
#[test]
fn an_empty_filter_crosses_to_an_empty_comparison() {
  let comparison = crossed(DetectedLanguageFilter::new());

  assert_eq!(ComplexComparison::<Sqlite>::conjuncts(&comparison), 0);
  assert_eq!(comparison.confidence().gte(), None);
}

/// **THE IDENTITY'S CROSSING IS THE IDENTITY'S**, so every conjunct it opens arrives here unchanged.
///
/// A whole-identity equality is still four — one per seat, with `IS NULL` in the empty ones — and
/// nothing about being half of a detection changes that.
#[test]
fn the_identity_half_crosses_exactly_as_it_does_alone() {
  let whole =
    DetectedLanguageFilter::new().with_language(LanguageIdFilter::new().with_eq(tag("zh-Hans")));
  let comparison = crossed(whole);

  assert_eq!(ComplexComparison::<Sqlite>::conjuncts(&comparison), 4);
  assert_eq!(
    comparison.language().language().eq().map(String::as_str),
    Some("zh")
  );
  assert_eq!(
    comparison
      .language()
      .script()
      .inner()
      .eq()
      .map(String::as_str),
    Some("Hans")
  );
  assert_eq!(comparison.language().region().null(), Some(true));
}

/// **THE TWO HALVES ARE SEPARATE CONJUNCTS**, so they narrow one another and guard ONE detection.
#[test]
fn the_identity_and_the_confidence_narrow_together() {
  let filter = DetectedLanguageFilter::new()
    .with_language(
      LanguageIdFilter::new()
        .with_language(ScalarFilter::new().with_eq(Language::new("zh").expect("a language"))),
    )
    .with_confidence(ConfidenceFilter::new().with_gte(threshold(0.8)));

  let comparison = crossed(filter);

  assert_eq!(
    ComplexComparison::<Sqlite>::conjuncts(&comparison),
    2,
    "one on the language column, one on the confidence"
  );
  assert_eq!(
    comparison.confidence().gte(),
    Score::new(0.8).as_ref(),
    "the column measures in `Score`, and the widthless threshold is read at that width"
  );
}

/// **THE CONFIDENCE TAKES THE FRAMEWORK'S SIX WORDS**, every one of them well-defined because a
/// [`Score`] refuses `NaN` and the resulting order is total.
///
/// The two that are NOT here are absent from the KIND: `ConfidenceFilter` publishes no `in` and no
/// `notIn`, so a membership over a continuous measurement is a slot that does not exist rather than
/// one that answers no rows. The absence is pinned by the count — three words set, three conjuncts.
#[test]
fn the_confidence_takes_every_word_the_kind_has() {
  let filter = DetectedLanguageFilter::new().with_confidence(
    ConfidenceFilter::new()
      .with_gt(threshold(0.1))
      .with_lte(threshold(0.9))
      .with_ne(threshold(0.0)),
  );

  let comparison = crossed(filter);
  assert_eq!(ComplexComparison::<Sqlite>::conjuncts(&comparison), 3);
  assert_eq!(comparison.confidence().ne(), Some(&Score::ZERO));
}

/// **THE REFUSAL IS THE IDENTITY'S**, reused rather than restated — the same doubled equality being
/// refused in the same place, whether the identity is a column of its own or a detection's half.
#[test]
fn the_doubled_equality_refuses_with_the_identitys_own_error() {
  let doubled = DetectedLanguageFilter::new().with_language(
    LanguageIdFilter::new()
      .with_eq(tag("de-DE"))
      .with_language(ScalarFilter::new().with_eq(Language::new("de").expect("a language"))),
  );

  assert_eq!(
    Crossed::try_from_filter(doubled),
    Err(LanguageIdCrossingError::LanguageConflict)
  );

  // …and the confidence half can refuse nothing at all: its crossing is `ScoreKind`'s, whose
  // refusal belongs to the EXACT width, and `Score` is binary — so the pair's error type is the
  // identity's alone.
  let confident = DetectedLanguageFilter::new()
    .with_confidence(ConfidenceFilter::new().with_gte(threshold(1.0)))
    .with_language(
      LanguageIdFilter::new().with_script(Nullable::new(ScalarFilter::new()).with_null(true)),
    );
  assert!(Crossed::try_from_filter(confident).is_ok());
}