assert2ify_macros/
lib.rs

1use proc_macro::TokenStream;
2
3use quote::quote;
4use syn::fold::Fold;
5use syn::{parse_macro_input, ItemFn};
6
7use crate::detail::{apply_unused_attributes_workaround, check_redefinition_of_assert2ify};
8use assert2ification::Assert2Ification;
9
10mod assert2ification;
11mod detail;
12mod macro_parsing;
13
14#[proc_macro_attribute]
15pub fn assert2ify(args: TokenStream, input: TokenStream) -> TokenStream {
16    // See this example by dtolnay on how to traverse a syntax tree and replace nodes
17    // https://github.com/dtolnay/syn/blob/master/examples/trace-var/trace-var/src/lib.rs
18    let func = parse_macro_input!(input as ItemFn);
19
20    // apply a workaround that will suppress clippy and compiler warnings when
21    // should_panic or ignore are encountered in tests. See the doc of the function for more info
22    let func = apply_unused_attributes_workaround(func);
23
24    // guard this macro (to some degree) against having this attribute specified twice
25    if let Err(error) = check_redefinition_of_assert2ify(&func) {
26        return error.into_compile_error().into();
27    }
28
29    // Parse the list of variables the user wanted to print.
30    let mut assert2ification = parse_macro_input!(args as Assert2Ification);
31
32    // Use a syntax tree traversal to transform the function body.
33    // there is other syntax traversal functionality like syn::visit_mut::VisitMut
34    // that allows us to traverse nodes and replace them with anything else.
35    // Fold just allows us to replace the node with a node of the same type,
36    // (i.e. macro with macro), which is fine for my use case
37    let output = assert2ification.fold_item_fn(func);
38
39    TokenStream::from(quote!(#output))
40}