From 9d5241afa5dbba8b3e8f84106c6bd4881c548f68 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Fri, 6 Jan 2023 12:46:10 +0100 Subject: [PATCH] rework generic backend --- src/backend/mod.rs | 26 ++++++++++++++++-- src/backend/pacman.rs | 25 ++++++++++++++--- src/backend/rust.rs | 25 ++++++++++++++--- src/package.rs | 62 +++++++++++++++++-------------------------- 4 files changed, 93 insertions(+), 45 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index d4acaf8..5c1533e 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -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 { + 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); } diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 1309159..611eccf 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -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); 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 { convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm()) } + + fn add_packages(&mut self, packages: HashSet) { + for p in packages { + self.0.insert(p); + } + } } fn get_all_installed_packages_from_alpm() -> HashSet { @@ -44,3 +51,15 @@ fn get_explicitly_installed_packages_from_alpm() -> HashSet { fn convert_to_pacdef_packages(packages: HashSet) -> HashSet { 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() + } +} diff --git a/src/backend/rust.rs b/src/backend/rust.rs index 3d9a501..97e1d9e 100644 --- a/src/backend/rust.rs +++ b/src/backend/rust.rs @@ -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); 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 { Self::get_all_installed_packages() } + + fn add_packages(&mut self, packages: HashSet) { + 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 + '_ { .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; diff --git a/src/package.rs b/src/package.rs index ea7ad7d..bb4618f 100644 --- a/src/package.rs +++ b/src/package.rs @@ -8,15 +8,28 @@ pub struct Package { repo: Option, } -impl From 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 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>, @@ -27,15 +40,11 @@ impl Package { .collect() } - fn split_into_name_and_repo(mut s: String) -> (String, Option) { - 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) { + 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); }