diff --git a/tools/src/recipe.rs b/tools/src/recipe.rs index 0571db1..8f9c0df 100644 --- a/tools/src/recipe.rs +++ b/tools/src/recipe.rs @@ -12,22 +12,42 @@ pub enum Unit { } /// `Quantity` measures the amount of a resource (Input or Output). -#[derive(Clone, Copy, Debug, serde::Deserialize)] +#[derive(Clone, Copy, serde::Deserialize)] pub struct Quantity { pub amount: f32, pub unit: Option, } +impl std::default::Default for Quantity { + fn default() -> Self { + Self { + amount: 1.0, + unit: None, + } + } +} + +impl std::fmt::Debug for Quantity { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match &self.unit { + None => write!(f, "{:#}", self.amount), + Some(unit) => write!(f, "{:#} {:?}", self.amount, unit), + } + } +} + #[derive(Debug, serde::Deserialize)] pub struct Input { // Quantity defaults to "amount=1" if omitted. - pub quantity: Option, + #[serde(default)] + pub quantity: Quantity, } #[derive(Debug, serde::Deserialize)] pub struct Output { // Quantity defaults to "amount=1" if omitted. - pub quantity: Option, + #[serde(default)] + pub quantity: Quantity, } #[derive(Debug, serde::Deserialize)] @@ -73,30 +93,6 @@ pub struct Recipe { pub outputs: Option>, } -impl Input { - pub fn get_quantity(&self) -> Quantity { - match &self.quantity { - None => Quantity { - amount: 1.0, - unit: None, - }, - Some(q) => q.clone(), - } - } -} - -impl Output { - pub fn get_quantity(&self) -> Quantity { - match &self.quantity { - None => Quantity { - amount: 1.0, - unit: None, - }, - Some(q) => q.clone(), - } - } -} - impl Recipe { pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result { let recipe_contents = std::fs::read_to_string(file)?; diff --git a/tools/src/repo.rs b/tools/src/repo.rs index 727cae1..0931a49 100644 --- a/tools/src/repo.rs +++ b/tools/src/repo.rs @@ -93,9 +93,7 @@ impl Repo { if input_name == "capital" { // The build process begins in capitalism :-( - let input_quantity = input_info.get_quantity(); - - let input_unit = match &input_quantity.unit { + let input_unit = match &input_info.quantity.unit { None => { return Err(anyhow::Error::msg(format!( "expected quantity unit USDollar on capital input" @@ -109,7 +107,7 @@ impl Repo { Some(unit) => unit, }; - let cost = input_quantity.amount; + let cost = input_info.quantity.amount; let outputs = match &recipe.outputs { None => return Err(anyhow::Error::msg(format!("no outputs!"))), @@ -118,9 +116,8 @@ impl Repo { if outputs.len() == 1 { for (_output_name, output_info) in outputs.iter() { - let output_quantity = output_info.get_quantity(); - let cost_each = cost / output_quantity.amount; - if let Some(output_unit) = output_quantity.unit { + let cost_each = cost / output_info.quantity.amount; + if let Some(output_unit) = output_info.quantity.unit { println!( "capital: {:.3} {:?} / {:?} :-(", cost_each, input_unit, output_unit @@ -134,7 +131,7 @@ impl Repo { } continue; } - println!("{input_name:?} {input_info:?}"); + println!("{input_name:?} ({:?})", input_info.quantity); match self.get_recipe(input_name) { None => { return Err(anyhow::Error::msg(format!(