Stream: t-compiler/rust-analyzer

Topic: Get a crate's public type information?


David Sherret (Nov 30 2020 at 20:03, on Zulip):

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!

matklad (Dec 01 2020 at 14:22, on Zulip):

You can use rust-analyzer for this, with a caveat that:

matklad (Dec 01 2020 at 14:23, on Zulip):

A good place to start would be rust-analyzer analysis-stats . command

https://github.com/rust-analyzer/rust-analyzer/blob/bb697727a2e43d734a1bf3aae7644a9249aee11e/crates/rust-analyzer/src/cli/analysis_stats.rs#L149

It traverses the crate and infers types of all of the functions defined therein.

Joshua Nelson (Dec 01 2020 at 14:34, on Zulip):

The rustdoc JSON backend will hopefully land this week and have the information you want.

Joshua Nelson (Dec 01 2020 at 14:35, on Zulip):

The format is likely to change significantly before being stabilized.
https://github.com/rust-lang/rust/pull/79539#pullrequestreview-541878688

Joshua Nelson (Dec 01 2020 at 14:39, on Zulip):

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.

David Sherret (Dec 01 2020 at 23:46, on Zulip):

@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.

pksunkara (Dec 03 2020 at 18:27, on Zulip):

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

pksunkara (Dec 03 2020 at 18:27, on Zulip):

@David Sherret ^^

Joshua Nelson (Dec 03 2020 at 18:51, on Zulip):

The JSON backend landed btw, it should be in the next nightly :)

David Sherret (Dec 15 2020 at 21:12, on Zulip):

@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)

Joshua Nelson (Dec 15 2020 at 21:43, on Zulip):

that's awesome! Glad it helped :D

Last update: Jul 24 2021 at 21:00UTC