implement section
This commit is contained in:
+30
-12
@@ -1,36 +1,33 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::fs::{read_to_string, File};
|
||||
use std::hash::Hash;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
|
||||
use crate::section::Section;
|
||||
use crate::Package;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Group {
|
||||
pub name: String,
|
||||
pub packages: HashSet<Package>,
|
||||
pub sections: HashSet<Section>,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub fn load_from_dir() -> Result<HashSet<Self>> {
|
||||
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,
|
||||
});
|
||||
let group = Group::try_from(name)?;
|
||||
result.insert(group);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
@@ -58,10 +55,31 @@ impl Hash for Group {
|
||||
|
||||
impl PartialEq for Group {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.name == other.name && self.packages == other.packages
|
||||
self.name == other.name
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Group {
|
||||
fn assert_receiver_is_total_eq(&self) {}
|
||||
}
|
||||
|
||||
impl<P> From<P> for Group
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
fn from(p: P) -> Self {
|
||||
let path = p.as_ref();
|
||||
let content = read_to_string(path).unwrap();
|
||||
let name = path.file_name().unwrap().to_string_lossy().to_string();
|
||||
|
||||
let mut lines = content.lines().peekable();
|
||||
let mut sections = HashSet::new();
|
||||
|
||||
while lines.peek().is_some() {
|
||||
let section = Section::from_lines(&mut lines);
|
||||
sections.insert(section);
|
||||
}
|
||||
|
||||
Self { name, sections }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user