use std::collections::HashSet; use std::fs::File; use std::hash::Hash; use std::io::{BufRead, BufReader}; use anyhow::{anyhow, Context, Result}; use crate::Package; #[derive(Debug)] pub struct Group { pub name: String, pub packages: HashSet, } impl Group { pub fn load_from_dir() -> Result> { let mut result = HashSet::new(); let path = crate::path::get_pacdef_group_dir().context("getting pacdef group dir")?; for entry in path.read_dir().context("reading group dir")? { let file = entry.context("getting a file")?; let name = file.file_name(); let f = File::open(file.path()).context("reading the file")?; let reader = BufReader::new(f); let packages = Package::from_lines(reader.lines()); result.insert(Group { name: name .into_string() .map_err(|e| anyhow!("could not get group name, {e:?}"))?, packages, }); } Ok(result) } } impl PartialOrd for Group { fn partial_cmp(&self, other: &Self) -> Option { match self.name.partial_cmp(&other.name) { Some(core::cmp::Ordering::Equal) => None, ord => ord, } } } impl Ord for Group { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.name.cmp(&other.name) } } impl Hash for Group { fn hash(&self, state: &mut H) { self.name.hash(state); } } impl PartialEq for Group { fn eq(&self, other: &Self) -> bool { self.name == other.name && self.packages == other.packages } } impl Eq for Group { fn assert_receiver_is_total_eq(&self) {} }