finish review

This commit is contained in:
Dr. Matthias Ratajczak
2023-02-01 16:37:00 +01:00
parent 3f1b477c2e
commit 2989e40925
2 changed files with 77 additions and 62 deletions
+23 -59
View File
@@ -1,7 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::process::Command; use std::process::Command;
use std::rc::Rc; use std::rc::Rc;
use std::{collections::HashSet, process::ExitStatus}; 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. /// Get all packages that were installed in the system explicitly.
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>; fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
// TODO continue here
fn assign_group(&self, to_assign: Vec<(Package, Rc<Group>)>) { fn assign_group(&self, to_assign: Vec<(Package, Rc<Group>)>) {
todo!(); let group_package_map = get_group_packages_map(to_assign);
let mut group_package_map = HashMap::new(); let section_header = format!("[{}]", self.get_section());
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();
}
for (group, packages) in group_package_map { for (group, packages) in group_package_map {
let group_file_content = read_to_string(&group.path).unwrap(); group.save_packages(&section_header, packages);
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();
} }
} }
@@ -170,3 +114,23 @@ pub(crate) trait Backend: Debug {
Ok(diff) 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
}
+54 -3
View File
@@ -1,12 +1,13 @@
use std::fmt::Write; use std::fmt::Write as FmtWrite;
use std::fs::read_to_string; use std::fs::{read_to_string, File};
use std::hash::Hash; use std::hash::Hash;
use std::io::Write as IoWrite;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{collections::HashSet, fmt::Display}; use std::{collections::HashSet, fmt::Display};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use super::Section; use super::{Package, Section};
use crate::Config; use crate::Config;
@@ -110,6 +111,19 @@ impl Group {
path, path,
}) })
} }
pub(crate) fn save_packages(&self, section_header: &str, packages: Vec<Package>) {
let mut content = read_to_string(&self.path).unwrap();
if content.contains(section_header) {
write_packages_to_existing_section(&mut content, section_header, &packages);
} else {
add_new_section_with_packages(&mut content, section_header, &packages);
}
let mut file = File::create(&self.path).unwrap();
write!(file, "{content}").unwrap();
}
} }
impl Display for Group { impl Display for Group {
@@ -128,3 +142,40 @@ impl Display for Group {
Ok(()) Ok(())
} }
} }
fn write_packages_to_existing_section(
group_file_content: &mut String,
section_header: &str,
packages: &[Package],
) {
let idx_of_first_package_line_in_section =
find_first_package_line_in_section(group_file_content, section_header);
let after = group_file_content.split_off(idx_of_first_package_line_in_section);
for p in packages {
group_file_content.push_str(&format!("{p}\n"));
}
group_file_content.push_str(&after);
}
fn find_first_package_line_in_section(group_file_content: &str, section_header: &str) -> usize {
let section_start = group_file_content.find(section_header).unwrap();
let distance_to_next_newline = group_file_content[section_start..].find('\n').unwrap();
section_start + distance_to_next_newline + 1 // + 1 to be after the newline
}
fn add_new_section_with_packages(
group_file_content: &mut String,
section_header: &str,
packages: &[Package],
) {
group_file_content.push('\n');
group_file_content.push_str(section_header);
group_file_content.push('\n');
for p in packages {
group_file_content.push_str(&format!("{p}\n"));
}
}