Skip to content

Commit 493a411

Browse files
committed
refactor(cli/rustup-mode): make DocPage::path() return Option<&Path>
1 parent cbd5458 commit 493a411

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/cli/rustup_mode.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::env::consts::EXE_SUFFIX;
23
use std::fmt;
34
use std::io::Write;
@@ -1425,7 +1426,7 @@ macro_rules! docs_data {
14251426
}
14261427

14271428
impl DocPage {
1428-
fn path(&self) -> Option<&'static str> {
1429+
fn path_str(&self) -> Option<&'static str> {
14291430
$( if self.$ident { return Some($path); } )+
14301431
None
14311432
}
@@ -1463,8 +1464,12 @@ docs_data![
14631464
];
14641465

14651466
impl DocPage {
1467+
fn path(&self) -> Option<&'static Path> {
1468+
self.path_str().map(Path::new)
1469+
}
1470+
14661471
fn name(&self) -> Option<&'static str> {
1467-
Some(self.path()?.rsplit_once('/')?.0)
1472+
Some(self.path_str()?.rsplit_once('/')?.0)
14681473
}
14691474
}
14701475

@@ -1502,18 +1507,18 @@ async fn doc(
15021507
}
15031508
};
15041509

1505-
let topical_path: PathBuf;
1506-
15071510
let doc_url = if let Some(topic) = topic {
1508-
topical_path = topical_doc::local_path(&toolchain.doc_path("").unwrap(), topic)?;
1509-
topical_path.to_str().unwrap()
1511+
Cow::Owned(topical_doc::local_path(
1512+
&toolchain.doc_path("").unwrap(),
1513+
topic,
1514+
)?)
15101515
} else {
15111516
topic = doc_page.name();
1512-
doc_page.path().unwrap_or("index.html")
1517+
Cow::Borrowed(doc_page.path().unwrap_or(Path::new("index.html")))
15131518
};
15141519

15151520
if path_only {
1516-
let doc_path = toolchain.doc_path(doc_url)?;
1521+
let doc_path = toolchain.doc_path(&doc_url)?;
15171522
writeln!(cfg.process.stdout().lock(), "{}", doc_path.display())?;
15181523
return Ok(utils::ExitCode(0));
15191524
}
@@ -1526,7 +1531,7 @@ async fn doc(
15261531
} else {
15271532
writeln!(cfg.process.stderr().lock(), "Opening docs in your browser")?;
15281533
}
1529-
toolchain.open_docs(doc_url)?;
1534+
toolchain.open_docs(&doc_url)?;
15301535
Ok(utils::ExitCode(0))
15311536
}
15321537

src/toolchain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a> Toolchain<'a> {
408408
buf
409409
}
410410

411-
pub fn doc_path(&self, relative: &str) -> anyhow::Result<PathBuf> {
411+
pub fn doc_path(&self, relative: impl AsRef<Path>) -> anyhow::Result<PathBuf> {
412412
let parts = vec!["share", "doc", "rust", "html"];
413413
let mut doc_dir = self.path.clone();
414414
for part in parts {
@@ -419,7 +419,7 @@ impl<'a> Toolchain<'a> {
419419
Ok(doc_dir)
420420
}
421421

422-
pub fn open_docs(&self, relative: &str) -> anyhow::Result<()> {
422+
pub fn open_docs(&self, relative: impl AsRef<Path>) -> anyhow::Result<()> {
423423
utils::open_browser(&self.doc_path(relative)?)
424424
}
425425

0 commit comments

Comments
 (0)