Skip to content

Commit f3eb1e6

Browse files
committed
Add unstable "--output-format" option to "doc" & "rustdoc"
This commit enables the mimicking of `--output-format` option of "rustdoc". We achieved this by: * Handle `--output-format` arguments, accepts `html` or `json`. * If `--ouput-format=json` we append the following to `compile_opts.target_rustc_args`: 1. `-Zunstable-commands` 2. `--output-format`
1 parent 928b956 commit f3eb1e6

File tree

16 files changed

+231
-6
lines changed

16 files changed

+231
-6
lines changed

src/bin/cargo/commands/doc.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::command_prelude::*;
22

3-
use cargo::ops::{self, DocOptions};
3+
use cargo::ops::{self, DocOptions, OutputFormat};
44

55
pub fn cli() -> Command {
66
subcommand("doc")
@@ -35,6 +35,7 @@ pub fn cli() -> Command {
3535
.arg_features()
3636
.arg_target_triple("Build for the target triple")
3737
.arg_target_dir()
38+
.arg(opt("output-format", "the output type to write (unstable)").value_name("PATH"))
3839
.arg_manifest_path()
3940
.arg_message_format()
4041
.arg_ignore_rust_version()
@@ -48,12 +49,28 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
4849
let mode = CompileMode::Doc {
4950
deps: !args.flag("no-deps"),
5051
};
52+
5153
let mut compile_opts =
5254
args.compile_options(config, mode, Some(&ws), ProfileChecking::Custom)?;
5355
compile_opts.rustdoc_document_private_items = args.flag("document-private-items");
56+
let mut target_args = values(args, "args");
57+
let output_format = args.output_format()?;
58+
if output_format.eq(&OutputFormat::Json) {
59+
if config.cli_unstable().unstable_options {
60+
target_args.push("-Zunstable-options".to_string());
61+
}
62+
target_args.push("--output-format=json".to_string());
63+
}
64+
65+
compile_opts.target_rustdoc_args = if target_args.is_empty() {
66+
None
67+
} else {
68+
Some(target_args)
69+
};
5470

5571
let doc_opts = DocOptions {
5672
open_result: args.flag("open"),
73+
output_format,
5774
compile_opts,
5875
};
5976
ops::doc(&ws, &doc_opts)?;

src/bin/cargo/commands/rustdoc.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo::ops::{self, DocOptions};
1+
use cargo::ops::{self, DocOptions, OutputFormat};
22

33
use crate::command_prelude::*;
44

@@ -35,6 +35,7 @@ pub fn cli() -> Command {
3535
.arg_features()
3636
.arg_target_triple("Build for the target triple")
3737
.arg_target_dir()
38+
.arg(opt("output-format", "the output type to write (unstable)").value_name("PATH"))
3839
.arg_manifest_path()
3940
.arg_message_format()
4041
.arg_unit_graph()
@@ -51,14 +52,26 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5152
Some(&ws),
5253
ProfileChecking::Custom,
5354
)?;
54-
let target_args = values(args, "args");
55+
let mut target_args = values(args, "args");
56+
57+
let output_format = args.output_format()?;
58+
59+
if output_format.eq(&OutputFormat::Json) {
60+
if config.cli_unstable().unstable_options {
61+
target_args.push("-Zunstable-options".to_string());
62+
}
63+
target_args.push("--output-format=json".to_string());
64+
}
65+
5566
compile_opts.target_rustdoc_args = if target_args.is_empty() {
5667
None
5768
} else {
5869
Some(target_args)
5970
};
71+
6072
let doc_opts = DocOptions {
6173
open_result: args.flag("open"),
74+
output_format,
6275
compile_opts,
6376
};
6477
ops::doc(&ws, &doc_opts)?;

src/cargo/ops/cargo_doc.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,33 @@ use std::path::Path;
66
use std::path::PathBuf;
77
use std::process::Command;
88

9+
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
10+
pub enum OutputFormat {
11+
#[default]
12+
Html,
13+
Json,
14+
}
15+
916
/// Strongly typed options for the `cargo doc` command.
1017
#[derive(Debug)]
1118
pub struct DocOptions {
1219
/// Whether to attempt to open the browser after compiling the docs
1320
pub open_result: bool,
21+
/// Same as `rustdoc --output-format`
22+
pub output_format: OutputFormat,
1423
/// Options to pass through to the compiler
1524
pub compile_opts: ops::CompileOptions,
1625
}
1726

1827
/// Main method for `cargo doc`.
1928
pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
29+
if options.open_result && options.output_format.eq(&OutputFormat::Json) {
30+
anyhow::bail!("\"--open\" is not allowed with \"json\" output format.");
31+
}
32+
2033
let compilation = ops::compile(ws, &options.compile_opts)?;
2134

22-
if options.open_result {
35+
if options.open_result && options.output_format.eq(&OutputFormat::Html) {
2336
let name = &compilation
2437
.root_crate_names
2538
.get(0)

src/cargo/ops/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use self::cargo_compile::{
55
compile, compile_with_exec, compile_ws, create_bcx, print, resolve_all_features, CompileOptions,
66
};
77
pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages};
8-
pub use self::cargo_doc::{doc, DocOptions};
8+
pub use self::cargo_doc::{doc, DocOptions, OutputFormat};
99
pub use self::cargo_fetch::{fetch, FetchOptions};
1010
pub use self::cargo_generate_lockfile::generate_lockfile;
1111
pub use self::cargo_generate_lockfile::update_lockfile;

src/cargo/util/command_prelude.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::core::compiler::{BuildConfig, MessageFormat, TimingOutput};
22
use crate::core::resolver::CliFeatures;
33
use crate::core::{Edition, Workspace};
4-
use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
4+
use crate::ops::{
5+
CompileFilter, CompileOptions, NewOptions, OutputFormat, Packages, VersionControl,
6+
};
57
use crate::util::important_paths::find_root_manifest_for_wd;
68
use crate::util::interning::InternedString;
79
use crate::util::restricted_names::is_glob_pattern;
@@ -366,6 +368,21 @@ pub trait ArgMatchesExt {
366368
Ok(ws)
367369
}
368370

371+
fn output_format(&self) -> CargoResult<OutputFormat> {
372+
let arg: OutputFormat = match self._value_of("output-format") {
373+
None => OutputFormat::default(),
374+
Some(arg) => match arg {
375+
"html" => OutputFormat::Html,
376+
"json" => OutputFormat::Json,
377+
_ => anyhow::bail!(
378+
"Allowed values are for \"--output-format\" are \"html\" or \"json\""
379+
),
380+
},
381+
};
382+
383+
Ok(arg)
384+
}
385+
369386
fn jobs(&self) -> CargoResult<Option<JobsConfig>> {
370387
let arg = match self._value_of("jobs") {
371388
None => None,

src/doc/man/cargo-doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ and supports common Unix glob patterns.
112112
{{#options}}
113113
{{> options-jobs }}
114114
{{> options-keep-going }}
115+
{{> options-output-format }}
115116
{{/options}}
116117

117118
{{> section-environment }}

src/doc/man/cargo-rustdoc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ if its name is the same as the lib target. Binaries are skipped if they have
100100
{{#options}}
101101
{{> options-jobs }}
102102
{{> options-keep-going }}
103+
{{> options-output-format }}
103104
{{/options}}
104105

105106
{{> section-environment }}

src/doc/man/generated_txt/cargo-doc.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ OPTIONS
305305
than aborting the build on the first one that fails to build.
306306
Unstable, requires -Zunstable-options.
307307

308+
--output-format
309+
The output type to write. Unstable, requires -Zunstable-options.
310+
308311
ENVIRONMENT
309312
See the reference
310313
<https://doc.rust-lang.org/cargo/reference/environment-variables.html>

src/doc/man/generated_txt/cargo-rustdoc.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ OPTIONS
321321
than aborting the build on the first one that fails to build.
322322
Unstable, requires -Zunstable-options.
323323

324+
--output-format
325+
The output type to write. Unstable, requires -Zunstable-options.
326+
324327
ENVIRONMENT
325328
See the reference
326329
<https://doc.rust-lang.org/cargo/reference/environment-variables.html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{#option "`--output-format`"}}
2+
The output type to write.
3+
Unstable, requires `-Zunstable-options`.
4+
{{/option}}

0 commit comments

Comments
 (0)