ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What the model itself answers, independently of any front end that fills it.
//!
//! The front end's suite pins that each word is READ; these pin the answers that belong to the model
//! — the closed half of the fold axis, the row order, and the two words the three grammars that
//! spend them read from one place.

use super::{AggregateModel, GROUP, Grouped, MEASURE, Measure, Measured, Owed};
use crate::model::Projection;

/// A model with the given group and measure column names, and nothing else true about it.
fn over(groups: &[&str], measures: &[&str]) -> AggregateModel {
  let ident = |name: &str| syn::Ident::new(name, proc_macro2::Span::call_site());
  AggregateModel {
    aggregate: ident("CodecCensus"),
    vis: syn::Visibility::Inherited,
    krate: syn::parse_quote!(::ingraph),
    krate_spelled: None,
    upstream: syn::parse_quote!(demux::Track),
    groups: groups
      .iter()
      .map(|name| Grouped {
        field: ident(name),
        from: ident(name),
        ty: syn::parse_quote!(u64),
        projection: Projection::Borrowed,
      })
      .collect(),
    measures: measures
      .iter()
      .map(|name| Measured {
        field: ident(name),
        by: Measure::Counting,
        from: None,
        ty: syn::parse_quote!(u64),
        projection: Projection::Borrowed,
      })
      .collect(),
    triggers: proc_macro2::TokenStream::new(),
    stores: proc_macro2::TokenStream::new(),
    graphql: None,
  }
}

/// **THE ROW IS ITS KEY FIRST** — every group column, then every measure, whatever order a front end
/// filled the two lists in.
///
/// A key is a row's first columns everywhere else in this framework, and an aggregate's key is its
/// group columns. Stated on the MODEL rather than at each reader, so a table's column order cannot
/// come to depend on who asked.
#[test]
fn the_columns_are_the_groups_then_the_measures() {
  let model = over(&["codec", "lang"], &["count", "bytes"]);

  assert_eq!(
    model.columns().map(ToString::to_string).collect::<Vec<_>>(),
    ["codec", "lang", "count", "bytes"],
  );
}

/// **THE BARE FOLD VOCABULARY IS CLOSED, and its two spellings round-trip.**
///
/// The list a refusal reads and the words the reader accepts are the same list, which is what keeps
/// a near miss from offering a word the next compile would refuse.
#[test]
fn every_shipped_fold_is_readable_by_the_word_that_names_it() {
  for word in Measure::SHIPPED {
    let shipped = Measure::shipped(word).unwrap_or_else(|| panic!("`{word}` is shipped"));
    assert_eq!(shipped.as_written(), word);
  }
  assert!(
    Measure::shipped("median").is_none(),
    "the bare half is closed — every other fold arrives quoted"
  );
}

/// **A DECLARED FOLD IS WRITTEN AS THE PATH IT WAS NAMED BY**, so a refusal about one can quote it.
#[test]
fn a_declared_fold_reads_back_as_its_path() {
  let declared = Measure::Declared(syn::parse_quote!(app::Median));

  assert_eq!(declared.as_written().replace(' ', ""), "app::Median");
}

/// **EACH SHIPPED FOLD SAYS WHAT IT OWES, and the declared half says it is not the framework's to
/// say.**
///
/// The axis the entry grammar forks on: a `summing` with no column and a `counting` with one are
/// each a mistake this crate can NAME because it wrote both folds. A type it never saw is neither,
/// so the third answer is what keeps the framework from deciding a declaring crate's question.
#[test]
fn what_a_fold_owes_is_known_for_the_ones_this_crate_ships() {
  assert_eq!(Measure::Counting.owed(), Owed::Nothing);
  assert_eq!(Measure::Summing.owed(), Owed::Column);
  assert_eq!(
    Measure::Declared(syn::parse_quote!(app::Median)).owed(),
    Owed::Theirs,
    "a fold this crate did not write is one it cannot answer for"
  );
}

/// **EVERY SHIPPED WORD ANSWERS THE AXIS**, so a third citizen cannot arrive without its author
/// deciding what it reads.
#[test]
fn every_shipped_fold_answers_what_it_owes() {
  for word in Measure::SHIPPED {
    let shipped = Measure::shipped(word).unwrap_or_else(|| panic!("`{word}` is shipped"));

    assert_ne!(
      shipped.owed(),
      Owed::Theirs,
      "`{word}` is this crate's own fold, so what it reads is this crate's to know"
    );
  }
}

/// **A MEASURE CARRIES THE COLUMN ITS FOLD READS**, and [`None`] is a fold over the rows.
#[test]
fn a_measure_carries_the_upstream_column_its_fold_reads() {
  let mut model = over(&["codec"], &["count", "bytes"]);
  model.measures[1].by = Measure::Summing;
  model.measures[1].from = Some(syn::Ident::new("size", proc_macro2::Span::call_site()));

  assert!(
    model.measures[0].from.is_none(),
    "a counted column reads no column of the rows it counts"
  );
  assert_eq!(
    model.measures[1]
      .from
      .as_ref()
      .expect("a summed column names the column it totals"),
    "size",
  );
}

/// **THE TWO ROLE WORDS HAVE ONE HOME.**
///
/// Three grammars spend them — the aggregate's own, the record family's and the derived indexes' —
/// and a word spelled once per grammar is a word that comes to mean two things without anybody
/// editing either of them. The pin is that they are what a declaration writes, so a rename that
/// missed a caller would be a rename that missed this.
#[test]
fn the_role_words_are_the_words_a_declaration_writes() {
  assert_eq!(GROUP, "group");
  assert_eq!(MEASURE, "measure");
  assert_ne!(GROUP, MEASURE);
}