ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! What a container's channel words become: the shelf, its keys, and the author's declared table.
//!
//! Read off the TOKENS, and off the flattened text rather than off a parsed file: the shelf is
//! declared twice inside the `serde` gate's two macro calls, so what a reader of the emission sees
//! is what a token assertion sees. What the shelf DOES — a document parsing into it, a key nobody
//! declared refused by name, a launch opening the edge the block named — is asserted where the
//! emission can be run, in `ingraph`'s own `tests/channel_shelf.rs`.

use crate::{
  model::{ChannelOptions, RecordModel, Subscription, Upstream, default_channels_name},
  tests::model,
};

/// A node drawing from three upstreams: one shipped bounded, one shipped unbounded, one silent.
fn fused() -> RecordModel {
  let mut model = model("Captions", &["media_id"], "id");
  model.subscriptions = Some(Subscription {
    upstreams: vec![
      upstream("asr::Segment", Some(ChannelOptions::Bounded(512))),
      upstream("media::Ocr", Some(ChannelOptions::Unbounded)),
      upstream("HttpFetch", None),
    ],
  });
  model
}

/// One entry, as the front end would have settled it.
fn upstream(node: &str, channel: Option<ChannelOptions>) -> Upstream {
  let node: syn::Path = syn::parse_str(node).expect("a path");
  let variant = node
    .segments
    .last()
    .expect("a path has a last segment")
    .ident
    .clone();
  Upstream {
    node,
    keyed: false,
    out: None,
    variant,
    channel,
    changes: None,
    notify: false,
  }
}

/// The emission with every space taken out — what a claim about a type or a word is read off.
fn compact(model: &RecordModel) -> String {
  model.emit().to_string().split_whitespace().collect()
}

/// ONE FIELD PER SOURCE, named by the P1 law: the word the edge is already known by, in snake case.
#[test]
fn every_source_gets_a_field_named_by_the_p1_law() {
  let model = fused();
  let emitted = compact(&model);
  assert_eq!(default_channels_name(&model.record), "CaptionsChannels");
  assert!(emitted.contains("structCaptionsChannels{"), "{emitted}");
  for field in ["segment", "ocr", "http_fetch"] {
    assert!(
      emitted.contains(&format!(
        "{field}:::core::option::Option<::ingraph::__private::drive::ChannelOptions>"
      )),
      "`{field}` is a field of the shelf: {emitted}"
    );
  }
}

/// TYPED AND CLOSED: every field defaults so a document may name one upstream of three, and a key
/// nobody declared is refused where the file is open.
#[test]
fn the_shelf_defaults_its_fields_and_refuses_unknown_keys() {
  let emitted = compact(&fused());
  assert_eq!(
    emitted.matches("#[serde(default,rename=").count(),
    3,
    "{emitted}"
  );
  assert!(emitted.contains("deny_unknown_fields"), "{emitted}");
}

/// The document's KEY is the P1 name itself, not the field's Rust spelling — so a upstream whose word
/// is a keyword is written in a document exactly as it is written in the declaration.
#[test]
fn a_keyword_named_source_keeps_its_own_key() {
  let mut model = model("Boxes", &["media_id"], "id");
  model.subscriptions = Some(Subscription {
    upstreams: vec![upstream("shape::Box", None)],
  });
  let emitted = compact(&model);
  assert!(emitted.contains(r#"rename="box""#), "{emitted}");
  assert!(
    emitted.contains("box_:::core::option::Option<"),
    "{emitted}"
  );
}

/// **THE DECLARED TABLE**: the author's own defaults, in declaration order, as a constant — the
/// middle layer of the precedence, travelling by its own road.
#[test]
fn the_declared_table_carries_the_authors_own_words_in_order() {
  let emitted = compact(&fused());
  assert!(
    emitted.contains(
      "constDECLARED:[::core::option::Option<::ingraph::__private::drive::ChannelOptions>;3usize]=[\
       ::core::option::Option::Some(::ingraph::__private::drive::ChannelOptions::Bounded(512usize)),\
       ::core::option::Option::Some(::ingraph::__private::drive::ChannelOptions::Unbounded),\
       ::core::option::Option::None,]"
    ),
    "{emitted}"
  );
  assert!(emitted.contains("constCOUNT:usize=3usize"), "{emitted}");
}

/// The shelf reads back through the framework's trait, position by position — which is the one shape
/// the launch floor asks for.
#[test]
fn the_shelf_hands_its_table_to_the_launch_floor() {
  let emitted = compact(&fused());
  assert!(
    emitted.contains("impl::ingraph::__private::drive::ChannelShelfforCaptionsChannels"),
    "{emitted}"
  );
  assert!(
    emitted.contains("into_iter([self.segment,self.ocr,self.http_fetch,])"),
    "{emitted}"
  );
}

/// A node with NO container emits no shelf — the collision rule must not report a name no expansion
/// writes, and a node with no upstreams has no edge to tune.
#[test]
fn a_node_with_no_container_emits_no_shelf() {
  let model = model("Plain", &["media_id"], "id");
  let shelf = default_channels_name(&model.record).to_string();
  assert!(
    !compact(&model).contains(&format!("struct{shelf}")),
    "{shelf}"
  );
}

/// ONE upstream still gets a shelf, where a fan-in of one collapses to its message type: the edge is
/// there to be tuned at every arity, and a shelf that appeared with the second upstream would take a
/// deployment's document with it.
#[test]
fn one_source_still_gets_a_shelf() {
  let mut model = model("Thumbs", &["media_id"], "id");
  model.subscriptions = Some(Subscription {
    upstreams: vec![upstream("media::Frame", Some(ChannelOptions::Bounded(4)))],
  });
  let emitted = compact(&model);
  assert!(emitted.contains("structThumbsChannels{"), "{emitted}");
  assert!(emitted.contains("constCOUNT:usize=1usize"), "{emitted}");
}