Skip to content

Commit 66b97dc

Browse files
committed
Auto merge of rust-lang#74955 - P1n3appl3:rustdoc-formats, r=GuillaumeGomez
Add `--output-format json` for Rustdoc on nightly This enables the previously deprecated `--output-format` flag so it can be used on nightly to host the experimental implementation of [rfc/2963](rust-lang/rfcs#2963). The actual implementation will come in later PRs so for now there's just a stub that gives you an ICE. I'm _pretty_ sure that the logic I added makes it inaccessible from stable, but someone should double check that. @tmandry @jyn514
2 parents ffa80f0 + 48c6f05 commit 66b97dc

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

src/librustdoc/config.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl Options {
508508
let output_format = match matches.opt_str("output-format") {
509509
Some(s) => match OutputFormat::try_from(s.as_str()) {
510510
Ok(o) => {
511-
if o.is_json() && !show_coverage {
511+
if o.is_json() && !(show_coverage || nightly_options::is_nightly_build()) {
512512
diag.struct_err("json output format isn't supported for doc generation")
513513
.emit();
514514
return Err(1);
@@ -626,7 +626,9 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han
626626

627627
for flag in deprecated_flags.iter() {
628628
if matches.opt_present(flag) {
629-
if *flag == "output-format" && matches.opt_present("show-coverage") {
629+
if *flag == "output-format"
630+
&& (matches.opt_present("show-coverage") || nightly_options::is_nightly_build())
631+
{
630632
continue;
631633
}
632634
let mut err =

src/librustdoc/json/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::clean;
2+
use crate::config::{RenderInfo, RenderOptions};
3+
use crate::error::Error;
4+
use crate::formats::cache::Cache;
5+
use crate::formats::FormatRenderer;
6+
7+
use rustc_span::edition::Edition;
8+
9+
#[derive(Clone)]
10+
pub struct JsonRenderer {}
11+
12+
impl FormatRenderer for JsonRenderer {
13+
fn init(
14+
_krate: clean::Crate,
15+
_options: RenderOptions,
16+
_render_info: RenderInfo,
17+
_edition: Edition,
18+
_cache: &mut Cache,
19+
) -> Result<(Self, clean::Crate), Error> {
20+
unimplemented!()
21+
}
22+
23+
fn item(&mut self, _item: clean::Item, _cache: &Cache) -> Result<(), Error> {
24+
unimplemented!()
25+
}
26+
27+
fn mod_item_in(
28+
&mut self,
29+
_item: &clean::Item,
30+
_item_name: &str,
31+
_cache: &Cache,
32+
) -> Result<(), Error> {
33+
unimplemented!()
34+
}
35+
36+
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
37+
unimplemented!()
38+
}
39+
40+
fn after_krate(&mut self, _krate: &clean::Crate, _cache: &Cache) -> Result<(), Error> {
41+
unimplemented!()
42+
}
43+
44+
fn after_run(&mut self, _diag: &rustc_errors::Handler) -> Result<(), Error> {
45+
unimplemented!()
46+
}
47+
}

src/librustdoc/lib.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ mod error;
6868
mod fold;
6969
crate mod formats;
7070
pub mod html;
71+
mod json;
7172
mod markdown;
7273
mod passes;
7374
mod test;
@@ -450,6 +451,28 @@ fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
450451
}
451452
}
452453

454+
fn run_renderer<T: formats::FormatRenderer>(
455+
krate: clean::Crate,
456+
renderopts: config::RenderOptions,
457+
render_info: config::RenderInfo,
458+
diag: &rustc_errors::Handler,
459+
edition: rustc_span::edition::Edition,
460+
) -> i32 {
461+
match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
462+
Ok(_) => rustc_driver::EXIT_SUCCESS,
463+
Err(e) => {
464+
let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
465+
let file = e.file.display().to_string();
466+
if file.is_empty() {
467+
msg.emit()
468+
} else {
469+
msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
470+
}
471+
rustc_driver::EXIT_FAILURE
472+
}
473+
}
474+
}
475+
453476
fn main_options(options: config::Options) -> i32 {
454477
let diag = core::new_handler(options.error_format, None, &options.debugging_options);
455478

@@ -480,6 +503,7 @@ fn main_options(options: config::Options) -> i32 {
480503
let result = rustc_driver::catch_fatal_errors(move || {
481504
let crate_name = options.crate_name.clone();
482505
let crate_version = options.crate_version.clone();
506+
let output_format = options.output_format;
483507
let (mut krate, renderinfo, renderopts) = core::run_core(options);
484508

485509
info!("finished with rustc");
@@ -502,20 +526,12 @@ fn main_options(options: config::Options) -> i32 {
502526
info!("going to format");
503527
let (error_format, edition, debugging_options) = diag_opts;
504528
let diag = core::new_handler(error_format, None, &debugging_options);
505-
match formats::run_format::<html::render::Context>(
506-
krate, renderopts, renderinfo, &diag, edition,
507-
) {
508-
Ok(_) => rustc_driver::EXIT_SUCCESS,
509-
Err(e) => {
510-
let mut msg =
511-
diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
512-
let file = e.file.display().to_string();
513-
if file.is_empty() {
514-
msg.emit()
515-
} else {
516-
msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
517-
}
518-
rustc_driver::EXIT_FAILURE
529+
match output_format {
530+
None | Some(config::OutputFormat::Html) => {
531+
run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
532+
}
533+
Some(config::OutputFormat::Json) => {
534+
run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)
519535
}
520536
}
521537
});

0 commit comments

Comments
 (0)