1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
mod conversions;
use std::cell::RefCell;
use std::fs::File;
use std::path::PathBuf;
use std::rc::Rc;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustdoc_json_types as types;
use crate::clean;
use crate::clean::ExternalCrate;
use crate::config::RenderOptions;
use crate::error::Error;
use crate::formats::cache::Cache;
use crate::formats::FormatRenderer;
use crate::html::render::cache::ExternalLocation;
use crate::json::conversions::{from_item_id, IntoWithTcx};
#[derive(Clone)]
crate struct JsonRenderer<'tcx> {
tcx: TyCtxt<'tcx>,
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
out_path: PathBuf,
cache: Rc<Cache>,
}
impl JsonRenderer<'tcx> {
fn sess(&self) -> &'tcx Session {
self.tcx.sess
}
fn get_trait_implementors(&mut self, id: DefId) -> Vec<types::Id> {
Rc::clone(&self.cache)
.implementors
.get(&id)
.map(|implementors| {
implementors
.iter()
.map(|i| {
let item = &i.impl_item;
self.item(item.clone()).unwrap();
from_item_id(item.def_id)
})
.collect()
})
.unwrap_or_default()
}
fn get_impls(&mut self, id: DefId) -> Vec<types::Id> {
Rc::clone(&self.cache)
.impls
.get(&id)
.map(|impls| {
impls
.iter()
.filter_map(|i| {
let item = &i.impl_item;
let mut is_primitive_impl = false;
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
if impl_.trait_.is_none() {
if let clean::types::Type::Primitive(_) = impl_.for_ {
is_primitive_impl = true;
}
}
}
if item.def_id.is_local() || is_primitive_impl {
self.item(item.clone()).unwrap();
Some(from_item_id(item.def_id))
} else {
None
}
})
.collect()
})
.unwrap_or_default()
}
fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
Rc::clone(&self.cache)
.traits
.iter()
.filter_map(|(&id, trait_item)| {
if !id.is_local() {
let trait_item = &trait_item.trait_;
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
Some((
from_item_id(id.into()),
types::Item {
id: from_item_id(id.into()),
crate_id: id.krate.as_u32(),
name: self
.cache
.paths
.get(&id)
.unwrap_or_else(|| {
self.cache
.external_paths
.get(&id)
.expect("Trait should either be in local or external paths")
})
.0
.last()
.map(Clone::clone),
visibility: types::Visibility::Public,
inner: types::ItemEnum::Trait(trait_item.clone().into_tcx(self.tcx)),
span: None,
docs: Default::default(),
links: Default::default(),
attrs: Default::default(),
deprecation: Default::default(),
},
))
} else {
None
}
})
.collect()
}
}
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
fn descr() -> &'static str {
"json"
}
const RUN_ON_MODULE: bool = false;
fn init(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error> {
debug!("Initializing json renderer");
Ok((
JsonRenderer {
tcx,
index: Rc::new(RefCell::new(FxHashMap::default())),
out_path: options.output,
cache: Rc::new(cache),
},
krate,
))
}
fn make_child_renderer(&self) -> Self {
self.clone()
}
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
let id = item.def_id;
if let Some(mut new_item) = self.convert_item(item) {
if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
t.implementors = self.get_trait_implementors(id.expect_def_id())
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
s.impls = self.get_impls(id.expect_def_id())
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
e.impls = self.get_impls(id.expect_def_id())
} else if let types::ItemEnum::Union(ref mut u) = new_item.inner {
u.impls = self.get_impls(id.expect_def_id())
}
let removed = self.index.borrow_mut().insert(from_item_id(id), new_item.clone());
if let Some(old_item) = removed {
assert_eq!(old_item, new_item);
}
}
Ok(())
}
fn mod_item_in(&mut self, _item: &clean::Item) -> Result<(), Error> {
unreachable!("RUN_ON_MODULE = false should never call mod_item_in")
}
fn after_krate(&mut self) -> Result<(), Error> {
debug!("Done with crate");
for primitive in Rc::clone(&self.cache).primitive_locations.values() {
self.get_impls(*primitive);
}
let mut index = (*self.index).clone().into_inner();
index.extend(self.get_trait_items());
#[allow(rustc::default_hash_types)]
let output = types::Crate {
root: types::Id(String::from("0:0")),
crate_version: self.cache.crate_version.clone(),
includes_private: self.cache.document_private,
index: index.into_iter().collect(),
paths: self
.cache
.paths
.clone()
.into_iter()
.chain(self.cache.external_paths.clone().into_iter())
.map(|(k, (path, kind))| {
(
from_item_id(k.into()),
types::ItemSummary {
crate_id: k.krate.as_u32(),
path,
kind: kind.into_tcx(self.tcx),
},
)
})
.collect(),
external_crates: self
.cache
.extern_locations
.iter()
.map(|(crate_num, external_location)| {
let e = ExternalCrate { crate_num: *crate_num };
(
crate_num.as_u32(),
types::ExternalCrate {
name: e.name(self.tcx).to_string(),
html_root_url: match external_location {
ExternalLocation::Remote(s) => Some(s.clone()),
_ => None,
},
},
)
})
.collect(),
format_version: types::FORMAT_VERSION,
};
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(())
}
fn cache(&self) -> &Cache {
&self.cache
}
}