propagate errors

This commit is contained in:
Dr. Matthias Ratajczak
2022-12-02 18:06:47 +01:00
parent 7fb416815c
commit 7652e9a6be
2 changed files with 10 additions and 9 deletions
+9 -7
View File
@@ -3,6 +3,8 @@ use std::fs::File;
use std::hash::Hash;
use std::io::{BufRead, BufReader};
use anyhow::{anyhow, Context, Result};
use crate::Package;
#[derive(Debug)]
@@ -12,22 +14,22 @@ pub struct Group {
}
impl Group {
pub fn load_from_dir() -> HashSet<Self> {
pub fn load_from_dir() -> Result<HashSet<Self>> {
let mut result = HashSet::new();
let path = crate::path::get_pacdef_group_dir().unwrap();
for entry in path.read_dir().unwrap() {
let file = entry.unwrap();
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()).unwrap();
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().unwrap(),
name: name.into_string().map_err(|e| anyhow!(e))?,
packages,
});
}
result
Ok(result)
}
}
+1 -2
View File
@@ -6,6 +6,5 @@ fn main() -> Result<()> {
let args = args::get_args();
let groups = Group::load_from_dir();
let pacdef = Pacdef::new(args, groups);
pacdef.run_action_from_arg().context("running action")?;
Ok(())
pacdef.run_action_from_arg().context("running action")
}