Is there a way to process an entire crate with Rust code or proc macro?

Is it possible to analyze the code of an entire crate and write output in a file? For example, list all defined structs and print their names line by line into a file.

I search and can't find anything relevant enough.

You can use syn::parse_file() to parse the source files of the crate.

Here is an example which collects all uses of structx!{} in a crate.

However it’s not perfect to analyze a crate using syn only. The definitions of structs can be generated by build scripts or macros, and controlled by features/environment variables.

If you want comprehensive analysis, use rust-analyze libraries.

1 Like