Skip to content

Commit f30518b

Browse files
committed
Extract struct from InstallMethods::Dist variant
1 parent fd201e4 commit f30518b

File tree

3 files changed

+64
-83
lines changed

3 files changed

+64
-83
lines changed

src/dist/mod.rs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use regex::Regex;
1111
use serde::{Deserialize, Serialize};
1212
use thiserror::Error as ThisError;
1313

14-
use crate::{currentprocess::Process, errors::RustupError, toolchain::ToolchainName, utils::utils};
14+
use crate::{
15+
config::Cfg, currentprocess::Process, errors::RustupError, toolchain::ToolchainName,
16+
utils::utils,
17+
};
1518

1619
pub mod component;
1720
pub(crate) mod config;
@@ -707,51 +710,69 @@ impl fmt::Display for Profile {
707710
}
708711
}
709712

713+
#[derive(Clone)]
714+
pub(crate) struct DistOptions<'a> {
715+
pub(crate) cfg: &'a Cfg<'a>,
716+
pub(crate) desc: &'a ToolchainDesc,
717+
pub(crate) profile: Profile,
718+
pub(crate) update_hash: Option<&'a Path>,
719+
pub(crate) dl_cfg: DownloadCfg<'a>,
720+
/// --force bool is whether to force an update/install
721+
pub(crate) force: bool,
722+
/// --allow-downgrade
723+
pub(crate) allow_downgrade: bool,
724+
/// toolchain already exists
725+
pub(crate) exists: bool,
726+
/// currently installed date and version
727+
pub(crate) old_date_version: Option<(String, String)>,
728+
/// Extra components to install from dist
729+
pub(crate) components: &'a [&'a str],
730+
/// Extra targets to install from dist
731+
pub(crate) targets: &'a [&'a str],
732+
}
733+
710734
// Installs or updates a toolchain from a dist server. If an initial
711735
// install then it will be installed with the default components. If
712736
// an upgrade then all the existing components will be upgraded.
713737
//
714738
// Returns the manifest's hash if anything changed.
715-
#[cfg_attr(feature = "otel", tracing::instrument(err, skip_all, fields(profile=format!("{profile:?}"), prefix=prefix.path().to_string_lossy().to_string())))]
739+
#[cfg_attr(feature = "otel", tracing::instrument(err, skip_all, fields(profile=format!("{:?}", opts.profile), prefix=prefix.path().to_string_lossy().to_string())))]
716740
pub(crate) async fn update_from_dist(
717-
download: DownloadCfg<'_>,
718-
update_hash: Option<&Path>,
719-
toolchain: &ToolchainDesc,
720-
profile: Option<Profile>,
721741
prefix: &InstallPrefix,
722-
force_update: bool,
723-
allow_downgrade: bool,
724-
old_date: Option<&str>,
725-
components: &[&str],
726-
targets: &[&str],
742+
opts: &DistOptions<'_>,
727743
) -> Result<Option<String>> {
728744
let fresh_install = !prefix.path().exists();
729-
let hash_exists = update_hash.map(Path::exists).unwrap_or(false);
745+
let hash_exists = opts.update_hash.map(Path::exists).unwrap_or(false);
730746
// fresh_install means the toolchain isn't present, but hash_exists means there is a stray hash file
731747
if fresh_install && hash_exists {
732748
// It's ok to unwrap, because hash have to exist at this point
733-
(download.notify_handler)(Notification::StrayHash(update_hash.unwrap()));
734-
std::fs::remove_file(update_hash.unwrap())?;
749+
(opts.dl_cfg.notify_handler)(Notification::StrayHash(opts.update_hash.unwrap()));
750+
std::fs::remove_file(opts.update_hash.unwrap())?;
735751
}
736752

737753
let res = update_from_dist_(
738-
download,
739-
update_hash,
740-
toolchain,
741-
profile,
754+
opts.dl_cfg,
755+
opts.update_hash,
756+
opts.desc,
757+
match opts.exists {
758+
true => None,
759+
false => Some(opts.profile),
760+
},
742761
prefix,
743-
force_update,
744-
allow_downgrade,
745-
old_date,
746-
components,
747-
targets,
762+
opts.force,
763+
opts.allow_downgrade,
764+
opts.old_date_version
765+
.as_ref()
766+
.map(|(date, _)| date.as_str()),
767+
opts.components,
768+
opts.targets,
748769
)
749770
.await;
750771

751772
// Don't leave behind an empty / broken installation directory
752773
if res.is_err() && fresh_install {
753774
// FIXME Ignoring cascading errors
754-
let _ = utils::remove_dir("toolchain", prefix.path(), download.notify_handler);
775+
let _ = utils::remove_dir("toolchain", prefix.path(), opts.dl_cfg.notify_handler);
755776
}
756777

757778
res

src/install.rs

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anyhow::Result;
66

77
use crate::{
88
config::Cfg,
9-
dist::{self, download::DownloadCfg, prefix::InstallPrefix, Notification},
9+
dist::{self, prefix::InstallPrefix, DistOptions, Notification},
1010
errors::RustupError,
1111
notifications::Notification as RootNotification,
1212
toolchain::{CustomToolchainName, LocalToolchainName, Toolchain},
@@ -32,25 +32,7 @@ pub(crate) enum InstallMethod<'a> {
3232
dest: &'a CustomToolchainName,
3333
cfg: &'a Cfg<'a>,
3434
},
35-
Dist {
36-
cfg: &'a Cfg<'a>,
37-
desc: &'a dist::ToolchainDesc,
38-
profile: dist::Profile,
39-
update_hash: Option<&'a Path>,
40-
dl_cfg: DownloadCfg<'a>,
41-
/// --force bool is whether to force an update/install
42-
force: bool,
43-
/// --allow-downgrade
44-
allow_downgrade: bool,
45-
/// toolchain already exists
46-
exists: bool,
47-
/// currently installed date and version
48-
old_date_version: Option<(String, String)>,
49-
/// Extra components to install from dist
50-
components: &'a [&'a str],
51-
/// Extra targets to install from dist
52-
targets: &'a [&'a str],
53-
},
35+
Dist(DistOptions<'a>),
5436
}
5537

5638
impl<'a> InstallMethod<'a> {
@@ -61,10 +43,10 @@ impl<'a> InstallMethod<'a> {
6143
match self {
6244
InstallMethod::Copy { .. }
6345
| InstallMethod::Link { .. }
64-
| InstallMethod::Dist {
46+
| InstallMethod::Dist(DistOptions {
6547
old_date_version: None,
6648
..
67-
} => (nh)(RootNotification::InstallingToolchain(&self.dest_basename())),
49+
}) => (nh)(RootNotification::InstallingToolchain(&self.dest_basename())),
6850
_ => (nh)(RootNotification::UpdatingToolchain(&self.dest_basename())),
6951
}
7052

@@ -83,10 +65,10 @@ impl<'a> InstallMethod<'a> {
8365
true => {
8466
(nh)(RootNotification::InstalledToolchain(&self.dest_basename()));
8567
match self {
86-
InstallMethod::Dist {
68+
InstallMethod::Dist(DistOptions {
8769
old_date_version: Some((_, v)),
8870
..
89-
} => UpdateStatus::Updated(v.clone()),
71+
}) => UpdateStatus::Updated(v.clone()),
9072
InstallMethod::Copy { .. }
9173
| InstallMethod::Link { .. }
9274
| InstallMethod::Dist { .. } => UpdateStatus::Installed,
@@ -121,36 +103,12 @@ impl<'a> InstallMethod<'a> {
121103
utils::symlink_dir(src, path, notify_handler)?;
122104
Ok(true)
123105
}
124-
InstallMethod::Dist {
125-
desc,
126-
profile,
127-
update_hash,
128-
dl_cfg,
129-
force: force_update,
130-
allow_downgrade,
131-
exists,
132-
old_date_version,
133-
components,
134-
targets,
135-
..
136-
} => {
106+
InstallMethod::Dist(opts) => {
137107
let prefix = &InstallPrefix::from(path.to_owned());
138-
let maybe_new_hash = dist::update_from_dist(
139-
*dl_cfg,
140-
update_hash.as_deref(),
141-
desc,
142-
if *exists { None } else { Some(*profile) },
143-
prefix,
144-
*force_update,
145-
*allow_downgrade,
146-
old_date_version.as_ref().map(|dv| dv.0.as_str()),
147-
components,
148-
targets,
149-
)
150-
.await?;
108+
let maybe_new_hash = dist::update_from_dist(prefix, opts).await?;
151109

152110
if let Some(hash) = maybe_new_hash {
153-
if let Some(hash_file) = update_hash {
111+
if let Some(hash_file) = opts.update_hash {
154112
utils::write_file("update hash", hash_file, &hash)?;
155113
}
156114

@@ -166,15 +124,15 @@ impl<'a> InstallMethod<'a> {
166124
match self {
167125
InstallMethod::Copy { cfg, .. } => cfg,
168126
InstallMethod::Link { cfg, .. } => cfg,
169-
InstallMethod::Dist { cfg, .. } => cfg,
127+
InstallMethod::Dist(DistOptions { cfg, .. }) => cfg,
170128
}
171129
}
172130

173131
fn local_name(&self) -> LocalToolchainName {
174132
match self {
175133
InstallMethod::Copy { dest, .. } => (*dest).into(),
176134
InstallMethod::Link { dest, .. } => (*dest).into(),
177-
InstallMethod::Dist { desc, .. } => (*desc).into(),
135+
InstallMethod::Dist(DistOptions { desc, .. }) => (*desc).into(),
178136
}
179137
}
180138

@@ -186,7 +144,9 @@ impl<'a> InstallMethod<'a> {
186144
match self {
187145
InstallMethod::Copy { cfg, dest, .. } => cfg.toolchain_path(&(*dest).into()),
188146
InstallMethod::Link { cfg, dest, .. } => cfg.toolchain_path(&(*dest).into()),
189-
InstallMethod::Dist { cfg, desc, .. } => cfg.toolchain_path(&(*desc).into()),
147+
InstallMethod::Dist(DistOptions { cfg, desc, .. }) => {
148+
cfg.toolchain_path(&(*desc).into())
149+
}
190150
}
191151
}
192152
}

src/toolchain/distributable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
manifest::{Component, ComponentStatus, Manifest},
1313
manifestation::{Changes, Manifestation},
1414
prefix::InstallPrefix,
15-
PartialToolchainDesc, Profile, ToolchainDesc,
15+
DistOptions, PartialToolchainDesc, Profile, ToolchainDesc,
1616
},
1717
install::{InstallMethod, UpdateStatus},
1818
notifications::Notification,
@@ -336,7 +336,7 @@ impl<'a> DistributableToolchain<'a> {
336336
let hash_path = cfg.get_hash_file(desc, true)?;
337337
let update_hash = Some(&hash_path as &Path);
338338

339-
let status = InstallMethod::Dist {
339+
let status = InstallMethod::Dist(DistOptions {
340340
cfg,
341341
desc,
342342
profile,
@@ -348,7 +348,7 @@ impl<'a> DistributableToolchain<'a> {
348348
old_date_version: None,
349349
components,
350350
targets,
351-
}
351+
})
352352
.install()
353353
.await?;
354354
Ok((status, Self::new(cfg, desc.clone())?))
@@ -412,7 +412,7 @@ impl<'a> DistributableToolchain<'a> {
412412
let hash_path = cfg.get_hash_file(&self.desc, true)?;
413413
let update_hash = Some(&hash_path as &Path);
414414

415-
InstallMethod::Dist {
415+
InstallMethod::Dist(DistOptions {
416416
cfg,
417417
desc: &self.desc,
418418
profile,
@@ -424,7 +424,7 @@ impl<'a> DistributableToolchain<'a> {
424424
old_date_version,
425425
components,
426426
targets,
427-
}
427+
})
428428
.install()
429429
.await
430430
}

0 commit comments

Comments
 (0)