From f2eadbae2b917b99dbf5b4ee9760311f383fc2a2 Mon Sep 17 00:00:00 2001 From: Matteo Settenvini Date: Wed, 3 Aug 2022 19:36:08 +0200 Subject: [PATCH] Rename Event -> Scheduling for readability --- src/commands/schedule_csv.rs | 37 ++++++++++++++++++--------------- src/main.rs | 2 +- src/{event.rs => scheduling.rs} | 14 ++++++------- 3 files changed, 28 insertions(+), 25 deletions(-) rename src/{event.rs => scheduling.rs} (87%) diff --git a/src/commands/schedule_csv.rs b/src/commands/schedule_csv.rs index fa02251..a1ae949 100644 --- a/src/commands/schedule_csv.rs +++ b/src/commands/schedule_csv.rs @@ -4,12 +4,13 @@ use { crate::api_client::ApiClient, crate::commands::import, - crate::event::{Event, Meal}, crate::recipe, + crate::scheduling::{Meal, Scheduling}, crate::{constants, helpers}, anyhow::{bail, Result}, chrono::naive::NaiveDate, futures::future::try_join_all, + icalendar::Event, reqwest::StatusCode, std::collections::{HashMap, HashSet}, std::fmt::Write, @@ -41,22 +42,22 @@ pub async fn with( // so we have to resort to fetch all of them to match them let recipes = get_all_recipes(&api_client).await?; - let events = records + let schedulings = records .iter() .flat_map(|r| { let lunch = recipes.get(&r.lunch); let dinner = recipes.get(&r.dinner); - let events = [ - lunch.map(|recipe| Event::new(r.day, Meal::Lunch, recipe.clone())), - dinner.map(|recipe| Event::new(r.day, Meal::Dinner, recipe.clone())), + let to_schedule = [ + lunch.map(|recipe| Scheduling::new(r.day, Meal::Lunch, recipe.clone())), + dinner.map(|recipe| Scheduling::new(r.day, Meal::Dinner, recipe.clone())), ]; - events + to_schedule }) .flatten(); - publish_events(&api_client, calendar, events, yearly_recurring_events).await?; + publish_events(&api_client, calendar, schedulings, yearly_recurring_events).await?; Ok(()) } @@ -112,14 +113,14 @@ async fn get_all_recipes(api_client: &ApiClient) -> Result( +async fn publish_events<'a, SchedulingsIter>( api_client: &ApiClient, calendar: &str, - events: EventsIter, + schedulings: SchedulingsIter, yearly_recurring_events: bool, ) -> Result<()> where - EventsIter: Iterator, + SchedulingsIter: Iterator, { let calendar_url = api_client.caldav_base_url().join(&format!( "calendars/{}/{}/", @@ -128,21 +129,23 @@ where ))?; let calendar_url = &calendar_url; - let update_requests = events.map(|ev| async move { - let url = calendar_url.join(&format!("{}.ics", ev.uid)).unwrap(); + let update_requests = schedulings.map(|scheduling| async move { + let url = calendar_url + .join(&format!("{}.ics", scheduling.uid)) + .unwrap(); let alarm_text_repr = format!( "BEGIN:VALARM\nACTION:DISPLAY\nTRIGGER:-PT15M\nDESCRIPTION:{}\nEND:VALARM", - &helpers::ical_escape_text(&ev.recipe.name) + &helpers::ical_escape_text(&scheduling.recipe.name) ); let info_message = format!( "Saving event at {} for '{}'", - &ev.ends_at.date(), - &ev.recipe.name + &scheduling.ends_at.date(), + &scheduling.recipe.name ); - let end_time = ev.ends_at; - let mut event: icalendar::Event = ev.into(); + let end_time = scheduling.ends_at; + let mut event: Event = scheduling.into(); if yearly_recurring_events { use chrono::Datelike; diff --git a/src/main.rs b/src/main.rs index 1530f20..68e8f23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ mod api_client; mod commands; mod config; mod constants; -mod event; mod helpers; mod recipe; +mod scheduling; use { crate::api_client::ApiClient, diff --git a/src/event.rs b/src/scheduling.rs similarity index 87% rename from src/event.rs rename to src/scheduling.rs index c1009ae..4510d64 100644 --- a/src/event.rs +++ b/src/scheduling.rs @@ -4,11 +4,11 @@ use { crate::recipe::Recipe, chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc}, - icalendar::Event as CalEvent, + icalendar::Event, std::rc::Rc, }; -pub struct Event { +pub struct Scheduling { pub uid: String, pub ends_at: NaiveDateTime, pub recipe: Rc, @@ -20,7 +20,7 @@ pub enum Meal { Dinner, } -impl Event { +impl Scheduling { pub fn new(date: NaiveDate, meal: Meal, recipe: Rc) -> Self { let uid = format!( "{}-{}@{}.montecristosoftware.eu", @@ -36,7 +36,7 @@ impl Event { let ends_at = NaiveDateTime::new(date, meal_time); - Event { + Self { uid, ends_at, recipe, @@ -44,12 +44,12 @@ impl Event { } } -impl From for CalEvent { - fn from(ev: Event) -> Self { +impl From for Event { + fn from(ev: Scheduling) -> Self { use icalendar::Component; let start_time = ev.ends_at - ev.recipe.total_time(); - let cal_event = CalEvent::new() + let cal_event = Event::new() .uid(&ev.uid) .summary(&ev.recipe.name) .description(&format!("cookbook@{}", ev.recipe.id))