More macro!

This commit is contained in:
Gabriel Simmer 2024-07-04 15:23:31 +01:00
parent 472d7c923a
commit 5d182997d9
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
2 changed files with 28 additions and 2 deletions

View file

@ -27,3 +27,29 @@ pub fn manifest_list(dir: proc_macro::TokenStream) -> proc_macro::TokenStream {
gen.into() gen.into()
} }
#[proc_macro]
pub fn mod_manifest_list(dir: proc_macro::TokenStream) -> proc_macro::TokenStream {
let dir = dir.to_string();
let dir = dir.trim_matches('"');
let paths: Vec<String> = fs::read_dir(dir).unwrap().filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_file() && path.file_name()?.to_str() != Some("mod.rs") {
path.file_name()?.to_str().map(|s| s.to_owned().replace(".rs", ""))
} else {
None
}
}).collect();
// Generate the output tokens
let paths: Vec<proc_macro2::TokenStream> = paths.iter().map(|path| {
let ident = syn::Ident::new(path, proc_macro2::Span::call_site());
quote! { mod #ident; }
}).collect();
let gen = quote! {
#(#paths)*
};
gen.into()
}

View file

@ -1,8 +1,8 @@
use infra_rs::manifest_list; use infra_rs::{mod_manifest_list, manifest_list};
use crate::infra_rs::ResourceList; use crate::infra_rs::ResourceList;
mod vaultwarden; mod_manifest_list!("./src/manifests");
pub fn render() -> ResourceList { pub fn render() -> ResourceList {
let resources = manifest_list!("./src/manifests"); let resources = manifest_list!("./src/manifests");