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