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 `changes(<db>)` WORD's acceptance: which store's feed an edge takes, and whose hand fills it.
//!
//! Two facts and one word. The DATABASE is named here because it cannot be read from anywhere else —
//! which databases an upstream stores in is the upstream's own declaration, and two macro expansions
//! cannot see each other. The FEEDER is named here because a change feed cannot report it: a feed
//! says what arrived, never who put it there, so `external` is a claim the declaration makes and the
//! launch walk holds it to.
//!
//! The word's shape is the axis law's, already written by `sqlite(flatten)`: per-database
//! knobs live inside the database's own block. Every refusal below is a spelling that puts one
//! somewhere else.

use ingraph_codegen::prelude::*;

/// The pass under test, over a declaration written the way the derive took one.
///
/// Under [`NodeKind::Entity`] rather than [`NodeKind::Record`]: the EDGE is the subject and rows are not, so the declarations parse under the rowless rung: an
/// entity carries every edge word a record does and owes no store.
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = crate::host::split(input);
  crate::parse::parse(&host, &options, NodeKind::Entity)
}

/// Every refusal the pass reported, JOINED.
fn refusals(input: syn::DeriveInput) -> String {
  parse(&input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// Each upstream's feed, in declaration order.
fn feeds(input: syn::DeriveInput) -> Vec<Option<ChangesFeed>> {
  parse(&input)
    .expect("the declaration is accepted")
    .subscriptions
    .expect("the container was written")
    .upstreams
    .iter()
    .map(|upstream| upstream.changes)
    .collect()
}

/// A body every case below reuses — the declaration's own half is not what any of them is about.
macro_rules! summaries {
  ($($word:tt)*) => {
    syn::parse_quote! {
      #[ingraph(subscribe($($word)*))]
      pub struct Summaries {
        #[ingraph(id)]
        id: u32,
        #[ingraph(copy)]
        media_id: u64,
      }
    }
  };
}

/// The DEFAULT is the channel, so a feed is a state of its own and the two mix in one container —
/// which is the mixability the two-mode ruling states, read off the model.
#[test]
fn a_feed_is_per_entry_and_the_default_is_the_channel() {
  assert_eq!(
    feeds(summaries!(
      upstream(node = "asr::Segment", changes(sqlite)),
      upstream(node = "media::Ocr"),
    )),
    vec![
      Some(ChangesFeed {
        database: DatabaseKind::Sqlite,
        external: false,
      }),
      None,
    ]
  );
}

/// `external` nests IN THE DATABASE'S BLOCK — the axis law, and the whole of what the word adds.
#[test]
fn the_external_word_lives_inside_the_databases_block() {
  assert_eq!(
    feeds(summaries!(upstream(
      node = "asr::Segment",
      changes(postgres(external))
    ))),
    vec![Some(ChangesFeed {
      database: DatabaseKind::Postgres,
      external: true,
    })]
  );
}

/// A bare `changes` names no store, and the refusal says WHY rather than only that: the database is
/// named here or nowhere, two declarations being unable to see each other.
#[test]
fn a_bare_changes_is_refused_at_the_wall_that_causes_it() {
  let refusal = refusals(summaries!(upstream(node = "asr::Segment", changes)));
  assert!(
    refusal.contains("`changes(<db>)` names WHICH store"),
    "{refusal}"
  );
  assert!(refusal.contains("cannot see each other"), "{refusal}");
}

/// `external` where the DATABASE belongs is the near miss worth naming: the word exists, and the
/// axis law puts it one block further in.
#[test]
fn external_outside_the_block_is_refused_by_name() {
  let refusal = refusals(summaries!(upstream(
    node = "asr::Segment",
    changes(external)
  )));
  assert!(
    refusal.contains("`external` is a per-database word"),
    "{refusal}"
  );
  assert!(refusal.contains("changes(sqlite(external))"), "{refusal}");
}

#[test]
fn a_database_nobody_ships_is_refused() {
  let refusal = refusals(summaries!(upstream(
    node = "asr::Segment",
    changes(cassandra)
  )));
  assert!(
    refusal.contains("`changes(<db>)` names WHICH store"),
    "{refusal}"
  );
}

#[test]
fn an_unknown_word_inside_the_block_is_refused_naming_the_one_that_belongs() {
  let refusal = refusals(summaries!(upstream(
    node = "asr::Segment",
    changes(sqlite(remote))
  )));
  assert!(
    refusal.contains("the only word inside a `changes(<db>(…))` block is `external`"),
    "{refusal}"
  );
}

#[test]
fn two_databases_in_one_word_are_refused() {
  let refusal = refusals(summaries!(upstream(
    node = "asr::Segment",
    changes(sqlite, postgres)
  )));
  assert!(refusal.contains("names ONE database"), "{refusal}");
}

#[test]
fn the_word_said_twice_on_one_entry_is_refused() {
  let refusal = refusals(summaries!(upstream(
    node = "asr::Segment",
    changes(sqlite),
    changes(postgres)
  )));
  assert!(
    refusal.contains("`changes` is declared more than once"),
    "{refusal}"
  );
}

/// The unknown-entry sentence names the word, so an author who misspelled it is told the vocabulary.
#[test]
fn the_unknown_entry_sentence_names_the_feed_word() {
  let refusal = refusals(summaries!(upstream(node = "asr::Segment", changed(sqlite))));
  assert!(refusal.contains("`changes(<db>)`"), "{refusal}");
}