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 CHANNEL WORD's grammar, read straight off the pass.
//!
//! A suite of its own beside [`crate::parse::tests`] rather than more of it, for the same reason
//! that file gives for not being more of `crate::tests`: what is asserted here is one word — which
//! shapes the pass takes, which it refuses, and whether a refusal names the shape an author meant.
//!
//! The word is written BARE (`channel = bounded(512)`), and that is the mint-bare/refer-quoted law
//! applied rather than bent: `bounded` and `unbounded` refer to nothing a declaration named — they
//! are the framework's own vocabulary, in the position `depth = 16` writes a count.

use ingraph_codegen::prelude::*;

/// The pass under test, at the kind these words belong to.
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = crate::host::split(input);
  crate::parse::parse(&host, &options, NodeKind::Operator)
}

/// Every refusal one declaration collects, as text.
fn refusals(input: syn::DeriveInput) -> Vec<String> {
  operator(&input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect()
}

/// Whether any of `messages` names `rule`.
fn names(messages: &[String], rule: &str) -> bool {
  messages.iter().any(|message| message.contains(rule))
}

/// The channel each source of a model declares, in declaration order.
fn channels(model: &RecordModel) -> Vec<Option<ChannelOptions>> {
  model
    .subscriptions
    .as_ref()
    .map(|subscription| {
      subscription
        .upstreams
        .iter()
        .map(|source| source.channel)
        .collect()
    })
    .unwrap_or_default()
}

/// **THE TWO SHAPES**, per upstream and mixable in one container — the poll precedent, one axis over.
#[test]
fn the_two_words_are_read_per_source() {
  let model = operator(&syn::parse_quote! {
    #[ingraph(subscribe(
      upstream(node = "asr::Segment", channel = bounded(512)),
      upstream(node = "media::Ocr", channel = unbounded),
      upstream(node = "HttpFetch"),
    ))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  })
  .expect("both words are legal, and saying nothing is legal");
  assert_eq!(
    channels(&model),
    [
      Some(ChannelOptions::Bounded(512)),
      Some(ChannelOptions::Unbounded),
      None
    ]
  );
}

/// ZERO is a rendezvous the runtime has always documented, so it is admitted rather than refused:
/// the sender waits until the consumer takes it, which is a posture and not a degenerate value.
#[test]
fn a_zero_depth_is_admitted_as_the_rendezvous_it_is() {
  let model = operator(&syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "media::Ocr", channel = bounded(0))))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  })
  .expect("zero is a depth the runtime has");
  assert_eq!(channels(&model), [Some(ChannelOptions::Bounded(0))]);
}

/// `bounded` names a DEPTH — the word without its number is refused naming the kind that needs
/// none.
#[test]
fn bounded_without_a_depth_is_refused_by_name() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "media::Ocr", channel = bounded)))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  });
  assert!(names(&said, "`bounded` is a depth"), "{said:?}");
  assert!(names(&said, "unbounded"), "{said:?}");
}

/// And `unbounded` takes nothing, a queue nothing bounds having no depth to name.
#[test]
fn unbounded_with_a_depth_is_refused_by_name() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "media::Ocr", channel = unbounded(512))))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  });
  assert!(names(&said, "`unbounded` takes nothing"), "{said:?}");
}

/// A bare number, a quoted word, a word nobody minted: all three are the same refusal, and it names
/// both shapes rather than the one the author missed.
#[test]
fn any_other_value_is_refused_naming_the_two_shapes() {
  for written in [
    quote::quote!(channel = 512),
    quote::quote!(channel = "unbounded"),
    quote::quote!(channel = spinning),
  ] {
    let said = refusals(syn::parse_quote! {
      #[ingraph(subscribe(upstream(node = "media::Ocr", #written)))]
      struct Captions { #[ingraph(id)] id: Uuid7 }
    });
    assert!(
      names(&said, "`channel = bounded(512)` and `channel = unbounded`"),
      "{written}: {said:?}"
    );
  }
}

/// Said twice on one entry is the said-it-twice refusal the rest of the grammar keeps.
#[test]
fn one_edge_is_opened_one_way() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(
      node = "media::Ocr",
      channel = bounded(4),
      channel = unbounded,
    )))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  });
  assert!(
    names(&said, "`channel` is declared more than once"),
    "{said:?}"
  );
}

/// **A POLL ENTRY HAS NO CHANNEL** — refused BY NAME rather than as an unknown word, because a tick
/// is a wake and not a message: there is no queue between two parties to bound.
#[test]
fn a_poll_entry_refuses_the_word_by_name() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(poll(orphans, every = "10m", channel = unbounded, light, async(inline)))]
    struct Reaper { #[ingraph(id)] id: Uuid7 }
  });
  assert!(names(&said, "a poll entry has NO channel"), "{said:?}");
  assert!(names(&said, "`subscribe(…)`"), "{said:?}");
}

/// The container's unknown-word sentence NAMES the new word, so an author who guessed at a spelling
/// is told the whole vocabulary rather than three quarters of it.
#[test]
fn the_containers_vocabulary_sentence_names_the_word() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(subscribe(upstream(node = "media::Ocr", depth = 4)))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  });
  assert!(names(&said, "channel = bounded(…) | unbounded"), "{said:?}");
}

/// **THE P1 COLLISION, at the shelf**: two variants that differ and whose SHELF NAMES do not would
/// be one field written twice — refused where the declaration can still be edited.
#[test]
fn two_sources_whose_shelf_names_collide_are_refused() {
  let said = refusals(syn::parse_quote! {
    #[ingraph(subscribe(
      upstream(node = "one::AB"),
      upstream(node = "two::Ab"),
    ))]
    struct Captions { #[ingraph(id)] id: Uuid7 }
  });
  assert!(
    names(&said, "CHANNEL SHELF names would collide"),
    "{said:?}"
  );
  assert!(names(&said, "`ab`"), "{said:?}");
}