From fcc0a0be3bf956792f8379c9bb53e83c9baa7e41 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 9 Aug 2024 18:31:10 +0800 Subject: [PATCH 1/4] refactor(config): simplify `toolchain_from_partial()` --- src/config.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 52c26285fa..e7331fbebc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -502,13 +502,13 @@ impl<'a> Cfg<'a> { &self, toolchain: Option, ) -> anyhow::Result> { - let toolchain = match toolchain { - Some(toolchain) => { - let desc = toolchain.resolve(&self.get_default_host_triple()?)?; - Some(LocalToolchainName::Named(ToolchainName::Official(desc))) - } - None => None, - }; + let toolchain = toolchain + .map(|desc| { + anyhow::Ok(LocalToolchainName::Named(ToolchainName::Official( + desc.resolve(&self.get_default_host_triple()?)?, + ))) + }) + .transpose()?; self.local_toolchain(toolchain) } From d2c9b1c33870f2612d4c03379cc79500851bc76a Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 9 Aug 2024 18:32:04 +0800 Subject: [PATCH 2/4] refactor(config): simplify `resolve_toolchain()` --- src/config.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index e7331fbebc..43d1ca5651 100644 --- a/src/config.rs +++ b/src/config.rs @@ -711,13 +711,9 @@ impl<'a> Cfg<'a> { &self, name: Option, ) -> Result> { - let toolchain = match name { - Some(name) => { - let desc = name.resolve(&self.get_default_host_triple()?)?; - Some(desc.into()) - } - None => None, - }; + let toolchain = name + .map(|name| anyhow::Ok(name.resolve(&self.get_default_host_triple()?)?.into())) + .transpose()?; self.local_toolchain(toolchain) } From 108138d0782c2c3e18d3013f7dd7c2f2e583f595 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 9 Aug 2024 19:57:55 +0800 Subject: [PATCH 3/4] refactor(config)!: return `LocalToolchainName` from `find_or_install_active_toolchain()` --- src/cli/rustup_mode.rs | 5 +---- src/config.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 89fd348f23..ed77adcc48 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -867,10 +867,7 @@ async fn update( } } else if ensure_active_toolchain { let (toolchain, reason) = cfg.find_or_install_active_toolchain(true).await?; - info!( - "the active toolchain `{}` has been installed", - toolchain.name() - ); + info!("the active toolchain `{toolchain}` has been installed"); info!("it's active because: {reason}"); } else { exit_code &= common::update_all_channels(cfg, self_update, opts.force).await?; diff --git a/src/config.rs b/src/config.rs index 43d1ca5651..177af99dae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -741,18 +741,18 @@ impl<'a> Cfg<'a> { #[tracing::instrument(level = "trace", skip_all)] pub(crate) async fn find_or_install_active_toolchain( - &'a self, + &self, verbose: bool, - ) -> Result<(Toolchain<'a>, ActiveReason)> { + ) -> Result<(LocalToolchainName, ActiveReason)> { match self.find_override_config()? { Some((override_config, reason)) => match override_config { OverrideCfg::PathBased(path_based_name) => { let toolchain = Toolchain::with_reason(self, path_based_name.into(), &reason)?; - Ok((toolchain, reason)) + Ok((toolchain.name().clone(), reason)) } OverrideCfg::Custom(custom_name) => { let toolchain = Toolchain::with_reason(self, custom_name.into(), &reason)?; - Ok((toolchain, reason)) + Ok((toolchain.name().clone(), reason)) } OverrideCfg::Official { toolchain, @@ -764,7 +764,7 @@ impl<'a> Cfg<'a> { .ensure_installed(&toolchain, components, targets, profile, verbose) .await? .1; - Ok((toolchain, reason)) + Ok((toolchain.name().clone(), reason)) } }, None => match self.get_default()? { @@ -772,7 +772,7 @@ impl<'a> Cfg<'a> { Some(ToolchainName::Custom(custom_name)) => { let reason = ActiveReason::Default; let toolchain = Toolchain::with_reason(self, custom_name.into(), &reason)?; - Ok((toolchain, reason)) + Ok((toolchain.name().clone(), reason)) } Some(ToolchainName::Official(toolchain_desc)) => { let reason = ActiveReason::Default; @@ -780,7 +780,7 @@ impl<'a> Cfg<'a> { .ensure_installed(&toolchain_desc, vec![], vec![], None, verbose) .await? .1; - Ok((toolchain, reason)) + Ok((toolchain.name().clone(), reason)) } }, } From 5e1683d22481ca220a12a586522bb6ace0202539 Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 9 Aug 2024 20:00:43 +0800 Subject: [PATCH 4/4] refactor(config): simplify `find_or_install_active_toolchain()` --- src/config.rs | 67 +++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index 177af99dae..d7a62d5d61 100644 --- a/src/config.rs +++ b/src/config.rs @@ -115,7 +115,7 @@ impl Display for ActiveReason { // RUSTUP_TOOLCHAIN environment variable, or rust-toolchain.toml file etc. Can // include components and targets from a rust-toolchain.toml that should be // downloaded and installed. -#[derive(Debug)] +#[derive(Clone, Debug)] enum OverrideCfg { PathBased(PathBasedToolchainName), Custom(CustomToolchainName), @@ -744,45 +744,32 @@ impl<'a> Cfg<'a> { &self, verbose: bool, ) -> Result<(LocalToolchainName, ActiveReason)> { - match self.find_override_config()? { - Some((override_config, reason)) => match override_config { - OverrideCfg::PathBased(path_based_name) => { - let toolchain = Toolchain::with_reason(self, path_based_name.into(), &reason)?; - Ok((toolchain.name().clone(), reason)) - } - OverrideCfg::Custom(custom_name) => { - let toolchain = Toolchain::with_reason(self, custom_name.into(), &reason)?; - Ok((toolchain.name().clone(), reason)) - } - OverrideCfg::Official { - toolchain, - components, - targets, - profile, - } => { - let toolchain = self - .ensure_installed(&toolchain, components, targets, profile, verbose) - .await? - .1; - Ok((toolchain.name().clone(), reason)) - } - }, - None => match self.get_default()? { - None => Err(no_toolchain_error(self.process)), - Some(ToolchainName::Custom(custom_name)) => { - let reason = ActiveReason::Default; - let toolchain = Toolchain::with_reason(self, custom_name.into(), &reason)?; - Ok((toolchain.name().clone(), reason)) - } - Some(ToolchainName::Official(toolchain_desc)) => { - let reason = ActiveReason::Default; - let toolchain = self - .ensure_installed(&toolchain_desc, vec![], vec![], None, verbose) - .await? - .1; - Ok((toolchain.name().clone(), reason)) - } - }, + if let Some((override_config, reason)) = self.find_override_config()? { + let toolchain = override_config.clone().into_local_toolchain_name(); + if let OverrideCfg::Official { + toolchain, + components, + targets, + profile, + } = override_config + { + self.ensure_installed(&toolchain, components, targets, profile, verbose) + .await?; + } else { + Toolchain::with_reason(self, toolchain.clone(), &reason)?; + } + Ok((toolchain, reason)) + } else if let Some(toolchain) = self.get_default()? { + let reason = ActiveReason::Default; + if let ToolchainName::Official(desc) = &toolchain { + self.ensure_installed(desc, vec![], vec![], None, verbose) + .await?; + } else { + Toolchain::with_reason(self, toolchain.clone().into(), &reason)?; + } + Ok((toolchain.into(), reason)) + } else { + Err(no_toolchain_error(self.process)) } }