Skip to content

Commit a26a881

Browse files
committed
refactor(toolchain): allow passing a fragment in Toolchain::doc_path()
1 parent 7085fec commit a26a881

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/cli/rustup_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ async fn doc(
15311531
} else {
15321532
writeln!(cfg.process.stderr().lock(), "Opening docs in your browser")?;
15331533
}
1534-
toolchain.open_docs(&doc_path)?;
1534+
toolchain.open_docs(&doc_path, None)?;
15351535
Ok(utils::ExitCode(0))
15361536
}
15371537

src/toolchain.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use std::{
1010
time::Duration,
1111
};
1212

13-
use anyhow::{anyhow, bail};
13+
use anyhow::{anyhow, bail, Context};
1414
use fs_at::OpenOptions;
1515
use tracing::info;
16+
use url::Url;
1617
use wait_timeout::ChildExt;
1718

1819
use crate::{
@@ -421,8 +422,17 @@ impl<'a> Toolchain<'a> {
421422
Ok(doc_dir)
422423
}
423424

424-
pub fn open_docs(&self, relative: impl AsRef<Path>) -> anyhow::Result<()> {
425-
utils::open_browser(&self.doc_path(relative)?)
425+
pub fn open_docs(
426+
&self,
427+
relative: impl AsRef<Path>,
428+
fragment: Option<&str>,
429+
) -> anyhow::Result<()> {
430+
let relative = relative.as_ref();
431+
let mut doc_url = Url::from_file_path(self.doc_path(relative)?)
432+
.ok()
433+
.with_context(|| anyhow!("invalid doc file absolute path `{}`", relative.display()))?;
434+
doc_url.set_fragment(fragment);
435+
utils::open_browser(doc_url.to_string())
426436
}
427437

428438
/// Remove the toolchain from disk

src/utils/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Utility functions for Rustup
22
33
use std::env;
4+
use std::ffi::OsStr;
45
use std::fs::{self, File};
56
use std::io::{self, BufReader, Write};
67
use std::ops::{BitAnd, BitAndAssign};
@@ -459,7 +460,7 @@ pub(crate) fn read_dir(name: &'static str, path: &Path) -> Result<fs::ReadDir> {
459460
})
460461
}
461462

462-
pub(crate) fn open_browser(path: &Path) -> Result<()> {
463+
pub(crate) fn open_browser(path: impl AsRef<OsStr>) -> Result<()> {
463464
opener::open_browser(path).context("couldn't open browser")
464465
}
465466

0 commit comments

Comments
 (0)