ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! WHAT THE SORTABLE CURSOR EMITS — the slot a `sortable` collection joins, the type it holds, and
//! the arm that reads its value back off the page's own row.
//!
//! A suite of its own beside [`super::sortable_tests`], which is the word's WRITE half: that one
//! asserts the companion is filled and ordered by, and this one asserts a PAGE can resume at it.
//! The two were one emission until the row learned to project the companion, and holding them apart
//! is what keeps the ordered scan's guarantee visible after the page's arrived.
//!
//! # The four rows, which are the storage form read off the emission
//!
//! | dialect | the collection lives | the cursor's slot |
//! |---|---|---|
//! | SQLite, MySQL | an edge table, always | the COMPANION's bytes |
//! | PostgreSQL, no `edge` word | a native array on the row | the LIST itself — the array cursor's own slot, untouched |
//! | PostgreSQL, `edge` written | an edge table, by the word | the COMPANION's bytes |
//! | any dialect, no `sortable` word | an edge table | NO SLOT — the page is refused, as it was |
//!
//! The last two are each worth an assertion of their own. The `edge`-declared PostgreSQL row is
//! what says the slot follows the STORAGE FORM rather than the dialect — one engine answering both
//! ways, on the same declaration, because a word beside the column moved where the collection
//! lives. And the wordless row is what says the page is opened by the WORD: a collection that never
//! wrote it keeps the refusal it had.

use crate::{
  emit::sortable_tests::sortable,
  model::RecordModel,
  tests::{
    edge_declared_in_postgres, emission, enum_named, gathered, gathered_in_mysql,
    gathered_in_postgres,
  },
};

/// The emission's tokens with every space removed, so an assertion reads as the source does rather
/// than as `to_string` spaces a punctuated path.
fn squeezed(model: &RecordModel) -> String {
  model.emit().to_string().replace(' ', "")
}

/// The variants of `model`'s cursor sum at `cursor`, which is the whole of *what a page over this
/// database can resume at*.
fn slots(model: &RecordModel, cursor: &str) -> Vec<String> {
  enum_named(&emission(model), cursor)
    .variants
    .iter()
    .map(|variant| variant.ident.to_string())
    .collect()
}

/// The type a companion column's value crosses as, in the spelling the emission writes.
const BYTES: &str = "::std::vec::Vec<::core::primitive::u8>";

/// **THE DELIVERABLE.** The collection that had no cursor slot at all now has one, and it holds the
/// COMPANION's bytes rather than the list — because the bytes are what the statement ordered by.
#[test]
fn a_sortable_collection_joins_the_cursor_sum_at_the_companions_type() {
  let worded = sortable(gathered());

  assert_eq!(
    slots(&worded, "MediaSqliteCursor"),
    ["Id", "Checksum", "Chapters", "CoverId"],
    "the collection is a boundary this database can take"
  );
  assert!(
    squeezed(&worded).contains(&format!("Chapters({BYTES})")),
    "and the slot holds the sort key, which is one ordinary blob"
  );
}

/// **AND THE ROW READS IT BACK OFF THE RAW DRIVER ROW**, under the alias the engine projected it —
/// never off the row's own collection, which arrives from a second statement and is a second reading
/// of the same order.
#[test]
fn the_value_arm_reads_the_projected_alias() {
  let tokens = squeezed(&sortable(gathered()));

  assert!(
    tokens.contains(&format!(
      "try_get::<{BYTES},_>(__ingraph_raw,__ingraph_alias)"
    )),
    "the boundary is the ordered row's own bytes: {tokens}"
  );
}

/// **MYSQL ANSWERS THE SAME**, being the other dialect with no array type — so the slot is the
/// STORAGE FORM's and not one dialect's.
#[test]
fn the_other_edge_dialect_answers_alike() {
  let worded = sortable(gathered_in_mysql());

  assert!(slots(&worded, "MediaMysqlCursor").contains(&"Chapters".to_owned()));
  assert!(squeezed(&worded).contains("__ingraph_alias"));
}

/// **POSTGRESQL IS UNTOUCHED.** Its array-stored collection already had a slot — the array cursor's,
/// holding the whole LIST — and the word emits no companion there, so nothing about this round
/// reaches it.
#[test]
fn the_array_dialect_keeps_the_list_slot_it_already_had() {
  let worded = sortable(gathered_in_postgres());
  let tokens = squeezed(&worded);

  assert!(
    slots(&worded, "MediaPostgresCursor").contains(&"Chapters".to_owned()),
    "the slot was already there — a native array binds as one parameter"
  );
  assert!(
    !tokens.contains(&format!("Chapters({BYTES})")),
    "and it still holds the LIST: {tokens}"
  );
  assert!(
    !tokens.contains("__ingraph_alias"),
    "no term of this order reads the raw row: {tokens}"
  );
}

/// **AND POSTGRESQL ANSWERS BOTH WAYS ON ONE DECLARATION.** The same record with
/// `postgres(edge)` beside the collection keeps it in a table of its own, so the word
/// mints a companion there too — and the slot follows, at the companion's bytes rather than at the
/// list the array row holds.
///
/// It is the sharpest form of the claim the whole fence rests on: what decides the slot is
/// [`DatabaseBackend::mirrors`](crate::model::DatabaseBackend), which reads the STORAGE FORM, and
/// one engine gives two answers because one word moved where the collection lives.
#[test]
fn the_array_dialect_takes_the_companion_slot_where_the_word_moved_the_collection() {
  let worded = sortable(edge_declared_in_postgres());
  let tokens = squeezed(&worded);

  assert!(
    slots(&worded, "MediaPostgresCursor").contains(&"Chapters".to_owned()),
    "the collection is a boundary here too, through its companion"
  );
  assert!(
    tokens.contains(&format!("Chapters({BYTES})")),
    "and the slot holds the sort key, on the dialect whose ARRAY row holds the list: {tokens}"
  );
  assert!(
    tokens.contains(&format!(
      "try_get::<{BYTES},_>(__ingraph_raw,__ingraph_alias)"
    )),
    "read off the ordered row, as on every other dialect: {tokens}"
  );
}

/// **A COLLECTION THAT NEVER WROTE THE WORD KEEPS ITS ABSENCE.** The slot is the `sortable` word's,
/// not the collection's: an edge-stored collection with no companion has no boundary value to write
/// into one, and its page is refused by name exactly as before.
#[test]
fn a_wordless_edge_collection_has_no_slot() {
  let wordless = gathered();

  assert_eq!(
    slots(&wordless, "MediaSqliteCursor"),
    ["Id", "Checksum", "CoverId"],
    "no companion, no boundary, no slot"
  );
  assert!(
    !squeezed(&wordless).contains("__ingraph_alias"),
    "and nothing reads the raw row"
  );
}

/// **ONE SPELLING OF THE COMPANION'S TYPE**, spent at both ends: the write plan binds these bytes and
/// the cursor slot holds them, so a boundary cannot be a shape the projection could not fill.
#[test]
fn the_slot_and_the_write_plan_name_one_type() {
  let tokens = squeezed(&sortable(gathered()));

  assert!(tokens.contains(&format!("Chapters({BYTES})")), "{tokens}");
  assert!(
    tokens.contains(&format!(
      "<{BYTES}as::ingraph::__private::comparison::sql::Carrier<__INGRAPH_DB__>>::push_bind"
    )),
    "the write plan binds the same type the slot holds: {tokens}"
  );
}