1 个不稳定版本
0.1.0 | 2022年8月17日 |
---|
#156 在 #load
7KB
list_files_macro
一个简单的过程宏,用于生成一个常量文件名列表,并可选择对每个文件应用一个宏。
use std::fs::canonicalize;
use list_files_macro::list_files;
fn get_full_path(path: &str) -> String {
canonicalize(path).unwrap().into_os_string().into_string().unwrap()
}
const FILENAMES: [&'static str; 3] = list_files!("../tests/files/*.rs");
assert_eq!(FILENAMES, [
"tests/files/a.rs",
"tests/files/b.rs",
"tests/files/c.rs",
].map(get_full_path));
const CONTENTS: [&'static str; 3] = list_files!(include_str, "../tests/files/*.rs");
assert_eq!(CONTENTS[0], r#"
pub fn run() -> &'static str {
"A"
}
"#);
macro_rules! run_file {
($x:expr) => {
{
#[path = $x]
mod file;
file::run()
}
};
}
let results = list_files!(run_file, "../tests/files/*.rs");
assert_eq!(results, [
"A",
"B",
"C",
]);
要使用它,将其添加到您的Cargo.toml依赖项中
[dependencies]
list_files_macro = "^0.1.0"
依赖项
~1.5MB
~36K SLoC