Add ingredient scaling

This commit is contained in:
Matteo Settenvini 2022-08-20 11:03:54 +02:00
parent bbcb32b119
commit d1803a16c6
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
3 changed files with 36 additions and 23 deletions

View file

@ -34,7 +34,9 @@ pub async fn with(
};
let ids = map_events_to_recipe_ids(api_client, calendar_name, &date_range).await?;
let ingredients = get_ingredients(api_client, ids).await?;
// TODO: make required_yield configurable!
let ingredients = get_ingredients(api_client, ids, 4.0).await?;
let ingredients = merge_ingredients(ingredients);
let md = prepare_grocery_list(&date_range, &ingredients)?;
log::debug!("Saving the following grocery list:\n\n{}", &md);
@ -68,6 +70,7 @@ where
async fn get_ingredients<RecipeIds>(
api_client: &ApiClient,
recipe_ids: RecipeIds,
required_yield: f64,
) -> Result<Vec<(Ingredient, String)>>
where
RecipeIds: IntoIterator<Item = usize>,
@ -88,9 +91,10 @@ where
response.json::<Recipe>().await.map(|r| {
log::info!("Retrieved ingredients for '{}'", r.name);
let recipe_name = r.name.clone();
let scale = required_yield / r.recipe_yield as f64;
r.ingredients
.into_iter()
.map(move |i| (i, recipe_name.clone()))
.map(move |i| (i * scale, recipe_name.clone()))
})
});

View file

@ -283,6 +283,15 @@ impl core::fmt::Display for Ingredient {
}
}
impl std::ops::Mul<f64> for Ingredient {
type Output = Ingredient;
fn mul(mut self, rhs: f64) -> Self::Output {
self.amount *= rhs;
self
}
}
impl core::fmt::Display for Unit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let u: &dyn core::fmt::Display = match self {