Skip to main content

icedwright_probe_macros/
lib.rs

1//! Derives for `icedwright-probe`, which re-exports them. Use them through the probe:
2//! `#[derive(icedwright_probe::TestId)]` and `#[derive(icedwright_probe::Messages)]`.
3//!
4//! Generated code names `::icedwright_probe` by absolute path. When the probe is renamed or
5//! reached through a re-export, `#[icedwright_probe(crate = path)]` on the enum points it
6//! elsewhere.
7
8mod attrs;
9mod case;
10mod format;
11mod messages;
12mod test_id;
13
14use proc_macro::TokenStream;
15use syn::{DeriveInput, parse_macro_input};
16
17/// Implements `icedwright_probe::TestId` for an enum.
18///
19/// Unit variants become their name in kebab case (`NewItem` is `"new-item"`).
20/// `#[test_id("...")]` sets the string; on a variant with fields it is a format string that
21/// names every field, as `{0}` for a tuple field or `{name}` for a named one. Literal text may
22/// hold only `a-z`, `0-9`, `.`, `_` and `-`, and two variants may not share a string.
23#[proc_macro_derive(TestId, attributes(test_id, icedwright_probe))]
24pub fn derive_test_id(input: TokenStream) -> TokenStream {
25    let input = parse_macro_input!(input as DeriveInput);
26    test_id::expand(&input)
27        .unwrap_or_else(syn::Error::into_compile_error)
28        .into()
29}
30
31/// Implements `icedwright_probe::Messages` for an enum: the table of every variant and the
32/// index of a value's variant.
33///
34/// `#[messages(nested)]` on a variant with one field whose type also implements `Messages`
35/// lists that type's variants under this one, as `Editor::Bold`.
36#[proc_macro_derive(Messages, attributes(messages, icedwright_probe))]
37pub fn derive_messages(input: TokenStream) -> TokenStream {
38    let input = parse_macro_input!(input as DeriveInput);
39    messages::expand(&input)
40        .unwrap_or_else(syn::Error::into_compile_error)
41        .into()
42}