better error reporting
This commit is contained in:
parent
1c4d46e07b
commit
de2f803fc9
2 changed files with 38 additions and 9 deletions
|
|
@ -6,7 +6,7 @@ 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, serde::Deserialize)]
|
#[derive(Clone, Copy, PartialEq, serde::Deserialize)]
|
||||||
pub struct Quantity {
|
pub struct Quantity {
|
||||||
pub amount: f32,
|
pub amount: f32,
|
||||||
pub unit: Option<Unit>,
|
pub unit: Option<Unit>,
|
||||||
|
|
@ -37,7 +37,7 @@ pub struct Input {
|
||||||
pub quantity: Quantity,
|
pub quantity: Quantity,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize)]
|
#[derive(Debug, PartialEq, serde::Deserialize)]
|
||||||
pub struct Output {
|
pub struct Output {
|
||||||
// Quantity defaults to "amount=1" if omitted.
|
// Quantity defaults to "amount=1" if omitted.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
|
@ -91,7 +91,18 @@ pub struct Recipe {
|
||||||
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)?;
|
||||||
let recipe: Recipe = toml::from_str(&recipe_contents)?;
|
let mut recipe: Recipe = toml::from_str(&recipe_contents)?;
|
||||||
|
if recipe.outputs == None {
|
||||||
|
if let Some(recipe_name) = file.file_stem() {
|
||||||
|
let mut outputs = std::collections::HashMap::<String, Output>::new();
|
||||||
|
let key = recipe_name.to_string_lossy().into_owned();
|
||||||
|
let value = Output {
|
||||||
|
quantity: Quantity::default(),
|
||||||
|
};
|
||||||
|
outputs.insert(key, value);
|
||||||
|
recipe.outputs = Some(outputs);
|
||||||
|
}
|
||||||
|
}
|
||||||
// let r = recipe.validate_recipe();
|
// let r = recipe.validate_recipe();
|
||||||
Ok(recipe)
|
Ok(recipe)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,15 @@ impl Repo {
|
||||||
// println!("reading Recipe from {:?}", path);
|
// println!("reading Recipe from {:?}", path);
|
||||||
if let Some(recipe_name) = path.file_stem() {
|
if let Some(recipe_name) = path.file_stem() {
|
||||||
let key = recipe_name.to_string_lossy().into_owned();
|
let key = recipe_name.to_string_lossy().into_owned();
|
||||||
let value = crate::Recipe::from_file(path)?;
|
let value = match crate::Recipe::from_file(path) {
|
||||||
|
Ok(recipe) => recipe,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(anyhow::Error::msg(format!(
|
||||||
|
"failed to read recipe {}: {:?}",
|
||||||
|
key, e
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
self.recipes.insert(key, value);
|
self.recipes.insert(key, value);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -93,7 +101,15 @@ impl Repo {
|
||||||
indent: usize,
|
indent: usize,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
for (input_name, input_info) in recipe.inputs.iter() {
|
for (input_name, input_info) in recipe.inputs.iter() {
|
||||||
let input_recipe = self.get_recipe(input_name).unwrap();
|
let input_recipe = match self.get_recipe(input_name) {
|
||||||
|
Some(recipe) => recipe,
|
||||||
|
None => {
|
||||||
|
return Err(anyhow::Error::msg(format!(
|
||||||
|
"failed to get recipe '{}'",
|
||||||
|
input_name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// A Recipe whose only input is Capital is a Vitamin.
|
// A Recipe whose only input is Capital is a Vitamin.
|
||||||
let cost = match input_recipe.is_vitamin() {
|
let cost = match input_recipe.is_vitamin() {
|
||||||
|
|
@ -109,10 +125,12 @@ impl Repo {
|
||||||
.ok_or(anyhow::Error::msg(format!(
|
.ok_or(anyhow::Error::msg(format!(
|
||||||
"can't find input capital for {input_name}"
|
"can't find input capital for {input_name}"
|
||||||
)))?;
|
)))?;
|
||||||
assert_eq!(
|
if input_capital.quantity.unit != Some(crate::recipe::Unit::USDollar) {
|
||||||
input_capital.quantity.unit,
|
return Err(anyhow::Error::msg(format!(
|
||||||
Some(crate::recipe::Unit::USDollar)
|
"{} input capital does not have units USDollar",
|
||||||
);
|
input_name
|
||||||
|
)));
|
||||||
|
}
|
||||||
let cost = input_capital.quantity.amount;
|
let cost = input_capital.quantity.amount;
|
||||||
|
|
||||||
let outputs = match &input_recipe.outputs {
|
let outputs = match &input_recipe.outputs {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue