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
//! `generations(<engine>)` — the ROOT's own word for where its current pointers are written down.
//!
//! It sits beside `engines(…)` and is the opposite kind of word. An engine BINDING is a name a
//! deployment mints for a type it wrote, so its vocabulary is open and its right-hand side is a path;
//! this names a store the framework builds out of a DSN with no type from anybody, so its vocabulary
//! is CLOSED and it takes no right-hand side at all.
//!
//! What is proved here is everything a token walk can decide: the word taken, the three accepted, the
//! one that is an engine everywhere else and not here, the shapes refused by name, and the fill-once
//! rule.

use ingraph_codegen::model::DatabaseKind;

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

/// The three the framework builds unaided, each accepted and each carried through to the model.
#[test]
fn the_three_shipped_engines_are_taken_and_carried() {
  for (word, kind) in [
    ("sqlite", DatabaseKind::Sqlite),
    ("postgres", DatabaseKind::Postgres),
    ("mysql", DatabaseKind::Mysql),
  ] {
    let word = syn::Ident::new(word, proc_macro2::Span::call_site());
    let model = accepted(quote::quote! {
      #[ingraph(generations(#word))]
      struct App {
        videos: Video,
      }
    });
    let seat = model
      .generations
      .expect("the word was written, so the seat is there");
    assert_eq!(seat.engine, kind);
  }
}

/// A root that wrote no word declares NO seat — which is what makes the spine's field conditional.
#[test]
fn a_root_that_wrote_nothing_declares_no_seat() {
  let model = accepted(quote::quote! {
    struct App {
      videos: Video,
    }
  });
  assert!(model.generations.is_none());
}

/// **`mongo` IS REFUSED BY NAME**, and the sentence says why rather than calling it a typo: the word
/// is a real engine one declaration over (`persist(mongo)`), so a reader who wrote it here is owed
/// the reason the vocabulary is one word shorter.
#[test]
fn a_document_store_is_not_a_word_here() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(mongo))]
    struct App {
      videos: Video,
    }
  });
  assert!(
    refusal.contains("`mongo` is not an engine the generations seat builds"),
    "{refusal}"
  );
  assert!(
    refusal.contains("MEMBERSHIP sweep"),
    "the refusal says WHY the vocabulary is shorter: {refusal}"
  );
  assert!(
    refusal.contains("`ingraph::GenerationsStore`"),
    "and where the road is instead: {refusal}"
  );
}

/// A word this crate ships no store for is refused with the three it does.
#[test]
fn a_word_that_is_no_engine_is_refused_with_the_three_that_are() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(redb))]
    struct App {
      videos: Video,
    }
  });
  assert!(refusal.contains("`redb` is not an engine"), "{refusal}");
  assert!(
    refusal.contains("`sqlite`, `postgres` or `mysql`"),
    "{refusal}"
  );
}

/// The engine takes NO options — where on that engine and how deep its history goes are the
/// deployment's, and the refusal says which document key they reach the framework through.
#[test]
fn the_engine_is_a_bare_word_and_the_knobs_are_the_documents() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(sqlite(dsn = "sqlite://var/x.db")))]
    struct App {
      videos: Video,
    }
  });
  assert!(refusal.contains("`sqlite` is a bare word"), "{refusal}");
  assert!(refusal.contains("`[generations]` section"), "{refusal}");
  assert!(refusal.contains("`keep = <n>`"), "{refusal}");
}

/// A path with several segments is not one word.
#[test]
fn a_multi_segment_path_is_not_an_engine_word() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(app::store::Redb))]
    struct App {
      videos: Video,
    }
  });
  assert!(
    refusal.contains("the engine inside `generations(…)` is ONE word"),
    "{refusal}"
  );
}

/// **FILL ONCE**: one root, one seat. The FIRST word is kept and the second is refused naming it, so
/// the rest of the pass reads a settled declaration.
#[test]
fn one_root_declares_one_seat() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(sqlite), generations(postgres))]
    struct App {
      videos: Video,
    }
  });
  assert!(
    refusal.contains("`generations(…)` already named `sqlite`"),
    "the FIRST word is the one kept: {refusal}"
  );
}

/// Two engines inside ONE block is the same rule, and earns the same sentence.
#[test]
fn one_block_names_one_engine() {
  let refusal = refused(quote::quote! {
    #[ingraph(generations(sqlite, postgres))]
    struct App {
      videos: Video,
    }
  });
  assert!(
    refusal.contains("`generations(…)` already named `sqlite`"),
    "{refusal}"
  );
}

/// The word is on the ROSTER a wrong guess is answered with — a reader who misspelled it is offered
/// it as a near miss rather than left with two entries that do not fit.
#[test]
fn the_word_is_on_the_roster_a_wrong_guess_is_answered_with() {
  let refusal = refused(quote::quote! {
    #[ingraph(generation(sqlite))]
    struct App {
      videos: Video,
    }
  });
  assert!(refusal.contains("generations(<engine>)"), "{refusal}");
}