2020-08-03 12:45:27 -05:00
|
|
|
//! Rustdoc's JSON backend
|
|
|
|
//!
|
|
|
|
//! This module contains the logic for rendering a crate as JSON rather than the normal static HTML
|
|
|
|
//! output. See [the RFC](https://github.com/rust-lang/rfcs/pull/2963) and the [`types`] module
|
|
|
|
//! docs for usage and details.
|
|
|
|
|
|
|
|
mod conversions;
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-12-31 23:25:30 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-12-11 22:36:51 -05:00
|
|
|
use rustc_session::Session;
|
2020-08-03 12:45:27 -05:00
|
|
|
use rustc_span::edition::Edition;
|
|
|
|
|
2021-01-25 16:23:43 -05:00
|
|
|
use rustdoc_json_types as types;
|
2021-01-22 16:09:24 -05:00
|
|
|
|
2020-07-30 13:54:26 -05:00
|
|
|
use crate::clean;
|
2021-02-11 21:29:22 -05:00
|
|
|
use crate::config::RenderOptions;
|
2020-07-30 13:54:26 -05:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::formats::cache::Cache;
|
|
|
|
use crate::formats::FormatRenderer;
|
2020-08-03 12:45:27 -05:00
|
|
|
use crate::html::render::cache::ExternalLocation;
|
2021-01-22 16:09:24 -05:00
|
|
|
use crate::json::conversions::from_def_id;
|
2020-07-30 13:54:26 -05:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2020-12-16 14:34:08 -05:00
|
|
|
crate struct JsonRenderer<'tcx> {
|
2020-12-31 23:25:30 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-08-03 12:45:27 -05:00
|
|
|
/// A mapping of IDs that contains all local items for this crate which gets output as a top
|
|
|
|
/// level field of the JSON blob.
|
|
|
|
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
|
|
|
|
/// The directory where the blob will be written to.
|
|
|
|
out_path: PathBuf,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Rc<Cache>,
|
2020-08-03 12:45:27 -05:00
|
|
|
}
|
|
|
|
|
2021-02-12 01:59:20 -05:00
|
|
|
impl JsonRenderer<'tcx> {
|
|
|
|
fn sess(&self) -> &'tcx Session {
|
2020-12-16 14:34:08 -05:00
|
|
|
self.tcx.sess
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
fn get_trait_implementors(&mut self, id: rustc_span::def_id::DefId) -> Vec<types::Id> {
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.implementors
|
|
|
|
.get(&id)
|
|
|
|
.map(|implementors| {
|
|
|
|
implementors
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
let item = &i.impl_item;
|
2021-01-12 23:36:04 +01:00
|
|
|
self.item(item.clone()).unwrap();
|
2021-01-22 16:09:24 -05:00
|
|
|
from_def_id(item.def_id)
|
2020-08-03 12:45:27 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
fn get_impls(&mut self, id: rustc_span::def_id::DefId) -> Vec<types::Id> {
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.impls
|
|
|
|
.get(&id)
|
|
|
|
.map(|impls| {
|
|
|
|
impls
|
|
|
|
.iter()
|
|
|
|
.filter_map(|i| {
|
|
|
|
let item = &i.impl_item;
|
|
|
|
if item.def_id.is_local() {
|
2021-01-12 23:36:04 +01:00
|
|
|
self.item(item.clone()).unwrap();
|
2021-01-22 16:09:24 -05:00
|
|
|
Some(from_def_id(item.def_id))
|
2020-08-03 12:45:27 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
|
2021-01-24 17:41:21 +01:00
|
|
|
Rc::clone(&self.cache)
|
2020-08-03 12:45:27 -05:00
|
|
|
.traits
|
|
|
|
.iter()
|
2021-02-12 14:33:32 +01:00
|
|
|
.filter_map(|(&id, trait_item)| {
|
2020-08-03 12:45:27 -05:00
|
|
|
// only need to synthesize items for external traits
|
|
|
|
if !id.is_local() {
|
2021-02-12 14:33:32 +01:00
|
|
|
let trait_item = &trait_item.trait_;
|
2021-01-12 23:36:04 +01:00
|
|
|
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
|
2020-08-03 12:45:27 -05:00
|
|
|
Some((
|
2021-01-22 16:09:24 -05:00
|
|
|
from_def_id(id),
|
2020-08-03 12:45:27 -05:00
|
|
|
types::Item {
|
2021-01-22 16:09:24 -05:00
|
|
|
id: from_def_id(id),
|
2020-08-03 12:45:27 -05:00
|
|
|
crate_id: id.krate.as_u32(),
|
2021-01-12 23:36:04 +01:00
|
|
|
name: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.paths
|
|
|
|
.get(&id)
|
|
|
|
.unwrap_or_else(|| {
|
2021-01-12 23:36:04 +01:00
|
|
|
self.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.external_paths
|
|
|
|
.get(&id)
|
|
|
|
.expect("Trait should either be in local or external paths")
|
|
|
|
})
|
|
|
|
.0
|
|
|
|
.last()
|
|
|
|
.map(Clone::clone),
|
|
|
|
visibility: types::Visibility::Public,
|
2021-02-27 19:44:17 -05:00
|
|
|
inner: types::ItemEnum::Trait(trait_item.clone().into()),
|
2020-08-03 12:45:27 -05:00
|
|
|
source: None,
|
|
|
|
docs: Default::default(),
|
|
|
|
links: Default::default(),
|
|
|
|
attrs: Default::default(),
|
|
|
|
deprecation: Default::default(),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 13:54:26 -05:00
|
|
|
|
2020-12-16 14:34:08 -05:00
|
|
|
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
2021-01-22 10:38:30 -05:00
|
|
|
fn descr() -> &'static str {
|
|
|
|
"json"
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:54:26 -05:00
|
|
|
fn init(
|
2020-08-03 12:45:27 -05:00
|
|
|
krate: clean::Crate,
|
|
|
|
options: RenderOptions,
|
2020-07-30 13:54:26 -05:00
|
|
|
_edition: Edition,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Cache,
|
2021-01-22 21:17:32 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-30 13:54:26 -05:00
|
|
|
) -> Result<(Self, clean::Crate), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
debug!("Initializing json renderer");
|
|
|
|
Ok((
|
|
|
|
JsonRenderer {
|
2020-12-16 14:34:08 -05:00
|
|
|
tcx,
|
2020-08-03 12:45:27 -05:00
|
|
|
index: Rc::new(RefCell::new(FxHashMap::default())),
|
|
|
|
out_path: options.output,
|
2021-01-12 23:36:04 +01:00
|
|
|
cache: Rc::new(cache),
|
2020-08-03 12:45:27 -05:00
|
|
|
},
|
|
|
|
krate,
|
|
|
|
))
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 19:12:03 -08:00
|
|
|
fn make_child_renderer(&self) -> Self {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:45:27 -05:00
|
|
|
/// Inserts an item into the index. This should be used rather than directly calling insert on
|
|
|
|
/// the hashmap because certain items (traits and types) need to have their mappings for trait
|
|
|
|
/// implementations filled out before they're inserted.
|
2021-01-12 23:36:04 +01:00
|
|
|
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
// Flatten items that recursively store other items
|
2021-01-12 23:36:04 +01:00
|
|
|
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
|
2020-08-03 12:45:27 -05:00
|
|
|
|
|
|
|
let id = item.def_id;
|
2020-12-11 23:57:18 -05:00
|
|
|
if let Some(mut new_item) = self.convert_item(item) {
|
2021-02-27 19:44:17 -05:00
|
|
|
if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
|
2021-01-12 23:36:04 +01:00
|
|
|
t.implementors = self.get_trait_implementors(id)
|
2021-02-27 19:44:17 -05:00
|
|
|
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
|
2021-01-12 23:36:04 +01:00
|
|
|
s.impls = self.get_impls(id)
|
2021-02-27 19:44:17 -05:00
|
|
|
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
|
2021-01-12 23:36:04 +01:00
|
|
|
e.impls = self.get_impls(id)
|
2020-11-30 20:24:48 +00:00
|
|
|
}
|
2021-01-22 16:09:24 -05:00
|
|
|
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
|
2020-12-05 22:23:17 +00:00
|
|
|
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
|
|
|
|
// to make sure the items are unique.
|
|
|
|
if let Some(old_item) = removed {
|
|
|
|
assert_eq!(old_item, new_item);
|
|
|
|
}
|
2020-08-03 12:45:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-01-12 23:36:04 +01:00
|
|
|
fn mod_item_in(&mut self, item: &clean::Item, _module_name: &str) -> Result<(), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
use clean::types::ItemKind::*;
|
2020-12-13 11:32:57 -05:00
|
|
|
if let ModuleItem(m) = &*item.kind {
|
2020-08-03 12:45:27 -05:00
|
|
|
for item in &m.items {
|
2020-12-13 11:32:57 -05:00
|
|
|
match &*item.kind {
|
2020-08-03 12:45:27 -05:00
|
|
|
// These don't have names so they don't get added to the output by default
|
2021-01-12 23:36:04 +01:00
|
|
|
ImportItem(_) => self.item(item.clone()).unwrap(),
|
2021-03-05 16:04:24 +01:00
|
|
|
ExternCrateItem { .. } => self.item(item.clone()).unwrap(),
|
2021-01-12 23:36:04 +01:00
|
|
|
ImplItem(i) => i.items.iter().for_each(|i| self.item(i.clone()).unwrap()),
|
2020-08-03 12:45:27 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 23:36:04 +01:00
|
|
|
self.item(item.clone()).unwrap();
|
2020-08-03 12:45:27 -05:00
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 01:59:45 -05:00
|
|
|
fn after_krate(
|
|
|
|
&mut self,
|
2021-02-25 00:16:47 -05:00
|
|
|
_krate: &clean::Crate,
|
2021-01-19 01:59:45 -05:00
|
|
|
_diag: &rustc_errors::Handler,
|
|
|
|
) -> Result<(), Error> {
|
2020-08-03 12:45:27 -05:00
|
|
|
debug!("Done with crate");
|
|
|
|
let mut index = (*self.index).clone().into_inner();
|
2021-01-12 23:36:04 +01:00
|
|
|
index.extend(self.get_trait_items());
|
2021-01-22 16:46:19 -05:00
|
|
|
// This needs to be the default HashMap for compatibility with the public interface for
|
|
|
|
// rustdoc-json
|
|
|
|
#[allow(rustc::default_hash_types)]
|
2020-08-03 12:45:27 -05:00
|
|
|
let output = types::Crate {
|
|
|
|
root: types::Id(String::from("0:0")),
|
2021-02-25 00:16:47 -05:00
|
|
|
crate_version: self.cache.crate_version.clone(),
|
2021-01-12 23:36:04 +01:00
|
|
|
includes_private: self.cache.document_private,
|
2021-01-22 18:30:01 -05:00
|
|
|
index: index.into_iter().collect(),
|
2021-01-12 23:36:04 +01:00
|
|
|
paths: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.paths
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2021-01-12 23:36:04 +01:00
|
|
|
.chain(self.cache.external_paths.clone().into_iter())
|
2020-08-03 12:45:27 -05:00
|
|
|
.map(|(k, (path, kind))| {
|
|
|
|
(
|
2021-01-22 16:09:24 -05:00
|
|
|
from_def_id(k),
|
2020-08-03 12:45:27 -05:00
|
|
|
types::ItemSummary { crate_id: k.krate.as_u32(), path, kind: kind.into() },
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-01-12 23:36:04 +01:00
|
|
|
external_crates: self
|
|
|
|
.cache
|
2020-08-03 12:45:27 -05:00
|
|
|
.extern_locations
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
(
|
|
|
|
k.as_u32(),
|
|
|
|
types::ExternalCrate {
|
2020-12-14 23:23:58 -05:00
|
|
|
name: v.0.to_string(),
|
2020-08-03 12:45:27 -05:00
|
|
|
html_root_url: match &v.2 {
|
|
|
|
ExternalLocation::Remote(s) => Some(s.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-03-06 15:37:07 -05:00
|
|
|
format_version: 4,
|
2020-08-03 12:45:27 -05:00
|
|
|
};
|
|
|
|
let mut p = self.out_path.clone();
|
|
|
|
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
|
|
|
|
p.set_extension("json");
|
|
|
|
let file = File::create(&p).map_err(|error| Error { error: error.to_string(), file: p })?;
|
|
|
|
serde_json::ser::to_writer(&file, &output).unwrap();
|
|
|
|
Ok(())
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|
2021-01-12 23:36:04 +01:00
|
|
|
|
|
|
|
fn cache(&self) -> &Cache {
|
|
|
|
&self.cache
|
|
|
|
}
|
2020-07-30 13:54:26 -05:00
|
|
|
}
|