ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **THE TWO GOODBYE ARMS OF THE EMITTED LAUNCH**, read off its tokens: the resource word's routing,
//! and the pre-ignition unwind.
//!
//! What a token stream can be read for is which DOOR each step names, and that is exactly what both
//! rulings come down to here — the `teardown` word changes one call and nothing else, and the
//! unwind is one arm per fallible phase over holders that were declared before the phase began.
//! What the arms DO is asserted where the emission can be run, in `ingraph`'s own
//! `tests/prelaunch_unwind.rs` and `tests/resource_goodbye_word.rs`.
//!
//! Beside the entry's own suite rather than inside it, which is this workspace's shape for a word
//! that arrives after an emission has already been proven.

use crate::{
  catalog::{ResourceField, ResourceSource},
  graph::GraphModel,
};

use super::tests::{app, compact, rendered};

/// The same root, with one worded resource and one silent one beside it.
fn resourced() -> GraphModel {
  GraphModel {
    interfaces: Vec::new(),
    resources: vec![
      ResourceField {
        mount: syn::parse_quote!(egemma),
        ty: syn::parse_quote!(models::Egemma),
        source: ResourceSource::Section,
        teardown: false,
      },
      ResourceField {
        mount: syn::parse_quote!(index),
        ty: syn::parse_quote!(Index),
        source: ResourceSource::Initializer(syn::parse_quote!(boot::index)),
        teardown: true,
      },
    ],
    ..app()
  }
}

/// **THE WORD PICKS THE DOOR, AND THAT IS ITS WHOLE RUNTIME EFFECT.** A resource that wrote
/// `teardown` is built through the enrolling door and every other through the plain one, in the same
/// walk, in declaration order.
#[test]
fn the_teardown_word_names_the_enrolling_door_and_nothing_else_moves() {
  let model = resourced();
  let emitted = compact(&model);

  assert!(
    emitted.contains("Creation::declared_resource::<models::Egemma,"),
    "the silent resource keeps the door it had: {}",
    rendered(&model)
  );
  assert!(
    emitted.contains("Creation::declared_resource_saying_goodbye::<Index,"),
    "and the worded one is enrolled: {}",
    rendered(&model)
  );
  assert!(
    !emitted.contains("Creation::declared_resource::<Index,"),
    "one door per resource, never both: {}",
    rendered(&model)
  );
}

/// **A ROOT WHOSE RESOURCES SAID NOTHING EMITS WHAT IT ALWAYS DID.** The enrolling door does not
/// appear at all, which is what *the word costs nothing* means where the cost would be paid.
#[test]
fn a_root_with_no_worded_resource_names_the_enrolling_door_nowhere() {
  let model = GraphModel {
    interfaces: Vec::new(),
    resources: vec![ResourceField {
      mount: syn::parse_quote!(egemma),
      ty: syn::parse_quote!(models::Egemma),
      source: ResourceSource::Section,
      teardown: false,
    }],
    ..app()
  };

  assert!(
    !compact(&model).contains("declared_resource_saying_goodbye"),
    "{}",
    rendered(&model)
  );
}

/// **THE BAG IS EMPTY BEFORE THE WALK AND FILLED IN PLACE.** That is the unwind's precondition
/// rather than a style: a struct literal's earlier temporaries are dropped by the `?` that abandons
/// it, so the values a refusal leaves behind have to be somewhere the walk still owns.
#[test]
fn the_node_bag_is_declared_empty_and_assigned_slot_by_slot() {
  let model = app();
  let emitted = compact(&model);

  assert!(
    emitted.contains("letmut__ingraph_nodes=AppNodes{videos:::core::option::Option::None,"),
    "{}",
    rendered(&model)
  );
  assert!(
    emitted.contains("__ingraph_nodes.videos=::ingraph::__private::drive::Creation::node::"),
    "{}",
    rendered(&model)
  );
}

/// **ONE ARM PER FALLIBLE PHASE**, and each hands its walk to that walk's own unwind. The outcome is
/// BOUND first, because the block borrows the walk the unwind then consumes.
#[test]
fn each_phase_hands_its_refusal_to_its_own_walks_unwind() {
  let model = app();
  let emitted = compact(&model);

  assert!(
    emitted.contains("let__ingraph_created=async{"),
    "the create phase is one block: {}",
    rendered(&model)
  );
  assert!(
    emitted.contains("Creation::unwind(__ingraph_creation,{"),
    "whose refusal reaches the create walk's unwind: {}",
    rendered(&model)
  );
  assert!(
    emitted.contains("let__ingraph_connected=(||->::core::result::Result<(),"),
    "the wiring phase is the other: {}",
    rendered(&model)
  );
  assert!(
    emitted.contains("Wiring::unwind(__ingraph_wiring,{"),
    "whose refusal reaches the wiring walk's: {}",
    rendered(&model)
  );
}

/// **THE HOLDERS ARE DECLARED BEFORE THE PHASE THAT FILLS THEM**, which is what lets ONE arm reach
/// every one of them — the same reason the bag starts empty.
#[test]
fn the_per_mount_holders_are_declared_ahead_of_the_wiring_walk() {
  let model = app();
  let emitted = compact(&model);

  for mount in ["videos", "scenes", "search"] {
    assert!(
      emitted.contains(&format!(
        "letmut__ingraph_body_{mount}=::core::option::Option::None;\
         letmut__ingraph_feed_{mount}=::core::option::Option::None;"
      )),
      "{}",
      rendered(&model)
    );
  }
  let holders = emitted
    .find("letmut__ingraph_body_videos")
    .expect("the holders are emitted");
  let walk = emitted
    .find("let__ingraph_connected")
    .expect("the wiring phase is emitted");
  assert!(holders < walk, "{}", rendered(&model));
}

/// **THE LEDGER READS BOTH PLACES A MOUNT'S VALUE CAN BE.** A party that was WIRED is staged
/// through its own mount cell — a buffered body out of its resident, a streaming one out of its
/// lane mount's warm set — and one the wiring never reached is staged straight out of the bag.
///
/// The emission names the CELL rather than either door, because it cannot see which pair the
/// declaration wrote; `Mount::stage` is where the two paths are written.
#[test]
fn the_wiring_ledger_stages_through_the_mount_cell_and_falls_back_to_the_bag() {
  let model = app();
  let emitted = compact(&model);

  assert!(
    emitted.contains(
      "as::ingraph::__private::drive::Mount<media::Video,>>::stage(&mut__ingraph_staged,\"videos\",\
       __ingraph_body);"
    ),
    "{}",
    rendered(&model)
  );
  assert!(
    emitted.contains(
      "::core::option::Option::None=>{iflet::core::option::Option::Some(__ingraph_value)=\
       AppNodes::take_videos(&mut__ingraph_nodes,)"
    ),
    "and the mount the wiring never reached falls back to the bag: {}",
    rendered(&model)
  );
}