From d4d0adaacc1b1cde96d7a006ba9e6eeeb249e59f Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Thu, 14 Aug 2025 03:14:37 +0000 Subject: [PATCH] refactor --- src/main.rs | 68 +++++++++++++--------------------------------------- src/tests.rs | 12 +++++----- 2 files changed, 22 insertions(+), 58 deletions(-) diff --git a/src/main.rs b/src/main.rs index 67bcddc..b458a26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -277,38 +277,7 @@ fn google_url( link.query_pairs_mut().append_pair("eid", &eid); Ok(Some(link.to_string())) } -/* -impl EventInstance { - fn url(&self, google_id: Option<&str>) -> Result> { - if let Some(url) = self.ev.get_url() { - return Ok(Some(url.to_string())); - } - if let Some(google_id) = google_id { - return self.google_url(google_id); - } - Ok(None) - } -} -struct EventWithUrl<'a> { - calendar: &'a ConfigIcal, - dtstart: DatePerhapsTime, - ev: &'a icalendar::Event, - url: Option, -} - -impl<'a> EventWithUrl<'a> { - fn from_ei(calendar: &'a ConfigIcal, ei: EventInstance<'a>) -> Result> { - let url = ei.url(calendar.google_id.as_deref())?; - Ok(Self { - calendar, - dtstart: ei.dtstart, - ev: ei.ev, - url, - }) - } -} -*/ fn ical_event_instances( config_ical: &ConfigIcal, params: &Parameters, @@ -358,6 +327,9 @@ fn ical_event_instances( struct ICal { /// The parsed ics file cal: icalendar::Calendar, + + /// The config used to load this calendar + config: ConfigIcal, } /// Used to link recurrence exceptions to the original events they replace @@ -368,15 +340,15 @@ struct RecurrenceKey<'a> { } impl ICal { - fn read_from_str(s: &str) -> Result { + fn read_from_str(config: ConfigIcal, s: &str) -> Result { let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?; - let cal = Self { cal }; + let cal = Self { cal, config }; Ok(cal) } - fn read_from_downloadable(dl: &Downloadable) -> Result { - let s = std::fs::read_to_string(&dl.file_path)?; - Self::read_from_str(&s) + fn read_from_downloadable(config: ConfigIcal) -> Result { + let s = std::fs::read_to_string(&config.dl.file_path)?; + Self::read_from_str(config, &s) } fn events(&self) -> impl Iterator { @@ -390,16 +362,12 @@ impl ICal { } /// Returns an unsorted list of event instances for this calendar - fn event_instances( - &self, - config_ical: &ConfigIcal, - params: &Parameters, - ) -> Result> { + fn event_instances(&self, params: &Parameters) -> Result> { let mut instances = vec![]; let mut recurrence_exceptions = BTreeSet::new(); for ev in self.events() { - let eis = match ical_event_instances(config_ical, params, ev) + let eis = match ical_event_instances(&self.config, params, ev) .with_context(|| format!("Failed to process event with UID '{:?}'", ev.get_uid())) { Ok(x) => x, @@ -456,14 +424,14 @@ impl ICal { #[derive(Default)] struct Data { - icals: Vec<(ICal, ConfigIcal)>, + icals: Vec, } fn read_data_from_disk(config: &Config) -> Result { let mut data = Data::default(); - for config in &config.icals { - let cal = ICal::read_from_downloadable(&config.dl)?; - data.icals.push((cal, config.clone())); + for config_ical in &config.icals { + let cal = ICal::read_from_downloadable(config_ical.clone())?; + data.icals.push(cal); } Ok(data) @@ -477,8 +445,8 @@ fn process_data<'a>( let params = Parameters::new(now)?; let mut instances = vec![]; - for (ical, config) in &data.icals { - for ei in ical.event_instances(config, ¶ms)? { + for ical in &data.icals { + for ei in ical.event_instances(¶ms)? { if let Some(uid) = &ei.uid && config_output.hide_uids.contains(uid) { @@ -489,7 +457,6 @@ fn process_data<'a>( { continue; } - // let ei = EventWithUrl::from_ei(config, ei)?; instances.push(ei); } } @@ -498,7 +465,6 @@ fn process_data<'a>( Ok(instances) } -// FIXME: Don't print to stdout / stderr fn output_html( config: &ConfigOutput, instances: &[EventInstance], @@ -533,8 +499,6 @@ fn output_html( } } if last_date_printed != Some(date) { - // println!("{date}"); - // FIXME: De-dupe if !day_list.is_empty() { html_list.push(maud::html! { diff --git a/src/tests.rs b/src/tests.rs index 6432539..3216a80 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -64,10 +64,10 @@ END:VEVENT END:VCALENDAR "#; - let ical = ICal::read_from_str(s)?; + let ical = ICal::read_from_str(ConfigIcal::default(), s)?; let now = dt_from_ts(1755000000); let params = Parameters::new(now)?; - let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?; + let instances = ical.event_instances(¶ms)?; assert_eq!(instances.len(), 1); let event = &instances[0]; @@ -102,14 +102,14 @@ END:VEVENT END:VCALENDAR "#; - let ical = ICal::read_from_str(s)?; + let ical = ICal::read_from_str(ConfigIcal::default(), s)?; let params = Parameters { ignore_before: chicago_time(2025, 1, 1, 0, 0, 0), output_start: chicago_time(2025, 7, 1, 0, 0, 0), output_stop: chicago_time(2025, 10, 1, 0, 0, 0), tz: chrono_tz::America::Chicago, }; - let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?; + let instances = ical.event_instances(¶ms)?; assert_eq!( [instances[0].dtstart, instances[1].dtstart,], @@ -188,14 +188,14 @@ END:VEVENT END:VCALENDAR "#; - let ical = ICal::read_from_str(s)?; + let ical = ICal::read_from_str(ConfigIcal::default(), s)?; let params = Parameters { ignore_before: chicago_time(2025, 1, 1, 0, 0, 0), output_start: chicago_time(2025, 7, 1, 0, 0, 0), output_stop: chicago_time(2025, 10, 1, 0, 0, 0), tz: chrono_tz::America::Chicago, }; - let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?; + let instances = ical.event_instances(¶ms)?; assert_eq!( [