rework generic backend

This commit is contained in:
steven-omaha
2023-01-06 12:46:10 +01:00
parent 4d5303b93a
commit ef4d0bc723
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;
+25 -37
View File
@@ -8,15 +8,28 @@ pub struct Package {
repo: Option<String>,
}
impl From<String> for Package {
fn from(mut s: String) -> Self {
s.remove_comment();
s.remove_whitespace();
let (name, repo) = Self::split_into_name_and_repo(s);
impl From<&str> for Package {
fn from(s: &str) -> Self {
let trimmed = remove_all_but_package_name(s);
let (name, repo) = Self::split_into_name_and_repo(trimmed);
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 {
pub(crate) fn from_lines(
lines: impl Iterator<Item = Result<String, std::io::Error>>,
@@ -27,15 +40,11 @@ impl Package {
.collect()
}
fn split_into_name_and_repo(mut s: String) -> (String, Option<String>) {
match s.find('/') {
None => (s, None),
Some(pos) => {
let mut name = s.split_off(pos);
name = name.split_off(1);
(name, Some(s))
}
}
fn split_into_name_and_repo(s: &str) -> (String, Option<String>) {
let mut iter = s.split('/').rev();
let name = iter.next().unwrap().to_string();
let repo = iter.next().map(|s| s.to_string());
(name, repo)
}
}
@@ -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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.repo {
@@ -99,12 +87,12 @@ mod tests {
#[test]
fn split_into_name_and_repo() {
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!(repo, Some("repo".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!(repo, None);
}