get generic backend to work (to some extent)
This commit is contained in:
+57
-23
@@ -8,37 +8,71 @@ use std::process::Command;
|
||||
use crate::Package;
|
||||
|
||||
pub use pacman::Pacman;
|
||||
type Switches = &'static [&'static str];
|
||||
type Text = &'static str;
|
||||
pub use rust::Rust;
|
||||
pub type Switches = &'static [&'static str];
|
||||
pub type Text = &'static str;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Backends {
|
||||
Pacman,
|
||||
Rust,
|
||||
}
|
||||
|
||||
impl Backends {
|
||||
pub fn iter() -> BackendIter {
|
||||
BackendIter(Some(Self::Pacman))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BackendIter(Option<Backends>);
|
||||
|
||||
impl Iterator for BackendIter {
|
||||
type Item = Box<dyn Backend>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.0 {
|
||||
Some(Backends::Pacman) => {
|
||||
self.0 = Some(Backends::Rust);
|
||||
Some(Box::new(Pacman::new()))
|
||||
}
|
||||
Some(Backends::Rust) => {
|
||||
self.0 = None;
|
||||
Some(Box::new(Rust::new()))
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Backends {
|
||||
pub fn get(&self) -> Box<dyn Backend> {
|
||||
match self {
|
||||
Self::Pacman => Box::new(Pacman {
|
||||
packages: HashSet::new(),
|
||||
}),
|
||||
Self::Rust => Box::new(Rust {
|
||||
packages: HashSet::new(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Backend {
|
||||
/// The name that introduces the section in the group file
|
||||
const SECTION: Text;
|
||||
|
||||
/// The binary that should be called to run the associated package manager.
|
||||
const BINARY: Text;
|
||||
|
||||
/// The switches that signals the `BINARY` that the packages should be installed.
|
||||
const SWITCHES_INSTALL: Switches;
|
||||
|
||||
/// The switches that signals the `BINARY` that the packages should be removed.
|
||||
const SWITCHES_REMOVE: Switches;
|
||||
fn get_binary(&self) -> Text;
|
||||
fn get_section(&self) -> Text;
|
||||
fn get_switches_install(&self) -> Switches;
|
||||
fn get_switches_remove(&self) -> Switches;
|
||||
|
||||
/// Get all packages that are installed in the system.
|
||||
fn get_all_installed_packages() -> HashSet<Package>;
|
||||
fn get_all_installed_packages(&self) -> HashSet<Package>;
|
||||
|
||||
/// Get all packages that were installed in the system explicitly.
|
||||
fn get_explicitly_installed_packages() -> HashSet<Package>;
|
||||
fn get_explicitly_installed_packages(&self) -> HashSet<Package>;
|
||||
|
||||
/// 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(&self, packages: Vec<Package>) {
|
||||
let mut cmd = Command::new(self.get_binary());
|
||||
cmd.args(self.get_switches_install());
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
@@ -46,9 +80,9 @@ 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(&self, packages: Vec<Package>) {
|
||||
let mut cmd = Command::new(self.get_binary());
|
||||
cmd.args(self.get_switches_remove());
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
@@ -56,10 +90,10 @@ pub trait Backend {
|
||||
}
|
||||
|
||||
/// extract packages from its own section as read from group files
|
||||
fn extract_packages_from_group_file_content(content: &str) -> HashSet<Package> {
|
||||
fn extract_packages_from_group_file_content(&self, content: &str) -> HashSet<Package> {
|
||||
content
|
||||
.lines()
|
||||
.skip_while(|line| !line.starts_with(&format!("[{}]", Self::SECTION)))
|
||||
.skip_while(|line| !line.starts_with(&format!("[{}]", self.get_section())))
|
||||
.skip(1)
|
||||
.filter(|line| !line.starts_with('['))
|
||||
.fuse()
|
||||
|
||||
Reference in New Issue
Block a user