Hello, apologies if this is not the right place to ask, but I know people in here would know the answer. I am looking to programatically generate a crate based on another crate. Is there an API available in rust-analyzer or something rust-analyzer uses to get type information? This must be possible since the crate documentation on crates.io seems to be able to resolve all the information I would need... I just have no idea how easily possible. Thanks!
You can use rust-analyzer for this, with a caveat that:
A good place to start would be
rust-analyzer analysis-stats . command
It traverses the crate and infers types of all of the functions defined therein.
The rustdoc JSON backend will hopefully land this week and have the information you want.
The format is likely to change significantly before being stabilized.
I'm still slightly nervous about how this interacts with --document-private-items, but since it's unstable I'm ok with it for now.
@matklad thanks! I'm interested in inspecting the structs. I don't mind too much depending on unstable api. On a sidenote, it would be great if Rust had something like TypeScript does with ts-morph (https://github.com/dsherret/ts-morph/tree/latest/packages/ts-morph#example) and some wrapper around rust-analyzer in the future might make that possible. Huge amount of work to implement though!
@Joshua Nelson oh, that is awesome! It's a little strange for me to rely on something like that, but it also means I could get the output and quickly write up something in a scripting language to do the code generation. My use case is also quite simple. Thanks for bringing that to my attention! I think I'm going to go with that.
I have visitor over the rust ast from rust-analyzer at https://github.com/pksunkara/cargo-up/tree/master/cargo-up/rust-visitor. And then I also have a little bit of code like tsmorph at https://github.com/pksunkara/cargo-up/blob/master/cargo-up/src/lib/runner.rs#L277. The linked function renames a struct
@David Sherret ^^
The JSON backend landed btw, it should be in the next nightly :)
@Joshua Nelson thanks once again for the great tip about using rustdoc's JSON backend! It worked out very well. I was able to code generate this library using the rustdoc output https://github.com/dsherret/dprint-swc-ecma-ast-view (still working on it to sort out some minor details, but it's basically done)
that's awesome! Glad it helped :D