refactor backend module
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
|
use crate::{Group, Package};
|
||||||
|
|
||||||
|
use super::types::{Switches, Text};
|
||||||
|
|
||||||
|
pub(crate) trait Backend {
|
||||||
|
fn get_binary(&self) -> Text;
|
||||||
|
fn get_section(&self) -> Text;
|
||||||
|
fn get_switches_install(&self) -> Switches;
|
||||||
|
fn get_switches_remove(&self) -> Switches;
|
||||||
|
fn get_managed_packages(&self) -> &HashSet<Package>;
|
||||||
|
fn load(&mut self, groups: &HashSet<Group>);
|
||||||
|
|
||||||
|
/// Get all packages that are installed in the system.
|
||||||
|
fn get_all_installed_packages(&self) -> Result<HashSet<Package>>;
|
||||||
|
|
||||||
|
/// Get all packages that were installed in the system explicitly.
|
||||||
|
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
|
||||||
|
|
||||||
|
/// Install the specified packages.
|
||||||
|
fn install_packages(&self, packages: &[Package]) {
|
||||||
|
if packages.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut cmd = Command::new(self.get_binary());
|
||||||
|
cmd.args(self.get_switches_install());
|
||||||
|
for p in packages {
|
||||||
|
cmd.arg(format!("{p}"));
|
||||||
|
}
|
||||||
|
cmd.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove the specified packages.
|
||||||
|
fn remove_packages(&self, packages: Vec<Package>) {
|
||||||
|
let mut cmd = Command::new(self.get_binary());
|
||||||
|
cmd.args(self.get_switches_remove());
|
||||||
|
for p in packages {
|
||||||
|
cmd.arg(format!("{p}"));
|
||||||
|
}
|
||||||
|
cmd.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// extract packages from its own section as read from group files
|
||||||
|
fn extract_packages_from_group_file_content(&self, content: &str) -> HashSet<Package> {
|
||||||
|
content
|
||||||
|
.lines()
|
||||||
|
.skip_while(|line| !line.starts_with(&format!("[{}]", self.get_section())))
|
||||||
|
.skip(1)
|
||||||
|
.filter(|line| !line.starts_with('['))
|
||||||
|
.fuse()
|
||||||
|
.filter_map(Package::try_from)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_missing_packages_sorted(&self) -> Result<Vec<Package>> {
|
||||||
|
let installed = self
|
||||||
|
.get_all_installed_packages()
|
||||||
|
.context("could not get installed packages")?;
|
||||||
|
let managed = self.get_managed_packages();
|
||||||
|
let mut diff: Vec<_> = managed.difference(&installed).cloned().collect();
|
||||||
|
diff.sort_unstable();
|
||||||
|
Ok(diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_packages(&mut self, packages: HashSet<Package>);
|
||||||
|
|
||||||
|
fn get_unmanaged_packages_sorted(&self) -> Result<Vec<Package>> {
|
||||||
|
let installed = self
|
||||||
|
.get_explicitly_installed_packages()
|
||||||
|
.context("could not get explicitly installed packages")?;
|
||||||
|
let required = self.get_managed_packages();
|
||||||
|
let mut diff: Vec<_> = installed.difference(required).cloned().collect();
|
||||||
|
diff.sort_unstable();
|
||||||
|
Ok(diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
use super::{Backend, Backends};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct BackendIter {
|
||||||
|
pub(crate) next: Option<Backends>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for BackendIter {
|
||||||
|
type Item = Box<dyn Backend>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
match &self.next {
|
||||||
|
None => None,
|
||||||
|
Some(b) => {
|
||||||
|
let result = b.get_backend();
|
||||||
|
self.next = b.next();
|
||||||
|
Some(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-104
@@ -1,117 +1,19 @@
|
|||||||
|
mod backend_trait;
|
||||||
|
mod iter;
|
||||||
mod macros;
|
mod macros;
|
||||||
mod pacman;
|
mod pacman;
|
||||||
mod rust;
|
mod rust;
|
||||||
mod todo_per_backend;
|
mod todo_per_backend;
|
||||||
|
mod types;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
pub(crate) use backend_trait::Backend;
|
||||||
use std::os::unix::process::CommandExt;
|
pub(crate) use iter::BackendIter;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
|
||||||
|
|
||||||
use crate::{Group, Package};
|
|
||||||
use pacdef_macro::Register;
|
|
||||||
|
|
||||||
pub(crate) use todo_per_backend::ToDoPerBackend;
|
pub(crate) use todo_per_backend::ToDoPerBackend;
|
||||||
|
|
||||||
pub(in crate::backend) type Switches = &'static [&'static str];
|
use pacdef_macro::Register;
|
||||||
pub(in crate::backend) type Text = &'static str;
|
|
||||||
|
|
||||||
#[derive(Debug, Register)]
|
#[derive(Debug, Register)]
|
||||||
pub(crate) enum Backends {
|
pub(crate) enum Backends {
|
||||||
Pacman,
|
Pacman,
|
||||||
Rust,
|
Rust,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct BackendIter {
|
|
||||||
next: Option<Backends>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for BackendIter {
|
|
||||||
type Item = Box<dyn Backend>;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
match &self.next {
|
|
||||||
None => None,
|
|
||||||
Some(b) => {
|
|
||||||
let result = b.get_backend();
|
|
||||||
self.next = b.next();
|
|
||||||
Some(result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) trait Backend {
|
|
||||||
fn get_binary(&self) -> Text;
|
|
||||||
fn get_section(&self) -> Text;
|
|
||||||
fn get_switches_install(&self) -> Switches;
|
|
||||||
fn get_switches_remove(&self) -> Switches;
|
|
||||||
fn get_managed_packages(&self) -> &HashSet<Package>;
|
|
||||||
fn load(&mut self, groups: &HashSet<Group>);
|
|
||||||
|
|
||||||
/// Get all packages that are installed in the system.
|
|
||||||
fn get_all_installed_packages(&self) -> Result<HashSet<Package>>;
|
|
||||||
|
|
||||||
/// Get all packages that were installed in the system explicitly.
|
|
||||||
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
|
|
||||||
|
|
||||||
/// Install the specified packages.
|
|
||||||
fn install_packages(&self, packages: &[Package]) {
|
|
||||||
if packages.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut cmd = Command::new(self.get_binary());
|
|
||||||
cmd.args(self.get_switches_install());
|
|
||||||
for p in packages {
|
|
||||||
cmd.arg(format!("{p}"));
|
|
||||||
}
|
|
||||||
cmd.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove the specified packages.
|
|
||||||
fn remove_packages(&self, packages: Vec<Package>) {
|
|
||||||
let mut cmd = Command::new(self.get_binary());
|
|
||||||
cmd.args(self.get_switches_remove());
|
|
||||||
for p in packages {
|
|
||||||
cmd.arg(format!("{p}"));
|
|
||||||
}
|
|
||||||
cmd.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// extract packages from its own section as read from group files
|
|
||||||
fn extract_packages_from_group_file_content(&self, content: &str) -> HashSet<Package> {
|
|
||||||
content
|
|
||||||
.lines()
|
|
||||||
.skip_while(|line| !line.starts_with(&format!("[{}]", self.get_section())))
|
|
||||||
.skip(1)
|
|
||||||
.filter(|line| !line.starts_with('['))
|
|
||||||
.fuse()
|
|
||||||
.filter_map(Package::try_from)
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_missing_packages_sorted(&self) -> Result<Vec<Package>> {
|
|
||||||
let installed = self
|
|
||||||
.get_all_installed_packages()
|
|
||||||
.context("could not get installed packages")?;
|
|
||||||
let managed = self.get_managed_packages();
|
|
||||||
let mut diff: Vec<_> = managed.difference(&installed).cloned().collect();
|
|
||||||
diff.sort_unstable();
|
|
||||||
Ok(diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_packages(&mut self, packages: HashSet<Package>);
|
|
||||||
|
|
||||||
fn get_unmanaged_packages_sorted(&self) -> Result<Vec<Package>> {
|
|
||||||
let installed = self
|
|
||||||
.get_explicitly_installed_packages()
|
|
||||||
.context("could not get explicitly installed packages")?;
|
|
||||||
let required = self.get_managed_packages();
|
|
||||||
let mut diff: Vec<_> = installed.difference(required).cloned().collect();
|
|
||||||
diff.sort_unstable();
|
|
||||||
Ok(diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ use alpm::Alpm;
|
|||||||
use alpm::PackageReason::Explicit;
|
use alpm::PackageReason::Explicit;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
use super::{Backend, Switches, Text};
|
use super::types::{Switches, Text};
|
||||||
|
use super::Backend;
|
||||||
use crate::{impl_backend_constants, Group, Package};
|
use crate::{impl_backend_constants, Group, Package};
|
||||||
|
|
||||||
pub(crate) struct Pacman {
|
pub(crate) struct Pacman {
|
||||||
|
|||||||
+2
-1
@@ -5,7 +5,8 @@ use std::path::PathBuf;
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::{Backend, Switches, Text};
|
use super::types::{Switches, Text};
|
||||||
|
use super::Backend;
|
||||||
use crate::{impl_backend_constants, Group, Package};
|
use crate::{impl_backend_constants, Group, Package};
|
||||||
|
|
||||||
pub(crate) struct Rust {
|
pub(crate) struct Rust {
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub(in crate::backend) type Switches = &'static [&'static str];
|
||||||
|
pub(in crate::backend) type Text = &'static str;
|
||||||
Reference in New Issue
Block a user