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 {
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<HashMap<String, Rc<re
))
}
async fn publish_events<'a, EventsIter>(
async fn publish_events<'a, SchedulingsIter>(
api_client: &ApiClient,
calendar: &str,
events: EventsIter,
schedulings: SchedulingsIter,
yearly_recurring_events: bool,
) -> Result<()>
where
EventsIter: Iterator<Item = Event>,
SchedulingsIter: Iterator<Item = Scheduling>,
{
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;

View File

@ -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,

View File

@ -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<Recipe>,
@ -20,7 +20,7 @@ pub enum Meal {
Dinner,
}
impl Event {
impl Scheduling {
pub fn new(date: NaiveDate, meal: Meal, recipe: Rc<Recipe>) -> 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<Event> for CalEvent {
fn from(ev: Event) -> Self {
impl From<Scheduling> 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))