more functional handling of Quantity
This commit is contained in:
parent
424cbaedf8
commit
dd2f41e3cf
22 changed files with 131 additions and 99 deletions
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
bearing_roller = {quantity=1}
|
||||
bearing_roller = {quantity={amount=1}}
|
||||
|
||||
# inputs list, similar to a cargo.toml file
|
||||
[inputs]
|
||||
|
|
@ -9,25 +9,25 @@ bearing_hub = {version=0.1, type="printed"}
|
|||
# alternate representation of an element of the inputs list
|
||||
[inputs.m4_20_shcs]
|
||||
type = "M4x20 SHCS"
|
||||
quantity = 3
|
||||
quantity = { amount=3 }
|
||||
comment = """These will hold the bearing rollers. Possibly Button Heads
|
||||
Cap Screws would be better? Probably either will work fine."""
|
||||
|
||||
[inputs.m4_washer]
|
||||
type = "M4 washer"
|
||||
quantity = 6
|
||||
quantity = { amount=6 }
|
||||
dimensions = "12 mm OD, 4.4 mm ID, 1 mm thick"
|
||||
comment = """The upstream repo calls for 12 mm OD but i think that's
|
||||
too big, it hits the outer race of the bearings. Use 8mm OD instead."""
|
||||
|
||||
[inputs.bearing_624]
|
||||
type = "624 bearing"
|
||||
quantity = 6
|
||||
quantity = { amount=6 }
|
||||
comments = "13mm OD, 4mm ID, 5mm wide"
|
||||
|
||||
[inputs.m4_nuts]
|
||||
type = "M4 nuts"
|
||||
quantity = 3
|
||||
quantity = { amount=3 }
|
||||
|
||||
[dependencies]
|
||||
tools = [
|
||||
|
|
|
|||
|
|
@ -1,24 +1,24 @@
|
|||
[inputs]
|
||||
pump_housing_front_with_stepper = {quantity=1}
|
||||
bearing_roller = {quantity=1}
|
||||
pump_housing_rear_with_stepper = {quantity=1}
|
||||
tube_support = {quantity=1}
|
||||
pump_housing_front_with_stepper = {quantity={amount=1}}
|
||||
bearing_roller = {quantity={amount=1}}
|
||||
pump_housing_rear_with_stepper = {quantity={amount=1}}
|
||||
tube_support = {quantity={amount=1}}
|
||||
|
||||
[inputs.silicone_tubing]
|
||||
type = "silicone tubing, 6mm OD, 4mm ID"
|
||||
amount = "1 meter"
|
||||
quantity = { amount = 1, unit="Meter" }
|
||||
comment = "PVC tube isn't flexible enough. Adjust quantity to suit your application."
|
||||
|
||||
[inputs.m4_25_bhcs]
|
||||
name = "m4_25_bhcs"
|
||||
type = "M4x25 BHCS"
|
||||
quantity = 4
|
||||
quantity = { amount = 4 }
|
||||
comment = """BHCS is preferred over SHCS so the screw heads sit recessed
|
||||
below the surface of the pump body."""
|
||||
|
||||
[inputs.m4_nuts]
|
||||
type = "M4 nuts"
|
||||
quantity = 4
|
||||
quantity = { amount = 4 }
|
||||
|
||||
[dependencies]
|
||||
tools = [ "3 mm hex driver" ]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
bearing_hub={version=0.1, quantity=1, type="printed"}
|
||||
bearing_hub={version=0.1, quantity={amount=1}, type="printed"}
|
||||
|
||||
[inputs]
|
||||
filament={}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
bearing_hub_top={version=0.1, quantity=1, type="printed"}
|
||||
bearing_hub_top={version=0.1, quantity={amount=1}, type="printed"}
|
||||
|
||||
[inputs]
|
||||
filament={}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
pump_housing_front_untapped={version=0.1, quantity=1, type="printed"}
|
||||
pump_housing_front_untapped={version=0.1, quantity={amount=1}, type="printed"}
|
||||
|
||||
[inputs]
|
||||
filament={}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
pump_housing_rear_untapped={version=0.1, quantity=1, type="printed"}
|
||||
pump_housing_rear_untapped={version=0.1, quantity={amount=1}, type="printed"}
|
||||
|
||||
[inputs]
|
||||
filament={}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
tube_support={version=0.1, quantity=1, type="printed"}
|
||||
tube_support={version=0.1, quantity={amount=1}, type="printed"}
|
||||
|
||||
[inputs]
|
||||
filament={}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[outputs]
|
||||
pump_housing_front={quantity=1}
|
||||
pump_housing_front={quantity={amount=1}}
|
||||
|
||||
[inputs]
|
||||
pump_housing_front_untapped={version=0.1, type="printed"}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
[outputs]
|
||||
pump_housing_front_with_stepper={quantity=1}
|
||||
pump_housing_front_with_stepper={quantity={amount=1}}
|
||||
|
||||
[inputs]
|
||||
pump_housing_front={version=0.1, quantity=1}
|
||||
m4_5_shcs={quantity=2, description="M4x5 SHCS"}
|
||||
stepper_28byj_48={quantity=1}
|
||||
pump_housing_front={version=0.1, quantity={amount=1}}
|
||||
m4_5_shcs={quantity={amount=2}, description="M4x5 SHCS"}
|
||||
stepper_28byj_48={quantity={amount=1}}
|
||||
|
||||
[dependencies]
|
||||
tools = [ "3 mm hex driver" ]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
pump_housing_rear = {quantity=1}
|
||||
pump_housing_rear = {quantity={amount=1}}
|
||||
|
||||
[inputs]
|
||||
pump_housing_rear_untapped = {quantity=1}
|
||||
pump_housing_rear_untapped = {quantity={amount=1}}
|
||||
|
||||
[dependencies]
|
||||
tools = [ "M4 tap" ]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
[outputs]
|
||||
pump_housing_rear_with_stepper = {quantity=1}
|
||||
pump_housing_rear_with_stepper = {quantity={amount=1}}
|
||||
|
||||
[inputs]
|
||||
pump_housing_rear = {version=0.1, quantity=1}
|
||||
m4_5_shcs = {quantity=2}
|
||||
stepper_28byj_48={quantity=1}
|
||||
pump_housing_rear = {version=0.1, quantity={amount=1}}
|
||||
m4_5_shcs = {quantity={amount=2}}
|
||||
stepper_28byj_48={quantity={amount=1}}
|
||||
|
||||
[dependencies]
|
||||
tools = [ "3 mm hex driver" ]
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
[outputs.bearing_624]
|
||||
quantity=10
|
||||
quantity = { amount=10 }
|
||||
comments = "13mm OD, 4mm ID, 5mm wide"
|
||||
|
||||
[inputs]
|
||||
capital={amount="8.99 usd"}
|
||||
capital={quantity={amount=8.99, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
filament = {amount="1 kg"}
|
||||
filament = { quantity = { amount = 1000, unit = "Gram" } }
|
||||
|
||||
[inputs]
|
||||
capital={amount="23.99 usd"}
|
||||
capital={ quantity = { amount=23.99, unit="USDollar" } }
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
m4_20_shcs = {quantity=100}
|
||||
m4_20_shcs = {quantity={amount=100}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="15.49 usd"}
|
||||
capital={quantity={amount=15.49, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
m4_25_bhcs = {quantity=100}
|
||||
m4_25_bhcs = {quantity={amount=100}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="18.45 usd"}
|
||||
capital={quantity={amount=18.45, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
m4_20_shcs = {quantity=100}
|
||||
m4_20_shcs = {quantity={amount=100}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="13.65 usd"}
|
||||
capital={quantity={amount=13.65, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
m4_nuts = {quantity=100}
|
||||
m4_nuts = {quantity={amount=100}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="1.35 usd"}
|
||||
capital = { quantity = { amount = 1.35, unit = "USDollar" } }
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
[outputs.m4_washer]
|
||||
quantity=100
|
||||
quantity = { amount=100 }
|
||||
comments = "4.4 mm ID, 8.8 mm OD, 0.8 mm thick"
|
||||
|
||||
[inputs]
|
||||
capital={amount="7.18 usd"}
|
||||
capital={quantity={amount=7.18, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
silicone_tubing = {amount="3 m"}
|
||||
silicone_tubing = {quantity={amount=3, unit="Meter"}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="18.75 usd"}
|
||||
capital={quantity={amount=18.75, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
[outputs]
|
||||
stepper_28byj_48 = {quantity=1}
|
||||
stepper_28byj_48 = {quantity={amount=1}}
|
||||
|
||||
[inputs]
|
||||
capital={amount="1.35 usd"}
|
||||
capital={quantity={amount=1.35, unit="USDollar"}}
|
||||
|
||||
[dependencies]
|
||||
operator = {skills=["vendor interaction"]}
|
||||
|
|
|
|||
|
|
@ -4,34 +4,30 @@
|
|||
// Amount(String),
|
||||
// }
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize)]
|
||||
pub enum Unit {
|
||||
USDollar,
|
||||
Meter,
|
||||
Gram,
|
||||
}
|
||||
|
||||
/// `Quantity` measures the amount of a resource (Input or Output).
|
||||
#[derive(Clone, Copy, Debug, serde::Deserialize)]
|
||||
pub struct Quantity {
|
||||
pub amount: f32,
|
||||
pub unit: Option<Unit>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct Input {
|
||||
/// Each input has either a `quantity` (for discrete things, like
|
||||
/// apples or bearings) or an `amount` (for continuous things,
|
||||
/// like rope or flour).
|
||||
///
|
||||
/// FIXME: Is there a good way to handle the "units" of the
|
||||
/// quantity/amount? Maybe the units could default to "count", but
|
||||
/// be optionally overridden by the recipe to things like "meters"
|
||||
/// (of tubing) or "grams" (of chromic acid). Then this struct
|
||||
/// would be `quantity: usize, units: Option<Unit>`.
|
||||
pub quantity: Option<usize>,
|
||||
pub amount: Option<String>,
|
||||
// Quantity defaults to "amount=1" if omitted.
|
||||
pub quantity: Option<Quantity>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct Output {
|
||||
/// Each output has either a `quantity` (for discrete things, like
|
||||
/// apples or bearings) or an `amount` (for continuous things,
|
||||
/// like rope or flour).
|
||||
///
|
||||
/// FIXME: Is there a good way to handle the "units" of the
|
||||
/// quantity/amount? Maybe the units could default to "count", but
|
||||
/// be optionally overridden by the recipe to things like "meters"
|
||||
/// (of tubing) or "grams" (of chromic acid). Then this struct
|
||||
/// would be `quantity: usize, units: Option<Unit>`.
|
||||
pub quantity: Option<usize>,
|
||||
pub amount: Option<String>,
|
||||
// Quantity defaults to "amount=1" if omitted.
|
||||
pub quantity: Option<Quantity>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
|
|
@ -77,6 +73,30 @@ pub struct Recipe {
|
|||
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 {
|
||||
pub fn from_file(file: &std::path::PathBuf) -> anyhow::Result<Self> {
|
||||
let recipe_contents = std::fs::read_to_string(file)?;
|
||||
|
|
@ -84,7 +104,9 @@ impl Recipe {
|
|||
// let r = recipe.validate_recipe();
|
||||
Ok(recipe)
|
||||
}
|
||||
}
|
||||
|
||||
impl Recipe {
|
||||
fn validate_recipe(self: &Self) -> anyhow::Result<()> {
|
||||
// if recipe.inputs.len() == 0 {
|
||||
// Err("recipe has no inputs!");
|
||||
|
|
|
|||
|
|
@ -85,51 +85,61 @@ impl Repo {
|
|||
}
|
||||
|
||||
fn compile_inner(self: &Self, recipe: &Recipe, indent: usize) -> anyhow::Result<()> {
|
||||
for (key, val) in recipe.inputs.iter() {
|
||||
for (input_name, input_info) in recipe.inputs.iter() {
|
||||
for _ in 0..indent {
|
||||
print!(" ");
|
||||
}
|
||||
|
||||
if key == "capital" {
|
||||
if input_name == "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.
|
||||
let input_quantity = input_info.get_quantity();
|
||||
|
||||
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:?} :-(",);
|
||||
}
|
||||
}
|
||||
}
|
||||
let input_unit = match &input_quantity.unit {
|
||||
None => {
|
||||
return Err(anyhow::Error::msg(format!(
|
||||
"expected quantity unit USDollar on capital input"
|
||||
)))
|
||||
}
|
||||
Some(unit) if *unit != crate::recipe::Unit::USDollar => {
|
||||
return Err(anyhow::Error::msg(format!(
|
||||
"expected quantity unit USDollar on capital input"
|
||||
)))
|
||||
}
|
||||
Some(unit) => unit,
|
||||
};
|
||||
|
||||
let cost = input_quantity.amount;
|
||||
|
||||
let outputs = match &recipe.outputs {
|
||||
None => return Err(anyhow::Error::msg(format!("no outputs!"))),
|
||||
Some(outputs) => outputs,
|
||||
};
|
||||
|
||||
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 {
|
||||
println!(
|
||||
"capital: {:.3} {:?} / {:?} :-(",
|
||||
cost_each, input_unit, output_unit
|
||||
);
|
||||
} else {
|
||||
println!("capital: {:.3} {:?} each :-(", cost_each, input_unit);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("no amount of capital?");
|
||||
return Err(anyhow::Error::msg(format!("no output!")));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
println!("{key:?} {val:?}");
|
||||
match self.get_recipe(key) {
|
||||
println!("{input_name:?} {input_info:?}");
|
||||
match self.get_recipe(input_name) {
|
||||
None => {
|
||||
return Err(anyhow::Error::msg(format!("recipe for {key} not found")));
|
||||
return Err(anyhow::Error::msg(format!(
|
||||
"recipe for {input_name} not found"
|
||||
)));
|
||||
}
|
||||
Some(input_recipe) => {
|
||||
self.compile_inner(input_recipe, indent + 4)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue