rework backend internals

This commit is contained in:
steven-omaha
2023-01-10 18:17:12 +01:00
parent 122665a088
commit ae0907c261
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 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;
+29 -24
View File
@@ -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<Package>,
pub(crate) struct Pacman {
pub(crate) packages: HashSet<Package>,
}
const BINARY: Text = "paru";
@@ -20,51 +20,56 @@ impl Backend for Pacman {
impl_backend_constants!();
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
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<HashSet<Package>> {
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<String> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap();
db.localdb()
fn get_all_installed_packages_from_alpm() -> Result<HashSet<String>> {
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<String> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap();
db.localdb()
fn get_explicitly_installed_packages_from_alpm() -> Result<HashSet<String>> {
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<String>) -> HashSet<Package> {
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 {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self {
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 crate::{impl_backend_constants, Group, Package};
pub struct Rust {
pub packages: HashSet<Package>,
pub(crate) struct Rust {
pub(crate) packages: HashSet<Package>,
}
const BINARY: Text = "cargo";
@@ -52,21 +52,15 @@ fn extract_packages(json: Value) -> Result<HashSet<Package>> {
}
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<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(".crates2.json");
Ok(result)