rework section loading
This commit is contained in:
@@ -20,5 +20,18 @@ macro_rules! impl_backend_constants {
|
||||
fn get_managed_packages(&self) -> &HashSet<Package> {
|
||||
&self.packages
|
||||
}
|
||||
|
||||
fn load(&mut self, groups: &HashSet<Group>) {
|
||||
let own_section_name = self.get_section();
|
||||
|
||||
groups
|
||||
.iter()
|
||||
.flat_map(|g| &g.sections)
|
||||
.filter(|section| section.name == own_section_name)
|
||||
.flat_map(|section| §ion.packages)
|
||||
.for_each(|package| {
|
||||
self.packages.insert(package.clone());
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+4
-5
@@ -6,7 +6,7 @@ use std::collections::HashSet;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::Command;
|
||||
|
||||
use crate::Package;
|
||||
use crate::{Group, Package};
|
||||
|
||||
pub use pacman::Pacman;
|
||||
pub use rust::Rust;
|
||||
@@ -36,6 +36,7 @@ impl Backends {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BackendIter(Option<Backends>);
|
||||
|
||||
impl Iterator for BackendIter {
|
||||
@@ -62,6 +63,7 @@ pub(crate) trait Backend {
|
||||
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) -> HashSet<Package>;
|
||||
@@ -118,10 +120,7 @@ pub(crate) trait Backend {
|
||||
fn get_unmanaged_packages_sorted(&self) -> Vec<Package> {
|
||||
let installed = self.get_explicitly_installed_packages();
|
||||
let required = self.get_managed_packages();
|
||||
let mut diff: Vec<_> = dbg!(installed)
|
||||
.difference(dbg!(required))
|
||||
.cloned()
|
||||
.collect();
|
||||
let mut diff: Vec<_> = installed.difference(required).cloned().collect();
|
||||
diff.sort_unstable();
|
||||
diff
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use alpm::Alpm;
|
||||
use alpm::PackageReason::Explicit;
|
||||
|
||||
use super::{Backend, Switches, Text};
|
||||
use crate::{impl_backend_constants, Package};
|
||||
use crate::{impl_backend_constants, Group, Package};
|
||||
|
||||
pub struct Pacman {
|
||||
pub packages: HashSet<Package>,
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
use std::{collections::HashSet, process::Command};
|
||||
|
||||
use super::{Backend, Switches, Text};
|
||||
use crate::{impl_backend_constants, Package};
|
||||
use crate::{impl_backend_constants, Group, Package};
|
||||
|
||||
pub struct Rust {
|
||||
pub packages: HashSet<Package>,
|
||||
|
||||
+8
-2
@@ -24,16 +24,22 @@ impl Pacdef {
|
||||
pub(crate) fn install_packages(&self) {
|
||||
let mut to_install = ToInstallPerBackend::new();
|
||||
|
||||
for b in Backends::iter() {
|
||||
for mut b in Backends::iter() {
|
||||
print!("{}: ", b.get_binary());
|
||||
|
||||
// dbg!(&self.groups);
|
||||
|
||||
b.load(&self.groups);
|
||||
|
||||
// dbg!(b.get_managed_packages());
|
||||
|
||||
let diff = b.get_missing_packages_sorted();
|
||||
if diff.is_empty() {
|
||||
println!("nothing to do");
|
||||
continue;
|
||||
}
|
||||
|
||||
println!(" Would install the following packages:");
|
||||
println!("would install the following packages");
|
||||
for p in &diff {
|
||||
println!(" {p}");
|
||||
}
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ impl Group {
|
||||
let file = entry.context("getting a file")?;
|
||||
let path = file.path();
|
||||
|
||||
let group = Group::try_from(dbg!(path))?;
|
||||
let group = Group::try_from(path)?;
|
||||
result.insert(group);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -13,7 +13,7 @@ impl Section {
|
||||
Self { name, packages }
|
||||
}
|
||||
|
||||
pub fn from_lines<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Self {
|
||||
pub fn from_lines<'a>(iter: &mut (impl Iterator<Item = &'a str> + std::fmt::Debug)) -> Self {
|
||||
let name = iter
|
||||
.find(|line| line.starts_with('['))
|
||||
.unwrap()
|
||||
@@ -24,8 +24,8 @@ impl Section {
|
||||
|
||||
let packages = iter
|
||||
.take_while(|line| !line.starts_with('['))
|
||||
.filter(|line| !line.contains(char::is_alphabetic))
|
||||
.map(Package::from)
|
||||
.map(Package::try_from)
|
||||
.filter_map(|p| p.ok())
|
||||
.collect();
|
||||
|
||||
Self::new(name, packages)
|
||||
|
||||
@@ -4,5 +4,5 @@ pub(crate) fn get_user_confirmation() -> bool {
|
||||
print!("Continue? [Y/n] ");
|
||||
std::io::stdout().flush().unwrap();
|
||||
let reply = std::io::stdin().lock().lines().next().unwrap().unwrap();
|
||||
!(reply.is_empty() || reply.to_lowercase().contains('y'))
|
||||
reply.trim().is_empty() || reply.to_lowercase().contains('y')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user