list_mod/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use quote::quote;
5use std::env;
6use std::fs;
7use std::path::Path;
8use syn::{parse_macro_input, LitStr};
9
10#[proc_macro]
11pub fn generate_module_list(__input: TokenStream) -> TokenStream {
12    // Parse the input path and list name from the TokenStream
13    let __input = parse_macro_input!(__input as LitStr);
14    let __input = __input.value();
15    let __internal_base_path = Path::new(&__input);
16
17    // Get the project directory
18    let __internal_project_dir =
19        env::var("CARGO_MANIFEST_DIR").expect("Failed to get project directory");
20    let __internal_project_path = Path::new(&__internal_project_dir);
21
22    // Construct the full base path by appending the input path to the project directory
23    let __internal_full_base_path = __internal_project_path.join(__internal_base_path);
24
25    // Collect the module names by iterating over the entries in the full base directory
26    let __internal_module_names: Vec<String> = fs::read_dir(__internal_full_base_path)
27        .expect("Failed to read directory")
28        .filter_map(|entry| {
29            if let Ok(entry) = entry {
30                if let Some(entry_name) = entry.file_name().to_str() {
31                    if entry_name.ends_with(".rs")
32                        && entry_name != "mod.rs"
33                        && entry_name != "archetypes.rs"
34                    {
35                        return Some(entry_name[..entry_name.len() - 3].to_owned());
36                    } else if entry_name != "mod.rs" && entry_name != "archetypes.rs" {
37                        return Some(entry_name.to_owned());
38                    }
39                }
40            }
41            None
42        })
43        .collect();
44
45    // Generate array of module names
46    let __internal_module_array = quote! {
47        [
48            #(#__internal_module_names),*
49        ]
50    };
51
52    // Generate the static list of string slices with the custom list name
53    let __internal_macro_output: proc_macro2::TokenStream = quote! {
54        pub const MODULE_LIST: [&str; (__internal_module_names.len())] = [#__internal_module_array];
55    };
56
57    __internal_macro_output.into()
58}