mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
rustdoc-json: Clean up serialization and printing.
This commit is contained in:
parent
569d7e3d15
commit
321d40f060
@ -39,8 +39,10 @@ pub(crate) struct JsonRenderer<'tcx> {
|
|||||||
/// A mapping of IDs that contains all local items for this crate which gets output as a top
|
/// A mapping of IDs that contains all local items for this crate which gets output as a top
|
||||||
/// level field of the JSON blob.
|
/// level field of the JSON blob.
|
||||||
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
|
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
|
||||||
/// The directory where the blob will be written to.
|
/// The directory where the JSON blob should be written to.
|
||||||
out_path: Option<PathBuf>,
|
///
|
||||||
|
/// If this is `None`, the blob will be printed to `stdout` instead.
|
||||||
|
out_dir: Option<PathBuf>,
|
||||||
cache: Rc<Cache>,
|
cache: Rc<Cache>,
|
||||||
imported_items: DefIdSet,
|
imported_items: DefIdSet,
|
||||||
}
|
}
|
||||||
@ -101,18 +103,20 @@ impl<'tcx> JsonRenderer<'tcx> {
|
|||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write<T: Write>(
|
fn serialize_and_write<T: Write>(
|
||||||
&self,
|
&self,
|
||||||
output: types::Crate,
|
output_crate: types::Crate,
|
||||||
mut writer: BufWriter<T>,
|
mut writer: BufWriter<T>,
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
self.tcx
|
self.sess().time("rustdoc_json_serialize_and_write", || {
|
||||||
.sess
|
try_err!(
|
||||||
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut writer, &output))
|
serde_json::ser::to_writer(&mut writer, &output_crate).map_err(|e| e.to_string()),
|
||||||
.unwrap();
|
path
|
||||||
try_err!(writer.flush(), path);
|
);
|
||||||
Ok(())
|
try_err!(writer.flush(), path);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +141,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
|||||||
JsonRenderer {
|
JsonRenderer {
|
||||||
tcx,
|
tcx,
|
||||||
index: Rc::new(RefCell::new(FxHashMap::default())),
|
index: Rc::new(RefCell::new(FxHashMap::default())),
|
||||||
out_path: if options.output_to_stdout { None } else { Some(options.output) },
|
out_dir: if options.output_to_stdout { None } else { Some(options.output) },
|
||||||
cache: Rc::new(cache),
|
cache: Rc::new(cache),
|
||||||
imported_items,
|
imported_items,
|
||||||
},
|
},
|
||||||
@ -237,7 +241,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
|||||||
let index = (*self.index).clone().into_inner();
|
let index = (*self.index).clone().into_inner();
|
||||||
|
|
||||||
debug!("Constructing Output");
|
debug!("Constructing Output");
|
||||||
let output = types::Crate {
|
let output_crate = types::Crate {
|
||||||
root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())),
|
root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())),
|
||||||
crate_version: self.cache.crate_version.clone(),
|
crate_version: self.cache.crate_version.clone(),
|
||||||
includes_private: self.cache.document_private,
|
includes_private: self.cache.document_private,
|
||||||
@ -278,20 +282,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
|||||||
.collect(),
|
.collect(),
|
||||||
format_version: types::FORMAT_VERSION,
|
format_version: types::FORMAT_VERSION,
|
||||||
};
|
};
|
||||||
if let Some(ref out_path) = self.out_path {
|
if let Some(ref out_dir) = self.out_dir {
|
||||||
let out_dir = out_path.clone();
|
|
||||||
try_err!(create_dir_all(&out_dir), out_dir);
|
try_err!(create_dir_all(&out_dir), out_dir);
|
||||||
|
|
||||||
let mut p = out_dir;
|
let mut p = out_dir.clone();
|
||||||
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
|
p.push(output_crate.index.get(&output_crate.root).unwrap().name.clone().unwrap());
|
||||||
p.set_extension("json");
|
p.set_extension("json");
|
||||||
self.write(
|
|
||||||
output,
|
self.serialize_and_write(
|
||||||
|
output_crate,
|
||||||
BufWriter::new(try_err!(File::create(&p), p)),
|
BufWriter::new(try_err!(File::create(&p), p)),
|
||||||
&p.display().to_string(),
|
&p.display().to_string(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
self.write(output, BufWriter::new(stdout()), "<stdout>")
|
self.serialize_and_write(output_crate, BufWriter::new(stdout().lock()), "<stdout>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user