diff --git a/tools/src/repo.rs b/tools/src/repo.rs index 6529463..dad8916 100644 --- a/tools/src/repo.rs +++ b/tools/src/repo.rs @@ -23,24 +23,29 @@ impl Repo { pub fn compile(self: &Self, target: &str) -> anyhow::Result<()> { let recipe = self.get_recipe(target); + + // FIXME: Accumulate `inputs` from our callees, pass up to our + // caller via the Result? + // + // Or have a "compile context" argument with the "cost" in it, + // and update that as we go? + // + // Maybe in the context we should also construct a parallel + // build process? + + // let inputs = std::collections::HashMap::::new(); + // let price_per_unit = std::collections::HashMap::::new(); + match recipe { None => { return Err(anyhow::Error::msg(format!("recipe for {target} not found"))); } Some(recipe) => { - println!("{recipe:#?}"); + println!("building {target:#?}"); 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)?; - } - } + self.compile_inner(recipe, 4) } } - Ok(()) } } @@ -78,4 +83,59 @@ impl Repo { } Ok(()) } + + fn compile_inner(self: &Self, recipe: &Recipe, indent: usize) -> anyhow::Result<()> { + for (key, val) in recipe.inputs.iter() { + for _ in 0..indent { + print!(" "); + } + + if key == "capital" { + // The build process begins in capitalism :-( + + if let Some(amount) = &val.amount { + // I'd like to divide the amount of capital by the + // quantity of the thing purchased. + // + // We can probably assume the `recipe.outputs` Option + // is Some, but inside the Some is a container with + // potentially any number of output things, which + // makes it hard to account. + // + // For now i'll just handle the common case specially. + + if let Some(a) = amount.split_whitespace().next() { + let cost = a.parse::().unwrap(); + if let Some(outputs) = &recipe.outputs { + if outputs.len() == 1 { + for (_k, v) in outputs.iter() { + if let Some(quantity) = &v.quantity { + println!( + "capital: {cost}/{quantity} = {:.2} each :-(", + cost / *quantity as f32 + ); + } else if let Some(amount) = &v.amount { + println!("capital: {cost}/{amount:?} :-(",); + } + } + } + } + } + } else { + panic!("no amount of capital?"); + } + continue; + } + println!("{key:?} {val:?}"); + match self.get_recipe(key) { + None => { + return Err(anyhow::Error::msg(format!("recipe for {key} not found"))); + } + Some(input_recipe) => { + self.compile_inner(input_recipe, indent + 4)?; + } + } + } + Ok(()) + } }