From 794e3bf12bb1ddaf227505db1376a3282028ef22 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 9 Apr 2024 09:45:59 +0200 Subject: [PATCH] refact(rustup): getting switches per repotype --- .../src/backend/actual/rustup/helpers.rs | 23 ------------------- .../src/backend/actual/rustup/mod.rs | 15 +++++------- .../src/backend/actual/rustup/types.rs | 23 ++++++++++++++++++- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/crates/pacdef_core/src/backend/actual/rustup/helpers.rs b/crates/pacdef_core/src/backend/actual/rustup/helpers.rs index d8e56b5..503fd83 100644 --- a/crates/pacdef_core/src/backend/actual/rustup/helpers.rs +++ b/crates/pacdef_core/src/backend/actual/rustup/helpers.rs @@ -1,5 +1,3 @@ -use crate::backend::backend_trait::Switches; - use super::types::{Repotype, RustupPackage}; pub fn toolchain_of_component_was_already_removed( @@ -25,27 +23,6 @@ pub fn sort_packages_into_toolchains_and_components( (toolchains, components) } -pub fn get_install_switches(repotype: Repotype) -> Switches { - match repotype { - Repotype::Toolchain => &["toolchain", "install"], - Repotype::Component => &["component", "add", "--toolchain"], - } -} - -pub fn get_remove_switches(repotype: Repotype) -> Switches { - match repotype { - Repotype::Toolchain => &["toolchain", "uninstall"], - Repotype::Component => &["component", "remove", "--toolchain"], - } -} - -pub fn get_info_switches(repotype: Repotype) -> Switches { - match repotype { - Repotype::Toolchain => &["toolchain", "list"], - Repotype::Component => &["component", "list", "--installed", "--toolchain"], - } -} - pub fn install_components(line: &str, toolchain: &str, val: &mut Vec) { let mut chunks = line.splitn(3, '-'); let component = chunks.next().expect("Component name is empty!"); diff --git a/crates/pacdef_core/src/backend/actual/rustup/mod.rs b/crates/pacdef_core/src/backend/actual/rustup/mod.rs index c0f2b77..6f3af8f 100644 --- a/crates/pacdef_core/src/backend/actual/rustup/mod.rs +++ b/crates/pacdef_core/src/backend/actual/rustup/mod.rs @@ -29,7 +29,7 @@ impl Backend for Rustup { fn get_all_installed_packages(&self) -> Result> { let toolchains_vec = self - .run_toolchain_command(helpers::get_info_switches(Repotype::Toolchain)) + .run_toolchain_command(Repotype::Toolchain.get_info_switches()) .context("Getting installed toolchains")?; let toolchains: HashSet = toolchains_vec @@ -38,10 +38,7 @@ impl Backend for Rustup { .collect(); let components: HashSet = self - .run_component_command( - helpers::get_info_switches(Repotype::Component), - &toolchains_vec, - ) + .run_component_command(Repotype::Component.get_install_switches(), &toolchains_vec) .context("Getting installed components")? .iter() .map(|name| ["component", name].join("/").into()) @@ -153,7 +150,7 @@ impl Rustup { return Ok(()); } let mut cmd = Command::new(self.get_binary()); - cmd.args(helpers::get_install_switches(Repotype::Toolchain)); + cmd.args(Repotype::Toolchain.get_install_switches()); for toolchain in toolchains { cmd.arg(&toolchain.toolchain); @@ -173,7 +170,7 @@ impl Rustup { for components_for_one_toolchain in components_by_toolchain { let mut cmd = Command::new(self.get_binary()); - cmd.args(helpers::get_install_switches(Repotype::Component)); + cmd.args(Repotype::Component.get_install_switches()); let the_toolchain = &components_for_one_toolchain .first() @@ -202,7 +199,7 @@ impl Rustup { let mut removed_toolchains = vec![]; if !toolchains.is_empty() { let mut cmd = Command::new(self.get_binary()); - cmd.args(helpers::get_remove_switches(Repotype::Toolchain)); + cmd.args(Repotype::Toolchain.get_remove_switches()); for toolchain_package in &toolchains { let name = toolchain_package.toolchain.as_str(); @@ -223,7 +220,7 @@ impl Rustup { ) -> Result<()> { for component_package in components { let mut cmd = Command::new(self.get_binary()); - cmd.args(helpers::get_remove_switches(Repotype::Component)); + cmd.args(Repotype::Component.get_remove_switches()); if helpers::toolchain_of_component_was_already_removed( &removed_toolchains, diff --git a/crates/pacdef_core/src/backend/actual/rustup/types.rs b/crates/pacdef_core/src/backend/actual/rustup/types.rs index 820120c..5ed94cb 100644 --- a/crates/pacdef_core/src/backend/actual/rustup/types.rs +++ b/crates/pacdef_core/src/backend/actual/rustup/types.rs @@ -1,7 +1,7 @@ use anyhow::{bail, Context, Result}; use std::collections::HashSet; -use crate::Package; +use crate::{backend::backend_trait::Switches, Package}; #[derive(Debug, Clone)] pub struct Rustup { @@ -27,6 +27,27 @@ impl Repotype { }; Ok(result) } + + pub fn get_install_switches(self) -> Switches { + match self { + Self::Toolchain => &["toolchain", "install"], + Self::Component => &["component", "add", "--toolchain"], + } + } + + pub fn get_remove_switches(self) -> Switches { + match self { + Self::Toolchain => &["toolchain", "uninstall"], + Self::Component => &["component", "remove", "--toolchain"], + } + } + + pub fn get_info_switches(self) -> Switches { + match self { + Self::Toolchain => &["toolchain", "list"], + Self::Component => &["component", "list", "--installed", "--toolchain"], + } + } } /// A package as used exclusively in the rustup backend. Contrary to other packages, this does not