I'm writing a Rust program that generates Rust files, but I'm not sure where I should place these files. The generated files rely on a module within the crate.
I'd like to get some advice on where I should generate these files - should I create a new crate within my current crate? How can I ensure that the generated files imports from my current crate and can be executed?
If it's generated at build time, the general correct way to do it is via a build.rs buildscript, and put the files in $env:OUT_DIR, which you then include! to mount into your crate's source tree.
If you want to generate the source ahead-of-time, and commit the generated source to source control, then whatever's most convenient for you and the source generation is fine.
Thanks for the response. It's generated at runtime - the purpose is to generate Rust files (the first run) and then after generating all of them, run these files (the second run). My main program is intended to generate programs that uses 1 module from the main program, but the main program doesn't use the generated programs
I'm currently using a shell script to do this, where I'm generating in the following location:
I'm currently modifying the mod.rs file during the first run by appending the names of the generated files. Doing this so the rust analyser doesn't shout at me for not including the generated files in the file tree.
However, when it comes to the second run I have to manually (or via shell script) compile and execute the generated files within module_that_i_want_generated_files_to_use.
Alternatively I can generate in the bin folder, but I'm not familiar having multiple binaries. I'm having a look at that at the moment but if you have any comments on the above please share!
Personally, I'd make this multiple crates within a single cargo workspace:
Crate 1 contains any modules that the generated code will need to rely on.
Crate 2 is the code generation library, which exports a function that takes the destination dir as a parameter. If necessary, it can use crate 1 as a dependency.
Crate 3 is the generated program, which lists crate2 as a build dependency and crate1 as a regular dependency. It calls the generator from build.rs and includes the result as @CAD97 describes. Any project-specific configuration can be baked into build.rs as additional arguments passed to the generator.