ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
use super::EmissionPrefix;

/// The seat a front end that says nothing occupies, and the string behind it.
///
/// Pinned against [`crate::alias::ALIAS_PREFIX`] rather than against a literal, because the two are
/// meant to be one string: a test that spelled `__ig` here would keep passing on the day the
/// constant moved and the type kept the old value.
#[test]
fn the_default_is_the_frameworks_own_mark() {
  assert_eq!(EmissionPrefix::default(), EmissionPrefix::INGRAPH);
  assert_eq!(EmissionPrefix::default().mark(), crate::alias::ALIAS_PREFIX);
  assert!(EmissionPrefix::default().is_default());
}

/// The two shapes, spelled for the framework and for a platform beside it.
#[test]
fn the_two_shapes_carry_the_mark() {
  let ingraph = EmissionPrefix::default();
  let media = EmissionPrefix::new("__mg");

  assert_eq!(ingraph.framed("key"), "__ig_key__");
  assert_eq!(media.framed("key"), "__mg_key__");

  assert_eq!(
    ingraph.prefixed("Clip_holds_one_storage"),
    "__ig_Clip_holds_one_storage"
  );
  assert_eq!(
    media.prefixed("Clip_holds_one_storage"),
    "__mg_Clip_holds_one_storage"
  );
}

/// The IDENT faces spell exactly what the string faces do, which is what lets a reader check one
/// and trust the other.
#[test]
fn the_ident_faces_agree_with_the_string_faces() {
  let media = EmissionPrefix::new("__mg");
  assert_eq!(
    media.framed_ident("lineage").to_string(),
    media.framed("lineage")
  );
  assert_eq!(
    media.prefixed_ident("engine").to_string(),
    media.prefixed("engine")
  );
}

/// A platform's mark is not the framework's, which is the whole question
/// [`EmissionPrefix::is_default`] is asked: it decides whether an emission may stay silent and let
/// the engine's own defaults answer.
#[test]
fn only_the_frameworks_own_mark_is_default() {
  assert!(!EmissionPrefix::new("__mg").is_default());
  assert!(!EmissionPrefix::new("__ig_").is_default());
  assert!(EmissionPrefix::new("__ig").is_default());
}

/// A mark computed at run time is as good as a literal one — the `Cow` is what makes the manifest
/// front end and the domain framework the same occupant.
#[test]
fn an_owned_mark_is_the_same_seat_as_a_borrowed_one() {
  let owned = EmissionPrefix::new(String::from("__mg"));
  assert_eq!(owned, EmissionPrefix::new("__mg"));
  assert_eq!(owned.framed("key"), "__mg_key__");
}

/// The FENCE rules, each refused for its own reason.
#[test]
fn a_mark_that_could_not_be_fenced_is_refused() {
  assert!(
    EmissionPrefix::refused("").is_some(),
    "an empty mark names no namespace"
  );
  assert!(
    EmissionPrefix::refused("mg").is_some(),
    "a bare word confiscates a namespace the author is using"
  );
  assert!(
    EmissionPrefix::refused("__m g").is_some(),
    "a minted name is one identifier, so a space cannot be inside it"
  );
  assert!(
    EmissionPrefix::refused("__m-g").is_some(),
    "nor a hyphen, which no Rust identifier carries"
  );
}

/// The two marks this repository actually ships behind are both admissible, which is the floor the
/// rules have to clear.
#[test]
fn both_shipped_marks_are_admitted() {
  assert_eq!(EmissionPrefix::refused(crate::alias::ALIAS_PREFIX), None);
  assert_eq!(EmissionPrefix::refused("__mg"), None);
}

/// MIXED GRAPHS: two marks side by side mint disjoint names for the same word, which is what makes
/// one deployment able to hold both frameworks' declarations.
///
/// The assertion is DISJOINTNESS rather than two literals, because that is the property a mixed
/// deployment needs: nothing downstream may be able to reach one framework's name by spelling the
/// other's word.
#[test]
fn two_marks_mint_disjoint_names_for_one_word() {
  let ingraph = EmissionPrefix::default();
  let media = EmissionPrefix::new("__mg");

  for word in ["key", "lineage", "engine", "kind", "union"] {
    assert_ne!(
      ingraph.framed(word),
      media.framed(word),
      "`{word}` reached one name under two frameworks"
    );
  }
  assert_ne!(
    ingraph.prefixed("Clip_holds_one_storage"),
    media.prefixed("Clip_holds_one_storage"),
    "a storage marker under two frameworks is two items, so one type may hold both"
  );
}