Skip to content

Commit f736345

Browse files
committed
dist: deduplicate DistOptions initialization
1 parent 7dbf604 commit f736345

File tree

3 files changed

+62
-50
lines changed

3 files changed

+62
-50
lines changed

src/dist/mod.rs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
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,
5+
env, fmt,
6+
io::Write,
7+
ops::Deref,
8+
path::{Path, PathBuf},
9+
str::FromStr,
10+
sync::LazyLock,
511
};
612

713
use anyhow::{Context, Result, anyhow, bail};
@@ -871,23 +877,48 @@ impl fmt::Display for Profile {
871877
}
872878

873879
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>,
880+
pub(super) cfg: &'a Cfg<'a>,
881+
pub(super) toolchain: &'a ToolchainDesc,
882+
profile: Profile,
883+
pub(super) update_hash: PathBuf,
884+
dl_cfg: DownloadCfg<'a>,
879885
/// --force bool is whether to force an update/install
880-
pub(crate) force: bool,
886+
force: bool,
881887
/// --allow-downgrade
882-
pub(crate) allow_downgrade: bool,
888+
pub(super) allow_downgrade: bool,
883889
/// toolchain already exists
884-
pub(crate) exists: bool,
890+
pub(super) exists: bool,
885891
/// currently installed date and version
886-
pub(crate) old_date_version: Option<(String, String)>,
892+
pub(super) old_date_version: Option<(String, String)>,
887893
/// Extra components to install from dist
888-
pub(crate) components: &'a [&'a str],
894+
components: &'a [&'a str],
889895
/// Extra targets to install from dist
890-
pub(crate) targets: &'a [&'a str],
896+
targets: &'a [&'a str],
897+
}
898+
899+
impl<'a> DistOptions<'a> {
900+
pub(super) fn new(
901+
components: &'a [&'a str],
902+
targets: &'a [&'a str],
903+
toolchain: &'a ToolchainDesc,
904+
profile: Profile,
905+
force: bool,
906+
cfg: &'a Cfg<'_>,
907+
) -> Result<Self> {
908+
Ok(Self {
909+
cfg,
910+
toolchain,
911+
profile,
912+
update_hash: cfg.get_hash_file(toolchain, true)?,
913+
dl_cfg: DownloadCfg::new(cfg),
914+
force,
915+
allow_downgrade: false,
916+
exists: false,
917+
old_date_version: None,
918+
components,
919+
targets,
920+
})
921+
}
891922
}
892923

893924
// Installs or updates a toolchain from a dist server. If an initial
@@ -902,12 +933,12 @@ pub(crate) async fn update_from_dist(
902933
) -> Result<Option<String>> {
903934
let fresh_install = !prefix.path().exists();
904935
// fresh_install means the toolchain isn't present, but hash_exists means there is a stray hash file
905-
if fresh_install && Path::exists(opts.update_hash) {
936+
if fresh_install && opts.update_hash.exists() {
906937
warn!(
907938
"removing stray hash file in order to continue: {}",
908939
opts.update_hash.display()
909940
);
910-
std::fs::remove_file(opts.update_hash)?;
941+
std::fs::remove_file(&opts.update_hash)?;
911942
}
912943

913944
let mut fetched = String::new();
@@ -960,7 +991,7 @@ pub(crate) async fn update_from_dist(
960991
let res = loop {
961992
let result = try_update_from_dist_(
962993
&opts.dl_cfg,
963-
opts.update_hash,
994+
&opts.update_hash,
964995
&toolchain,
965996
match opts.exists {
966997
false => Some(opts.profile),

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 = dist::update_from_dist(prefix, opts).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)