impl std::default::Default for Quantity, simplifies the code a bunch

This commit is contained in:
Sebastian Kuzminsky 2025-01-18 21:18:22 -07:00
parent dd2f41e3cf
commit e7b5e60aae
2 changed files with 28 additions and 35 deletions

View file

@ -12,22 +12,42 @@ pub enum Unit {
} }
/// `Quantity` measures the amount of a resource (Input or Output). /// `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 struct Quantity {
pub amount: f32, pub amount: f32,
pub unit: Option<Unit>, pub unit: Option<Unit>,
} }
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)] #[derive(Debug, serde::Deserialize)]
pub struct Input { pub struct Input {
// Quantity defaults to "amount=1" if omitted. // Quantity defaults to "amount=1" if omitted.
pub quantity: Option<Quantity>, #[serde(default)]
pub quantity: Quantity,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub struct Output { pub struct Output {
// Quantity defaults to "amount=1" if omitted. // Quantity defaults to "amount=1" if omitted.
pub quantity: Option<Quantity>, #[serde(default)]
pub quantity: Quantity,
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
@ -73,30 +93,6 @@ pub struct Recipe {
pub outputs: Option<std::collections::HashMap<String, Output>>, pub outputs: Option<std::collections::HashMap<String, Output>>,
} }
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 { impl Recipe {
pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result<Self> { pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result<Self> {
let recipe_contents = std::fs::read_to_string(file)?; let recipe_contents = std::fs::read_to_string(file)?;

View file

@ -93,9 +93,7 @@ impl Repo {
if input_name == "capital" { if input_name == "capital" {
// The build process begins in capitalism :-( // The build process begins in capitalism :-(
let input_quantity = input_info.get_quantity(); let input_unit = match &input_info.quantity.unit {
let input_unit = match &input_quantity.unit {
None => { None => {
return Err(anyhow::Error::msg(format!( return Err(anyhow::Error::msg(format!(
"expected quantity unit USDollar on capital input" "expected quantity unit USDollar on capital input"
@ -109,7 +107,7 @@ impl Repo {
Some(unit) => unit, Some(unit) => unit,
}; };
let cost = input_quantity.amount; let cost = input_info.quantity.amount;
let outputs = match &recipe.outputs { let outputs = match &recipe.outputs {
None => return Err(anyhow::Error::msg(format!("no outputs!"))), None => return Err(anyhow::Error::msg(format!("no outputs!"))),
@ -118,9 +116,8 @@ impl Repo {
if outputs.len() == 1 { if outputs.len() == 1 {
for (_output_name, output_info) in outputs.iter() { for (_output_name, output_info) in outputs.iter() {
let output_quantity = output_info.get_quantity(); let cost_each = cost / output_info.quantity.amount;
let cost_each = cost / output_quantity.amount; if let Some(output_unit) = output_info.quantity.unit {
if let Some(output_unit) = output_quantity.unit {
println!( println!(
"capital: {:.3} {:?} / {:?} :-(", "capital: {:.3} {:?} / {:?} :-(",
cost_each, input_unit, output_unit cost_each, input_unit, output_unit
@ -134,7 +131,7 @@ impl Repo {
} }
continue; continue;
} }
println!("{input_name:?} {input_info:?}"); println!("{input_name:?} ({:?})", input_info.quantity);
match self.get_recipe(input_name) { match self.get_recipe(input_name) {
None => { None => {
return Err(anyhow::Error::msg(format!( return Err(anyhow::Error::msg(format!(