ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
Documentation
//! The engine's own proofs: what it says, what it refuses to say, and where the cutoff sits.
//!
//! The POSITIONS are proved through the grammars themselves — see `tests/near_misses.rs`, which
//! reads real declarations. What is proved here is the reader, which is one function and has to be
//! pinned at its boundaries: a rule stated in a doc comment and not in a test is a rule the next
//! edit is free to move.

use ingraph_codegen::{
  aggregate::Measure,
  element::ElementFamily,
  enumeration::{Namespace, StorageForm},
  index::{Dedup, Fold},
  prelude::{DatabaseKind, Label, TracingLevel},
};

use super::{did_you_mean, word};

/// The whole point, at the ruling's own example.
#[test]
fn a_near_word_is_suggested() {
  assert_eq!(
    did_you_mean("sortble", ["scalar", "string", "bytes", "enum", "sortable"]),
    " — did you mean `sortable`?"
  );
}

/// And the other half of it: a word that is near NOTHING leaves the sentence exactly as it was.
#[test]
fn a_far_word_stays_silent() {
  assert_eq!(
    did_you_mean("hippopotamus", ["scalar", "string", "bytes", "enum"]),
    ""
  );
  assert_eq!(did_you_mean("own", ["ask", "key", "out", "set"]), "");
}

/// An empty vocabulary is a position that takes NO words, and it says nothing rather than reaching
/// for the nearest of none.
#[test]
fn a_position_with_no_vocabulary_says_nothing() {
  let none: [&str; 0] = [];
  assert_eq!(did_you_mean("anything", none), "");
}

/// THE RAW-IDENT ROW. `r#type` is the word `type`, which is what a schema grammar means by a Rust
/// keyword, so the reader lands on it at distance zero and says the word.
#[test]
fn the_raw_spelling_of_a_word_the_position_takes_is_suggested() {
  assert_eq!(
    did_you_mean("r#type", ["field", "type"]),
    " — did you mean `type`?"
  );
  // And the same word read off a path, which is how every caller reaches it.
  let raw: syn::Path = syn::parse_quote!(r#type);
  assert_eq!(word(&raw).as_deref(), Some("type"));
  let escaped: syn::Path = syn::parse_quote!(r#enum);
  assert_eq!(word(&escaped).as_deref(), Some("enum"));
}

/// A path with several segments named no word, so there is nothing for a distance to be measured
/// from — the position's own sentence is already about the shape.
#[test]
fn a_path_of_several_segments_names_no_word() {
  let path: syn::Path = syn::parse_quote!(app::hnsw::Hnsw);
  assert_eq!(word(&path), None);
}

/// THE CUTOFF, pinned at both of its rules. One edit is a typo at three characters and two is not;
/// two is a typo at four; three is a typo at no length.
#[test]
fn the_cutoff_is_two_edits_and_half_the_word() {
  // Three characters, one edit — inside.
  assert_eq!(did_you_mean("asl", ["ask"]), " — did you mean `ask`?");
  // Three characters, two edits — outside, because less than half of what was written survives.
  assert_eq!(did_you_mean("owk", ["ask"]), "");
  // Four characters, two edits — inside.
  assert_eq!(did_you_mean("lixt", ["list"]), " — did you mean `list`?");
  assert_eq!(did_you_mean("lixx", ["list"]), " — did you mean `list`?");
  // Any length, three edits — outside.
  assert_eq!(did_you_mean("lixxx", ["list"]), "");
  assert_eq!(did_you_mean("sortabxxx", ["sortable"]), "");
}

/// The CLOSEST word wins, and a tie goes to the one the position lists first — a deterministic
/// answer rather than whichever the walk happened to reach last.
#[test]
fn the_closest_word_wins_and_a_tie_goes_to_the_first() {
  assert_eq!(
    did_you_mean("orde_by", ["order", "order_by"]),
    " — did you mean `order_by`?"
  );
  // `copx` is one edit from both, and `copy` is written first.
  assert_eq!(
    did_you_mean("copx", ["copy", "cop_"]),
    " — did you mean `copy`?"
  );
}

/// THE TUNING MEASUREMENT the ruling asks to be lettered: at this cutoff, no word of a shipped
/// vocabulary is a near miss of a DIFFERENT word of the same vocabulary.
///
/// That is the property a cutoff has to have to be usable at all. If two legal words of one
/// position were within it, then every author who wrote one correctly would be a near miss of the
/// other — and the day the grammar grew such a pair, the suggestion at that position would start
/// pointing readers away from the word they meant.
///
/// The framework's own closed tables plus the widest hand-written positions, which are where a
/// collision would first appear — and the poll entry's BARE words, which is the one position whose
/// vocabulary a rework has actually widened.
#[test]
fn the_cutoff_keeps_every_shipped_vocabulary_apart() {
  let vocabularies: [Vec<&str>; 15] = [
    Label::ALL.into_iter().map(Label::as_str).collect(),
    DatabaseKind::ALL
      .into_iter()
      .map(DatabaseKind::as_str)
      .collect(),
    TracingLevel::ALL
      .into_iter()
      .map(TracingLevel::as_str)
      .collect(),
    StorageForm::ALL
      .into_iter()
      .map(StorageForm::as_str)
      .collect(),
    Namespace::ALL.into_iter().map(Namespace::as_str).collect(),
    ElementFamily::ALL
      .into_iter()
      .map(ElementFamily::word)
      .collect(),
    Fold::WORDS.to_vec(),
    Dedup::WORDS.to_vec(),
    Measure::SHIPPED.to_vec(),
    // The record's own head, its field's, and a column's database block — the widest hand-written
    // positions in the crate.
    vec![
      "key",
      "lineage",
      "persist",
      "graphql",
      "derived",
      "implements",
      "source",
      "subscribe",
      "emits",
      "poll",
      "tracing",
      "teardown",
      "async",
      "crate",
    ],
    vec![
      "id", "copy", "repeated", "optional", "persist", "graphql", "ref", "key", "lineage", "label",
    ],
    vec![
      "rename", "indexed", "unique", "copy", "flatten", "type", "edge", "from", "try_from", "into",
      "try_into",
    ],
    // The `poll(…)` entry's bare position, where the body word joined the mode words: a second bare
    // word is measured against these, so a legal one that was near another would send an author who
    // spelled it right towards the wrong road.
    crate::parse::BARE_POLL_WORDS.to_vec(),
    // The AGGREGATE's two positions. Its head carries the store words beside its own, so the pin is
    // over the union a reader is actually offered rather than over the three words this grammar
    // added — a collision between `measure` and `mysql` would be as bad as one between two of the
    // new words, and only the union can see it.
    crate::aggregate::head_vocabulary(),
    crate::aggregate::field_vocabulary(),
  ];
  for vocabulary in &vocabularies {
    for word in vocabulary {
      let others = vocabulary.iter().filter(|other| *other != word);
      assert_eq!(
        did_you_mean(word, others),
        "",
        "`{word}` is a near miss of another word of its own position: {vocabulary:?}"
      );
    }
  }
}