load all recipes from repo

This commit is contained in:
Sebastian Kuzminsky 2025-01-17 20:10:00 -07:00
parent 57b8dae357
commit f28aeaf321
3 changed files with 68 additions and 10 deletions

View file

@ -1,6 +1,56 @@
use crate::recipe::Recipe;
#[derive(Debug)]
pub struct Repo {
path: String,
recipes: std::collections::HashMap<String, Recipe>,
}
impl Repo {
pub fn new(path: &str) -> Self {
let mut repo = Self {
path: std::string::String::from(path),
recipes: std::collections::HashMap::<String, Recipe>::new(),
};
repo.add_dir(path).unwrap();
repo
}
pub fn get_recipe(self: &Self, recipe_name: &str) -> Option<&Recipe> {
self.recipes.get(recipe_name)
}
fn add_dir(self: &mut Self, path: &str) -> anyhow::Result<()> {
// println!("reading Recipes from {path}");
let dir_entries = std::fs::read_dir(path).unwrap();
for dir_entry in dir_entries {
let dir_entry = dir_entry.unwrap();
let file_type = dir_entry.file_type().unwrap();
// println!("trying {:?} ({:?})", dir_entry, file_type);
if file_type.is_file() {
let path = dir_entry.path();
match self.add_file(&path) {
Ok(()) => {
// println!("added {:?}", dir_entry);
}
Err(e) => {
println!("failed to read recipe from {:?}: {:?}", path, e);
}
}
} else if file_type.is_dir() {
let _ = self.add_dir(dir_entry.path().to_str().unwrap());
}
}
Ok(())
}
fn add_file(self: &mut Self, path: &std::path::PathBuf) -> anyhow::Result<()> {
// println!("reading Recipe from {:?}", path);
if let Some(recipe_name) = path.file_stem() {
let key = recipe_name.to_string_lossy().into_owned();
let value = crate::Recipe::from_file(path)?;
self.recipes.insert(key, value);
}
Ok(())
}
}