I am doing advent of code, which is a collection of 25 programming problems, one for each of day of the advent.
I structure each day in it's own separate file/module, so for example year 2021 day 7 would be at src/years/year2021/day07.rs
. So src/years/year2021/mod.rs
ends up being just pub mod
s
pub mod day01;
pub mod day02;
pub mod day04;
// and so on...
Is there a way I could generate this list dynamically (with something like a recursive macro), so check if module day01 is accessible from this context (or alternatively if ./day01.rs exists) and generate the pub mod
automatically, and add more as files are created.
The best would be the ability to check if any name exists, like a module or a function inside a module.
You could use the build.rs to generate a module based on the files that exist at build time.
Something like this possably
let years_path = path::Path::new("./src/years");
let mut mod_file = fs::File::create(years_path.join("mod.rs")).unwrap();
let paths = fs::read_dir(years_path).unwrap();
for entry in paths {
let entry = entry.unwrap();
if entry.metadata().unwrap().is_dir() {
writeln!(
mod_file,
"mod {};",
entry.path().file_name().unwrap().to_str().unwrap()
)
.unwrap();
}
}