get generic backend to work (to some extent)

This commit is contained in:
timeshifter
2023-01-06 13:31:50 +01:00
parent 9d5241afa5
commit cb5c53c1c5
6 changed files with 218 additions and 120 deletions
+57 -23
View File
@@ -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()
+29 -9
View File
@@ -6,25 +6,43 @@ use alpm::PackageReason::Explicit;
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Pacman(HashSet<Package>);
pub struct Pacman {
pub packages: HashSet<Package>,
}
const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
impl Backend for Pacman {
const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
fn get_binary(&self) -> Text {
BINARY
}
fn get_all_installed_packages() -> HashSet<Package> {
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
}
fn get_explicitly_installed_packages() -> HashSet<Package> {
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm())
}
fn add_packages(&mut self, packages: HashSet<Package>) {
for p in packages {
self.0.insert(p);
self.packages.insert(p);
}
}
}
@@ -54,7 +72,9 @@ fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
impl Pacman {
pub fn new() -> Self {
Self(HashSet::new())
Self {
packages: HashSet::new(),
}
}
}
+30 -10
View File
@@ -3,27 +3,45 @@ use std::{collections::HashSet, process::Command};
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Rust(HashSet<Package>);
pub struct Rust {
pub packages: HashSet<Package>,
}
const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
impl Backend for Rust {
const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
fn get_binary(&self) -> Text {
BINARY
}
fn get_all_installed_packages() -> HashSet<Package> {
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> {
extract_packages_names(&run_cargo_install_list())
.map(Package::from)
.collect()
}
fn get_explicitly_installed_packages() -> HashSet<Package> {
Self::get_all_installed_packages()
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
self.get_all_installed_packages()
}
fn add_packages(&mut self, packages: HashSet<Package>) {
for p in packages {
self.0.insert(p);
self.packages.insert(p);
}
}
}
@@ -46,7 +64,9 @@ fn extract_packages_names(output: &str) -> impl Iterator<Item = String> + '_ {
impl Rust {
pub fn new() -> Self {
Self(HashSet::new())
Self {
packages: HashSet::new(),
}
}
}