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 ID DEVICE's FRONT END — the three shape laws, and the words this door refuses by pointing at
//! the door that has them.
//!
//! `ingraph`'s own `tests/key_admission/derive/*.rs` is where the refusals are pinned as rendered
//! TEXT, which is the assertion that matters to a reader. What is checked here is the pass itself:
//! which declarations reach a model at all, and that a refusal SAYS the thing rather than merely
//! failing.

use super::parse;

/// The refusal a declaration draws, as one string.
fn refused(input: syn::DeriveInput) -> String {
  match parse(&input) {
    Ok(_) => panic!("the declaration was admitted"),
    Err(refusal) => refusal
      .into_iter()
      .map(|one| one.to_string())
      .collect::<Vec<_>>()
      .join("\n"),
  }
}

/// The DRIVING declaration, admitted with nothing beside it.
#[test]
fn a_transparent_tuple_wrapper_is_admitted() {
  let model = parse(&syn::parse_quote! {
    #[repr(transparent)]
    pub struct TrackId(uuid::Uuid);
  })
  .expect("the shape is the one this device equips");
  assert_eq!(model.subject, "TrackId");
  assert_eq!(
    quote::ToTokens::to_token_stream(&model.inner).to_string(),
    "uuid :: Uuid"
  );
  assert_eq!(
    quote::ToTokens::to_token_stream(&model.krate).to_string(),
    ":: ingraph"
  );
}

/// A representation is a SET, so the word is looked for among the entries rather than read off the
/// first one.
#[test]
fn transparent_is_found_beside_another_repr_entry() {
  parse(&syn::parse_quote! {
    #[repr(align(16), transparent)]
    pub struct TrackId(uuid::Uuid);
  })
  .expect("`transparent` is present, whatever else the representation says");
}

/// THE ADMISSION REQUIREMENT, refused by the sentence the ruling wrote.
#[test]
fn a_declaration_without_the_representation_is_refused_by_name() {
  let refusal = refused(syn::parse_quote! {
    pub struct TrackId(uuid::Uuid);
  });
  assert!(
    refusal.contains("the Id derive's admission requires `#[repr(transparent)]`"),
    "{refusal}"
  );
  assert!(refusal.contains("from_ref"), "{refusal}");
}

/// A `repr` that is not the one asked for is the same absence.
#[test]
fn another_representation_is_not_the_one_asked_for() {
  let refusal = refused(syn::parse_quote! {
    #[repr(C)]
    pub struct TrackId(uuid::Uuid);
  });
  assert!(
    refusal.contains("requires `#[repr(transparent)]`"),
    "{refusal}"
  );
}

/// The TUPLE law: a named field is refused, and told the one-line fix.
#[test]
fn a_named_field_is_refused_with_the_rewrite() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub struct TrackId {
      id: uuid::Uuid,
    }
  });
  assert!(refusal.contains("equips a TUPLE wrapper"), "{refusal}");
  assert!(refusal.contains("struct Name(Inner);"), "{refusal}");
}

/// Two values are two answers with no rule for choosing between them.
#[test]
fn a_second_field_is_refused_with_the_count() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub struct TrackId(uuid::Uuid, u32);
  });
  assert!(
    refusal.contains("EXACTLY ONE value, and this one holds 2"),
    "{refusal}"
  );
  assert!(refusal.contains("KEY STRUCT"), "{refusal}");
}

/// A wrapper holding nothing addresses nothing.
#[test]
fn an_empty_wrapper_is_refused() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub struct TrackId;
  });
  assert!(refusal.contains("holds NOTHING"), "{refusal}");
}

/// An enum's content is its alternatives, and the sentence points at the device that equips one.
#[test]
fn an_enum_is_refused_and_pointed_at_the_vocabulary_door() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub enum TrackId {
      One(uuid::Uuid),
    }
  });
  assert!(refusal.contains("this declaration is an ENUM"), "{refusal}");
  assert!(refusal.contains("#[derive(ingraph::Enum)]"), "{refusal}");
}

/// A union has no identity anything can compare.
#[test]
fn a_union_is_refused() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub union TrackId {
      one: u64,
    }
  });
  assert!(refusal.contains("this declaration is a UNION"), "{refusal}");
}

/// An id is a CONCRETE address, and the three rows that break are named rather than left to rustc.
#[test]
fn a_generic_wrapper_is_refused_with_the_three_rows() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    pub struct Wrap<T>(T);
  });
  assert!(refusal.contains("equips a CONCRETE wrapper"), "{refusal}");
  assert!(refusal.contains("E0210"), "{refusal}");
  assert!(refusal.contains("E0392"), "{refusal}");
}

/// EVERY rule the pass can still see is reported in one compile — a declaration breaking two laws
/// hears about both.
#[test]
fn the_laws_report_together() {
  let refusal = refused(syn::parse_quote! {
    pub struct Wrap<T>(T);
  });
  assert!(refusal.contains("equips a CONCRETE wrapper"), "{refusal}");
  assert!(
    refusal.contains("requires `#[repr(transparent)]`"),
    "{refusal}"
  );
}

/// The one word this door takes.
#[test]
fn the_crate_word_moves_the_root() {
  let model = parse(&syn::parse_quote! {
    #[repr(transparent)]
    #[ingraph(crate = "other::place")]
    pub struct TrackId(uuid::Uuid);
  })
  .expect("`crate = \"…\"` is this device's only option");
  assert_eq!(
    quote::ToTokens::to_token_stream(&model.krate).to_string(),
    "other :: place"
  );
}

/// A NEWTYPE word is refused BY NAME and pointed at the door that has it — never told it does not
/// exist, which is the one thing that is not true of it.
#[test]
fn a_newtype_word_is_refused_by_name() {
  for (word, said) in [
    (
      quote::quote!(#[ingraph(sqlite)]),
      "the dialect words are `#[derive(ingraph::NewType)]`'s",
    ),
    (
      quote::quote!(#[ingraph(cursor)]),
      "`cursor` is `#[derive(ingraph::NewType)]`'s word",
    ),
    (
      quote::quote!(#[ingraph(marker(scalar))]),
      "`marker(<family>)` and `filter` are `#[derive(ingraph::NewType)]`'s words",
    ),
    (
      quote::quote!(#[ingraph(graphql(async_graphql(scalar)))]),
      "`graphql(<backend>(…))` is `#[derive(ingraph::NewType)]`'s word",
    ),
    (
      quote::quote!(#[ingraph(mongo)]),
      "`mongo` names the document kit",
    ),
  ] {
    let declaration: syn::DeriveInput = syn::parse_quote! {
      #[repr(transparent)]
      #word
      pub struct TrackId(uuid::Uuid);
    };
    let refusal = refused(declaration);
    assert!(refusal.contains(said), "missing {said} in {refusal}");
  }
}

/// An unknown word is an unknown word, and the near miss is measured against the one built word.
#[test]
fn an_unknown_word_is_refused_as_one() {
  let refusal = refused(syn::parse_quote! {
    #[repr(transparent)]
    #[ingraph(crat = "x")]
    pub struct TrackId(uuid::Uuid);
  });
  assert!(
    refusal.contains("unknown entry in `#[ingraph(…)]` on an id wrapper"),
    "{refusal}"
  );
}