1 //! Rustdoc's JSON backend
3 //! This module contains the logic for rendering a crate as JSON rather than the normal static HTML
4 //! output. See [the RFC](https://github.com/rust-lang/rfcs/pull/2963) and the [`types`] module
5 //! docs for usage and details.
9 use std::cell::RefCell;
11 use std::path::PathBuf;
14 use rustc_data_structures::fx::FxHashMap;
15 use rustc_middle::ty::TyCtxt;
16 use rustc_session::Session;
18 use rustdoc_json_types as types;
21 use crate::config::RenderOptions;
22 use crate::error::Error;
23 use crate::formats::cache::Cache;
24 use crate::formats::FormatRenderer;
25 use crate::html::render::cache::ExternalLocation;
26 use crate::json::conversions::{from_def_id, IntoWithTcx};
29 crate struct JsonRenderer<'tcx> {
31 /// A mapping of IDs that contains all local items for this crate which gets output as a top
32 /// level field of the JSON blob.
33 index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
34 /// The directory where the blob will be written to.
39 impl JsonRenderer<'tcx> {
40 fn sess(&self) -> &'tcx Session {
44 fn get_trait_implementors(&mut self, id: rustc_span::def_id::DefId) -> Vec<types::Id> {
45 Rc::clone(&self.cache)
52 let item = &i.impl_item;
53 self.item(item.clone()).unwrap();
54 from_def_id(item.def_id)
61 fn get_impls(&mut self, id: rustc_span::def_id::DefId) -> Vec<types::Id> {
62 Rc::clone(&self.cache)
69 let item = &i.impl_item;
70 if item.def_id.is_local() {
71 self.item(item.clone()).unwrap();
72 Some(from_def_id(item.def_id))
82 fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
83 Rc::clone(&self.cache)
86 .filter_map(|(&id, trait_item)| {
87 // only need to synthesize items for external traits
89 let trait_item = &trait_item.trait_;
90 trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
95 crate_id: id.krate.as_u32(),
104 .expect("Trait should either be in local or external paths")
109 visibility: types::Visibility::Public,
110 inner: types::ItemEnum::Trait(trait_item.clone().into_tcx(self.tcx)),
112 docs: Default::default(),
113 links: Default::default(),
114 attrs: Default::default(),
115 deprecation: Default::default(),
126 impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
127 fn descr() -> &'static str {
131 const RUN_ON_MODULE: bool = false;
135 options: RenderOptions,
138 ) -> Result<(Self, clean::Crate), Error> {
139 debug!("Initializing json renderer");
143 index: Rc::new(RefCell::new(FxHashMap::default())),
144 out_path: options.output,
145 cache: Rc::new(cache),
151 fn make_child_renderer(&self) -> Self {
155 /// Inserts an item into the index. This should be used rather than directly calling insert on
156 /// the hashmap because certain items (traits and types) need to have their mappings for trait
157 /// implementations filled out before they're inserted.
158 fn item(&mut self, item: clean::Item) -> Result<(), Error> {
159 // Flatten items that recursively store other items
160 item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
162 let id = item.def_id;
163 if let Some(mut new_item) = self.convert_item(item) {
164 if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
165 t.implementors = self.get_trait_implementors(id)
166 } else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
167 s.impls = self.get_impls(id)
168 } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
169 e.impls = self.get_impls(id)
171 let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
173 // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
174 // to make sure the items are unique. The main place this happens is when an item, is
175 // reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
176 if let Some(old_item) = removed {
177 assert_eq!(old_item, new_item);
184 fn mod_item_in(&mut self, _item: &clean::Item) -> Result<(), Error> {
185 unreachable!("RUN_ON_MODULE = false should never call mod_item_in")
188 fn after_krate(&mut self) -> Result<(), Error> {
189 debug!("Done with crate");
190 let mut index = (*self.index).clone().into_inner();
191 index.extend(self.get_trait_items());
192 // This needs to be the default HashMap for compatibility with the public interface for
194 #[allow(rustc::default_hash_types)]
195 let output = types::Crate {
196 root: types::Id(String::from("0:0")),
197 crate_version: self.cache.crate_version.clone(),
198 includes_private: self.cache.document_private,
199 index: index.into_iter().collect(),
205 .chain(self.cache.external_paths.clone().into_iter())
206 .map(|(k, (path, kind))| {
210 crate_id: k.krate.as_u32(),
212 kind: kind.into_tcx(self.tcx),
217 external_crates: self
224 types::ExternalCrate {
225 name: v.0.to_string(),
226 html_root_url: match &v.2 {
227 ExternalLocation::Remote(s) => Some(s.clone()),
236 let mut p = self.out_path.clone();
237 p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
238 p.set_extension("json");
239 let file = File::create(&p).map_err(|error| Error { error: error.to_string(), file: p })?;
240 serde_json::ser::to_writer(&file, &output).unwrap();
244 fn cache(&self) -> &Cache {