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.
10 use std
::cell
::RefCell
;
12 use std
::path
::PathBuf
;
15 use rustc_data_structures
::fx
::FxHashMap
;
17 use rustc_session
::Session
;
18 use rustc_span
::edition
::Edition
;
21 use crate::config
::{RenderInfo, RenderOptions}
;
22 use crate::error
::Error
;
23 use crate::formats
::cache
::Cache
;
24 use crate::formats
::FormatRenderer
;
25 use crate::html
::render
::cache
::ExternalLocation
;
28 crate struct JsonRenderer
<'tcx
> {
29 tcx
: ty
::TyCtxt
<'tcx
>,
30 /// A mapping of IDs that contains all local items for this crate which gets output as a top
31 /// level field of the JSON blob.
32 index
: Rc
<RefCell
<FxHashMap
<types
::Id
, types
::Item
>>>,
33 /// The directory where the blob will be written to.
37 impl JsonRenderer
<'_
> {
38 fn sess(&self) -> &Session
{
42 fn get_trait_implementors(
44 id
: rustc_span
::def_id
::DefId
,
54 let item
= &i
.impl_item
;
55 self.item(item
.clone(), cache
).unwrap();
63 fn get_impls(&mut self, id
: rustc_span
::def_id
::DefId
, cache
: &Cache
) -> Vec
<types
::Id
> {
71 let item
= &i
.impl_item
;
72 if item
.def_id
.is_local() {
73 self.item(item
.clone(), cache
).unwrap();
74 Some(item
.def_id
.into())
84 fn get_trait_items(&mut self, cache
: &Cache
) -> Vec
<(types
::Id
, types
::Item
)> {
88 .filter_map(|(&id
, trait_item
)| {
89 // only need to synthesize items for external traits
91 trait_item
.items
.clone().into_iter().for_each(|i
| self.item(i
, cache
).unwrap());
96 crate_id
: id
.krate
.as_u32(),
104 .expect("Trait should either be in local or external paths")
109 visibility
: types
::Visibility
::Public
,
110 kind
: types
::ItemKind
::Trait
,
111 inner
: types
::ItemEnum
::TraitItem(trait_item
.clone().into()),
113 docs
: Default
::default(),
114 links
: Default
::default(),
115 attrs
: Default
::default(),
116 deprecation
: Default
::default(),
127 impl<'tcx
> FormatRenderer
<'tcx
> for JsonRenderer
<'tcx
> {
130 options
: RenderOptions
,
131 _render_info
: RenderInfo
,
134 tcx
: ty
::TyCtxt
<'tcx
>,
135 ) -> Result
<(Self, clean
::Crate
), Error
> {
136 debug
!("Initializing json renderer");
140 index
: Rc
::new(RefCell
::new(FxHashMap
::default())),
141 out_path
: options
.output
,
147 /// Inserts an item into the index. This should be used rather than directly calling insert on
148 /// the hashmap because certain items (traits and types) need to have their mappings for trait
149 /// implementations filled out before they're inserted.
150 fn item(&mut self, item
: clean
::Item
, cache
: &Cache
) -> Result
<(), Error
> {
151 // Flatten items that recursively store other items
152 item
.kind
.inner_items().for_each(|i
| self.item(i
.clone(), cache
).unwrap());
154 let id
= item
.def_id
;
155 if let Some(mut new_item
) = self.convert_item(item
) {
156 if let types
::ItemEnum
::TraitItem(ref mut t
) = new_item
.inner
{
157 t
.implementors
= self.get_trait_implementors(id
, cache
)
158 } else if let types
::ItemEnum
::StructItem(ref mut s
) = new_item
.inner
{
159 s
.impls
= self.get_impls(id
, cache
)
160 } else if let types
::ItemEnum
::EnumItem(ref mut e
) = new_item
.inner
{
161 e
.impls
= self.get_impls(id
, cache
)
163 let removed
= self.index
.borrow_mut().insert(id
.into(), new_item
.clone());
164 // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
165 // to make sure the items are unique.
166 if let Some(old_item
) = removed
{
167 assert_eq
!(old_item
, new_item
);
179 ) -> Result
<(), Error
> {
180 use clean
::types
::ItemKind
::*;
181 if let ModuleItem(m
) = &item
.kind
{
182 for item
in &m
.items
{
184 // These don't have names so they don't get added to the output by default
185 ImportItem(_
) => self.item(item
.clone(), cache
).unwrap(),
186 ExternCrateItem(_
, _
) => self.item(item
.clone(), cache
).unwrap(),
188 i
.items
.iter().for_each(|i
| self.item(i
.clone(), cache
).unwrap())
194 self.item(item
.clone(), cache
).unwrap();
198 fn mod_item_out(&mut self, _item_name
: &str) -> Result
<(), Error
> {
202 fn after_krate(&mut self, krate
: &clean
::Crate
, cache
: &Cache
) -> Result
<(), Error
> {
203 debug
!("Done with crate");
204 let mut index
= (*self.index
).clone().into_inner();
205 index
.extend(self.get_trait_items(cache
));
206 let output
= types
::Crate
{
207 root
: types
::Id(String
::from("0:0")),
208 crate_version
: krate
.version
.clone(),
209 includes_private
: cache
.document_private
,
215 .chain(cache
.external_paths
.clone().into_iter())
216 .map(|(k
, (path
, kind
))| {
219 types
::ItemSummary { crate_id: k.krate.as_u32(), path, kind: kind.into() }
,
223 external_crates
: cache
229 types
::ExternalCrate
{
230 name
: v
.0.to_string(),
231 html_root_url
: match &v
.2 {
232 ExternalLocation
::Remote(s
) => Some(s
.clone()),
241 let mut p
= self.out_path
.clone();
242 p
.push(output
.index
.get(&output
.root
).unwrap().name
.clone().unwrap());
243 p
.set_extension("json");
244 let file
= File
::create(&p
).map_err(|error
| Error { error: error.to_string(), file: p }
)?
;
245 serde_json
::ser
::to_writer(&file
, &output
).unwrap();
249 fn after_run(&mut self, _diag
: &rustc_errors
::Handler
) -> Result
<(), Error
> {