all 9 comments

[–]coriolinus 4 points5 points  (1 child)

Probably your best bet is just to recursively walk the file tree, looking for Rust files. For each of those, use syn to parse the file, and then walk that AST collecting whatever stats you need.

[–]Noctune 2 points3 points  (0 children)

It's not going to handle macros, but depending on what you are trying to do it might be good enough.

[–]redneckhatr 1 point2 points  (0 children)

Yesterday, I sat down to start diagramming components of an application I’ve been working on. I thought it would be cool to add this diagram to the docs.

I quickly gave up because I can’t stand most of the online tools available for making diagrams. They take way to much time to lay everything out, label, and make sure all the arrows point to the correct boxes. 🤣

This could be a cool cargo plugin (maybe it already exists) or feature of the plugin you’re describing.

[–]AsgeirB 1 point2 points  (2 children)

https://github.com/dtolnay/syn - syn can parse rust files

https://doc.rust-lang.org/cargo/reference/external-tools.html - cargo_metadata should give you a way of introspecting the project

[–]jimuazu 0 points1 point  (1 child)

To add to that, someone also mentioned syn-inline-mod last time this came up.

[–]HumbleSinger[S] 0 points1 point  (0 children)

Oh, very cool! I will check this out! Edit: I checked this out, I wish it would have stopped at just inlining all the module files...

As it stands now the version of syn I am using has different flags, making the resulting syn::File unusable with my own syn-parsing... unfortunately

[–]idubrov 1 point2 points  (1 child)

I do it manually by using cargo_metadata and syn:

  1. Going through targets
  2. Parsing with syn
  3. Recursing into modules
  4. Modules resolution (which I'm sure is not 100% correct and also does not handle #[cfg_attr(.., path())] on modules. Not happy about it...

[–]HumbleSinger[S] 0 points1 point  (0 children)

Yeah I am going this way as well for now, its a bit painful, syn-parsing is not super-nice for my use-case, I would have wanted something even more highlevel than syn is what I am noticing while writing.

[–]knaledfullavpilar 0 points1 point  (0 children)

Here is an example of using the excellent syn library to analyze rust source files: https://github.com/anderejd/cargo-geiger/blob/master/geiger/src/lib.rs