Skip to content

Commit 1d34efe

Browse files
committed
refactor: extract opener to a module
1 parent 44a548e commit 1d34efe

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

src/cargo/ops/cargo_doc.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ use crate::core::shell::Verbosity;
44
use crate::ops;
55
use crate::util;
66
use crate::util::CargoResult;
7-
use crate::util::context::{GlobalContext, PathAndArgs};
87

98
use anyhow::{Error, bail};
109
use cargo_util::ProcessBuilder;
1110

1211
use std::ffi::OsString;
13-
use std::path::Path;
1412
use std::path::PathBuf;
15-
use std::process::Command;
1613
use std::str::FromStr;
1714

1815
/// Format of rustdoc [`--output-format`][1].
@@ -76,7 +73,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
7673
let path = path_by_output_format(&compilation, &kind, &name, &options.output_format);
7774

7875
if path.exists() {
79-
open(&path, ws.gctx())?;
76+
util::open::open(&path, ws.gctx())?;
8077
}
8178
} else if ws.gctx().shell().verbosity() == Verbosity::Verbose {
8279
for name in &compilation.root_crate_names {
@@ -210,37 +207,3 @@ fn path_by_output_format(
210207
.join("index.html")
211208
}
212209
}
213-
214-
fn open(path: &Path, gctx: &GlobalContext) -> CargoResult<()> {
215-
let config_browser = {
216-
let cfg: Option<PathAndArgs> = gctx.get("doc.browser")?;
217-
cfg.map(|path_args| (path_args.path.resolve_program(gctx), path_args.args))
218-
};
219-
220-
let mut shell = gctx.shell();
221-
let link = shell.err_file_hyperlink(&path);
222-
shell.status("Opening", format!("{link}{}{link:#}", path.display()))?;
223-
224-
let browser =
225-
config_browser.or_else(|| Some((PathBuf::from(gctx.get_env_os("BROWSER")?), Vec::new())));
226-
227-
match browser {
228-
Some((browser, initial_args)) => {
229-
if let Err(e) = Command::new(&browser).args(initial_args).arg(path).status() {
230-
shell.warn(format!(
231-
"Couldn't open docs with {}: {}",
232-
browser.to_string_lossy(),
233-
e
234-
))?;
235-
}
236-
}
237-
None => {
238-
if let Err(e) = opener::open(&path) {
239-
let e = e.into();
240-
crate::display_warning_with_error("couldn't open docs", &e, &mut shell);
241-
}
242-
}
243-
};
244-
245-
Ok(())
246-
}

src/cargo/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub mod logger;
6161
pub mod machine_message;
6262
pub mod network;
6363
mod once;
64+
pub mod open;
6465
mod progress;
6566
mod queue;
6667
pub mod restricted_names;

src/cargo/util/open.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! For opening files or URLs with the preferred application.
2+
3+
use std::path::Path;
4+
use std::path::PathBuf;
5+
use std::process::Command;
6+
7+
use crate::CargoResult;
8+
use crate::GlobalContext;
9+
use crate::util::context::PathAndArgs;
10+
11+
/// Opens a file path using the preferred application.
12+
///
13+
/// 1. Try `doc.browser` config first
14+
/// 2. Then `$BROWSER`
15+
/// 3. Finally system default opener
16+
pub fn open(path: &Path, gctx: &GlobalContext) -> CargoResult<()> {
17+
let config_browser = {
18+
let cfg: Option<PathAndArgs> = gctx.get("doc.browser")?;
19+
cfg.map(|path_args| (path_args.path.resolve_program(gctx), path_args.args))
20+
};
21+
22+
let mut shell = gctx.shell();
23+
let link = shell.err_file_hyperlink(&path);
24+
shell.status("Opening", format!("{link}{}{link:#}", path.display()))?;
25+
26+
let browser =
27+
config_browser.or_else(|| Some((PathBuf::from(gctx.get_env_os("BROWSER")?), Vec::new())));
28+
29+
match browser {
30+
Some((browser, initial_args)) => {
31+
if let Err(e) = Command::new(&browser).args(initial_args).arg(path).status() {
32+
shell.warn(format!(
33+
"Couldn't open docs with {}: {}",
34+
browser.to_string_lossy(),
35+
e
36+
))?;
37+
}
38+
}
39+
None => {
40+
if let Err(e) = opener::open(&path) {
41+
let e = e.into();
42+
crate::display_warning_with_error("couldn't open docs", &e, &mut shell);
43+
}
44+
}
45+
};
46+
47+
Ok(())
48+
}

0 commit comments

Comments
 (0)