WIP: report target recipe inputs better

This commit is contained in:
Sebastian Kuzminsky 2025-01-17 23:30:20 -07:00
parent 92ac5497e4
commit a42031917c

View file

@ -23,25 +23,30 @@ 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::<String, usize>::new();
// let price_per_unit = std::collections::HashMap::<String, f32>::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(())
}
}
impl Repo {
@ -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::<f32>().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(())
}
}