Is it possible to generate text file from source code during compilation?

The file content is based on Rust source code.
build.rs is not helpful since it runs before compiling Rust source code.

I imagine a developer experience like:

// Rust source code
#[some_macro]
struct A;

Then run cargo build as usual, I can get a text file (perhaps in $OUT_DIR):

// The generated text fle
You defined a struct A.

Is it possible to have such macros?

What do you want to do with this text file?

One technique I've used quite often is to create a test which will make sure a generated file, in this case auto-generated types for an AST, is always up to date:

It's a trick I borrowed from how rust-analyzer handles its AST and various other auto-generated file, which in turn uses matklad's self-modifying code trick.

The above example is for generating Rust code, but I've also used it to make sure a schema.grapql file stays in sync with the upstream GraphQL API.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.