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 `notify` WORD's acceptance: the receipt a upstream entry chooses, and every way it can be
//! written wrongly.
//!
//! It is a bare entry MODE-WORD, the way `key` is, and the two are deliberately alike: both say
//! something about HOW this upstream arrives rather than about which upstream it is, and both sit on
//! the entry whose upstream they qualify. What separates them is the axis — `key` is about JOINING
//! and `notify` is about the PAYLOAD — so they compose freely, which the first test states.

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 `(variant, keyed, notify)`, in declaration order.
fn receipts(input: syn::DeriveInput) -> Vec<(String, bool, bool)> {
  parse(&input)
    .expect("the declaration is accepted")
    .subscriptions
    .expect("the container was written")
    .upstreams
    .iter()
    .map(|upstream| {
      (
        upstream.variant.to_string(),
        upstream.keyed,
        upstream.notify,
      )
    })
    .collect()
}

/// The default is the MESSAGE, and `notify` is per entry — so one container legally holds both,
/// which is the ruling's *one store feeds both kinds of downstream at once* read from the consumer's
/// side.
#[test]
fn the_receipt_is_per_entry_and_the_default_is_the_message() {
  let receipts = receipts(syn::parse_quote! {
    #[ingraph(subscribe(
      upstream(node = "asr::Segment", notify),
      upstream(node = "media::Ocr"),
    ))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert_eq!(
    receipts,
    vec![
      ("Segment".to_owned(), false, true),
      ("Ocr".to_owned(), false, false),
    ]
  );
}

/// The two bare words are about different axes, so they compose: a keyed group member may take the
/// key receipt like any other entry.
#[test]
fn the_two_bare_words_compose_on_one_entry() {
  let receipts = receipts(syn::parse_quote! {
    #[ingraph(subscribe(
      upstream(node = "asr::Segment", key, notify),
      upstream(node = "media::Ocr", key),
    ))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert_eq!(
    receipts,
    vec![
      ("Segment".to_owned(), true, true),
      ("Ocr".to_owned(), true, false),
    ]
  );
}

#[test]
fn the_word_said_twice_on_one_entry_is_refused() {
  let refusal = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "asr::Segment", notify, notify)))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert!(
    refusal.contains("`notify` is written twice in one `upstream(…)`"),
    "{refusal}"
  );
}

/// The word takes nothing, and the refusal says WHY rather than only that: the key's type is the
/// upstream's own answer, so there is no type for this declaration to name beside the word.
#[test]
fn a_notify_that_names_a_payload_is_refused() {
  let refusal = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "asr::Segment", notify = "SegmentKey")))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert!(refusal.contains("`notify` is written bare"), "{refusal}");
  assert!(refusal.contains("the upstream's own answer"), "{refusal}");
}

/// The unknown-entry sentence names the word, so an author who misspelled it is told the vocabulary
/// rather than left to guess it.
#[test]
fn the_unknown_entry_sentence_names_the_receipt_word() {
  let refusal = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "asr::Segment", notifies)))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert!(
    refusal.contains("the bare words `key` and `notify`"),
    "{refusal}"
  );
}

/// The word belongs to a `upstream(…)` entry inside `subscribe(…)`, which is where messages travel —
/// a poll entry has no payload to choose the shape of.
#[test]
fn the_word_is_not_a_poll_entrys() {
  let refusal = refusals(syn::parse_quote! {
    #[ingraph(poll(orphans, every = "10m", notify, light, async(inline)))]
    pub struct Summaries {
      #[ingraph(id)]
      id: u32,
      #[ingraph(copy)]
      media_id: u64,
    }
  });
  assert!(refusal.contains("a poll entry has NO receipt"), "{refusal}");
  assert!(
    refusal.contains("`upstream(…)` entry inside `subscribe(…)`"),
    "{refusal}"
  );
}