Skip to content

Commit 7454c94

Browse files
committed
rustdoc-json: Clean up serialization and printing.
1 parent 569d7e3 commit 7454c94

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/librustdoc/json/mod.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ pub(crate) struct JsonRenderer<'tcx> {
3939
/// A mapping of IDs that contains all local items for this crate which gets output as a top
4040
/// level field of the JSON blob.
4141
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
42-
/// The directory where the blob will be written to.
43-
out_path: Option<PathBuf>,
42+
/// The directory where the JSON blob should be written to.
43+
///
44+
/// If this is `None`, the blob will be printed to `stdout` instead.
45+
out_dir: Option<PathBuf>,
4446
cache: Rc<Cache>,
4547
imported_items: DefIdSet,
4648
}
@@ -101,18 +103,21 @@ impl<'tcx> JsonRenderer<'tcx> {
101103
.unwrap_or_default()
102104
}
103105

104-
fn write<T: Write>(
106+
fn serialize_and_write<T: Write>(
105107
&self,
106-
output: types::Crate,
108+
output_document: types::Crate,
107109
mut writer: BufWriter<T>,
108110
path: &str,
109111
) -> Result<(), Error> {
110-
self.tcx
111-
.sess
112-
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut writer, &output))
113-
.unwrap();
114-
try_err!(writer.flush(), path);
115-
Ok(())
112+
self.sess().time("rustdoc_json_serialize_and_write", || {
113+
try_err!(
114+
serde_json::ser::to_writer(&mut writer, &output_document)
115+
.map_err(|e| e.to_string()),
116+
path
117+
);
118+
try_err!(writer.flush(), path);
119+
Ok(())
120+
})
116121
}
117122
}
118123

@@ -137,7 +142,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
137142
JsonRenderer {
138143
tcx,
139144
index: Rc::new(RefCell::new(FxHashMap::default())),
140-
out_path: if options.output_to_stdout { None } else { Some(options.output) },
145+
out_dir: if options.output_to_stdout { None } else { Some(options.output) },
141146
cache: Rc::new(cache),
142147
imported_items,
143148
},
@@ -237,7 +242,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
237242
let index = (*self.index).clone().into_inner();
238243

239244
debug!("Constructing Output");
240-
let output = types::Crate {
245+
let output_crate = types::Crate {
241246
root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())),
242247
crate_version: self.cache.crate_version.clone(),
243248
includes_private: self.cache.document_private,
@@ -278,20 +283,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
278283
.collect(),
279284
format_version: types::FORMAT_VERSION,
280285
};
281-
if let Some(ref out_path) = self.out_path {
282-
let out_dir = out_path.clone();
286+
if let Some(ref out_dir) = self.out_dir {
283287
try_err!(create_dir_all(&out_dir), out_dir);
284288

285-
let mut p = out_dir;
286-
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
289+
let mut p = out_dir.clone();
290+
p.push(output_crate.index.get(&output_crate.root).unwrap().name.clone().unwrap());
287291
p.set_extension("json");
288-
self.write(
289-
output,
292+
293+
self.serialize_and_write(
294+
output_crate,
290295
BufWriter::new(try_err!(File::create(&p), p)),
291296
&p.display().to_string(),
292297
)
293298
} else {
294-
self.write(output, BufWriter::new(stdout()), "<stdout>")
299+
self.serialize_and_write(output_crate, BufWriter::new(stdout().lock()), "<stdout>")
295300
}
296301
}
297302

0 commit comments

Comments
 (0)