From b4ad51b1059230371de778db58263abe35460383 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 9 Apr 2024 19:51:50 +0200 Subject: [PATCH] refact: todo_per_backend --- .../src/backend/todo_per_backend.rs | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/crates/pacdef_core/src/backend/todo_per_backend.rs b/crates/pacdef_core/src/backend/todo_per_backend.rs index 973309c..e74f840 100644 --- a/crates/pacdef_core/src/backend/todo_per_backend.rs +++ b/crates/pacdef_core/src/backend/todo_per_backend.rs @@ -35,41 +35,25 @@ impl ToDoPerBackend { } pub(crate) fn install_missing_packages(&self, noconfirm: bool) -> Result<()> { - self.handle_backend_command( - Backend::install_packages, - noconfirm, - "install", - "installing", - ) - .context("installing packages") - } - - pub(crate) fn remove_unmanaged_packages(&self, noconfirm: bool) -> Result<()> { - self.handle_backend_command(Backend::remove_packages, noconfirm, "remove", "removing") - .context("removing packages") - } - - /// # Todo - /// - /// refactor this! preferably inline it to all callers - fn handle_backend_command<'a, F>( - &'a self, - func: F, - noconfirm: bool, - verb: &'_ str, - verb_continuous: &'_ str, - ) -> Result<()> - where - F: Fn(&'a dyn Backend, &'a [Package], bool) -> Result<()>, - { for (backend, packages) in &self.0 { if packages.is_empty() { continue; } - func(&**backend, packages, noconfirm).with_context(|| { - format!("{verb_continuous} packages for {}", backend.get_section()) - })?; + Backend::install_packages(&**backend, packages, noconfirm) + .with_context(|| format!("installing packages for {}", backend.get_section()))?; + } + Ok(()) + } + + pub(crate) fn remove_unmanaged_packages(&self, noconfirm: bool) -> Result<()> { + for (backend, packages) in &self.0 { + if packages.is_empty() { + continue; + } + + Backend::remove_packages(&**backend, packages, noconfirm) + .with_context(|| format!("removing packages for {}", backend.get_section()))?; } Ok(()) }