From 92c30167df2f61c89e400f67c9fb5fdccc8c5a30 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Thu, 14 Aug 2025 03:33:12 +0000 Subject: [PATCH] refactored to make it more input-agnostic --- src/main.rs | 47 +++++++++++++++++++++++++---------------------- src/wac_ical.rs | 17 +++++++++++++++-- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index df3674b..d97701a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,7 @@ struct CliAuto { } #[derive(clap::Parser)] -struct CliIcsDebug { +struct CliDebugOutput { #[arg(long)] config: Utf8PathBuf, } @@ -82,7 +82,7 @@ struct CliIcsDebug { #[derive(clap::Subcommand)] enum Commands { Auto(CliAuto), - IcsDebug(CliIcsDebug), + DebugOutput(CliDebugOutput), } #[derive(clap::Parser)] @@ -154,17 +154,26 @@ struct EventInstance { calendar_ui: CalendarUi, dtstart: DatePerhapsTime, location: Option, - recurrence_id: Option, + recurrence_id: Option, summary: Option, uid: Option, url: Option, } -/// Used to link recurrence exceptions to the original events they replace -#[derive(Eq, Ord, PartialOrd, PartialEq)] -struct RecurrenceKey<'a> { - recurrence_id: DatePerhapsTime, - uid: &'a str, +impl EventInstance { + fn filter(&self, config_output: &ConfigOutput) -> bool { + if let Some(uid) = &self.uid + && config_output.hide_uids.contains(uid) + { + return false; + } + if let Some(summary) = &self.summary + && config_output.hide_summaries.contains(summary) + { + return false; + } + true + } } #[derive(Default)] @@ -191,17 +200,11 @@ fn process_data<'a>( let mut instances = vec![]; for ical in &data.icals { - for ei in ical.event_instances(¶ms)? { - if let Some(uid) = &ei.uid - && config_output.hide_uids.contains(uid) - { - continue; - } - if let Some(summary) = &ei.summary - && config_output.hide_summaries.contains(summary) - { - continue; - } + for ei in ical + .event_instances(¶ms)? + .into_iter() + .filter(|x| x.filter(config_output)) + { instances.push(ei); } } @@ -412,7 +415,7 @@ async fn do_everything(cli: &CliAuto) -> Result<()> { } let bytes = resp.bytes().await?; - let temp_path = dl.file_path.with_extension(".ics.temp"); + let temp_path = dl.file_path.with_extension(".wac_temp"); std::fs::write(&temp_path, &bytes)?; std::fs::rename(&temp_path, &dl.file_path)?; } @@ -443,7 +446,7 @@ fn main_auto(cli: CliAuto) -> Result<()> { } } -fn main_ics_debug(cli: CliIcsDebug) -> Result<()> { +fn main_debug_output(cli: CliDebugOutput) -> Result<()> { tracing_subscriber::fmt::init(); tracing::info!("Started tracing"); let config = std::fs::read_to_string(&cli.config)?; @@ -464,6 +467,6 @@ fn main() -> Result<()> { match cli.command { Commands::Auto(x) => main_auto(x), - Commands::IcsDebug(x) => main_ics_debug(x), + Commands::DebugOutput(x) => main_debug_output(x), } } diff --git a/src/wac_ical.rs b/src/wac_ical.rs index 1913878..a7db026 100644 --- a/src/wac_ical.rs +++ b/src/wac_ical.rs @@ -1,6 +1,6 @@ //! Structs and functions specific to gathering input from ics files, which is a popular format that Google Calendar happens to put out -use super::{CalendarUi, DatePerhapsTime, Downloadable, EventInstance, Parameters, RecurrenceKey}; +use super::{CalendarUi, DatePerhapsTime, Downloadable, EventInstance, Parameters}; use anyhow::{Context as _, Result, anyhow}; use base64::Engine as _; use chrono::TimeZone as _; @@ -163,11 +163,17 @@ fn ical_event_instances( None }; + let recurrence_id = ev + .get_recurrence_id() + .as_ref() + .map(|x| normalize_date_perhaps_time(x, params.tz)) + .transpose()?; + Ok::<_, anyhow::Error>(EventInstance { calendar_ui: config_ical.ui.clone(), dtstart, location: ev.get_location().map(|s| s.to_string()), - recurrence_id: ev.get_recurrence_id(), + recurrence_id, summary: ev.get_summary().map(|s| s.to_string()), uid, url, @@ -177,6 +183,13 @@ fn ical_event_instances( instances } +/// Used to link recurrence exceptions to the original events they replace +#[derive(Eq, Ord, PartialOrd, PartialEq)] +struct RecurrenceKey<'a> { + recurrence_id: DatePerhapsTime, + uid: &'a str, +} + impl Calendar { pub(crate) fn read_from_str(config: Config, s: &str) -> Result { let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?;