Skip to content

Commit fb96ac2

Browse files
committed
dist: deduplicate DistOptions initialization
1 parent dac7225 commit fb96ac2

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

src/dist/mod.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Installation from a Rust distribution server
22
33
use std::{
4-
collections::HashSet, env, fmt, io::Write, ops::Deref, path::Path, str::FromStr, sync::LazyLock,
4+
collections::HashSet, env, fmt, io::Write, ops::Deref, path::PathBuf, str::FromStr,
5+
sync::LazyLock,
56
};
67

78
use anyhow::{Context, Result, anyhow, bail};
@@ -871,26 +872,49 @@ impl fmt::Display for Profile {
871872
}
872873

873874
pub(crate) struct DistOptions<'a> {
874-
pub(crate) cfg: &'a Cfg<'a>,
875-
pub(crate) toolchain: &'a ToolchainDesc,
876-
pub(crate) profile: Profile,
877-
pub(crate) update_hash: &'a Path,
878-
pub(crate) dl_cfg: DownloadCfg<'a>,
875+
pub(super) cfg: &'a Cfg<'a>,
876+
pub(super) toolchain: &'a ToolchainDesc,
877+
profile: Profile,
878+
pub(super) update_hash: PathBuf,
879+
dl_cfg: DownloadCfg<'a>,
879880
/// --force bool is whether to force an update/install
880-
pub(crate) force: bool,
881+
force: bool,
881882
/// --allow-downgrade
882-
pub(crate) allow_downgrade: bool,
883+
pub(super) allow_downgrade: bool,
883884
/// toolchain already exists
884-
pub(crate) exists: bool,
885+
pub(super) exists: bool,
885886
/// currently installed date and version
886-
pub(crate) old_date_version: Option<(String, String)>,
887+
pub(super) old_date_version: Option<(String, String)>,
887888
/// Extra components to install from dist
888-
pub(crate) components: &'a [&'a str],
889+
components: &'a [&'a str],
889890
/// Extra targets to install from dist
890-
pub(crate) targets: &'a [&'a str],
891+
targets: &'a [&'a str],
891892
}
892893

893894
impl<'a> DistOptions<'a> {
895+
pub(super) fn new(
896+
components: &'a [&'a str],
897+
targets: &'a [&'a str],
898+
toolchain: &'a ToolchainDesc,
899+
profile: Profile,
900+
force: bool,
901+
cfg: &'a Cfg<'_>,
902+
) -> Result<Self> {
903+
Ok(Self {
904+
cfg,
905+
toolchain,
906+
profile,
907+
update_hash: cfg.get_hash_file(toolchain, true)?,
908+
dl_cfg: DownloadCfg::new(cfg),
909+
force,
910+
allow_downgrade: false,
911+
exists: false,
912+
old_date_version: None,
913+
components,
914+
targets,
915+
})
916+
}
917+
894918
// Installs or updates a toolchain from a dist server. If an initial
895919
// install then it will be installed with the default components. If
896920
// an upgrade then all the existing components will be upgraded.
@@ -900,12 +924,12 @@ impl<'a> DistOptions<'a> {
900924
pub(crate) async fn install_into(&self, prefix: &InstallPrefix) -> Result<Option<String>> {
901925
let fresh_install = !prefix.path().exists();
902926
// fresh_install means the toolchain isn't present, but hash_exists means there is a stray hash file
903-
if fresh_install && Path::exists(self.update_hash) {
927+
if fresh_install && self.update_hash.exists() {
904928
warn!(
905929
"removing stray hash file in order to continue: {}",
906930
self.update_hash.display()
907931
);
908-
std::fs::remove_file(self.update_hash)?;
932+
std::fs::remove_file(&self.update_hash)?;
909933
}
910934

911935
let mut fetched = String::new();
@@ -1067,7 +1091,7 @@ impl<'a> DistOptions<'a> {
10671091
// So if components or targets is not empty, we skip passing `update_hash` so that
10681092
// we essentially degenerate to `rustup component add` / `rustup target add`
10691093
if self.components.is_empty() && self.targets.is_empty() {
1070-
Some(self.update_hash)
1094+
Some(&self.update_hash)
10711095
} else {
10721096
None
10731097
},

src/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl InstallMethod<'_> {
107107
let maybe_new_hash = opts.install_into(prefix).await?;
108108

109109
if let Some(hash) = maybe_new_hash {
110-
utils::write_file("update hash", opts.update_hash, &hash)?;
110+
utils::write_file("update hash", &opts.update_hash, &hash)?;
111111
Ok(true)
112112
} else {
113113
Ok(false)

src/toolchain/distributable.rs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,8 @@ impl<'a> DistributableToolchain<'a> {
4343
profile: Profile,
4444
force: bool,
4545
) -> anyhow::Result<(UpdateStatus, Self)> {
46-
let hash_path = cfg.get_hash_file(toolchain, true)?;
47-
let status = InstallMethod::Dist(DistOptions {
48-
cfg,
49-
toolchain,
50-
profile,
51-
update_hash: &hash_path,
52-
dl_cfg: DownloadCfg::new(cfg),
53-
force,
54-
allow_downgrade: false,
55-
exists: false,
56-
old_date_version: None,
57-
components,
58-
targets,
59-
})
60-
.install()
61-
.await?;
46+
let options = DistOptions::new(components, targets, toolchain, profile, force, cfg)?;
47+
let status = InstallMethod::Dist(options).install().await?;
6248
Ok((status, Self::new(cfg, toolchain.clone())?))
6349
}
6450

@@ -385,7 +371,18 @@ impl<'a> DistributableToolchain<'a> {
385371
force: bool,
386372
allow_downgrade: bool,
387373
) -> anyhow::Result<UpdateStatus> {
388-
let old_date_version =
374+
let mut options = DistOptions::new(
375+
components,
376+
targets,
377+
&self.desc,
378+
profile,
379+
force,
380+
self.toolchain.cfg,
381+
)?;
382+
383+
options.allow_downgrade = allow_downgrade;
384+
options.exists = true;
385+
options.old_date_version =
389386
// Ignore a missing manifest: we can't report the old version
390387
// correctly, and it probably indicates an incomplete install, so do
391388
// not report an old rustc version either.
@@ -400,23 +397,7 @@ impl<'a> DistributableToolchain<'a> {
400397
})
401398
.ok();
402399

403-
let cfg = self.toolchain.cfg;
404-
let hash_path = cfg.get_hash_file(&self.desc, true)?;
405-
InstallMethod::Dist(DistOptions {
406-
cfg,
407-
toolchain: &self.desc,
408-
profile,
409-
update_hash: &hash_path,
410-
dl_cfg: DownloadCfg::new(cfg),
411-
force,
412-
allow_downgrade,
413-
exists: true,
414-
old_date_version,
415-
components,
416-
targets,
417-
})
418-
.install()
419-
.await
400+
InstallMethod::Dist(options).install().await
420401
}
421402

422403
pub fn recursion_error(&self, binary_lossy: String) -> Result<Infallible, anyhow::Error> {

0 commit comments

Comments
 (0)