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; pub use pacman::Pacman;
type Switches = &'static [&'static str]; type Switches = &'static [&'static str];
type Binary = &'static str; type Text = &'static str;
pub enum Backends {
Pacman,
Rust,
}
pub trait Backend { 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. /// 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. /// The switches that signals the `BINARY` that the packages should be installed.
const SWITCHES_INSTALL: Switches; const SWITCHES_INSTALL: Switches;
@@ -46,4 +54,18 @@ pub trait Backend {
} }
cmd.exec(); 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::Alpm;
use alpm::PackageReason::Explicit; use alpm::PackageReason::Explicit;
use super::{Backend, Binary, Switches}; use super::{Backend, Switches, Text};
use crate::Package; use crate::Package;
pub struct Pacman; pub struct Pacman(HashSet<Package>);
impl Backend for Pacman { impl Backend for Pacman {
const BINARY: Binary = "paru"; const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"]; const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"]; const SWITCHES_REMOVE: Switches = &["-Rsn"];
@@ -20,6 +21,12 @@ impl Backend for Pacman {
fn get_explicitly_installed_packages() -> HashSet<Package> { fn get_explicitly_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm()) 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> { 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> { fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
packages.into_iter().map(Package::from).collect() 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 std::{collections::HashSet, process::Command};
use super::{Backend, Binary, Switches}; use super::{Backend, Switches, Text};
use crate::Package; use crate::Package;
pub struct Rust; pub struct Rust(HashSet<Package>);
impl Backend for Rust { impl Backend for Rust {
const BINARY: Binary = "cargo"; const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"]; const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"]; const SWITCHES_REMOVE: Switches = &["uninstall"];
@@ -19,6 +20,12 @@ impl Backend for Rust {
fn get_explicitly_installed_packages() -> HashSet<Package> { fn get_explicitly_installed_packages() -> HashSet<Package> {
Self::get_all_installed_packages() 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 { 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()) .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)] #[cfg(test)]
mod tests { mod tests {
use super::extract_packages_names; use super::extract_packages_names;
+25 -37
View File
@@ -8,15 +8,28 @@ pub struct Package {
repo: Option<String>, repo: Option<String>,
} }
impl From<String> for Package { impl From<&str> for Package {
fn from(mut s: String) -> Self { fn from(s: &str) -> Self {
s.remove_comment(); let trimmed = remove_all_but_package_name(s);
s.remove_whitespace();
let (name, repo) = Self::split_into_name_and_repo(s); let (name, repo) = Self::split_into_name_and_repo(trimmed);
Self { name, repo } Self { name, repo }
} }
} }
impl From<String> for Package {
fn from(value: String) -> Self {
Package::from(value.as_ref())
}
}
fn remove_all_but_package_name(s: &str) -> &str {
s.split('#') // remove comment
.next()
.expect("line contains something")
.trim() // remove whitespace
}
impl Package { impl Package {
pub(crate) fn from_lines( pub(crate) fn from_lines(
lines: impl Iterator<Item = Result<String, std::io::Error>>, lines: impl Iterator<Item = Result<String, std::io::Error>>,
@@ -27,15 +40,11 @@ impl Package {
.collect() .collect()
} }
fn split_into_name_and_repo(mut s: String) -> (String, Option<String>) { fn split_into_name_and_repo(s: &str) -> (String, Option<String>) {
match s.find('/') { let mut iter = s.split('/').rev();
None => (s, None), let name = iter.next().unwrap().to_string();
Some(pos) => { let repo = iter.next().map(|s| s.to_string());
let mut name = s.split_off(pos); (name, repo)
name = name.split_off(1);
(name, Some(s))
}
}
} }
} }
@@ -58,27 +67,6 @@ impl Hash for Package {
} }
} }
trait Whitespace {
fn remove_comment(&mut self) {}
fn remove_whitespace(&mut self) {}
}
impl Whitespace for String {
fn remove_comment(&mut self) {
match self.find('#') {
None => (),
Some(idx) => self.truncate(idx),
}
}
fn remove_whitespace(&mut self) {
match self.find(char::is_whitespace) {
None => (),
Some(idx) => self.truncate(idx),
}
}
}
impl Display for Package { impl Display for Package {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.repo { match &self.repo {
@@ -99,12 +87,12 @@ mod tests {
#[test] #[test]
fn split_into_name_and_repo() { fn split_into_name_and_repo() {
let x = "repo/name".to_string(); let x = "repo/name".to_string();
let (name, repo) = Package::split_into_name_and_repo(x); let (name, repo) = Package::split_into_name_and_repo(&x);
assert_eq!(name, "name"); assert_eq!(name, "name");
assert_eq!(repo, Some("repo".to_string())); assert_eq!(repo, Some("repo".to_string()));
let x = "something".to_string(); let x = "something".to_string();
let (name, repo) = super::Package::split_into_name_and_repo(x); let (name, repo) = super::Package::split_into_name_and_repo(&x);
assert_eq!(name, "something"); assert_eq!(name, "something");
assert_eq!(repo, None); assert_eq!(repo, None);
} }