Rename Event -> Scheduling for readability

This commit is contained in:
Matteo Settenvini 2022-08-03 19:36:08 +02:00
parent e6288cfde1
commit f2eadbae2b
Signed by: matteo
GPG Key ID: 8576CC1AD97D42DF
3 changed files with 28 additions and 25 deletions

View File

@ -4,12 +4,13 @@
use { use {
crate::api_client::ApiClient, crate::api_client::ApiClient,
crate::commands::import, crate::commands::import,
crate::event::{Event, Meal},
crate::recipe, crate::recipe,
crate::scheduling::{Meal, Scheduling},
crate::{constants, helpers}, crate::{constants, helpers},
anyhow::{bail, Result}, anyhow::{bail, Result},
chrono::naive::NaiveDate, chrono::naive::NaiveDate,
futures::future::try_join_all, futures::future::try_join_all,
icalendar::Event,
reqwest::StatusCode, reqwest::StatusCode,
std::collections::{HashMap, HashSet}, std::collections::{HashMap, HashSet},
std::fmt::Write, std::fmt::Write,
@ -41,22 +42,22 @@ pub async fn with(
// so we have to resort to fetch all of them to match them // so we have to resort to fetch all of them to match them
let recipes = get_all_recipes(&api_client).await?; let recipes = get_all_recipes(&api_client).await?;
let events = records let schedulings = records
.iter() .iter()
.flat_map(|r| { .flat_map(|r| {
let lunch = recipes.get(&r.lunch); let lunch = recipes.get(&r.lunch);
let dinner = recipes.get(&r.dinner); let dinner = recipes.get(&r.dinner);
let events = [ let to_schedule = [
lunch.map(|recipe| Event::new(r.day, Meal::Lunch, recipe.clone())), lunch.map(|recipe| Scheduling::new(r.day, Meal::Lunch, recipe.clone())),
dinner.map(|recipe| Event::new(r.day, Meal::Dinner, recipe.clone())), dinner.map(|recipe| Scheduling::new(r.day, Meal::Dinner, recipe.clone())),
]; ];
events to_schedule
}) })
.flatten(); .flatten();
publish_events(&api_client, calendar, events, yearly_recurring_events).await?; publish_events(&api_client, calendar, schedulings, yearly_recurring_events).await?;
Ok(()) Ok(())
} }
@ -112,14 +113,14 @@ async fn get_all_recipes(api_client: &ApiClient) -> Result<HashMap<String, Rc<re
)) ))
} }
async fn publish_events<'a, EventsIter>( async fn publish_events<'a, SchedulingsIter>(
api_client: &ApiClient, api_client: &ApiClient,
calendar: &str, calendar: &str,
events: EventsIter, schedulings: SchedulingsIter,
yearly_recurring_events: bool, yearly_recurring_events: bool,
) -> Result<()> ) -> Result<()>
where where
EventsIter: Iterator<Item = Event>, SchedulingsIter: Iterator<Item = Scheduling>,
{ {
let calendar_url = api_client.caldav_base_url().join(&format!( let calendar_url = api_client.caldav_base_url().join(&format!(
"calendars/{}/{}/", "calendars/{}/{}/",
@ -128,21 +129,23 @@ where
))?; ))?;
let calendar_url = &calendar_url; let calendar_url = &calendar_url;
let update_requests = events.map(|ev| async move { let update_requests = schedulings.map(|scheduling| async move {
let url = calendar_url.join(&format!("{}.ics", ev.uid)).unwrap(); let url = calendar_url
.join(&format!("{}.ics", scheduling.uid))
.unwrap();
let alarm_text_repr = format!( let alarm_text_repr = format!(
"BEGIN:VALARM\nACTION:DISPLAY\nTRIGGER:-PT15M\nDESCRIPTION:{}\nEND:VALARM", "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!( let info_message = format!(
"Saving event at {} for '{}'", "Saving event at {} for '{}'",
&ev.ends_at.date(), &scheduling.ends_at.date(),
&ev.recipe.name &scheduling.recipe.name
); );
let end_time = ev.ends_at; let end_time = scheduling.ends_at;
let mut event: icalendar::Event = ev.into(); let mut event: Event = scheduling.into();
if yearly_recurring_events { if yearly_recurring_events {
use chrono::Datelike; use chrono::Datelike;

View File

@ -5,9 +5,9 @@ mod api_client;
mod commands; mod commands;
mod config; mod config;
mod constants; mod constants;
mod event;
mod helpers; mod helpers;
mod recipe; mod recipe;
mod scheduling;
use { use {
crate::api_client::ApiClient, crate::api_client::ApiClient,

View File

@ -4,11 +4,11 @@
use { use {
crate::recipe::Recipe, crate::recipe::Recipe,
chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc}, chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc},
icalendar::Event as CalEvent, icalendar::Event,
std::rc::Rc, std::rc::Rc,
}; };
pub struct Event { pub struct Scheduling {
pub uid: String, pub uid: String,
pub ends_at: NaiveDateTime, pub ends_at: NaiveDateTime,
pub recipe: Rc<Recipe>, pub recipe: Rc<Recipe>,
@ -20,7 +20,7 @@ pub enum Meal {
Dinner, Dinner,
} }
impl Event { impl Scheduling {
pub fn new(date: NaiveDate, meal: Meal, recipe: Rc<Recipe>) -> Self { pub fn new(date: NaiveDate, meal: Meal, recipe: Rc<Recipe>) -> Self {
let uid = format!( let uid = format!(
"{}-{}@{}.montecristosoftware.eu", "{}-{}@{}.montecristosoftware.eu",
@ -36,7 +36,7 @@ impl Event {
let ends_at = NaiveDateTime::new(date, meal_time); let ends_at = NaiveDateTime::new(date, meal_time);
Event { Self {
uid, uid,
ends_at, ends_at,
recipe, recipe,
@ -44,12 +44,12 @@ impl Event {
} }
} }
impl From<Event> for CalEvent { impl From<Scheduling> for Event {
fn from(ev: Event) -> Self { fn from(ev: Scheduling) -> Self {
use icalendar::Component; use icalendar::Component;
let start_time = ev.ends_at - ev.recipe.total_time(); let start_time = ev.ends_at - ev.recipe.total_time();
let cal_event = CalEvent::new() let cal_event = Event::new()
.uid(&ev.uid) .uid(&ev.uid)
.summary(&ev.recipe.name) .summary(&ev.recipe.name)
.description(&format!("cookbook@{}", ev.recipe.id)) .description(&format!("cookbook@{}", ev.recipe.id))