ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What a SLOT is called, what a shipped word answers, and how a binding is carried.

use quote::format_ident;

use super::{Engine, EngineBinding, EngineKind, FieldEngines, Slot};
use crate::model::NodeKind;

/// A slot for `<index>`, `<field>`, `<engine>`.
fn slot(field: &str, engine: &str) -> Slot {
  Slot {
    field: format_ident!("{field}"),
    engine: format_ident!("{engine}"),
  }
}

/// One field's declaration, as the derive would hand it over.
fn stored(field: &str, engines: &[&str], fanout: bool) -> FieldEngines {
  FieldEngines {
    field: format_ident!("{field}"),
    engines: engines
      .iter()
      .map(|word| Engine::bare(format_ident!("{word}")))
      .collect(),
    fanout,
  }
}

#[test]
fn a_slot_name_carries_the_index_the_field_and_the_engine() {
  let name = slot("image_embedding", "hnsw").name(&format_ident!("KeyframeVectors"));
  assert_eq!(name, "keyframe_vectors__image_embedding__hnsw");
}

/// The separator is what keeps two slots from minting one name, and a SINGLE underscore would not:
/// a field name may contain one, so `a_b × c` and `a × b_c` would collide — and two slots sharing a
/// name is two corpora sharing a store, the wrong-write class the whole brand exists for.
#[test]
fn the_double_underscore_is_what_keeps_two_slots_apart() {
  let index = format_ident!("Doc");
  let one = slot("a_b", "c").name(&index);
  let other = slot("a", "b_c").name(&index);
  assert_ne!(one, other);
  assert_eq!(one, "doc__a_b__c");
  assert_eq!(other, "doc__a__b_c");
}

/// The name is a legal SLOT NAME — the charset `ingraph::SlotName` refuses at a `const`. A mint that
/// produced anything else would be `E0080` inside an expansion, at a span the declaration cannot
/// edit.
#[test]
fn a_minted_slot_name_is_inside_the_slot_charset() {
  let name = slot("title", "tantivy").name(&format_ident!("SearchDocument"));
  assert!(!name.is_empty());
  assert!(
    name
      .bytes()
      .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
  );
  assert!(!name.as_bytes()[0].is_ascii_digit());
}

#[test]
fn a_slot_marker_is_the_index_the_field_and_the_engine_in_camel_case() {
  let marker = slot("image_embedding", "hnsw").marker(&format_ident!("Keyframe"));
  assert_eq!(marker, format_ident!("KeyframeImageEmbeddingHnsw"));
}

/// The SLOT SET is the product, in declared order — which is the whole of ruling 4 as the model
/// carries it.
#[test]
fn the_slot_set_is_the_field_crossed_with_the_engine() {
  let declared = stored("title", &["tantivy", "hnsw"], false);
  let slots = declared.slots();
  assert_eq!(slots.len(), 2);
  assert_eq!(slots[0], slot("title", "tantivy"));
  assert_eq!(slots[1], slot("title", "hnsw"));
}

/// An election that named NO engine is a field with no slot — legal, and the portable case: the
/// deployment says where it lives.
#[test]
fn a_field_that_named_no_engine_declares_no_slot() {
  assert!(stored("title", &[], false).slots().is_empty());
}

/// TWO FIELDS sharing an engine repeat the word, and that is two slots rather than one. The
/// distinguishing half is the field, which is why it is in the name.
#[test]
fn two_fields_sharing_one_engine_are_two_slots() {
  let index = format_ident!("Doc");
  let title = stored("title", &["tantivy"], false);
  let body = stored("body", &["tantivy"], false);
  let names: Vec<String> = title
    .slots()
    .into_iter()
    .chain(body.slots())
    .map(|slot| slot.name(&index))
    .collect();
  assert_eq!(names, ["doc__title__tantivy", "doc__body__tantivy"]);
}

/// PORTABILITY and FANOUT are the same declaration with one word between them, and the model is
/// where the difference is visible: the slot SET is identical, and the liveness is not.
#[test]
fn portability_and_fanout_declare_one_slot_set_and_differ_in_liveness() {
  let portable = stored("title", &["tantivy", "hnsw"], false);
  let fanned = stored("title", &["tantivy", "hnsw"], true);
  assert_eq!(portable.slots(), fanned.slots());
  assert!(!portable.fanout);
  assert!(fanned.fanout);
}

/// The GATE is mechanical from the word, which is what keeps the word a declaration writes, the
/// feature the framework carries and the gate the impls ride from drifting into three engines.
///
/// FEATURE-CARRYING engines only. An engine at the root under nothing has no gate to be mechanical
/// from, and the pair below says which is which rather than letting a `None` pass unexamined.
#[test]
fn a_shipped_engines_gate_is_its_own_word_where_it_has_one() {
  assert_eq!(
    EngineKind::Tantivy.gate(),
    Some(format_ident!("if_tantivy"))
  );
  assert_eq!(EngineKind::Fs.gate(), None);
}

/// The shipped table's two members and the kind each serves — the fact every wrong-kind refusal is
/// read off.
#[test]
fn the_shipped_engines_are_tantivy_and_fs_and_they_serve_two_kinds() {
  assert_eq!(
    EngineKind::of(&format_ident!("tantivy")),
    Some(EngineKind::Tantivy)
  );
  assert_eq!(EngineKind::Tantivy.serves(), NodeKind::Fulltext);

  assert_eq!(EngineKind::of(&format_ident!("fs")), Some(EngineKind::Fs));
  assert_eq!(EngineKind::Fs.serves(), NodeKind::Blob);
}

/// A word outside the shipped table is not a refusal here — it is the OPEN half, bound at the graph
/// root. This is the property that keeps the vocabulary open, and it is pinned because closing it is
/// a one-line edit that would compile.
#[test]
fn a_third_party_word_is_not_in_the_shipped_table() {
  assert_eq!(EngineKind::of(&format_ident!("hnsw")), None);
  assert_eq!(EngineKind::of(&format_ident!("lance")), None);
}

#[test]
fn a_declared_block_answers_whether_it_is_shipped() {
  let shipped = Engine::bare(format_ident!("tantivy"));
  let theirs = Engine::bare(format_ident!("hnsw"));
  assert_eq!(shipped.shipped(), Some(EngineKind::Tantivy));
  assert_eq!(theirs.shipped(), None);
}

#[test]
fn a_binding_marker_is_the_root_and_the_word() {
  let binding = EngineBinding {
    word: format_ident!("hnsw"),
    engine: syn::parse_quote!(app::hnsw::Hnsw),
  };
  assert_eq!(
    binding.marker(&format_ident!("Media")),
    format_ident!("MediaEngineHnsw")
  );
}