finish review

This commit is contained in:
steven-omaha
2023-02-01 16:37:00 +01:00
parent ab253cfcc1
commit 64d44337b0
2 changed files with 77 additions and 62 deletions
+23 -59
View File
@@ -1,7 +1,5 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::process::Command;
use std::rc::Rc;
use std::{collections::HashSet, process::ExitStatus};
@@ -32,66 +30,12 @@ pub(crate) trait Backend: Debug {
/// Get all packages that were installed in the system explicitly.
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
// TODO continue here
fn assign_group(&self, to_assign: Vec<(Package, Rc<Group>)>) {
todo!();
let mut group_package_map = HashMap::new();
for (p, group) in to_assign {
if !group_package_map.contains_key(&group) {
group_package_map.insert(group.clone(), vec![]);
}
let inner = group_package_map.get_mut(&group).unwrap();
inner.push(p);
}
for vecs in group_package_map.values_mut() {
vecs.sort();
}
let group_package_map = get_group_packages_map(to_assign);
let section_header = format!("[{}]", self.get_section());
for (group, packages) in group_package_map {
let group_file_content = read_to_string(&group.path).unwrap();
let old_length = dbg!(group_file_content.len());
let mut must_write_section_header = false;
let mut lines = group_file_content.lines();
lines.find(|line| line.contains(&format!("[{}]", self.get_section())));
lines.next().unwrap();
// let start_of_section = if let Some(start_of_section) = group_file_content.find() {
// start_of_section
// } else {
// must_write_section_header = true;
// old_length
// };
// dbg!(&start_of_section);
// let mut new_file_content = group_file_content
// .get(..start_of_section)
// .unwrap()
// .to_owned();
// dbg!(&new_file_content);
// todo!();
// if dbg!(must_write_section_header) {
// new_file_content.push_str(&format!("\n[{}]", self.get_section()));
// }
// for package in packages {
// new_file_content.push_str(&format!("\n{package}"));
// }
// new_file_content.push('\n');
// if old_length > start_of_section {
// new_file_content.push_str(group_file_content.get(old_length..).unwrap());
// }
// let mut file = File::create(&group.path).unwrap();
// write!(file, "{new_file_content}").unwrap();
group.save_packages(&section_header, packages);
}
}
@@ -170,3 +114,23 @@ pub(crate) trait Backend: Debug {
Ok(diff)
}
}
fn get_group_packages_map(
to_assign: Vec<(Package, Rc<Group>)>,
) -> HashMap<Rc<Group>, Vec<Package>> {
let mut group_package_map = HashMap::new();
for (p, group) in to_assign {
if !group_package_map.contains_key(&group) {
group_package_map.insert(group.clone(), vec![]);
}
let inner = group_package_map.get_mut(&group).unwrap();
inner.push(p);
}
for vecs in group_package_map.values_mut() {
vecs.sort();
}
group_package_map
}