From 741c9f06c26bd42168f2d1ff6fd9a5ab096fd651 Mon Sep 17 00:00:00 2001 From: ripytide Date: Thu, 14 Nov 2024 14:54:47 +0000 Subject: [PATCH] refactor: separate out upper and lower backend meta-variables --- CHANGELOG.md | 2 +- src/backends/all.rs | 94 ++++++++++++++++++++++----------------------- src/backends/mod.rs | 12 +++++- src/core.rs | 6 +-- src/groups.rs | 26 ++++++------- 5 files changed, 73 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 858c05d..32a177c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. ### Fixed -- Fixed WinGet Command not working (#49) +- Fixed WinGet commands not working (#49) - Fixed `metapac unmanaged` output backend names in lowercase (#49) ## [0.2.2] - 2024-11-10 diff --git a/src/backends/all.rs b/src/backends/all.rs index 0d10959..11a18b8 100644 --- a/src/backends/all.rs +++ b/src/backends/all.rs @@ -5,44 +5,44 @@ use crate::prelude::*; use color_eyre::Result; macro_rules! append { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { pub fn append(&mut self, other: &mut Self) { $( - self.$backend.append(&mut other.$backend); + self.$lower_backend.append(&mut other.$lower_backend); )* } }; } macro_rules! is_empty { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { pub fn is_empty(&self) -> bool { $( - self.$backend.is_empty() && + self.$lower_backend.is_empty() && )* true } }; } macro_rules! to_package_ids { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { pub fn to_package_ids(&self) -> PackageIds { PackageIds { - $( $backend: self.$backend.keys().cloned().collect() ),* + $( $lower_backend: self.$lower_backend.keys().cloned().collect() ),* } } }; } macro_rules! any { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::FromStr, derive_more::Display)] pub enum AnyBackend { - $($backend,)* + $($upper_backend,)* } impl AnyBackend { pub fn remove_packages(&self, packages: &BTreeSet, no_confirm: bool, config: &Config) -> Result<()> { match self { - $( AnyBackend::$backend => $backend::remove_packages(packages, no_confirm, config), )* + $( AnyBackend::$upper_backend => $upper_backend::remove_packages(packages, no_confirm, config), )* } } } @@ -51,18 +51,17 @@ macro_rules! any { apply_public_backends!(any); macro_rules! raw_package_ids { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] - #[allow(non_snake_case)] pub struct RawPackageIds { $( - pub $backend: Vec, + pub $lower_backend: Vec, )* } impl RawPackageIds { pub fn contains(&self, backend: AnyBackend, package: &str) -> bool { match backend { - $( AnyBackend::$backend => self.$backend.iter().any(|p| p == package) ),* + $( AnyBackend::$upper_backend => self.$lower_backend.iter().any(|p| p == package) ),* } } } @@ -71,28 +70,27 @@ macro_rules! raw_package_ids { apply_public_backends!(raw_package_ids); macro_rules! package_ids { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default, Serialize)] - #[allow(non_snake_case)] pub struct PackageIds { $( #[serde(skip_serializing_if = "BTreeSet::is_empty")] - pub $backend: BTreeSet, + pub $lower_backend: BTreeSet, )* } impl PackageIds { - append!($($backend),*); - is_empty!($($backend),*); + append!($(($upper_backend, $lower_backend)),*); + is_empty!($(($upper_backend, $lower_backend)),*); pub fn contains(&self, backend: AnyBackend, package: &str) -> bool { match backend { - $( AnyBackend::$backend => self.$backend.contains(package) ),* + $( AnyBackend::$upper_backend => self.$lower_backend.contains(package) ),* } } pub fn remove(&mut self, backend: AnyBackend, package: &str) -> bool { match backend { - $( AnyBackend::$backend => self.$backend.remove(package) ),* + $( AnyBackend::$upper_backend => self.$lower_backend.remove(package) ),* } } @@ -100,7 +98,7 @@ macro_rules! package_ids { let mut output = Self::default(); $( - output.$backend = self.$backend.difference(&other.$backend).cloned().collect(); + output.$lower_backend = self.$lower_backend.difference(&other.$lower_backend).cloned().collect(); )* output @@ -108,8 +106,8 @@ macro_rules! package_ids { pub fn remove_packages(&self, no_confirm: bool, config: &Config) -> Result<()> { $( - if is_enabled(AnyBackend::$backend, config) { - AnyBackend::$backend.remove_packages(&self.$backend, no_confirm, config)?; + if is_enabled(AnyBackend::$upper_backend, config) { + AnyBackend::$upper_backend.remove_packages(&self.$lower_backend, no_confirm, config)?; } )* @@ -119,9 +117,9 @@ macro_rules! package_ids { impl std::fmt::Display for PackageIds { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { $( - if !self.$backend.is_empty() { - writeln!(f, "[{}]", AnyBackend::$backend)?; - for package_id in self.$backend.iter() { + if !self.$lower_backend.is_empty() { + writeln!(f, "[{}]", AnyBackend::$upper_backend)?; + for package_id in self.$lower_backend.iter() { writeln!(f, "{package_id}")?; } writeln!(f)?; @@ -136,25 +134,24 @@ macro_rules! package_ids { apply_public_backends!(package_ids); macro_rules! query_infos { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] - #[allow(non_snake_case)] pub struct QueryInfos { $( - pub $backend: BTreeMap::QueryInfo>, + pub $lower_backend: BTreeMap::QueryInfo>, )* } impl QueryInfos { - append!($($backend),*); - is_empty!($($backend),*); - to_package_ids!($($backend),*); + append!($(($upper_backend, $lower_backend)),*); + is_empty!($(($upper_backend, $lower_backend)),*); + to_package_ids!($(($upper_backend, $lower_backend)),*); pub fn query_installed_packages(config: &Config) -> Result { Ok(Self { $( - $backend: - if is_enabled(AnyBackend::$backend, config) { - $backend::query_installed_packages(config)? + $lower_backend: + if is_enabled(AnyBackend::$upper_backend, config) { + $upper_backend::query_installed_packages(config)? } else { Default::default() }, @@ -167,20 +164,19 @@ macro_rules! query_infos { apply_public_backends!(query_infos); macro_rules! raw_install_options { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] - #[allow(non_snake_case)] pub struct RawInstallOptions { $( - pub $backend: Vec<(String, <$backend as Backend>::InstallOptions)>, + pub $lower_backend: Vec<(String, <$upper_backend as Backend>::InstallOptions)>, )* } impl RawInstallOptions { - append!($($backend),*); + append!($(($upper_backend, $lower_backend)),*); pub fn to_raw_package_ids(&self) -> RawPackageIds { RawPackageIds { - $( $backend: self.$backend.iter().map(|(x, _)| x).cloned().collect() ),* + $( $lower_backend: self.$lower_backend.iter().map(|(x, _)| x).cloned().collect() ),* } } } @@ -189,23 +185,23 @@ macro_rules! raw_install_options { apply_public_backends!(raw_install_options); macro_rules! install_options { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { #[derive(Debug, Clone, Default)] #[allow(non_snake_case)] pub struct InstallOptions { $( - pub $backend: BTreeMap::InstallOptions>, + pub $lower_backend: BTreeMap::InstallOptions>, )* } impl InstallOptions { - append!($($backend),*); - is_empty!($($backend),*); - to_package_ids!($($backend),*); + append!($(($upper_backend, $lower_backend)),*); + is_empty!($(($upper_backend, $lower_backend)),*); + to_package_ids!($(($upper_backend, $lower_backend)),*); pub fn map_install_packages(mut self, config: &Config) -> Result { $( - if is_enabled(AnyBackend::$backend, config) { - self.$backend = $backend::map_managed_packages(self.$backend, config)?; + if is_enabled(AnyBackend::$upper_backend, config) { + self.$lower_backend = $upper_backend::map_managed_packages(self.$lower_backend, config)?; } )* @@ -214,8 +210,8 @@ macro_rules! install_options { pub fn install_packages(self, no_confirm: bool, config: &Config) -> Result<()> { $( - if is_enabled(AnyBackend::$backend, config) { - $backend::install_packages(&self.$backend, no_confirm, config)?; + if is_enabled(AnyBackend::$upper_backend, config) { + $upper_backend::install_packages(&self.$lower_backend, no_confirm, config)?; } )* diff --git a/src/backends/mod.rs b/src/backends/mod.rs index dc64bf7..b5820aa 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -18,7 +18,17 @@ use serde::{Deserialize, Serialize}; macro_rules! apply_public_backends { ($macro:ident) => { - $macro! { Arch, Apt, Brew, Cargo, Dnf, Flatpak, Pipx, Rustup, WinGet, Xbps } + $macro! { + (Arch, arch), + (Apt, apt), + (Brew, brew), + (Cargo, cargo), + (Dnf, dnf), + (Flatpak, flatpak), + (Pipx, pipx), + (Rustup, rustup), + (WinGet, winget), + (Xbps, xbps) } }; } pub(crate) use apply_public_backends; diff --git a/src/core.rs b/src/core.rs index 6978b70..d72a720 100644 --- a/src/core.rs +++ b/src/core.rs @@ -180,10 +180,10 @@ fn missing(managed: &InstallOptions, config: &Config) -> Result let mut missing = managed.clone(); macro_rules! x { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { $( - for package_id in installed.$backend { - missing.$backend.remove(&package_id); + for package_id in installed.$lower_backend { + missing.$lower_backend.remove(&package_id); } )* }; diff --git a/src/groups.rs b/src/groups.rs index 5559c55..e07d211 100644 --- a/src/groups.rs +++ b/src/groups.rs @@ -37,11 +37,11 @@ impl Groups { let raw_package_ids = raw_install_options.to_raw_package_ids(); macro_rules! x { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { $( - for package_id in raw_package_ids.$backend { + for package_id in raw_package_ids.$lower_backend { reoriented - .entry((AnyBackend::$backend, package_id.clone())) + .entry((AnyBackend::$upper_backend, package_id.clone())) .or_default() .entry(group_file.clone()) .or_default() @@ -68,11 +68,11 @@ impl Groups { let mut install_options = InstallOptions::default(); macro_rules! x { - ($($backend:ident),*) => { - $( - install_options.$backend = merged_raw_install_options.$backend.into_iter().collect(); - )* - }; + ($(($upper_backend:ident, $lower_backend:ident)),*) => { + $( + install_options.$lower_backend = merged_raw_install_options.$lower_backend.into_iter().collect(); + )* + }; } apply_public_backends!(x); @@ -135,13 +135,13 @@ fn parse_group_file(group_file: &Path, contents: &str) -> Result Result { macro_rules! x { - ($($backend:ident),*) => { + ($(($upper_backend:ident, $lower_backend:ident)),*) => { $( - if key.to_lowercase() == $backend.to_string().to_lowercase() { + if key.to_lowercase() == $upper_backend.to_string().to_lowercase() { let mut raw_install_options = RawInstallOptions::default(); let packages = value.as_array().ok_or( - eyre!("the {} backend in the {group_file:?} group file has a non-array value", $backend) + eyre!("the {} backend in the {group_file:?} group file has a non-array value", $upper_backend) )?; for package_id in packages { @@ -152,10 +152,10 @@ fn parse_toml_key_value(group_file: &Path, key: &str, value: &Value) -> Result()?.package, x.clone().try_into()?, ), - _ => return Err(eyre!("the {} backend in the {group_file:?} group file has a package which is neither a string or a table", $backend)), + _ => return Err(eyre!("the {} backend in the {group_file:?} group file has a package which is neither a string or a table", $upper_backend)), }; - raw_install_options.$backend.push((package_id, package_install_options)); + raw_install_options.$lower_backend.push((package_id, package_install_options)); } return Ok(raw_install_options);