From b9315228db2b0d40e0a459b134ecac33967ee1ee Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 18 Jan 2024 18:01:04 +0800 Subject: [PATCH 1/3] refactor(cli): avoid nested combinators in `has_at_most_one_target` --- src/cli/rustup_mode.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 189899561c..4c92cfab6f 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1340,14 +1340,17 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result { if target == default_target { warn!("after removing the default host target, proc-macros and build scripts might no longer build"); } - let has_at_most_one_target = { - let components = distributable.components()?; - // Every component target that is not `None` (wildcard). - let targets = components - .iter() - .filter_map(|c| c.installed.then(|| c.component.target.clone()).flatten()); - targets.unique().at_most_one().is_ok() - }; + // Whether we have at most 1 component target that is not `None` (wildcard). + let has_at_most_one_target = distributable + .components()? + .into_iter() + .filter_map(|c| match (c.installed, c.component.target) { + (true, Some(t)) => Some(t), + _ => None, + }) + .unique() + .at_most_one() + .is_ok(); if has_at_most_one_target { warn!("after removing the last target, no build targets will be available"); } From 887b8cb80c14b084976699d65ca49855c5280053 Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 18 Jan 2024 18:03:28 +0800 Subject: [PATCH 2/3] refactor(distributable): import `ComponentStatus` --- src/toolchain/distributable.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/toolchain/distributable.rs b/src/toolchain/distributable.rs index 8bc77901c2..a22b026739 100644 --- a/src/toolchain/distributable.rs +++ b/src/toolchain/distributable.rs @@ -10,7 +10,7 @@ use crate::{ dist::{ config::Config, dist::{Profile, ToolchainDesc}, - manifest::{Component, Manifest}, + manifest::{Component, ComponentStatus, Manifest}, manifestation::{Changes, Manifestation}, prefix::InstallPrefix, }, @@ -115,7 +115,7 @@ impl<'a> DistributableToolchain<'a> { Ok(()) } - pub(crate) fn components(&self) -> anyhow::Result> { + pub(crate) fn components(&self) -> anyhow::Result> { let manifestation = self.get_manifestation()?; let config = manifestation.read_config()?.unwrap_or_default(); let manifest = self.get_manifest()?; From 1c0c119ecf7a2699a54971f97b3ab64ba2995deb Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 18 Jan 2024 18:06:51 +0800 Subject: [PATCH 3/3] refactor: simplify `is_proxyable_tools` --- src/lib.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index af9875c969..b5215ea09f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub use crate::errors::*; pub(crate) use crate::notifications::*; pub(crate) use crate::utils::toml_utils; use anyhow::{anyhow, Result}; +use itertools::{chain, Itertools}; #[macro_use] extern crate rs_tracing; @@ -41,23 +42,15 @@ pub static DUP_TOOLS: &[&str] = &["rust-analyzer", "rustfmt", "cargo-fmt"]; // If the given name is one of the tools we proxy. pub fn is_proxyable_tools(tool: &str) -> Result<()> { - if TOOLS - .iter() - .chain(DUP_TOOLS.iter()) - .any(|&name| name == tool) - { + if chain!(TOOLS, DUP_TOOLS).contains(&tool) { Ok(()) } else { - Err(anyhow!(format!( - "unknown proxy name: '{}'; valid proxy names are {}", - tool, - TOOLS - .iter() - .chain(DUP_TOOLS.iter()) + Err(anyhow!( + "unknown proxy name: '{tool}'; valid proxy names are {}", + chain!(TOOLS, DUP_TOOLS) .map(|s| format!("'{s}'")) - .collect::>() - .join(", ") - ))) + .join(", "), + )) } }