From bbcb32b119440976dce7edda1f9c31ae3bacdbee Mon Sep 17 00:00:00 2001 From: Matteo Settenvini Date: Sat, 13 Aug 2022 08:04:15 +0200 Subject: [PATCH] Add date range to grocery list --- src/api_client.rs | 2 +- src/commands/groceries.rs | 48 +++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/api_client.rs b/src/api_client.rs index 6217140..dfde2b9 100644 --- a/src/api_client.rs +++ b/src/api_client.rs @@ -101,7 +101,7 @@ impl ApiClient { pub async fn get_events( &self, calendar_name: &str, - date_range: Range>, + date_range: &Range>, ) -> Result> where Tz: chrono::TimeZone, diff --git a/src/commands/groceries.rs b/src/commands/groceries.rs index 3280340..db76bc1 100644 --- a/src/commands/groceries.rs +++ b/src/commands/groceries.rs @@ -20,23 +20,7 @@ pub async fn with( start_date: Option, days: u32, ) -> Result<()> { - let ids = map_events_to_recipe_ids(api_client, calendar_name, start_date, days).await?; - let ingredients = get_ingredients(api_client, ids).await?; - let ingredients = merge_ingredients(ingredients); - let md = prepare_grocery_list(&ingredients)?; - log::debug!("Saving the following grocery list:\n\n{}", &md); - save_grocery_list(api_client, location, &md).await?; - Ok(()) -} - -async fn map_events_to_recipe_ids( - api_client: &ApiClient, - calendar_name: &str, - start_date: Option, - days: u32, -) -> Result> { use chrono::{NaiveDateTime, NaiveTime, TimeZone}; - let start = start_date .map(|d| { let day_start = NaiveDateTime::new(d, NaiveTime::from_hms(0, 0, 0)); @@ -49,6 +33,24 @@ async fn map_events_to_recipe_ids( end: start + Duration::days(days as i64), }; + let ids = map_events_to_recipe_ids(api_client, calendar_name, &date_range).await?; + let ingredients = get_ingredients(api_client, ids).await?; + let ingredients = merge_ingredients(ingredients); + let md = prepare_grocery_list(&date_range, &ingredients)?; + log::debug!("Saving the following grocery list:\n\n{}", &md); + save_grocery_list(api_client, location, &md).await?; + Ok(()) +} + +async fn map_events_to_recipe_ids( + api_client: &ApiClient, + calendar_name: &str, + date_range: &Range>, +) -> Result> +where + Tz: chrono::TimeZone, + Tz::Offset: std::fmt::Display, +{ let all_events = api_client.get_events(calendar_name, date_range).await?; let recipe_id_regex: Regex = Regex::new(r"^cookbook@(\d+)$").unwrap(); @@ -131,14 +133,22 @@ fn merge_ingredients(mut ingredients: Vec<(Ingredient, String)>) -> Vec<(Ingredi merged_ingredients } -fn prepare_grocery_list(ingredients: &Vec<(Ingredient, Vec)>) -> Result { +fn prepare_grocery_list( + date_range: &Range>, + ingredients: &Vec<(Ingredient, Vec)>, +) -> Result +where + Tz: chrono::TimeZone, + Tz::Offset: std::fmt::Display, +{ let mut out = String::new(); use std::fmt::Write; writeln!( out, - "# Grocery list - {}", - chrono::Local::now().format("%Y-%m-%d").to_string() + "# Grocery list\n## From {} to {}", + date_range.start.date_naive(), + date_range.end.date_naive() )?; writeln!(out)?; // leave an empty line for (ingredient, recipes) in ingredients {