add more generic backend stuff

This commit is contained in:
Dr. Matthias Ratajczak
2023-01-05 17:57:38 +01:00
parent 4c22f1cc89
commit 265d67089b
3 changed files with 59 additions and 19 deletions
+35 -11
View File
@@ -8,29 +8,50 @@ use std::process::Command;
use crate::Package;
pub use pacman::Pacman;
use rust::Rust;
pub fn get_backends() -> Vec<Box<dyn Backend>> {
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<Package>;
fn get_all_installed_packages() -> HashSet<Package>
where
Self: Sized;
/// Get all packages that were installed in the system explicitly.
fn get_explicitly_installed_packages() -> HashSet<Package>;
fn get_explicitly_installed_packages() -> HashSet<Package>
where
Self: Sized;
/// Install the specified packages.
fn install_packages(packages: Vec<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.args(Self::SWITCHES_INSTALL);
fn install_packages(packages: Vec<Package>)
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<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.args(Self::SWITCHES_REMOVE);
fn remove_packages(packages: Vec<Package>)
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}"));
}