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 `tracing(level = …)` WORD's grammar, read straight off the pass.
//!
//! A suite of its own beside [`crate::parse::tests`] for the reason [`crate::parse::channel_tests`]
//! gives: what is asserted here is ONE word — which shape it takes, which it refuses, and whether a
//! refusal names the fix.
//!
//! Everything here runs in EVERY build of this crate, which is the word's own ruling made
//! measurable. The level parses, refuses and reaches the model whether or not `ingraph` was
//! compiled with observation; what the feature decides is only what the emitter does with what these
//! walks read. A suite that could only run under a feature would be a grammar that quietly had two
//! meanings.

use ingraph_codegen::prelude::*;

/// The pass under test, at the kind this word belongs to.
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = crate::host::split(input);
  crate::parse::parse(&host, &options, NodeKind::Operator)
}

/// Every refusal one declaration collects, as text.
fn refusals(input: syn::DeriveInput) -> Vec<String> {
  operator(&input)
    .expect_err("the declaration is refused")
    .into_iter()
    .map(|e| e.to_string())
    .collect()
}

/// Whether any of `messages` names `rule`.
fn names(messages: &[String], rule: &str) -> bool {
  messages.iter().any(|message| message.contains(rule))
}

/// The word's own reading — one bare level, kept as the model's own vocabulary.
#[test]
fn the_level_is_a_bare_word() {
  let model = operator(&syn::parse_quote! {
    #[ingraph(tracing(level = debug))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("one word, one level");
  assert_eq!(model.tracing, Some(TracingLevel::Debug));
}

/// **EVERY LEVEL THE FRAMEWORK SHIPS IS WRITABLE**, and the walk reads the vocabulary off the enum
/// rather than off a list beside it — so a level added to one and not the other fails here.
#[test]
fn every_shipped_level_is_a_word_a_declaration_can_write() {
  for level in TracingLevel::ALL {
    let word = syn::Ident::new(level.as_str(), proc_macro2::Span::call_site());
    let model = operator(&syn::parse_quote! {
      #[ingraph(tracing(level = #word))]
      struct Decode {
        #[ingraph(id)]
        id: u64,
      }
    })
    .unwrap_or_else(|_| {
      panic!(
        "`{word}` is shipped and must be writable",
        word = level.as_str()
      )
    });
    assert_eq!(model.tracing, Some(level));
  }
}

/// **ABSENT IS THE FRAMEWORK'S DEFAULT, and the model says nothing rather than saying `info`**: a
/// declaration that wrote no word emits no const, so `Declared::TRACING`'s own default is the ONE
/// place the default lives.
#[test]
fn a_declaration_with_no_word_carries_no_level() {
  let model = operator(&syn::parse_quote! {
    struct Keep {
      #[ingraph(id)]
      id: u64,
    }
  })
  .expect("the word is optional");
  assert_eq!(model.tracing, None);
}

/// A level the framework does not ship is refused AT THE WORD, with the five it does ship named.
#[test]
fn a_level_the_framework_does_not_ship_is_refused_by_name() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(tracing(level = verbose))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&messages, "`verbose` is not a level this framework names"),
    "{messages:?}"
  );
  for level in TracingLevel::ALL {
    let word = level.as_str();
    assert!(
      names(&messages, &format!("`{word}`")),
      "the refusal omits `{word}`: {messages:?}"
    );
  }
}

/// The MINT-BARE law, applied: a quoted level is refused, and the refusal says which spelling the
/// grammar takes and why the other one means something else here.
#[test]
fn a_quoted_level_is_refused_and_the_refusal_names_the_law() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(tracing(level = "debug"))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&messages, "`level` takes a BARE WORD"),
    "{messages:?}"
  );
  assert!(
    names(&messages, "REFERS to something declared elsewhere"),
    "the refusal does not say why the quoted form is not merely a second spelling: {messages:?}"
  );
}

/// The word is a BLOCK: bare, it says nothing, and the refusal shows the whole form.
#[test]
fn the_bare_word_is_refused_with_the_form_it_takes() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(tracing)]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(
      &messages,
      "`tracing` is a block — write `tracing(level = debug)`"
    ),
    "{messages:?}"
  );
}

/// An EMPTY block is the same absence written with parentheses, and earns the same sentence.
#[test]
fn an_empty_block_is_refused_the_way_the_bare_word_is() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(tracing())]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(names(&messages, "`tracing` is a block"), "{messages:?}");
}

/// The block carries ONE word, and anything else in it is refused by name rather than ignored.
#[test]
fn an_unknown_entry_in_the_block_is_refused() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(tracing(level = debug, target = "media"))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&messages, "unknown entry in `tracing(…)`"),
    "{messages:?}"
  );
}

/// SAID IT TWICE, both ways round: two blocks, and two levels inside one block.
#[test]
fn the_word_is_declared_once() {
  let two_blocks = refusals(syn::parse_quote! {
    #[ingraph(tracing(level = debug), tracing(level = warn))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&two_blocks, "`tracing(…)` is declared more than once"),
    "{two_blocks:?}"
  );

  let two_levels = refusals(syn::parse_quote! {
    #[ingraph(tracing(level = debug, level = warn))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&two_levels, "`level` is declared more than once"),
    "{two_levels:?}"
  );
}

/// The word is the NODE's and not the record's, so every node kind takes it — a full-text index
/// observes itself exactly as an operator does.
#[test]
fn every_node_kind_takes_the_word() {
  for kind in [
    NodeKind::Operator,
    NodeKind::Record,
    NodeKind::Fulltext,
    NodeKind::Vector,
    NodeKind::Blob,
  ] {
    let input: syn::DeriveInput = syn::parse_quote! {
      #[ingraph(tracing(level = error))]
      struct Decode {
        #[ingraph(id)]
        id: u64,
      }
    };
    let (host, options) = crate::host::split(&input);
    // The other refusals a kind earns for a declaration this thin are not this word's business —
    // what is asserted is that NONE of them is about `tracing`.
    match crate::parse::parse(&host, &options, kind) {
      Ok(model) => assert_eq!(model.tracing, Some(TracingLevel::Error), "{kind:?}"),
      Err(refused) => {
        let messages: Vec<String> = refused.into_iter().map(|e| e.to_string()).collect();
        assert!(
          !names(&messages, "tracing"),
          "{kind:?} refused the word: {messages:?}"
        );
      }
    }
  }
}

/// The container vocabulary a reader is offered NAMES the word, so an author who writes it wrong
/// is told it exists at all.
#[test]
fn the_unknown_attribute_sentence_lists_the_word() {
  let messages = refusals(syn::parse_quote! {
    #[ingraph(traicng(level = debug))]
    struct Decode {
      #[ingraph(id)]
      id: u64,
    }
  });
  assert!(
    names(&messages, "unknown `ingraph` record attribute"),
    "{messages:?}"
  );
  assert!(
    names(&messages, "`tracing(…)`"),
    "the vocabulary a misspelling is answered with omits the word: {messages:?}"
  );
}