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

@ -17,12 +17,11 @@ struct Args {
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = Args::parse(); let args = Args::parse();
let repo_contents = std::fs::read_dir(args.repo).unwrap(); let repo = Repo::new(&args.repo);
println!("{:#?}", repo);
let target_str = std::fs::read_to_string(args.target)?; let recipe: Option<&Recipe> = repo.get_recipe(&args.target);
let recipe: Recipe = toml::from_str(&target_str)?;
println!("{recipe:#?}"); println!("{recipe:#?}");
let r = tools::recipe::validate_recipe(&recipe);
println!("valid? {r:#?}");
Ok(()) Ok(())
} }

View file

@ -54,9 +54,18 @@ pub struct Recipe {
outputs: Option<Vec<String>>, outputs: Option<Vec<String>>,
} }
pub fn validate_recipe(recipe: &Recipe) -> anyhow::Result<()> { impl Recipe {
pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result<Self> {
let recipe_contents = std::fs::read_to_string(file)?;
let recipe: Recipe = toml::from_str(&recipe_contents)?;
// let r = recipe.validate_recipe();
Ok(recipe)
}
fn validate_recipe(self: &Self) -> anyhow::Result<()> {
// if recipe.inputs.len() == 0 { // if recipe.inputs.len() == 0 {
// Err("recipe has no inputs!"); // Err("recipe has no inputs!");
// } // }
Ok(()) Ok(())
} }
}

View file

@ -1,6 +1,56 @@
use crate::recipe::Recipe; use crate::recipe::Recipe;
#[derive(Debug)]
pub struct Repo { pub struct Repo {
path: String, path: String,
recipes: std::collections::HashMap<String, Recipe>, 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(())
}
}