Skip to content

Commit 668e323

Browse files
Add --output-to-stdout option to rustdoc
1 parent c9bd03c commit 668e323

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/librustdoc/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ pub(crate) struct RenderOptions {
286286
pub(crate) no_emit_shared: bool,
287287
/// If `true`, HTML source code pages won't be generated.
288288
pub(crate) html_no_source: bool,
289+
/// This option only works for the JSON output. If it's set to true, no file will be created
290+
/// and content will be displayed in stdout directly.
291+
pub(crate) output_to_stdout: bool,
289292
}
290293

291294
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -695,6 +698,12 @@ impl Options {
695698
},
696699
None => OutputFormat::default(),
697700
};
701+
let output_to_stdout = matches.opt_present("output-to-stdout");
702+
if output_to_stdout {
703+
if !output_format.is_json() {
704+
dcx.fatal("`--output-to-stdout` is only support for JSON output format");
705+
}
706+
}
698707
let crate_name = matches.opt_str("crate-name");
699708
let bin_crate = crate_types.contains(&CrateType::Executable);
700709
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
@@ -816,6 +825,7 @@ impl Options {
816825
call_locations,
817826
no_emit_shared: false,
818827
html_no_source,
828+
output_to_stdout,
819829
};
820830
Some((options, render_options))
821831
}

src/librustdoc/json/mod.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod import_finder;
99

1010
use std::cell::RefCell;
1111
use std::fs::{create_dir_all, File};
12-
use std::io::{BufWriter, Write};
12+
use std::io::{stdout, BufWriter, Write};
1313
use std::path::PathBuf;
1414
use std::rc::Rc;
1515

@@ -37,7 +37,7 @@ pub(crate) struct JsonRenderer<'tcx> {
3737
/// level field of the JSON blob.
3838
index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>,
3939
/// The directory where the blob will be written to.
40-
out_path: PathBuf,
40+
out_path: Option<PathBuf>,
4141
cache: Rc<Cache>,
4242
imported_items: DefIdSet,
4343
}
@@ -97,6 +97,20 @@ impl<'tcx> JsonRenderer<'tcx> {
9797
})
9898
.unwrap_or_default()
9999
}
100+
101+
fn write<T: Write>(
102+
&self,
103+
output: types::Crate,
104+
mut writer: BufWriter<T>,
105+
path: &str,
106+
) -> Result<(), Error> {
107+
self.tcx
108+
.sess
109+
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut writer, &output))
110+
.unwrap();
111+
try_err!(writer.flush(), path);
112+
Ok(())
113+
}
100114
}
101115

102116
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
@@ -120,7 +134,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
120134
JsonRenderer {
121135
tcx,
122136
index: Rc::new(RefCell::new(FxHashMap::default())),
123-
out_path: options.output,
137+
out_path: if options.output_to_stdout { None } else { Some(options.output) },
124138
cache: Rc::new(cache),
125139
imported_items,
126140
},
@@ -264,20 +278,21 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
264278
.collect(),
265279
format_version: types::FORMAT_VERSION,
266280
};
267-
let out_dir = self.out_path.clone();
268-
try_err!(create_dir_all(&out_dir), out_dir);
281+
if let Some(ref out_path) = self.out_path {
282+
let out_dir = out_path.clone();
283+
try_err!(create_dir_all(&out_dir), out_dir);
269284

270-
let mut p = out_dir;
271-
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
272-
p.set_extension("json");
273-
let mut file = BufWriter::new(try_err!(File::create(&p), p));
274-
self.tcx
275-
.sess
276-
.time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut file, &output))
277-
.unwrap();
278-
try_err!(file.flush(), p);
279-
280-
Ok(())
285+
let mut p = out_dir;
286+
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
287+
p.set_extension("json");
288+
self.write(
289+
output,
290+
BufWriter::new(try_err!(File::create(&p), p)),
291+
&p.display().to_string(),
292+
)
293+
} else {
294+
self.write(output, BufWriter::new(stdout()), "<stdout>")
295+
}
281296
}
282297

283298
fn cache(&self) -> &Cache {

src/librustdoc/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ fn opts() -> Vec<RustcOptGroup> {
653653
unstable("html-no-source", |o| {
654654
o.optflag("", "html-no-source", "Disable HTML source code pages generation")
655655
}),
656+
unstable("output-to-stdout", |o| {
657+
o.optflag(
658+
"",
659+
"output-to-stdout",
660+
"Make output generated into stdout. Only works with JSON output format",
661+
)
662+
}),
656663
]
657664
}
658665

0 commit comments

Comments
 (0)