rework generic backend

This commit is contained in:
timeshifter
2023-01-06 12:46:10 +01:00
parent 0dcf3822eb
commit 9d5241afa5
4 changed files with 93 additions and 45 deletions
+24 -2
View File
@@ -9,11 +9,19 @@ use crate::Package;
pub use pacman::Pacman;
type Switches = &'static [&'static str];
type Binary = &'static str;
type Text = &'static str;
pub enum Backends {
Pacman,
Rust,
}
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: Binary;
const BINARY: Text;
/// The switches that signals the `BINARY` that the packages should be installed.
const SWITCHES_INSTALL: Switches;
@@ -46,4 +54,18 @@ pub trait Backend {
}
cmd.exec();
}
/// extract packages from its own section as read from group files
fn extract_packages_from_group_file_content(content: &str) -> HashSet<Package> {
content
.lines()
.skip_while(|line| !line.starts_with(&format!("[{}]", Self::SECTION)))
.skip(1)
.filter(|line| !line.starts_with('['))
.fuse()
.map(Package::from)
.collect()
}
fn add_packages(&mut self, packages: HashSet<Package>);
}
+22 -3
View File
@@ -3,13 +3,14 @@ use std::collections::HashSet;
use alpm::Alpm;
use alpm::PackageReason::Explicit;
use super::{Backend, Binary, Switches};
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Pacman;
pub struct Pacman(HashSet<Package>);
impl Backend for Pacman {
const BINARY: Binary = "paru";
const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
@@ -20,6 +21,12 @@ impl Backend for Pacman {
fn get_explicitly_installed_packages() -> 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);
}
}
}
fn get_all_installed_packages_from_alpm() -> HashSet<String> {
@@ -44,3 +51,15 @@ fn get_explicitly_installed_packages_from_alpm() -> HashSet<String> {
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
packages.into_iter().map(Package::from).collect()
}
impl Pacman {
pub fn new() -> Self {
Self(HashSet::new())
}
}
impl Default for Pacman {
fn default() -> Self {
Self::new()
}
}
+22 -3
View File
@@ -1,12 +1,13 @@
use std::{collections::HashSet, process::Command};
use super::{Backend, Binary, Switches};
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Rust;
pub struct Rust(HashSet<Package>);
impl Backend for Rust {
const BINARY: Binary = "cargo";
const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
@@ -19,6 +20,12 @@ impl Backend for Rust {
fn get_explicitly_installed_packages() -> HashSet<Package> {
Self::get_all_installed_packages()
}
fn add_packages(&mut self, packages: HashSet<Package>) {
for p in packages {
self.0.insert(p);
}
}
}
fn run_cargo_install_list() -> String {
@@ -37,6 +44,18 @@ fn extract_packages_names(output: &str) -> impl Iterator<Item = String> + '_ {
.map(|line| line.split_whitespace().next().unwrap().to_owned())
}
impl Rust {
pub fn new() -> Self {
Self(HashSet::new())
}
}
impl Default for Rust {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::extract_packages_names;