rework backend internals

This commit is contained in:
Dr. Matthias Ratajczak
2023-01-10 18:17:12 +01:00
parent 1365ee5b5c
commit 96265648e9
3 changed files with 35 additions and 36 deletions
+2 -2
View File
@@ -13,8 +13,8 @@ use crate::{Group, Package};
pub(crate) use todo_per_backend::ToDoPerBackend; pub(crate) use todo_per_backend::ToDoPerBackend;
pub use pacman::Pacman; pub(crate) use pacman::Pacman;
pub use rust::Rust; pub(crate) use rust::Rust;
pub(in crate::backend) type Switches = &'static [&'static str]; pub(in crate::backend) type Switches = &'static [&'static str];
pub(in crate::backend) type Text = &'static str; pub(in crate::backend) type Text = &'static str;
+29 -24
View File
@@ -2,13 +2,13 @@ use std::collections::HashSet;
use alpm::Alpm; use alpm::Alpm;
use alpm::PackageReason::Explicit; use alpm::PackageReason::Explicit;
use anyhow::Result; use anyhow::{Context, Result};
use super::{Backend, Switches, Text}; use super::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package}; use crate::{impl_backend_constants, Group, Package};
pub struct Pacman { pub(crate) struct Pacman {
pub packages: HashSet<Package>, pub(crate) packages: HashSet<Package>,
} }
const BINARY: Text = "paru"; const BINARY: Text = "paru";
@@ -20,51 +20,56 @@ impl Backend for Pacman {
impl_backend_constants!(); impl_backend_constants!();
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> { fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
Ok(convert_to_pacdef_packages( let alpm_packages = get_all_installed_packages_from_alpm()
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<HashSet<Package>> { fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
Ok(convert_to_pacdef_packages( let alpm_packages = get_explicitly_installed_packages_from_alpm()
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<String> { fn get_all_installed_packages_from_alpm() -> Result<HashSet<String>> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap(); let db = get_db_handle().context("getting DB handle")?;
db.localdb() let result = db
.localdb()
.pkgs() .pkgs()
.iter() .iter()
.map(|p| p.name().to_string()) .map(|p| p.name().to_string())
.collect() .collect();
Ok(result)
} }
fn get_explicitly_installed_packages_from_alpm() -> HashSet<String> { fn get_explicitly_installed_packages_from_alpm() -> Result<HashSet<String>> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap(); let db = get_db_handle().context("getting DB handle")?;
db.localdb() let result = db
.localdb()
.pkgs() .pkgs()
.iter() .iter()
.filter(|p| p.reason() == Explicit) .filter(|p| p.reason() == Explicit)
.map(|p| p.name().to_string()) .map(|p| p.name().to_string())
.collect() .collect();
Ok(result)
} }
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> { fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
packages.into_iter().map(Package::from).collect() packages.into_iter().map(Package::from).collect()
} }
fn get_db_handle() -> Result<Alpm> {
Alpm::new("/", "/var/lib/pacman").context("connecting to DB using expected default values")
}
impl Pacman { impl Pacman {
pub fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
packages: HashSet::new(), packages: HashSet::new(),
} }
} }
} }
impl Default for Pacman {
fn default() -> Self {
Self::new()
}
}
+4 -10
View File
@@ -8,8 +8,8 @@ use serde_json::Value;
use super::{Backend, Switches, Text}; use super::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package}; use crate::{impl_backend_constants, Group, Package};
pub struct Rust { pub(crate) struct Rust {
pub packages: HashSet<Package>, pub(crate) packages: HashSet<Package>,
} }
const BINARY: Text = "cargo"; const BINARY: Text = "cargo";
@@ -52,21 +52,15 @@ fn extract_packages(json: Value) -> Result<HashSet<Package>> {
} }
impl Rust { impl Rust {
pub fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
packages: HashSet::new(), packages: HashSet::new(),
} }
} }
} }
impl Default for Rust {
fn default() -> Self {
Self::new()
}
}
fn get_crates_file() -> Result<PathBuf> { fn get_crates_file() -> Result<PathBuf> {
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(".cargo");
result.push(".crates2.json"); result.push(".crates2.json");
Ok(result) Ok(result)