diff --git a/tools/src/bin/parser/main.rs b/tools/src/bin/parser/main.rs index dda1452..056416a 100644 --- a/tools/src/bin/parser/main.rs +++ b/tools/src/bin/parser/main.rs @@ -17,12 +17,11 @@ struct Args { fn main() -> anyhow::Result<()> { 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: Recipe = toml::from_str(&target_str)?; + let recipe: Option<&Recipe> = repo.get_recipe(&args.target); println!("{recipe:#?}"); - let r = tools::recipe::validate_recipe(&recipe); - println!("valid? {r:#?}"); + Ok(()) } diff --git a/tools/src/recipe.rs b/tools/src/recipe.rs index 480615a..4465d5d 100644 --- a/tools/src/recipe.rs +++ b/tools/src/recipe.rs @@ -54,9 +54,18 @@ pub struct Recipe { outputs: Option>, } -pub fn validate_recipe(recipe: &Recipe) -> anyhow::Result<()> { - // if recipe.inputs.len() == 0 { - // Err("recipe has no inputs!"); - // } - Ok(()) +impl Recipe { + pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result { + 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 { + // Err("recipe has no inputs!"); + // } + Ok(()) + } } diff --git a/tools/src/repo.rs b/tools/src/repo.rs index 3e966a8..b7861f7 100644 --- a/tools/src/repo.rs +++ b/tools/src/repo.rs @@ -1,6 +1,56 @@ use crate::recipe::Recipe; +#[derive(Debug)] pub struct Repo { path: String, recipes: std::collections::HashMap, } + +impl Repo { + pub fn new(path: &str) -> Self { + let mut repo = Self { + path: std::string::String::from(path), + recipes: std::collections::HashMap::::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(()) + } +}