diff --git a/src/backend/macros.rs b/src/backend/macros.rs new file mode 100644 index 0000000..d13c183 --- /dev/null +++ b/src/backend/macros.rs @@ -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 + } + }; +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 3b4404b..9deed76 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,3 +1,4 @@ +mod macros; mod pacman; mod rust; diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 036ad65..190ca71 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -4,7 +4,7 @@ use alpm::Alpm; use alpm::PackageReason::Explicit; use super::{Backend, Switches, Text}; -use crate::Package; +use crate::{impl_backend_constants, Package}; pub struct Pacman { pub packages: HashSet, @@ -16,21 +16,7 @@ const SWITCHES_INSTALL: Switches = &["-S"]; const SWITCHES_REMOVE: Switches = &["-Rsn"]; impl Backend for Pacman { - 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 - } + impl_backend_constants!(Pacman); fn get_all_installed_packages(&self) -> HashSet { convert_to_pacdef_packages(get_all_installed_packages_from_alpm()) diff --git a/src/backend/rust.rs b/src/backend/rust.rs index 992a038..4cc6586 100644 --- a/src/backend/rust.rs +++ b/src/backend/rust.rs @@ -1,7 +1,7 @@ use std::{collections::HashSet, process::Command}; use super::{Backend, Switches, Text}; -use crate::Package; +use crate::{impl_backend_constants, Package}; pub struct Rust { pub packages: HashSet, @@ -13,21 +13,7 @@ const SWITCHES_INSTALL: Switches = &["install"]; const SWITCHES_REMOVE: Switches = &["uninstall"]; impl Backend for Rust { - 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 - } + impl_backend_constants!(Rust); fn get_all_installed_packages(&self) -> HashSet { extract_packages_names(&run_cargo_install_list())