I'd like to write some software in Rust that does some heavy-duty analysis of arbitrary Rust source code. I could try re-implementing a Rust parser, but that seems like a lot of unnecessary work since rustc already exists and has a working parser. As such, I'd like to somehow link to it.
When I started looking into how to do that, however, I came across some information, but some of my questions are still unanswered. I found a relevant book that describes the internals of rustc, which is extremely useful, but not enough information, as it focuses on helping to make contributions to rustc. While I have nothing against making such contributions should I find missing feature(s) that I want, it also isn't what I'm trying to do — the software that I want to write is well beyond the scope of a compiler.
Specifically, I need to know how one consumes rustc externally. It seems like a lot of its code is "internal" (see this for an example), suggesting some "external" APIs that I may want to look into to see if they meet my needs, but I can't find anything about that other than the aforementioned inference. It seems like there are no crates available that are really just bits of rustc either. Am I supposed to pull rustc's source into my program, and make changes to integrate it? If so, what are the best practices for reducing maintenance overhead so I'm not constantly refactoring the same code in slightly different ways over and over again every time it updates? If not, what am I supposed to do? I've also seen inklings of some sort of JSON-outputting option from rustc, but I can't find any detailed documentation on how this works, but it may be sufficient for my needs.