major overhaul of Backend from dynamic to static dispatch using an big enum

This commit is contained in:
ripytide
2024-04-22 14:08:53 +01:00
parent 636a7a02ec
commit df5b229d23
36 changed files with 382 additions and 590 deletions
+17 -13
View File
@@ -12,9 +12,23 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Arch {
pub(crate) binary: String,
pub(crate) aur_rm_args: Vec<String>,
pub(crate) packages: HashSet<Package>,
pub binary: String,
pub aur_rm_args: Vec<String>,
pub packages: HashSet<Package>,
}
impl Arch {
pub fn new() -> Self {
Self {
binary: BINARY.to_string(),
aur_rm_args: vec![],
packages: HashSet::new(),
}
}
}
impl Default for Arch {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "pacman";
@@ -117,13 +131,3 @@ fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
fn get_db_handle() -> Result<Alpm> {
Alpm::new("/", "/var/lib/pacman").context("connecting to DB using expected default values")
}
impl Arch {
pub(crate) fn new() -> Self {
Self {
binary: BINARY.to_string(),
aur_rm_args: vec![],
packages: HashSet::new(),
}
}
}
+13 -9
View File
@@ -12,7 +12,19 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Debian {
pub(crate) packages: HashSet<Package>,
pub packages: HashSet<Package>,
}
impl Debian {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Default for Debian {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "apt";
@@ -94,11 +106,3 @@ impl Backend for Debian {
run_external_command(cmd)
}
}
impl Debian {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
+13 -9
View File
@@ -10,7 +10,19 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Fedora {
pub(crate) packages: HashSet<Package>,
pub packages: HashSet<Package>,
}
impl Fedora {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Default for Fedora {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "dnf";
@@ -123,14 +135,6 @@ impl Backend for Fedora {
}
}
impl Fedora {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
fn create_package(package: &str) -> Package {
if DEFAULT_REPOS.iter().any(|repo| package.contains(repo)) && !package.contains("copr") {
let package = package.split('/').nth(1).expect("Cannot be empty!");
+40 -36
View File
@@ -10,8 +10,46 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Flatpak {
pub(crate) packages: HashSet<Package>,
pub(crate) systemwide: bool,
pub packages: HashSet<Package>,
pub systemwide: bool,
}
impl Flatpak {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
systemwide: true,
}
}
fn get_switches_runtime(&self) -> Switches {
if self.systemwide {
&[]
} else {
&["--user"]
}
}
fn get_installed_packages(&self, include_implicit: bool) -> Result<HashSet<Package>> {
let mut cmd = Command::new(BINARY);
cmd.args(["list", "--columns=application"]);
if !include_implicit {
cmd.arg("--app");
}
if !self.systemwide {
cmd.arg("--user");
}
let output = String::from_utf8(cmd.output()?.stdout)?;
Ok(output
.lines()
.map(Package::from)
.collect::<HashSet<Package>>())
}
}
impl Default for Flatpak {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "flatpak";
@@ -84,37 +122,3 @@ impl Backend for Flatpak {
run_external_command(cmd)
}
}
impl Flatpak {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
systemwide: true,
}
}
fn get_switches_runtime(&self) -> Switches {
if self.systemwide {
&[]
} else {
&["--user"]
}
}
fn get_installed_packages(&self, include_implicit: bool) -> Result<HashSet<Package>> {
let mut cmd = Command::new(BINARY);
cmd.args(["list", "--columns=application"]);
if !include_implicit {
cmd.arg("--app");
}
if !self.systemwide {
cmd.arg("--user");
}
let output = String::from_utf8(cmd.output()?.stdout)?;
Ok(output
.lines()
.map(Package::from)
.collect::<HashSet<Package>>())
}
}
+44 -40
View File
@@ -9,10 +9,52 @@ use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::{Group, Package};
macro_rules! ERROR{
($bin:expr) => {
panic!("Cannot use {} for package management in python. Please use a valid package manager like pip or pipx.", $bin)
};
}
#[derive(Debug, Clone)]
pub struct Python {
pub(crate) binary: String,
pub(crate) packages: HashSet<Package>,
pub binary: String,
pub packages: HashSet<Package>,
}
impl Python {
pub fn new() -> Self {
Self {
binary: BINARY.to_string(),
packages: HashSet::new(),
}
}
fn get_switches_runtime(&self) -> Switches {
match self.get_binary() {
"pip" => &["list", "--format", "json", "--not-required", "--user"],
"pipx" => &["list", "--json"],
_ => ERROR!(self.get_binary()),
}
}
fn get_switches_explicit(&self) -> Switches {
match self.get_binary() {
"pip" => &["list", "--format", "json", "--user"],
"pipx" => &["list", "--json"],
_ => ERROR!(self.get_binary()),
}
}
fn extract_packages(&self, output: Value) -> Result<HashSet<Package>> {
match self.get_binary() {
"pip" => extract_pacdef_packages(output),
"pipx" => extract_pacdef_packages_pipx(output),
_ => ERROR!(self.get_binary()),
}
}
}
impl Default for Python {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "pip";
@@ -26,12 +68,6 @@ const SWITCHES_REMOVE: Switches = &["uninstall"];
const SUPPORTS_AS_DEPENDENCY: bool = false;
macro_rules! ERROR{
($bin:expr) => {
panic!("Cannot use {} for package management in python. Please use a valid package manager like pip or pipx.", $bin)
};
}
impl Backend for Python {
impl_backend_constants!();
@@ -64,38 +100,6 @@ fn run_pip_command(cmd: &mut Command, args: &[&str]) -> Result<Value> {
Ok(val)
}
impl Python {
pub(crate) fn new() -> Self {
Self {
binary: BINARY.to_string(),
packages: HashSet::new(),
}
}
fn get_switches_runtime(&self) -> Switches {
match self.get_binary() {
"pip" => &["list", "--format", "json", "--not-required", "--user"],
"pipx" => &["list", "--json"],
_ => ERROR!(self.get_binary()),
}
}
fn get_switches_explicit(&self) -> Switches {
match self.get_binary() {
"pip" => &["list", "--format", "json", "--user"],
"pipx" => &["list", "--json"],
_ => ERROR!(self.get_binary()),
}
}
fn extract_packages(&self, output: Value) -> Result<HashSet<Package>> {
match self.get_binary() {
"pip" => extract_pacdef_packages(output),
"pipx" => extract_pacdef_packages_pipx(output),
_ => ERROR!(self.get_binary()),
}
}
}
fn extract_pacdef_packages(value: Value) -> Result<HashSet<Package>> {
let result = value
.as_array()
+13 -9
View File
@@ -12,7 +12,19 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Rust {
pub(crate) packages: HashSet<Package>,
pub packages: HashSet<Package>,
}
impl Rust {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Default for Rust {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "cargo";
@@ -76,14 +88,6 @@ fn extract_packages(json: &Value) -> Result<HashSet<Package>> {
Ok(result)
}
impl Rust {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
fn get_crates_file() -> Result<PathBuf> {
let mut result = crate::path::get_cargo_home().context("getting cargo home dir")?;
result.push(".crates2.json");
+17 -1
View File
@@ -12,9 +12,25 @@ use std::process::Command;
use self::helpers::{
group_components_by_toolchains, install_components, toolchain_of_component_was_already_removed,
};
pub use self::types::Rustup;
use self::types::{Repotype, RustupPackage};
#[derive(Debug, Clone)]
pub struct Rustup {
pub packages: HashSet<Package>,
}
impl Rustup {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Default for Rustup {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "rustup";
const SECTION: Text = "rustup";
@@ -1,13 +1,7 @@
use anyhow::{bail, Context, Result};
use std::collections::HashSet;
use crate::{backend::backend_trait::Switches, Package};
#[derive(Debug, Clone)]
pub struct Rustup {
pub(crate) packages: HashSet<Package>,
}
#[derive(Debug)]
pub enum Repotype {
Toolchain,
@@ -28,14 +22,6 @@ pub struct RustupPackage {
pub component: Option<String>,
}
impl Rustup {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Repotype {
fn try_from<T>(value: T) -> Result<Self>
where
+13 -9
View File
@@ -12,7 +12,19 @@ use crate::{Group, Package};
#[derive(Debug, Clone)]
pub struct Void {
pub(crate) packages: HashSet<Package>,
pub packages: HashSet<Package>,
}
impl Void {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Default for Void {
fn default() -> Self {
Self::new()
}
}
const BINARY: Text = "xbps-install";
@@ -124,11 +136,3 @@ impl Backend for Void {
run_external_command(cmd)
}
}
impl Void {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}