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

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.

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 ( 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 And then I also have a little bit of code like tsmorph at 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 (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