I want to implement some file contents mutation functionality in Rust, but I am a bit lost on how to achieve this using procedural macros. I have an input file that is specified using an environment variable at build time. I want to get the content of this file and apply some mutations to it.
I am aware that nesting macro calls is not possible. Is there however another way to combine multiple macro calls? I was thinking of the following:
let filename = std::env!("FILE_PATH");
let mutated_file_contents = mutate_file!(filename);
...
#[proc_macro]
pub fn static_encrypt_file(tokens: TokenStream) -> TokenStream {
for token in tokens {
println!("{}", token);
}
}
In the mutate_file
macro, I want to load the file, do some mutations, and return the contents. However, instead of the path to the file, filename
as a symbol is passed to the macro.
So the question is: Is it possible to combine multiple macros or should macros not have any dependencies to other macros?
If the latter is the case, it would make a lot of macro code less generic (for instance if I load the specific env variable in the mutate_file
macro, I would need to create another identical macro if I wanted to reuse the functionality for another file).
3