test_group/
lib.rs

1// Copyright Kamu Data, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10extern crate proc_macro2;
11
12#[proc_macro_attribute]
13pub fn group(
14    args: proc_macro::TokenStream,
15    tokens: proc_macro::TokenStream,
16) -> proc_macro::TokenStream {
17    let tokens = proc_macro2::TokenStream::from(tokens);
18
19    let mut groups: Vec<syn::Ident> =
20        syn::parse_macro_input!(args with syn::punctuated::Punctuated::<syn::Ident, syn::Token![,]>::parse_terminated)
21            .into_iter()
22            .collect();
23
24    use sha2::Digest;
25    let digest = sha2::Sha256::digest(&tokens.to_string());
26    let hash = hex::encode(digest);
27
28    let mut body = tokens;
29
30    while let Some(group) = groups.pop() {
31        body = quote::quote! {
32            mod #group {
33                use super::*;
34                #body
35            }
36        };
37    }
38
39    // Wrap into a module named after the test body hash to avoid module name
40    // conflicts
41    let modname = syn::Ident::new(&format!("g{}", &hash[0..4]), proc_macro2::Span::call_site());
42    body = quote::quote! {
43        mod #modname {
44            use super::*;
45            #body
46        }
47    };
48
49    body.into()
50}