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
//! Every rule this pass states, each put to a declaration that breaks it — and the shapes it
//! deliberately does NOT refuse.
//!
//! The refusals are asserted here rather than only as fixtures, because a `trybuild` file pins the
//! MESSAGE a user sees and this pins the rule: a pass that stopped checking would still print
//! nothing, and an empty error list is what both look like from the outside.

/// The pass under test, over a declaration written the way the derive took one.
fn parse(input: &syn::DeriveInput) -> syn::Result<ingraph_codegen::resource::ResourceModel> {
  let (host, arguments) = crate::host::split(input);
  super::parse(&host, &arguments)
}

/// Parse a declaration, expecting it to be accepted.
fn accepted(input: proc_macro2::TokenStream) -> ingraph_codegen::resource::ResourceModel {
  parse(&syn::parse2(input).expect("the declaration parses"))
    .expect("the declaration is a resource")
}

/// Parse a declaration, expecting the refusal it earns.
fn refused(input: proc_macro2::TokenStream) -> String {
  parse(&syn::parse2(input).expect("the declaration parses"))
    .expect_err("this declaration breaks a rule")
    .into_iter()
    .map(|error| error.to_string())
    .collect::<Vec<_>>()
    .join("\n")
}

/// The whole content of a resource declaration is that it IS one: the type's name, and where
/// `ingraph` lives.
#[test]
fn a_resource_is_its_name_and_the_crate_it_reaches() {
  let model = accepted(quote::quote! {
    pub struct Egemma {
      model: EgemmaModel,
    }
  });

  assert_eq!(model.name, "Egemma");
  assert_eq!(
    quote::ToTokens::to_token_stream(&model.krate).to_string(),
    ":: ingraph"
  );
}

/// `crate = "…"` moves where the expansion reaches the framework, exactly as every other grammar
/// here does.
#[test]
fn a_resource_can_name_the_crate() {
  let model = accepted(quote::quote! {
    #[ingraph(crate = "crate::shim")]
    pub struct Egemma;
  });

  assert_eq!(
    quote::ToTokens::to_token_stream(&model.krate).to_string(),
    "crate :: shim"
  );
}

/// THE SHAPES THIS PASS ADMITS, and the reason it admits them: nothing in the expansion reads a
/// field or a variant, so a tuple struct, a unit struct and an enum are all perfectly good
/// resources. Refusing one would be this pass inventing a rule the tier does not have.
#[test]
fn a_resource_may_be_any_shape_at_all() {
  assert_eq!(
    accepted(quote::quote!(
      pub struct Egemma;
    ))
    .name,
    "Egemma"
  );
  assert_eq!(
    accepted(quote::quote!(
      pub struct Egemma(EgemmaModel);
    ))
    .name,
    "Egemma"
  );
  assert_eq!(
    accepted(quote::quote! {
      pub enum Backend {
        Cpu,
        Metal(Device),
      }
    })
    .name,
    "Backend"
  );
}

/// A GENERIC declaration is refused: the expansion registers ONE type as a citizen, and a parameter
/// is not a type until it is substituted.
#[test]
fn a_resource_takes_no_generic_parameter() {
  let refusal = refused(quote::quote! {
    pub struct Model<B> {
      backend: B,
    }
  });
  assert!(
    refusal.contains("no generic parameter"),
    "the refusal names the rule: {refusal}"
  );
  assert!(
    refusal.contains("citizen"),
    "and says what the registration is: {refusal}"
  );
}

/// The option block a derive would have carried beside the declaration is a named MIGRATION, as it
/// is under every other word in this family.
#[test]
fn the_options_are_the_attributes_arguments() {
  let refusal = refused(quote::quote! {
    #[ingraph(crate = "crate::shim")]
    #[ingraph(crate = "crate::shim")]
    pub struct Egemma;
  });
  assert!(
    refusal.contains("declared more than once"),
    "a repeated `crate` is refused: {refusal}"
  );
}

/// An unknown entry names the two places a resource's other facts actually live, rather than
/// listing what this grammar happens to take.
#[test]
fn an_unknown_entry_names_where_the_fact_belongs() {
  let refusal = refused(quote::quote! {
    #[ingraph(options = "EgemmaOptions")]
    pub struct Egemma;
  });
  assert!(
    refusal.contains("`AsyncResource` impl"),
    "the refusal points at the contract: {refusal}"
  );
  assert!(
    refusal.contains("launch"),
    "and at the launch, for the options' source: {refusal}"
  );
}