wasmtime_test_macros/
flags_test.rs

1use proc_macro2::TokenStream;
2use quote::{format_ident, quote};
3use syn::parse::{Parse, ParseStream};
4use syn::{parse_macro_input, Error, Result, Token};
5
6#[derive(Debug)]
7struct FlagsTest {
8    name: String,
9    flag_count: usize,
10}
11
12impl Parse for FlagsTest {
13    fn parse(input: ParseStream) -> Result<Self> {
14        let name = input.parse::<syn::Ident>()?.to_string();
15        input.parse::<Token![,]>()?;
16        let flag_count = input.parse::<syn::LitInt>()?.base10_parse()?;
17
18        Ok(Self { name, flag_count })
19    }
20}
21
22pub fn run(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
23    expand(&parse_macro_input!(input as FlagsTest))
24        .unwrap_or_else(Error::into_compile_error)
25        .into()
26}
27
28fn expand(test: &FlagsTest) -> Result<TokenStream> {
29    let name = format_ident!("{}", test.name);
30    let flags = (0..test.flag_count)
31        .map(|index| {
32            let name = format_ident!("F{}", index);
33            quote!(const #name;)
34        })
35        .collect::<TokenStream>();
36
37    let expanded = quote! {
38        wasmtime::component::flags! {
39            #name {
40                #flags
41            }
42        }
43    };
44
45    Ok(expanded)
46}