From 265d67089b0dbf887a1a06708b61f20aec227bcf Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Thu, 5 Jan 2023 17:57:38 +0100 Subject: [PATCH] add more generic backend stuff --- src/backend/mod.rs | 46 ++++++++++++++++++++++++++++++++----------- src/backend/pacman.rs | 16 +++++++++++---- src/backend/rust.rs | 16 +++++++++++---- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index d4acaf8..5854b83 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -8,29 +8,50 @@ use std::process::Command; use crate::Package; pub use pacman::Pacman; +use rust::Rust; + +pub fn get_backends() -> Vec> { + let pacman = Pacman; + let rust = Rust; + vec![Box::new(pacman), Box::new(rust)] +} + type Switches = &'static [&'static str]; type Binary = &'static str; pub trait Backend { /// The binary that should be called to run the associated package manager. - const BINARY: Binary; + fn get_binary() -> Binary + where + Self: Sized; /// The switches that signals the `BINARY` that the packages should be installed. - const SWITCHES_INSTALL: Switches; + fn get_switches_install() -> Switches + where + Self: Sized; /// The switches that signals the `BINARY` that the packages should be removed. - const SWITCHES_REMOVE: Switches; + fn get_switches_remove() -> Switches + where + Self: Sized; /// Get all packages that are installed in the system. - fn get_all_installed_packages() -> HashSet; + fn get_all_installed_packages() -> HashSet + where + Self: Sized; /// Get all packages that were installed in the system explicitly. - fn get_explicitly_installed_packages() -> HashSet; + fn get_explicitly_installed_packages() -> HashSet + where + Self: Sized; /// Install the specified packages. - fn install_packages(packages: Vec) { - let mut cmd = Command::new(Self::BINARY); - cmd.args(Self::SWITCHES_INSTALL); + fn install_packages(packages: Vec) + where + Self: Sized, + { + let mut cmd = Command::new(Self::get_binary()); + cmd.args(Self::get_switches_install()); for p in packages { cmd.arg(format!("{p}")); } @@ -38,9 +59,12 @@ pub trait Backend { } /// Remove the specified packages. - fn remove_packages(packages: Vec) { - let mut cmd = Command::new(Self::BINARY); - cmd.args(Self::SWITCHES_REMOVE); + fn remove_packages(packages: Vec) + where + Self: Sized, + { + let mut cmd = Command::new(Self::get_binary()); + cmd.args(Self::get_switches_remove()); for p in packages { cmd.arg(format!("{p}")); } diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 1309159..9c535b5 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -9,10 +9,6 @@ use crate::Package; pub struct Pacman; impl Backend for Pacman { - const BINARY: Binary = "paru"; - const SWITCHES_INSTALL: Switches = &["-S"]; - const SWITCHES_REMOVE: Switches = &["-Rsn"]; - fn get_all_installed_packages() -> HashSet { convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) } @@ -20,6 +16,18 @@ impl Backend for Pacman { fn get_explicitly_installed_packages() -> HashSet { convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm()) } + + fn get_binary() -> Binary { + "paru" + } + + fn get_switches_install() -> Switches { + &["-S"] + } + + fn get_switches_remove() -> Switches { + &["-Rsn"] + } } fn get_all_installed_packages_from_alpm() -> HashSet { diff --git a/src/backend/rust.rs b/src/backend/rust.rs index 3d9a501..6f4969d 100644 --- a/src/backend/rust.rs +++ b/src/backend/rust.rs @@ -6,10 +6,6 @@ use crate::Package; pub struct Rust; impl Backend for Rust { - const BINARY: Binary = "cargo"; - const SWITCHES_INSTALL: Switches = &["install"]; - const SWITCHES_REMOVE: Switches = &["uninstall"]; - fn get_all_installed_packages() -> HashSet { extract_packages_names(&run_cargo_install_list()) .map(Package::from) @@ -19,6 +15,18 @@ impl Backend for Rust { fn get_explicitly_installed_packages() -> HashSet { Self::get_all_installed_packages() } + + fn get_binary() -> Binary { + "cargo" + } + + fn get_switches_install() -> Switches { + &["install"] + } + + fn get_switches_remove() -> Switches { + &["uninstall"] + } } fn run_cargo_install_list() -> String {