Add ingredient scaling
This commit is contained in:
parent
bbcb32b119
commit
d1803a16c6
3 changed files with 36 additions and 23 deletions
|
@ -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()))
|
||||
})
|
||||
});
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue