From d1803a16c66ed4eeec933622f9c6a71e3d15eb6c Mon Sep 17 00:00:00 2001 From: Matteo Settenvini Date: Sat, 20 Aug 2022 11:03:54 +0200 Subject: [PATCH] Add ingredient scaling --- examples/example-schedule.csv | 42 +++++++++++++++++------------------ src/commands/groceries.rs | 8 +++++-- src/recipe.rs | 9 ++++++++ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/examples/example-schedule.csv b/examples/example-schedule.csv index 54bed71..5fc629d 100644 --- a/examples/example-schedule.csv +++ b/examples/example-schedule.csv @@ -114,13 +114,13 @@ "2022-04-23","sabato",, "2022-04-24","domenica","https://ricette.giallozafferano.it/Torta-Pasqualina.html", "2022-04-25","lunedì",, -"2022-04-26","martedì",, +"2022-04-26","martedì",,"https://ricette.giallozafferano.it/Fusilli-con-crema-di-piselli-e-pecorino.html" "2022-04-27","mercoledì",,"https://ricette.giallozafferano.it/Orata-al-forno.html" "2022-04-28","giovedì",, "2022-04-29","venerdì",,"https://ricette.giallozafferano.it/Cordon-bleu-di-melanzane.html" "2022-04-30","sabato",,"https://ricette.giallozafferano.it/Hamburger-di-ceci.html" "2022-05-01","domenica",, -"2022-05-02","lunedì",, +"2022-05-02","lunedì",,"https://ricette.giallozafferano.it/Pasta-panna-e-asparagi.html" "2022-05-03","martedì",, "2022-05-04","mercoledì",, "2022-05-05","giovedì",,"https://ricette.giallozafferano.it/Scaloppine-saporite-con-cotto-e-formaggio.html" @@ -132,7 +132,7 @@ "2022-05-11","mercoledì",,"https://ricette.giallozafferano.it/Mezze-maniche-al-tonno.html" "2022-05-12","giovedì",, "2022-05-13","venerdì",,"https://ricette.giallozafferano.it/Spiedini-di-pollo.html" -"2022-05-14","sabato",, +"2022-05-14","sabato",,"https://ricette.giallozafferano.it/Fusilli-con-crema-di-piselli-e-pecorino.html" "2022-05-15","domenica",, "2022-05-16","lunedì",, "2022-05-17","martedì",, @@ -169,15 +169,15 @@ "2022-06-17","venerdì",,"https://ricette.giallozafferano.it/Salmorejo.html" "2022-06-18","sabato","https://ricette.giallozafferano.it/Insalata-di-ceci-estiva.html", "2022-06-19","domenica",, -"2022-06-20","lunedì",, +"2022-06-20","lunedì",,"https://ricette.giallozafferano.it/Fusilli-con-crema-di-piselli-e-pecorino.html" "2022-06-21","martedì",, -"2022-06-22","mercoledì",,"https://ricette.giallozafferano.it/Pasta-fredda-con-pesto-senz-aglio.html" -"2022-06-23","giovedì",, -"2022-06-24","venerdì",, +"2022-06-22","mercoledì",, +"2022-06-23","giovedì",,"https://ricette.giallozafferano.it/Riso-saltato-con-tonno.html" +"2022-06-24","venerdì",,"https://ricette.giallozafferano.it/Pasta-fredda-con-pesto-senz-aglio.html" "2022-06-25","sabato","https://ricette.giallozafferano.it/Orata-al-forno.html", "2022-06-26","domenica",, "2022-06-27","lunedì",,"https://ricette.giallozafferano.it/Spaghetti-poveri.html" -"2022-06-28","martedì",, +"2022-06-28","martedì",,"https://ricette.giallozafferano.it/Insalata-di-orzo-con-pesto-di-rucola-fagiolini-e-zucchine.html" "2022-06-29","mercoledì",,"https://ricette.giallozafferano.it/Insalata-Shirazi.html" "2022-06-30","giovedì",,"https://ricette.giallozafferano.it/Scaloppine-saporite-con-cotto-e-formaggio.html" "2022-07-01","venerdì",, @@ -201,13 +201,13 @@ "2022-07-19","martedì",,"https://ricette.giallozafferano.it/Verdure-gratinate-al-forno.html" "2022-07-20","mercoledì",, "2022-07-21","giovedì",,"https://ricette.giallozafferano.it/Insalata-di-pasta-Mediterranea.html" -"2022-07-22","venerdì",, +"2022-07-22","venerdì",,"https://ricette.giallozafferano.it/Riso-saltato-con-tonno.html" "2022-07-23","sabato",,"https://ricette.giallozafferano.it/Cocotte-di-quinoa.html" "2022-07-24","domenica",, "2022-07-25","lunedì",,"https://ricette.giallozafferano.it/Tempeh-alle-verdure.html" "2022-07-26","martedì",,"https://ricette.giallozafferano.it/Spiedini-di-pollo.html" "2022-07-27","mercoledì",,"https://ricette.giallozafferano.it/Pasta-con-crema-di-peperoni.html" -"2022-07-28","giovedì",, +"2022-07-28","giovedì",,"https://ricette.giallozafferano.it/Insalata-di-orzo-con-pesto-di-rucola-fagiolini-e-zucchine.html" "2022-07-29","venerdì",,"https://ricette.giallozafferano.it/Orata-al-forno.html" "2022-07-30","sabato",, "2022-07-31","domenica",, @@ -231,22 +231,22 @@ "2022-08-18","giovedì",,"https://ricette.giallozafferano.it/Spaghetti-poveri.html" "2022-08-19","venerdì",,"https://ricette.giallozafferano.it/Bocconcini-di-pollo-con-yogurt-e-curcuma.html" "2022-08-20","sabato",,"https://ricette.giallozafferano.it/Salmorejo.html" -"2022-08-21","domenica",, +"2022-08-21","domenica","https://ricette.giallozafferano.it/Scaloppine-in-rosa.html", "2022-08-22","lunedì",,"https://ricette.giallozafferano.it/Insalata-di-quinoa-alla-greca.html" -"2022-08-23","martedì",, +"2022-08-23","martedì",,"https://ricette.giallozafferano.it/Omelette-caprese.html" "2022-08-24","mercoledì",,"https://ricette.giallozafferano.it/Pasta-fredda-con-pesto-senz-aglio.html" -"2022-08-25","giovedì",, -"2022-08-26","venerdì",,"https://ricette.giallozafferano.it/Spiedini-di-pollo.html" -"2022-08-27","sabato","https://ricette.giallozafferano.it/Orata-al-forno.html", +"2022-08-25","giovedì",,"https://ricette.giallozafferano.it/Braciole-in-salsa-all-arancia.html" +"2022-08-26","venerdì",,"https://ricette.giallozafferano.it/Pasta-ricotta-e-noci.html" +"2022-08-27","sabato","https://ricette.giallozafferano.it/Orata-al-forno.html","https://ricette.giallozafferano.it/Spiedini-di-pollo.html" "2022-08-28","domenica",, -"2022-08-29","lunedì",, +"2022-08-29","lunedì",,"https://ricette.giallozafferano.it/Riso-saltato-con-tonno.html" "2022-08-30","martedì",,"https://ricette.giallozafferano.it/Scaloppine-saporite-con-cotto-e-formaggio.html" "2022-08-31","mercoledì",,"https://ricette.giallozafferano.it/Tempeh-alle-verdure.html" -"2022-09-01","giovedì",, +"2022-09-01","giovedì",,"https://ricette.giallozafferano.it/Insalata-di-orzo-con-pesto-di-rucola-fagiolini-e-zucchine.html" "2022-09-02","venerdì",,"https://ricette.giallozafferano.it/Petto-di-pollo-ai-peperoni.html" "2022-09-03","sabato",, "2022-09-04","domenica",, -"2022-09-05","lunedì",, +"2022-09-05","lunedì",,"https://ricette.giallozafferano.it/Pasta-ricotta-e-noci.html" "2022-09-06","martedì",, "2022-09-07","mercoledì",, "2022-09-08","giovedì",,"https://ricette.giallozafferano.it/Pasta-con-le-melanzane.html" @@ -287,7 +287,7 @@ "2022-10-13","giovedì",,"https://ricette.giallozafferano.it/Maccheroncini-al-fume.html" "2022-10-14","venerdì",,"https://ricette.giallozafferano.it/Polpette-al-sugo.html" "2022-10-15","sabato",,"https://ricette.giallozafferano.it/Ratatouille.html" -"2022-10-16","domenica",, +"2022-10-16","domenica",,"https://ricette.giallozafferano.it/Pasta-ricotta-e-noci.html" "2022-10-17","lunedì",, "2022-10-18","martedì",, "2022-10-19","mercoledì",,"https://ricette.giallozafferano.it/Scaloppine-ai-funghi.html" @@ -317,7 +317,7 @@ "2022-11-12","sabato",,"https://ricette.giallozafferano.it/Chili-con-carne.html" "2022-11-13","domenica",, "2022-11-14","lunedì",, -"2022-11-15","martedì",, +"2022-11-15","martedì",,"https://ricette.giallozafferano.it/Lenticchie-e-salsiccia.html" "2022-11-16","mercoledì",, "2022-11-17","giovedì",, "2022-11-18","venerdì",,"https://ricette.giallozafferano.it/Torta-rustica.html" @@ -354,7 +354,7 @@ "2022-12-19","lunedì",, "2022-12-20","martedì",, "2022-12-21","mercoledì",, -"2022-12-22","giovedì",, +"2022-12-22","giovedì",,"https://ricette.giallozafferano.it/Lenticchie-e-salsiccia.html" "2022-12-23","venerdì",, "2022-12-24","sabato",, "2022-12-25","domenica","https://ricette.giallozafferano.it/Filetto-in-crosta.html", diff --git a/src/commands/groceries.rs b/src/commands/groceries.rs index db76bc1..9dec036 100644 --- a/src/commands/groceries.rs +++ b/src/commands/groceries.rs @@ -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( api_client: &ApiClient, recipe_ids: RecipeIds, + required_yield: f64, ) -> Result> where RecipeIds: IntoIterator, @@ -88,9 +91,10 @@ where response.json::().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())) }) }); diff --git a/src/recipe.rs b/src/recipe.rs index d229d65..6db80a7 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -283,6 +283,15 @@ impl core::fmt::Display for Ingredient { } } +impl std::ops::Mul 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 {