implement generic backend methods with a macro

This commit is contained in:
timeshifter
2023-01-09 17:23:34 +01:00
parent 098ab765b3
commit 20a37c4d5f
4 changed files with 25 additions and 32 deletions
+20
View File
@@ -0,0 +1,20 @@
#[macro_export]
macro_rules! impl_backend_constants {
($name:ident) => {
fn get_binary(&self) -> Text {
BINARY
}
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
};
}
+1
View File
@@ -1,3 +1,4 @@
mod macros;
mod pacman; mod pacman;
mod rust; mod rust;
+2 -16
View File
@@ -4,7 +4,7 @@ use alpm::Alpm;
use alpm::PackageReason::Explicit; use alpm::PackageReason::Explicit;
use super::{Backend, Switches, Text}; use super::{Backend, Switches, Text};
use crate::Package; use crate::{impl_backend_constants, Package};
pub struct Pacman { pub struct Pacman {
pub packages: HashSet<Package>, pub packages: HashSet<Package>,
@@ -16,21 +16,7 @@ const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"]; const SWITCHES_REMOVE: Switches = &["-Rsn"];
impl Backend for Pacman { impl Backend for Pacman {
fn get_binary(&self) -> Text { impl_backend_constants!(Pacman);
BINARY
}
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> { fn get_all_installed_packages(&self) -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
+2 -16
View File
@@ -1,7 +1,7 @@
use std::{collections::HashSet, process::Command}; use std::{collections::HashSet, process::Command};
use super::{Backend, Switches, Text}; use super::{Backend, Switches, Text};
use crate::Package; use crate::{impl_backend_constants, Package};
pub struct Rust { pub struct Rust {
pub packages: HashSet<Package>, pub packages: HashSet<Package>,
@@ -13,21 +13,7 @@ const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"]; const SWITCHES_REMOVE: Switches = &["uninstall"];
impl Backend for Rust { impl Backend for Rust {
fn get_binary(&self) -> Text { impl_backend_constants!(Rust);
BINARY
}
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> { fn get_all_installed_packages(&self) -> HashSet<Package> {
extract_packages_names(&run_cargo_install_list()) extract_packages_names(&run_cargo_install_list())