From 96265648e93910242d593a9e4b91b9dd3b9b4901 Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Tue, 10 Jan 2023 18:17:12 +0100 Subject: [PATCH] rework backend internals --- src/backend/mod.rs | 4 ++-- src/backend/pacman.rs | 53 +++++++++++++++++++++++-------------------- src/backend/rust.rs | 14 ++++-------- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7e62f79..248abf5 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -13,8 +13,8 @@ use crate::{Group, Package}; pub(crate) use todo_per_backend::ToDoPerBackend; -pub use pacman::Pacman; -pub use rust::Rust; +pub(crate) use pacman::Pacman; +pub(crate) use rust::Rust; pub(in crate::backend) type Switches = &'static [&'static str]; pub(in crate::backend) type Text = &'static str; diff --git a/src/backend/pacman.rs b/src/backend/pacman.rs index 519c680..ccd2986 100644 --- a/src/backend/pacman.rs +++ b/src/backend/pacman.rs @@ -2,13 +2,13 @@ use std::collections::HashSet; use alpm::Alpm; use alpm::PackageReason::Explicit; -use anyhow::Result; +use anyhow::{Context, Result}; use super::{Backend, Switches, Text}; use crate::{impl_backend_constants, Group, Package}; -pub struct Pacman { - pub packages: HashSet, +pub(crate) struct Pacman { + pub(crate) packages: HashSet, } const BINARY: Text = "paru"; @@ -20,51 +20,56 @@ impl Backend for Pacman { impl_backend_constants!(); fn get_all_installed_packages(&self) -> Result> { - Ok(convert_to_pacdef_packages( - get_all_installed_packages_from_alpm(), - )) + let alpm_packages = get_all_installed_packages_from_alpm() + .context("getting all installed packages from alpm")?; + + let result = convert_to_pacdef_packages(alpm_packages); + Ok(result) } fn get_explicitly_installed_packages(&self) -> Result> { - Ok(convert_to_pacdef_packages( - get_explicitly_installed_packages_from_alpm(), - )) + let alpm_packages = get_explicitly_installed_packages_from_alpm() + .context("getting all installed packages from alpm")?; + let result = convert_to_pacdef_packages(alpm_packages); + Ok(result) } } -fn get_all_installed_packages_from_alpm() -> HashSet { - let db = Alpm::new("/", "/var/lib/pacman").unwrap(); - db.localdb() +fn get_all_installed_packages_from_alpm() -> Result> { + let db = get_db_handle().context("getting DB handle")?; + let result = db + .localdb() .pkgs() .iter() .map(|p| p.name().to_string()) - .collect() + .collect(); + Ok(result) } -fn get_explicitly_installed_packages_from_alpm() -> HashSet { - let db = Alpm::new("/", "/var/lib/pacman").unwrap(); - db.localdb() +fn get_explicitly_installed_packages_from_alpm() -> Result> { + let db = get_db_handle().context("getting DB handle")?; + let result = db + .localdb() .pkgs() .iter() .filter(|p| p.reason() == Explicit) .map(|p| p.name().to_string()) - .collect() + .collect(); + Ok(result) } fn convert_to_pacdef_packages(packages: HashSet) -> HashSet { packages.into_iter().map(Package::from).collect() } +fn get_db_handle() -> Result { + Alpm::new("/", "/var/lib/pacman").context("connecting to DB using expected default values") +} + impl Pacman { - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self { packages: HashSet::new(), } } } - -impl Default for Pacman { - fn default() -> Self { - Self::new() - } -} diff --git a/src/backend/rust.rs b/src/backend/rust.rs index ef00356..12d3aae 100644 --- a/src/backend/rust.rs +++ b/src/backend/rust.rs @@ -8,8 +8,8 @@ use serde_json::Value; use super::{Backend, Switches, Text}; use crate::{impl_backend_constants, Group, Package}; -pub struct Rust { - pub packages: HashSet, +pub(crate) struct Rust { + pub(crate) packages: HashSet, } const BINARY: Text = "cargo"; @@ -52,21 +52,15 @@ fn extract_packages(json: Value) -> Result> { } impl Rust { - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self { packages: HashSet::new(), } } } -impl Default for Rust { - fn default() -> Self { - Self::new() - } -} - fn get_crates_file() -> Result { - let mut result = crate::path::get_home_dir()?; + let mut result = crate::path::get_home_dir().context("getting home dir")?; result.push(".cargo"); result.push(".crates2.json"); Ok(result)