Skip to content

Commit

Permalink
Auto merge of rust-lang#74955 - P1n3appl3:rustdoc-formats, r=Guillaum…
Browse files Browse the repository at this point in the history
…eGomez

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
  • Loading branch information
bors committed Jul 31, 2020
2 parents ffa80f0 + 48c6f05 commit 66b97dc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl Options {
let output_format = match matches.opt_str("output-format") {
Some(s) => match OutputFormat::try_from(s.as_str()) {
Ok(o) => {
if o.is_json() && !show_coverage {
if o.is_json() && !(show_coverage || nightly_options::is_nightly_build()) {
diag.struct_err("json output format isn't supported for doc generation")
.emit();
return Err(1);
Expand Down Expand Up @@ -626,7 +626,9 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han

for flag in deprecated_flags.iter() {
if matches.opt_present(flag) {
if *flag == "output-format" && matches.opt_present("show-coverage") {
if *flag == "output-format"
&& (matches.opt_present("show-coverage") || nightly_options::is_nightly_build())
{
continue;
}
let mut err =
Expand Down
47 changes: 47 additions & 0 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::error::Error;
use crate::formats::cache::Cache;
use crate::formats::FormatRenderer;

use rustc_span::edition::Edition;

#[derive(Clone)]
pub struct JsonRenderer {}

impl FormatRenderer for JsonRenderer {
fn init(
_krate: clean::Crate,
_options: RenderOptions,
_render_info: RenderInfo,
_edition: Edition,
_cache: &mut Cache,
) -> Result<(Self, clean::Crate), Error> {
unimplemented!()
}

fn item(&mut self, _item: clean::Item, _cache: &Cache) -> Result<(), Error> {
unimplemented!()
}

fn mod_item_in(
&mut self,
_item: &clean::Item,
_item_name: &str,
_cache: &Cache,
) -> Result<(), Error> {
unimplemented!()
}

fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
unimplemented!()
}

fn after_krate(&mut self, _krate: &clean::Crate, _cache: &Cache) -> Result<(), Error> {
unimplemented!()
}

fn after_run(&mut self, _diag: &rustc_errors::Handler) -> Result<(), Error> {
unimplemented!()
}
}
44 changes: 30 additions & 14 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod error;
mod fold;
crate mod formats;
pub mod html;
mod json;
mod markdown;
mod passes;
mod test;
Expand Down Expand Up @@ -450,6 +451,28 @@ fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
}
}

fn run_renderer<T: formats::FormatRenderer>(
krate: clean::Crate,
renderopts: config::RenderOptions,
render_info: config::RenderInfo,
diag: &rustc_errors::Handler,
edition: rustc_span::edition::Edition,
) -> i32 {
match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
Ok(_) => rustc_driver::EXIT_SUCCESS,
Err(e) => {
let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
let file = e.file.display().to_string();
if file.is_empty() {
msg.emit()
} else {
msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
}
rustc_driver::EXIT_FAILURE
}
}
}

fn main_options(options: config::Options) -> i32 {
let diag = core::new_handler(options.error_format, None, &options.debugging_options);

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

info!("finished with rustc");
Expand All @@ -502,20 +526,12 @@ fn main_options(options: config::Options) -> i32 {
info!("going to format");
let (error_format, edition, debugging_options) = diag_opts;
let diag = core::new_handler(error_format, None, &debugging_options);
match formats::run_format::<html::render::Context>(
krate, renderopts, renderinfo, &diag, edition,
) {
Ok(_) => rustc_driver::EXIT_SUCCESS,
Err(e) => {
let mut msg =
diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
let file = e.file.display().to_string();
if file.is_empty() {
msg.emit()
} else {
msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
}
rustc_driver::EXIT_FAILURE
match output_format {
None | Some(config::OutputFormat::Html) => {
run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
}
Some(config::OutputFormat::Json) => {
run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)
}
}
});
Expand Down

0 comments on commit 66b97dc

Please sign in to comment.