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 `teardown` WORD ON A ROOT'S RESOURCE FIELD** — every composition, and every way of writing
//! it wrong.
//!
//! The word is a SECOND AXIS rather than a fourth source, so what these pin is that it rides beside
//! each of the three source spellings without disturbing any of them: the source a field declares
//! and whether it says goodbye are read off one attribute and answered independently.
//!
//! The sibling suite pins the sources themselves. This one adds only the axis, which is the division
//! the file split records.

use ingraph_codegen::catalog::ResourceSource;

use super::tests::{accepted, refused};

/// **THE THREE COMPOSITIONS.** The bare word, bare `factory` and the named initializer each take
/// `teardown` beside them, and none of them loses its source to it.
#[test]
fn the_goodbye_composes_with_every_source_spelling() {
  let model = accepted(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(resource(teardown))]
      egemma: models::Egemma,
      #[ingraph(resource(factory, teardown))]
      pool: Pool,
      #[ingraph(resource(factory = "boot::cache", teardown))]
      cache: Cache,
    }
  });

  assert_eq!(model.resources.len(), 3);
  assert_eq!(model.resources[0].source, ResourceSource::Section);
  assert_eq!(model.resources[1].source, ResourceSource::Prepare);
  assert!(matches!(
    model.resources[2].source,
    ResourceSource::Initializer(_)
  ));
  assert!(
    model.resources.iter().all(|field| field.teardown),
    "the axis is orthogonal to the source: every one of them said it"
  );
}

/// **THE ORDER INSIDE THE PARENTHESES IS NOT LOAD-BEARING.** `teardown` before `factory` says the
/// same thing, which is what makes the two entries independent rather than a sequence.
#[test]
fn the_goodbye_reads_the_same_written_first() {
  let model = accepted(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(resource(teardown, factory = "boot::cache"))]
      cache: Cache,
    }
  });

  assert!(matches!(
    model.resources[0].source,
    ResourceSource::Initializer(_)
  ));
  assert!(model.resources[0].teardown);
}

/// **A RESOURCE THAT SAID NOTHING SAYS NOTHING.** The absent word is the shipped posture, and it is
/// read as `false` rather than as anything the emitter has to be told.
#[test]
fn a_resource_without_the_word_carries_no_goodbye() {
  let model = accepted(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(resource)]
      egemma: models::Egemma,
      #[ingraph(resource(factory))]
      pool: Pool,
    }
  });

  assert!(model.resources.iter().all(|field| !field.teardown));
}

/// **IT IS A BARE WORD.** What a resource says on the way out is its own `AsyncResource::goodbye`, so
/// there is nothing for a value here to name.
#[test]
fn the_goodbye_takes_no_value() {
  let refusal = refused(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(resource(teardown = "shut_down"))]
      egemma: models::Egemma,
    }
  });

  assert!(refusal.contains("`teardown` is a bare word"), "{refusal}");
}

/// **ONE DECLARATION SAYS GOODBYE ONCE.** A repeated word is a second answer to a question that
/// already had one.
#[test]
fn the_goodbye_is_refused_where_it_is_written_twice() {
  let refusal = refused(quote::quote! {
    pub struct App {
      videos: media::Video,
      #[ingraph(resource(teardown, teardown))]
      egemma: models::Egemma,
    }
  });

  assert!(
    refusal.contains("`teardown` is declared more than once on one resource"),
    "{refusal}"
  );
}

/// **THE WORD BELONGS TO `resource(…)` AND TO NOTHING ELSE.** Written bare on a field, it is not a
/// resource word out of place — it is a word this grammar does not have, and it earns the
/// orphan-attribute refusal, whose sentence names every spelling that DOES exist.
#[test]
fn a_teardown_outside_the_resource_word_is_an_orphan_attribute() {
  let refusal = refused(quote::quote! {
    pub struct App {
      #[ingraph(teardown)]
      videos: media::Video,
    }
  });

  assert!(
    refusal.contains("unknown entry in `#[ingraph(…)]` on a graph root's field"),
    "{refusal}"
  );
  assert!(
    refusal.contains("a `teardown` composed with any of them"),
    "and the sentence names the spellings that exist: {refusal}"
  );
}