add repo.compile(target)

This commit is contained in:
Sebastian Kuzminsky 2025-01-17 21:57:45 -07:00
parent cd1f922953
commit ed5ec61e94
3 changed files with 44 additions and 17 deletions

View file

@ -16,12 +16,7 @@ struct Args {
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = Args::parse(); let args = Args::parse();
let repo = Repo::new(&args.repo); let repo = Repo::new(&args.repo);
println!("{:#?}", repo); repo.compile(&args.target).unwrap();
let recipe: Option<&Recipe> = repo.get_recipe(&args.target);
println!("{recipe:#?}");
Ok(()) Ok(())
} }

View file

@ -15,8 +15,8 @@ pub struct Input {
/// be optionally overridden by the recipe to things like "meters" /// be optionally overridden by the recipe to things like "meters"
/// (of tubing) or "grams" (of chromic acid). Then this struct /// (of tubing) or "grams" (of chromic acid). Then this struct
/// would be `quantity: usize, units: Option<Unit>`. /// would be `quantity: usize, units: Option<Unit>`.
quantity: Option<usize>, pub quantity: Option<usize>,
amount: Option<String>, pub amount: Option<String>,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
@ -30,36 +30,43 @@ pub struct Output {
/// be optionally overridden by the recipe to things like "meters" /// be optionally overridden by the recipe to things like "meters"
/// (of tubing) or "grams" (of chromic acid). Then this struct /// (of tubing) or "grams" (of chromic acid). Then this struct
/// would be `quantity: usize, units: Option<Unit>`. /// would be `quantity: usize, units: Option<Unit>`.
quantity: Option<usize>, pub quantity: Option<usize>,
amount: Option<String>, pub amount: Option<String>,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub struct Operator { pub struct Operator {
skills: Vec<String>, pub skills: Vec<String>,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub struct Dependencies { pub struct Dependencies {
tools: Vec<String>, pub tools: Option<Vec<String>>,
operator: Option<Operator>, pub operator: Option<Operator>,
}
#[derive(Debug, serde::Deserialize)]
pub struct Purchase {
pub vendor: Vec<String>,
pub documentation: Option<Vec<String>>,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub enum Action { pub enum Action {
process(String), process(String),
print, print,
purchase(Purchase),
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub struct Recipe { pub struct Recipe {
/// `[inputs]` is a Table where each key the the name (unique id) /// `[inputs]` is a Table where each key the the name (unique id)
/// of a recipe in the repo, and the value is an Input object. /// of a recipe in the repo, and the value is an Input object.
inputs: std::collections::HashMap<String, Input>, pub inputs: std::collections::HashMap<String, Input>,
dependencies: Dependencies, pub dependencies: Dependencies,
action: Action, pub action: Action,
/// If a recipe has no `[outputs]`, we assume it produces 1x of the /// If a recipe has no `[outputs]`, we assume it produces 1x of the
/// thing identified by the name of the recipe. /// thing identified by the name of the recipe.
@ -67,7 +74,7 @@ pub struct Recipe {
/// FIXME: Or is that always the case, and we should have no outputs /// FIXME: Or is that always the case, and we should have no outputs
/// section? None of the recipes we've been doodling around with /// section? None of the recipes we've been doodling around with
/// have anything like byproducts or waste streams... /// have anything like byproducts or waste streams...
outputs: Option<std::collections::HashMap<String, Output>>, pub outputs: Option<std::collections::HashMap<String, Output>>,
} }
impl Recipe { impl Recipe {

View file

@ -1,4 +1,5 @@
use crate::recipe::Recipe; use crate::recipe::Recipe;
use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub struct Repo { pub struct Repo {
@ -20,6 +21,30 @@ impl Repo {
self.recipes.get(recipe_name) self.recipes.get(recipe_name)
} }
pub fn compile(self: &Self, target: &str) -> anyhow::Result<()> {
let recipe = self.get_recipe(target);
match recipe {
None => {
return Err(anyhow::Error::msg(format!("recipe for {target} not found")));
}
Some(recipe) => {
println!("{recipe:#?}");
println!("inputs:");
for (key, val) in recipe.inputs.iter() {
println!("{key:?} {val:?}");
if key == "capital" {
println!("aquire capital: {:?}", val.amount.to_owned());
} else {
self.compile(key)?;
}
}
}
}
Ok(())
}
}
impl Repo {
fn add_dir(self: &mut Self, path: &str) -> anyhow::Result<()> { fn add_dir(self: &mut Self, path: &str) -> anyhow::Result<()> {
// println!("reading Recipes from {path}"); // println!("reading Recipes from {path}");
let dir_entries = std::fs::read_dir(path).unwrap(); let dir_entries = std::fs::read_dir(path).unwrap();