@@ -40,7 +40,7 @@ impl DateRange { |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | | -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 43 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 44 | 44 | pub struct EventDateTime { |
| 45 | 45 | pub date: CalendarDate, |
| 46 | 46 | pub time: Time, |
@@ -141,6 +141,117 @@ impl EventTiming { |
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 145 | +pub enum RecurrenceFrequency { |
| 146 | + Daily, |
| 147 | + Weekly, |
| 148 | + Monthly, |
| 149 | + Yearly, |
| 150 | +} |
| 151 | + |
| 152 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 153 | +pub enum RecurrenceEnd { |
| 154 | + Never, |
| 155 | + Until(CalendarDate), |
| 156 | + Count(u32), |
| 157 | +} |
| 158 | + |
| 159 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 160 | +pub enum RecurrenceOrdinal { |
| 161 | + Number(u8), |
| 162 | + Last, |
| 163 | +} |
| 164 | + |
| 165 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 166 | +pub enum RecurrenceMonthlyRule { |
| 167 | + DayOfMonth(u8), |
| 168 | + WeekdayOrdinal { |
| 169 | + ordinal: RecurrenceOrdinal, |
| 170 | + weekday: Weekday, |
| 171 | + }, |
| 172 | +} |
| 173 | + |
| 174 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 175 | +pub enum RecurrenceYearlyRule { |
| 176 | + Date { |
| 177 | + month: Month, |
| 178 | + day: u8, |
| 179 | + }, |
| 180 | + WeekdayOrdinal { |
| 181 | + month: Month, |
| 182 | + ordinal: RecurrenceOrdinal, |
| 183 | + weekday: Weekday, |
| 184 | + }, |
| 185 | +} |
| 186 | + |
| 187 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 188 | +pub struct RecurrenceRule { |
| 189 | + pub frequency: RecurrenceFrequency, |
| 190 | + pub interval: u16, |
| 191 | + pub end: RecurrenceEnd, |
| 192 | + pub weekdays: Vec<Weekday>, |
| 193 | + pub monthly: Option<RecurrenceMonthlyRule>, |
| 194 | + pub yearly: Option<RecurrenceYearlyRule>, |
| 195 | +} |
| 196 | + |
| 197 | +impl RecurrenceRule { |
| 198 | + pub fn new(frequency: RecurrenceFrequency) -> Self { |
| 199 | + Self { |
| 200 | + frequency, |
| 201 | + interval: 1, |
| 202 | + end: RecurrenceEnd::Never, |
| 203 | + weekdays: Vec::new(), |
| 204 | + monthly: None, |
| 205 | + yearly: None, |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + pub fn with_interval(mut self, interval: u16) -> Self { |
| 210 | + self.interval = interval.max(1); |
| 211 | + self |
| 212 | + } |
| 213 | + |
| 214 | + pub fn interval(&self) -> u16 { |
| 215 | + self.interval.max(1) |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 220 | +pub enum OccurrenceAnchor { |
| 221 | + AllDay { date: CalendarDate }, |
| 222 | + Timed { start: EventDateTime }, |
| 223 | +} |
| 224 | + |
| 225 | +impl OccurrenceAnchor { |
| 226 | + pub const fn date(self) -> CalendarDate { |
| 227 | + match self { |
| 228 | + Self::AllDay { date } => date, |
| 229 | + Self::Timed { start } => start.date, |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + fn storage_key(self) -> String { |
| 234 | + match self { |
| 235 | + Self::AllDay { date } => format!("{date}"), |
| 236 | + Self::Timed { start } => { |
| 237 | + format!("{}T{}", start.date, format_time(start.time)) |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 244 | +pub struct OccurrenceMetadata { |
| 245 | + pub series_id: String, |
| 246 | + pub anchor: OccurrenceAnchor, |
| 247 | +} |
| 248 | + |
| 249 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 250 | +pub struct OccurrenceOverride { |
| 251 | + pub anchor: OccurrenceAnchor, |
| 252 | + pub draft: CreateEventDraft, |
| 253 | +} |
| 254 | + |
| 144 | 255 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 145 | 256 | pub struct Event { |
| 146 | 257 | pub id: String, |
@@ -150,6 +261,9 @@ pub struct Event { |
| 150 | 261 | pub reminders: Vec<Reminder>, |
| 151 | 262 | pub source: SourceMetadata, |
| 152 | 263 | pub timing: EventTiming, |
| 264 | + pub recurrence: Option<RecurrenceRule>, |
| 265 | + pub occurrence: Option<OccurrenceMetadata>, |
| 266 | + pub occurrence_overrides: Vec<OccurrenceOverride>, |
| 153 | 267 | } |
| 154 | 268 | |
| 155 | 269 | impl Event { |
@@ -167,6 +281,9 @@ impl Event { |
| 167 | 281 | reminders: Vec::new(), |
| 168 | 282 | source, |
| 169 | 283 | timing: EventTiming::AllDay { date }, |
| 284 | + recurrence: None, |
| 285 | + occurrence: None, |
| 286 | + occurrence_overrides: Vec::new(), |
| 170 | 287 | } |
| 171 | 288 | } |
| 172 | 289 | |
@@ -189,6 +306,9 @@ impl Event { |
| 189 | 306 | reminders: Vec::new(), |
| 190 | 307 | source, |
| 191 | 308 | timing: EventTiming::Timed { start, end }, |
| 309 | + recurrence: None, |
| 310 | + occurrence: None, |
| 311 | + occurrence_overrides: Vec::new(), |
| 192 | 312 | }) |
| 193 | 313 | } |
| 194 | 314 | |
@@ -207,6 +327,11 @@ impl Event { |
| 207 | 327 | self |
| 208 | 328 | } |
| 209 | 329 | |
| 330 | + pub fn with_recurrence(mut self, recurrence: RecurrenceRule) -> Self { |
| 331 | + self.recurrence = Some(recurrence); |
| 332 | + self |
| 333 | + } |
| 334 | + |
| 210 | 335 | pub const fn is_all_day(&self) -> bool { |
| 211 | 336 | self.timing.is_all_day() |
| 212 | 337 | } |
@@ -219,6 +344,14 @@ impl Event { |
| 219 | 344 | self.source.source_id == "local" |
| 220 | 345 | } |
| 221 | 346 | |
| 347 | + pub const fn is_recurring_series(&self) -> bool { |
| 348 | + self.recurrence.is_some() |
| 349 | + } |
| 350 | + |
| 351 | + pub const fn occurrence(&self) -> Option<&OccurrenceMetadata> { |
| 352 | + self.occurrence.as_ref() |
| 353 | + } |
| 354 | + |
| 222 | 355 | pub fn intersects_range(&self, range: DateRange) -> bool { |
| 223 | 356 | match self.timing { |
| 224 | 357 | EventTiming::AllDay { date } => range.contains_date(date), |
@@ -239,6 +372,7 @@ pub struct CreateEventDraft { |
| 239 | 372 | pub location: Option<String>, |
| 240 | 373 | pub notes: Option<String>, |
| 241 | 374 | pub reminders: Vec<Reminder>, |
| 375 | + pub recurrence: Option<RecurrenceRule>, |
| 242 | 376 | } |
| 243 | 377 | |
| 244 | 378 | impl CreateEventDraft { |
@@ -254,8 +388,14 @@ impl CreateEventDraft { |
| 254 | 388 | event.location = self.location; |
| 255 | 389 | event.notes = self.notes; |
| 256 | 390 | event.reminders = self.reminders; |
| 391 | + event.recurrence = self.recurrence; |
| 257 | 392 | Ok(event) |
| 258 | 393 | } |
| 394 | + |
| 395 | + fn without_recurrence(mut self) -> Self { |
| 396 | + self.recurrence = None; |
| 397 | + self |
| 398 | + } |
| 259 | 399 | } |
| 260 | 400 | |
| 261 | 401 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
@@ -395,6 +535,10 @@ pub trait AgendaSource { |
| 395 | 535 | fn events_intersecting(&self, range: DateRange) -> Vec<Event>; |
| 396 | 536 | |
| 397 | 537 | fn holidays_in(&self, range: DateRange) -> Vec<Holiday>; |
| 538 | + |
| 539 | + fn local_event_by_id(&self, _id: &str) -> Option<Event> { |
| 540 | + None |
| 541 | + } |
| 398 | 542 | } |
| 399 | 543 | |
| 400 | 544 | #[derive(Debug)] |
@@ -460,13 +604,18 @@ impl ConfiguredAgendaSource { |
| 460 | 604 | return Err(LocalEventStoreError::EventNotEditable { id: id.to_string() }); |
| 461 | 605 | } |
| 462 | 606 | |
| 463 | | - let event = |
| 607 | + let mut event = |
| 464 | 608 | draft |
| 465 | 609 | .into_event(id.to_string()) |
| 466 | 610 | .map_err(|err| LocalEventStoreError::Encode { |
| 467 | 611 | path: self.events_file.clone(), |
| 468 | 612 | reason: err.to_string(), |
| 469 | 613 | })?; |
| 614 | + let existing_overrides = std::mem::take(&mut events[index].occurrence_overrides); |
| 615 | + event.occurrence_overrides = existing_overrides |
| 616 | + .into_iter() |
| 617 | + .filter(|override_record| event_generates_anchor(&event, override_record.anchor)) |
| 618 | + .collect(); |
| 470 | 619 | events[index] = event.clone(); |
| 471 | 620 | |
| 472 | 621 | if let Some(path) = &self.events_file { |
@@ -476,6 +625,58 @@ impl ConfiguredAgendaSource { |
| 476 | 625 | Ok(event) |
| 477 | 626 | } |
| 478 | 627 | |
| 628 | + pub fn update_occurrence( |
| 629 | + &mut self, |
| 630 | + series_id: &str, |
| 631 | + anchor: OccurrenceAnchor, |
| 632 | + draft: CreateEventDraft, |
| 633 | + ) -> Result<Event, LocalEventStoreError> { |
| 634 | + let mut events = self.events.events().to_vec(); |
| 635 | + let Some(index) = events.iter().position(|event| event.id == series_id) else { |
| 636 | + return Err(LocalEventStoreError::EventNotFound { |
| 637 | + id: series_id.to_string(), |
| 638 | + }); |
| 639 | + }; |
| 640 | + if !events[index].is_local() || !events[index].is_recurring_series() { |
| 641 | + return Err(LocalEventStoreError::EventNotEditable { |
| 642 | + id: series_id.to_string(), |
| 643 | + }); |
| 644 | + } |
| 645 | + if !event_generates_anchor(&events[index], anchor) { |
| 646 | + return Err(LocalEventStoreError::OccurrenceNotFound { |
| 647 | + id: series_id.to_string(), |
| 648 | + anchor: anchor.storage_key(), |
| 649 | + }); |
| 650 | + } |
| 651 | + |
| 652 | + let override_record = OccurrenceOverride { |
| 653 | + anchor, |
| 654 | + draft: draft.without_recurrence(), |
| 655 | + }; |
| 656 | + if let Some(existing) = events[index] |
| 657 | + .occurrence_overrides |
| 658 | + .iter_mut() |
| 659 | + .find(|existing| existing.anchor == anchor) |
| 660 | + { |
| 661 | + *existing = override_record; |
| 662 | + } else { |
| 663 | + events[index].occurrence_overrides.push(override_record); |
| 664 | + } |
| 665 | + |
| 666 | + let event = occurrence_override_event(&events[index], anchor).ok_or_else(|| { |
| 667 | + LocalEventStoreError::OccurrenceNotFound { |
| 668 | + id: series_id.to_string(), |
| 669 | + anchor: anchor.storage_key(), |
| 670 | + } |
| 671 | + })?; |
| 672 | + |
| 673 | + if let Some(path) = &self.events_file { |
| 674 | + write_events_file(path, &events)?; |
| 675 | + } |
| 676 | + self.events.events = events; |
| 677 | + Ok(event) |
| 678 | + } |
| 679 | + |
| 479 | 680 | fn next_local_event_id(&self, title: &str) -> String { |
| 480 | 681 | let now = SystemTime::now() |
| 481 | 682 | .duration_since(UNIX_EPOCH) |
@@ -499,6 +700,10 @@ impl AgendaSource for ConfiguredAgendaSource { |
| 499 | 700 | fn holidays_in(&self, range: DateRange) -> Vec<Holiday> { |
| 500 | 701 | self.holidays.holidays_in(range) |
| 501 | 702 | } |
| 703 | + |
| 704 | + fn local_event_by_id(&self, id: &str) -> Option<Event> { |
| 705 | + self.events.local_event_by_id(id) |
| 706 | + } |
| 502 | 707 | } |
| 503 | 708 | |
| 504 | 709 | #[derive(Debug)] |
@@ -761,11 +966,17 @@ impl InMemoryAgendaSource { |
| 761 | 966 | |
| 762 | 967 | impl AgendaSource for InMemoryAgendaSource { |
| 763 | 968 | fn events_intersecting(&self, range: DateRange) -> Vec<Event> { |
| 764 | | - self.events |
| 969 | + let mut events = self |
| 970 | + .events |
| 765 | 971 | .iter() |
| 766 | | - .filter(|event| event.intersects_range(range)) |
| 767 | | - .cloned() |
| 768 | | - .collect() |
| 972 | + .flat_map(|event| events_intersecting_range(event, range)) |
| 973 | + .collect::<Vec<_>>(); |
| 974 | + events.sort_by(|left, right| { |
| 975 | + event_sort_key(left) |
| 976 | + .cmp(&event_sort_key(right)) |
| 977 | + .then(left.id.cmp(&right.id)) |
| 978 | + }); |
| 979 | + events |
| 769 | 980 | } |
| 770 | 981 | |
| 771 | 982 | fn holidays_in(&self, range: DateRange) -> Vec<Holiday> { |
@@ -775,6 +986,331 @@ impl AgendaSource for InMemoryAgendaSource { |
| 775 | 986 | .cloned() |
| 776 | 987 | .collect() |
| 777 | 988 | } |
| 989 | + |
| 990 | + fn local_event_by_id(&self, id: &str) -> Option<Event> { |
| 991 | + self.events |
| 992 | + .iter() |
| 993 | + .find(|event| event.id == id && event.is_local()) |
| 994 | + .cloned() |
| 995 | + } |
| 996 | +} |
| 997 | + |
| 998 | +fn events_intersecting_range(event: &Event, range: DateRange) -> Vec<Event> { |
| 999 | + if event.recurrence.is_none() { |
| 1000 | + return event |
| 1001 | + .intersects_range(range) |
| 1002 | + .then(|| event.clone()) |
| 1003 | + .into_iter() |
| 1004 | + .collect(); |
| 1005 | + } |
| 1006 | + |
| 1007 | + expand_recurring_event(event, range) |
| 1008 | + .into_iter() |
| 1009 | + .filter(|event| event.intersects_range(range)) |
| 1010 | + .collect() |
| 1011 | +} |
| 1012 | + |
| 1013 | +fn expand_recurring_event(event: &Event, range: DateRange) -> Vec<Event> { |
| 1014 | + let Some(recurrence) = &event.recurrence else { |
| 1015 | + return Vec::new(); |
| 1016 | + }; |
| 1017 | + let Some(start_date) = event_start_date(event) else { |
| 1018 | + return Vec::new(); |
| 1019 | + }; |
| 1020 | + |
| 1021 | + let mut events = Vec::new(); |
| 1022 | + let final_date = range.end.add_days(-1); |
| 1023 | + let mut date = start_date; |
| 1024 | + let mut generated_count = 0_u32; |
| 1025 | + |
| 1026 | + while date <= final_date { |
| 1027 | + if let RecurrenceEnd::Until(until) = recurrence.end |
| 1028 | + && date > until |
| 1029 | + { |
| 1030 | + break; |
| 1031 | + } |
| 1032 | + |
| 1033 | + if recurs_on_date(date, start_date, recurrence) { |
| 1034 | + generated_count = generated_count.saturating_add(1); |
| 1035 | + if let RecurrenceEnd::Count(max_count) = recurrence.end |
| 1036 | + && generated_count > max_count |
| 1037 | + { |
| 1038 | + break; |
| 1039 | + } |
| 1040 | + |
| 1041 | + let anchor = occurrence_anchor_for_date(event, date); |
| 1042 | + let instance = occurrence_override_event(event, anchor) |
| 1043 | + .unwrap_or_else(|| generated_occurrence_event(event, anchor)); |
| 1044 | + events.push(instance); |
| 1045 | + } |
| 1046 | + |
| 1047 | + date = date.add_days(1); |
| 1048 | + } |
| 1049 | + |
| 1050 | + events |
| 1051 | +} |
| 1052 | + |
| 1053 | +fn event_generates_anchor(event: &Event, anchor: OccurrenceAnchor) -> bool { |
| 1054 | + let Some(recurrence) = &event.recurrence else { |
| 1055 | + return false; |
| 1056 | + }; |
| 1057 | + let Some(start_date) = event_start_date(event) else { |
| 1058 | + return false; |
| 1059 | + }; |
| 1060 | + if anchor.date() < start_date || !recurs_on_date(anchor.date(), start_date, recurrence) { |
| 1061 | + return false; |
| 1062 | + } |
| 1063 | + if !anchor_is_within_recurrence_end(anchor.date(), start_date, recurrence) { |
| 1064 | + return false; |
| 1065 | + } |
| 1066 | + occurrence_anchor_for_date(event, anchor.date()) == anchor |
| 1067 | +} |
| 1068 | + |
| 1069 | +fn anchor_is_within_recurrence_end( |
| 1070 | + anchor_date: CalendarDate, |
| 1071 | + start_date: CalendarDate, |
| 1072 | + recurrence: &RecurrenceRule, |
| 1073 | +) -> bool { |
| 1074 | + if let RecurrenceEnd::Until(until) = recurrence.end |
| 1075 | + && anchor_date > until |
| 1076 | + { |
| 1077 | + return false; |
| 1078 | + } |
| 1079 | + |
| 1080 | + if let RecurrenceEnd::Count(max_count) = recurrence.end { |
| 1081 | + let mut count = 0_u32; |
| 1082 | + let mut date = start_date; |
| 1083 | + while date <= anchor_date { |
| 1084 | + if recurs_on_date(date, start_date, recurrence) { |
| 1085 | + count = count.saturating_add(1); |
| 1086 | + } |
| 1087 | + date = date.add_days(1); |
| 1088 | + } |
| 1089 | + return count <= max_count; |
| 1090 | + } |
| 1091 | + |
| 1092 | + true |
| 1093 | +} |
| 1094 | + |
| 1095 | +fn occurrence_override_event(series: &Event, anchor: OccurrenceAnchor) -> Option<Event> { |
| 1096 | + let override_record = series |
| 1097 | + .occurrence_overrides |
| 1098 | + .iter() |
| 1099 | + .find(|override_record| override_record.anchor == anchor)?; |
| 1100 | + occurrence_event_from_draft(series, anchor, override_record.draft.clone()).ok() |
| 1101 | +} |
| 1102 | + |
| 1103 | +fn generated_occurrence_event(series: &Event, anchor: OccurrenceAnchor) -> Event { |
| 1104 | + let mut event = series.clone(); |
| 1105 | + event.id = occurrence_event_id(series, anchor); |
| 1106 | + event.timing = occurrence_timing(series, anchor); |
| 1107 | + event.occurrence = Some(OccurrenceMetadata { |
| 1108 | + series_id: series.id.clone(), |
| 1109 | + anchor, |
| 1110 | + }); |
| 1111 | + event.recurrence = None; |
| 1112 | + event.occurrence_overrides = Vec::new(); |
| 1113 | + event |
| 1114 | +} |
| 1115 | + |
| 1116 | +fn occurrence_event_from_draft( |
| 1117 | + series: &Event, |
| 1118 | + anchor: OccurrenceAnchor, |
| 1119 | + draft: CreateEventDraft, |
| 1120 | +) -> Result<Event, AgendaError> { |
| 1121 | + let mut event = draft |
| 1122 | + .without_recurrence() |
| 1123 | + .into_event(occurrence_event_id(series, anchor))?; |
| 1124 | + event.source = series.source.clone(); |
| 1125 | + event.occurrence = Some(OccurrenceMetadata { |
| 1126 | + series_id: series.id.clone(), |
| 1127 | + anchor, |
| 1128 | + }); |
| 1129 | + Ok(event) |
| 1130 | +} |
| 1131 | + |
| 1132 | +fn occurrence_event_id(series: &Event, anchor: OccurrenceAnchor) -> String { |
| 1133 | + format!("{}#{}", series.id, anchor.storage_key()) |
| 1134 | +} |
| 1135 | + |
| 1136 | +fn occurrence_anchor_for_date(event: &Event, date: CalendarDate) -> OccurrenceAnchor { |
| 1137 | + match event.timing { |
| 1138 | + EventTiming::AllDay { .. } => OccurrenceAnchor::AllDay { date }, |
| 1139 | + EventTiming::Timed { start, .. } => OccurrenceAnchor::Timed { |
| 1140 | + start: EventDateTime::new(date, start.time), |
| 1141 | + }, |
| 1142 | + } |
| 1143 | +} |
| 1144 | + |
| 1145 | +fn occurrence_timing(event: &Event, anchor: OccurrenceAnchor) -> EventTiming { |
| 1146 | + match (event.timing, anchor) { |
| 1147 | + (EventTiming::AllDay { .. }, OccurrenceAnchor::AllDay { date }) => { |
| 1148 | + EventTiming::AllDay { date } |
| 1149 | + } |
| 1150 | + ( |
| 1151 | + EventTiming::Timed { start, end }, |
| 1152 | + OccurrenceAnchor::Timed { |
| 1153 | + start: anchor_start, |
| 1154 | + }, |
| 1155 | + ) => { |
| 1156 | + let duration_minutes = datetime_distance_minutes(start, end); |
| 1157 | + EventTiming::Timed { |
| 1158 | + start: anchor_start, |
| 1159 | + end: add_minutes(anchor_start, duration_minutes), |
| 1160 | + } |
| 1161 | + } |
| 1162 | + _ => event.timing, |
| 1163 | + } |
| 1164 | +} |
| 1165 | + |
| 1166 | +fn event_start_date(event: &Event) -> Option<CalendarDate> { |
| 1167 | + match event.timing { |
| 1168 | + EventTiming::AllDay { date } => Some(date), |
| 1169 | + EventTiming::Timed { start, .. } => Some(start.date), |
| 1170 | + } |
| 1171 | +} |
| 1172 | + |
| 1173 | +fn recurs_on_date(date: CalendarDate, start_date: CalendarDate, rule: &RecurrenceRule) -> bool { |
| 1174 | + if date < start_date { |
| 1175 | + return false; |
| 1176 | + } |
| 1177 | + |
| 1178 | + match rule.frequency { |
| 1179 | + RecurrenceFrequency::Daily => { |
| 1180 | + days_between(start_date, date) % i32::from(rule.interval()) == 0 |
| 1181 | + } |
| 1182 | + RecurrenceFrequency::Weekly => { |
| 1183 | + let days = days_between(start_date, date); |
| 1184 | + let week_index = days / 7; |
| 1185 | + let weekdays = recurrence_weekdays(rule, start_date); |
| 1186 | + week_index % i32::from(rule.interval()) == 0 && weekdays.contains(&date.weekday()) |
| 1187 | + } |
| 1188 | + RecurrenceFrequency::Monthly => { |
| 1189 | + let months = months_between(start_date, date); |
| 1190 | + if months < 0 || months % i32::from(rule.interval()) != 0 { |
| 1191 | + return false; |
| 1192 | + } |
| 1193 | + let monthly = rule |
| 1194 | + .monthly |
| 1195 | + .unwrap_or(RecurrenceMonthlyRule::DayOfMonth(start_date.day())); |
| 1196 | + match monthly { |
| 1197 | + RecurrenceMonthlyRule::DayOfMonth(day) => { |
| 1198 | + CalendarDate::from_ymd(date.year(), date.month(), day).ok() == Some(date) |
| 1199 | + } |
| 1200 | + RecurrenceMonthlyRule::WeekdayOrdinal { ordinal, weekday } => { |
| 1201 | + weekday_ordinal_date(date.year(), date.month(), ordinal, weekday) == Some(date) |
| 1202 | + } |
| 1203 | + } |
| 1204 | + } |
| 1205 | + RecurrenceFrequency::Yearly => { |
| 1206 | + let years = date.year() - start_date.year(); |
| 1207 | + if years < 0 || years % i32::from(rule.interval()) != 0 { |
| 1208 | + return false; |
| 1209 | + } |
| 1210 | + let yearly = rule.yearly.unwrap_or(RecurrenceYearlyRule::Date { |
| 1211 | + month: start_date.month(), |
| 1212 | + day: start_date.day(), |
| 1213 | + }); |
| 1214 | + match yearly { |
| 1215 | + RecurrenceYearlyRule::Date { month, day } => { |
| 1216 | + CalendarDate::from_ymd(date.year(), month, day).ok() == Some(date) |
| 1217 | + } |
| 1218 | + RecurrenceYearlyRule::WeekdayOrdinal { |
| 1219 | + month, |
| 1220 | + ordinal, |
| 1221 | + weekday, |
| 1222 | + } => { |
| 1223 | + date.month() == month |
| 1224 | + && weekday_ordinal_date(date.year(), month, ordinal, weekday) == Some(date) |
| 1225 | + } |
| 1226 | + } |
| 1227 | + } |
| 1228 | + } |
| 1229 | +} |
| 1230 | + |
| 1231 | +fn recurrence_weekdays(rule: &RecurrenceRule, start_date: CalendarDate) -> Vec<Weekday> { |
| 1232 | + if rule.weekdays.is_empty() { |
| 1233 | + vec![start_date.weekday()] |
| 1234 | + } else { |
| 1235 | + rule.weekdays.clone() |
| 1236 | + } |
| 1237 | +} |
| 1238 | + |
| 1239 | +fn weekday_ordinal_date( |
| 1240 | + year: i32, |
| 1241 | + month: Month, |
| 1242 | + ordinal: RecurrenceOrdinal, |
| 1243 | + weekday: Weekday, |
| 1244 | +) -> Option<CalendarDate> { |
| 1245 | + match ordinal { |
| 1246 | + RecurrenceOrdinal::Number(number) if (1..=4).contains(&number) => { |
| 1247 | + let first = CalendarDate::from_ymd(year, month, 1).ok()?; |
| 1248 | + let first_weekday = first.weekday().number_days_from_sunday(); |
| 1249 | + let target_weekday = weekday.number_days_from_sunday(); |
| 1250 | + let offset = (target_weekday + 7 - first_weekday) % 7; |
| 1251 | + let day = 1 + offset + (number - 1) * 7; |
| 1252 | + CalendarDate::from_ymd(year, month, day).ok() |
| 1253 | + } |
| 1254 | + RecurrenceOrdinal::Last => { |
| 1255 | + let mut date = CalendarDate::from_ymd(year, month, month.length(year)).ok()?; |
| 1256 | + while date.weekday() != weekday { |
| 1257 | + date = date.add_days(-1); |
| 1258 | + } |
| 1259 | + Some(date) |
| 1260 | + } |
| 1261 | + _ => None, |
| 1262 | + } |
| 1263 | +} |
| 1264 | + |
| 1265 | +pub fn recurrence_ordinal_for_date(date: CalendarDate) -> RecurrenceOrdinal { |
| 1266 | + if date.day().saturating_add(7) > date.month().length(date.year()) { |
| 1267 | + RecurrenceOrdinal::Last |
| 1268 | + } else { |
| 1269 | + RecurrenceOrdinal::Number(((date.day() - 1) / 7) + 1) |
| 1270 | + } |
| 1271 | +} |
| 1272 | + |
| 1273 | +fn days_between(start: CalendarDate, end: CalendarDate) -> i32 { |
| 1274 | + end.inner().to_julian_day() - start.inner().to_julian_day() |
| 1275 | +} |
| 1276 | + |
| 1277 | +fn months_between(start: CalendarDate, end: CalendarDate) -> i32 { |
| 1278 | + (end.year() - start.year()) * 12 + i32::from(u8::from(end.month())) |
| 1279 | + - i32::from(u8::from(start.month())) |
| 1280 | +} |
| 1281 | + |
| 1282 | +fn datetime_distance_minutes(start: EventDateTime, end: EventDateTime) -> i32 { |
| 1283 | + days_between(start.date, end.date) * 24 * 60 + time_minutes(end.time) - time_minutes(start.time) |
| 1284 | +} |
| 1285 | + |
| 1286 | +fn add_minutes(start: EventDateTime, duration_minutes: i32) -> EventDateTime { |
| 1287 | + let absolute_minutes = time_minutes(start.time) + duration_minutes; |
| 1288 | + let day_offset = absolute_minutes.div_euclid(24 * 60); |
| 1289 | + let minute_of_day = absolute_minutes.rem_euclid(24 * 60); |
| 1290 | + EventDateTime::new( |
| 1291 | + start.date.add_days(day_offset), |
| 1292 | + Time::from_hms( |
| 1293 | + u8::try_from(minute_of_day / 60).expect("hour stays in range"), |
| 1294 | + u8::try_from(minute_of_day % 60).expect("minute stays in range"), |
| 1295 | + 0, |
| 1296 | + ) |
| 1297 | + .expect("computed time is valid"), |
| 1298 | + ) |
| 1299 | +} |
| 1300 | + |
| 1301 | +fn time_minutes(time: Time) -> i32 { |
| 1302 | + i32::from(time.hour()) * 60 + i32::from(time.minute()) |
| 1303 | +} |
| 1304 | + |
| 1305 | +fn event_sort_key(event: &Event) -> (CalendarDate, DayMinute, String) { |
| 1306 | + match event.timing { |
| 1307 | + EventTiming::AllDay { date } => (date, DayMinute::START, event.title.clone()), |
| 1308 | + EventTiming::Timed { start, .. } => ( |
| 1309 | + start.date, |
| 1310 | + DayMinute::from_time(start.time), |
| 1311 | + event.title.clone(), |
| 1312 | + ), |
| 1313 | + } |
| 778 | 1314 | } |
| 779 | 1315 | |
| 780 | 1316 | pub fn default_events_file() -> PathBuf { |
@@ -810,6 +1346,10 @@ pub enum LocalEventStoreError { |
| 810 | 1346 | EventNotFound { |
| 811 | 1347 | id: String, |
| 812 | 1348 | }, |
| 1349 | + OccurrenceNotFound { |
| 1350 | + id: String, |
| 1351 | + anchor: String, |
| 1352 | + }, |
| 813 | 1353 | EventNotEditable { |
| 814 | 1354 | id: String, |
| 815 | 1355 | }, |
@@ -838,6 +1378,12 @@ impl fmt::Display for LocalEventStoreError { |
| 838 | 1378 | path.display() |
| 839 | 1379 | ), |
| 840 | 1380 | Self::EventNotFound { id } => write!(f, "local event '{id}' was not found"), |
| 1381 | + Self::OccurrenceNotFound { id, anchor } => { |
| 1382 | + write!( |
| 1383 | + f, |
| 1384 | + "recurring occurrence '{anchor}' was not found for local event '{id}'" |
| 1385 | + ) |
| 1386 | + } |
| 841 | 1387 | Self::EventNotEditable { id } => write!(f, "event '{id}' is not editable locally"), |
| 842 | 1388 | Self::Encode { path, reason } => { |
| 843 | 1389 | if let Some(path) = path { |
@@ -876,7 +1422,7 @@ fn load_events_file(path: &Path) -> Result<InMemoryAgendaSource, LocalEventStore |
| 876 | 1422 | } |
| 877 | 1423 | })?; |
| 878 | 1424 | |
| 879 | | - if file.version != LOCAL_EVENTS_VERSION { |
| 1425 | + if !matches!(file.version, 1 | LOCAL_EVENTS_VERSION) { |
| 880 | 1426 | return Err(LocalEventStoreError::UnsupportedVersion { |
| 881 | 1427 | path: path.to_path_buf(), |
| 882 | 1428 | version: file.version, |
@@ -927,7 +1473,7 @@ fn write_events_file(path: &Path, events: &[Event]) -> Result<(), LocalEventStor |
| 927 | 1473 | }) |
| 928 | 1474 | } |
| 929 | 1475 | |
| 930 | | -const LOCAL_EVENTS_VERSION: u8 = 1; |
| 1476 | +const LOCAL_EVENTS_VERSION: u8 = 2; |
| 931 | 1477 | |
| 932 | 1478 | #[derive(Debug, Serialize, Deserialize)] |
| 933 | 1479 | struct LocalEventsFile { |
@@ -952,6 +1498,10 @@ enum LocalEventRecord { |
| 952 | 1498 | notes: Option<String>, |
| 953 | 1499 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 954 | 1500 | reminders_minutes_before: Vec<u16>, |
| 1501 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1502 | + recurrence: Option<LocalRecurrenceRecord>, |
| 1503 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 1504 | + overrides: Vec<LocalOccurrenceOverrideRecord>, |
| 955 | 1505 | }, |
| 956 | 1506 | AllDay { |
| 957 | 1507 | id: String, |
@@ -963,6 +1513,10 @@ enum LocalEventRecord { |
| 963 | 1513 | notes: Option<String>, |
| 964 | 1514 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 965 | 1515 | reminders_minutes_before: Vec<u16>, |
| 1516 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1517 | + recurrence: Option<LocalRecurrenceRecord>, |
| 1518 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 1519 | + overrides: Vec<LocalOccurrenceOverrideRecord>, |
| 966 | 1520 | }, |
| 967 | 1521 | } |
| 968 | 1522 | |
@@ -973,6 +1527,15 @@ impl LocalEventRecord { |
| 973 | 1527 | .iter() |
| 974 | 1528 | .map(|reminder| reminder.minutes_before) |
| 975 | 1529 | .collect::<Vec<_>>(); |
| 1530 | + let recurrence = event |
| 1531 | + .recurrence |
| 1532 | + .as_ref() |
| 1533 | + .map(LocalRecurrenceRecord::from_rule); |
| 1534 | + let overrides = event |
| 1535 | + .occurrence_overrides |
| 1536 | + .iter() |
| 1537 | + .map(LocalOccurrenceOverrideRecord::from_override) |
| 1538 | + .collect::<Vec<_>>(); |
| 976 | 1539 | |
| 977 | 1540 | match event.timing { |
| 978 | 1541 | EventTiming::AllDay { date } => Self::AllDay { |
@@ -982,6 +1545,8 @@ impl LocalEventRecord { |
| 982 | 1545 | location: event.location.clone(), |
| 983 | 1546 | notes: event.notes.clone(), |
| 984 | 1547 | reminders_minutes_before, |
| 1548 | + recurrence, |
| 1549 | + overrides, |
| 985 | 1550 | }, |
| 986 | 1551 | EventTiming::Timed { start, end } => Self::Timed { |
| 987 | 1552 | id: event.id.clone(), |
@@ -993,6 +1558,8 @@ impl LocalEventRecord { |
| 993 | 1558 | location: event.location.clone(), |
| 994 | 1559 | notes: event.notes.clone(), |
| 995 | 1560 | reminders_minutes_before, |
| 1561 | + recurrence, |
| 1562 | + overrides, |
| 996 | 1563 | }, |
| 997 | 1564 | } |
| 998 | 1565 | } |
@@ -1009,6 +1576,8 @@ impl LocalEventRecord { |
| 1009 | 1576 | location, |
| 1010 | 1577 | notes, |
| 1011 | 1578 | reminders_minutes_before, |
| 1579 | + recurrence, |
| 1580 | + overrides, |
| 1012 | 1581 | } => { |
| 1013 | 1582 | let start = EventDateTime::new( |
| 1014 | 1583 | parse_local_date(&start_date, path)?, |
@@ -1032,6 +1601,13 @@ impl LocalEventRecord { |
| 1032 | 1601 | event.location = empty_to_none(location); |
| 1033 | 1602 | event.notes = empty_to_none(notes); |
| 1034 | 1603 | event.reminders = reminders_from_minutes(reminders_minutes_before); |
| 1604 | + event.recurrence = recurrence |
| 1605 | + .map(|recurrence| recurrence.into_rule(path)) |
| 1606 | + .transpose()?; |
| 1607 | + event.occurrence_overrides = overrides |
| 1608 | + .into_iter() |
| 1609 | + .map(|override_record| override_record.into_override(path)) |
| 1610 | + .collect::<Result<Vec<_>, _>>()?; |
| 1035 | 1611 | Ok(event) |
| 1036 | 1612 | } |
| 1037 | 1613 | Self::AllDay { |
@@ -1041,6 +1617,8 @@ impl LocalEventRecord { |
| 1041 | 1617 | location, |
| 1042 | 1618 | notes, |
| 1043 | 1619 | reminders_minutes_before, |
| 1620 | + recurrence, |
| 1621 | + overrides, |
| 1044 | 1622 | } => { |
| 1045 | 1623 | let mut event = Event::all_day( |
| 1046 | 1624 | id.clone(), |
@@ -1051,41 +1629,510 @@ impl LocalEventRecord { |
| 1051 | 1629 | event.location = empty_to_none(location); |
| 1052 | 1630 | event.notes = empty_to_none(notes); |
| 1053 | 1631 | event.reminders = reminders_from_minutes(reminders_minutes_before); |
| 1632 | + event.recurrence = recurrence |
| 1633 | + .map(|recurrence| recurrence.into_rule(path)) |
| 1634 | + .transpose()?; |
| 1635 | + event.occurrence_overrides = overrides |
| 1636 | + .into_iter() |
| 1637 | + .map(|override_record| override_record.into_override(path)) |
| 1638 | + .collect::<Result<Vec<_>, _>>()?; |
| 1054 | 1639 | Ok(event) |
| 1055 | 1640 | } |
| 1056 | 1641 | } |
| 1057 | 1642 | } |
| 1058 | 1643 | } |
| 1059 | 1644 | |
| 1060 | | -fn reminders_from_minutes(minutes: Vec<u16>) -> Vec<Reminder> { |
| 1061 | | - let mut reminders = minutes |
| 1062 | | - .into_iter() |
| 1063 | | - .map(Reminder::minutes_before) |
| 1064 | | - .collect::<Vec<_>>(); |
| 1065 | | - reminders.sort(); |
| 1066 | | - reminders.dedup(); |
| 1067 | | - reminders |
| 1645 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 1646 | +struct LocalRecurrenceRecord { |
| 1647 | + frequency: String, |
| 1648 | + interval: u16, |
| 1649 | + #[serde(default)] |
| 1650 | + weekdays: Vec<String>, |
| 1651 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1652 | + monthly: Option<LocalRecurrenceMonthlyRecord>, |
| 1653 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1654 | + yearly: Option<LocalRecurrenceYearlyRecord>, |
| 1655 | + end: LocalRecurrenceEndRecord, |
| 1068 | 1656 | } |
| 1069 | 1657 | |
| 1070 | | -fn empty_to_none(value: Option<String>) -> Option<String> { |
| 1071 | | - value.and_then(|value| { |
| 1072 | | - let trimmed = value.trim(); |
| 1073 | | - if trimmed.is_empty() { |
| 1074 | | - None |
| 1075 | | - } else { |
| 1076 | | - Some(trimmed.to_string()) |
| 1658 | +impl LocalRecurrenceRecord { |
| 1659 | + fn from_rule(rule: &RecurrenceRule) -> Self { |
| 1660 | + Self { |
| 1661 | + frequency: match rule.frequency { |
| 1662 | + RecurrenceFrequency::Daily => "daily", |
| 1663 | + RecurrenceFrequency::Weekly => "weekly", |
| 1664 | + RecurrenceFrequency::Monthly => "monthly", |
| 1665 | + RecurrenceFrequency::Yearly => "yearly", |
| 1666 | + } |
| 1667 | + .to_string(), |
| 1668 | + interval: rule.interval(), |
| 1669 | + weekdays: rule |
| 1670 | + .weekdays |
| 1671 | + .iter() |
| 1672 | + .map(|weekday| weekday_name(*weekday)) |
| 1673 | + .collect(), |
| 1674 | + monthly: rule.monthly.map(LocalRecurrenceMonthlyRecord::from_rule), |
| 1675 | + yearly: rule.yearly.map(LocalRecurrenceYearlyRecord::from_rule), |
| 1676 | + end: LocalRecurrenceEndRecord::from_rule(rule.end), |
| 1077 | 1677 | } |
| 1078 | | - }) |
| 1678 | + } |
| 1679 | + |
| 1680 | + fn into_rule(self, path: &Path) -> Result<RecurrenceRule, LocalEventStoreError> { |
| 1681 | + let frequency = match self.frequency.as_str() { |
| 1682 | + "daily" => RecurrenceFrequency::Daily, |
| 1683 | + "weekly" => RecurrenceFrequency::Weekly, |
| 1684 | + "monthly" => RecurrenceFrequency::Monthly, |
| 1685 | + "yearly" => RecurrenceFrequency::Yearly, |
| 1686 | + value => { |
| 1687 | + return Err(LocalEventStoreError::Parse { |
| 1688 | + path: path.to_path_buf(), |
| 1689 | + reason: format!("invalid recurrence frequency '{value}'"), |
| 1690 | + }); |
| 1691 | + } |
| 1692 | + }; |
| 1693 | + let weekdays = self |
| 1694 | + .weekdays |
| 1695 | + .into_iter() |
| 1696 | + .map(|weekday| parse_weekday_record(&weekday, path)) |
| 1697 | + .collect::<Result<Vec<_>, _>>()?; |
| 1698 | + |
| 1699 | + Ok(RecurrenceRule { |
| 1700 | + frequency, |
| 1701 | + interval: self.interval.max(1), |
| 1702 | + end: self.end.into_rule(path)?, |
| 1703 | + weekdays, |
| 1704 | + monthly: self |
| 1705 | + .monthly |
| 1706 | + .map(|monthly| monthly.into_rule(path)) |
| 1707 | + .transpose()?, |
| 1708 | + yearly: self |
| 1709 | + .yearly |
| 1710 | + .map(|yearly| yearly.into_rule(path)) |
| 1711 | + .transpose()?, |
| 1712 | + }) |
| 1713 | + } |
| 1079 | 1714 | } |
| 1080 | 1715 | |
| 1081 | | -fn parse_local_date(value: &str, path: &Path) -> Result<CalendarDate, LocalEventStoreError> { |
| 1082 | | - parse_iso_date(value).ok_or_else(|| LocalEventStoreError::Parse { |
| 1083 | | - path: path.to_path_buf(), |
| 1084 | | - reason: format!("invalid date '{value}'"), |
| 1085 | | - }) |
| 1716 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 1717 | +#[serde(tag = "mode", rename_all = "snake_case")] |
| 1718 | +enum LocalRecurrenceEndRecord { |
| 1719 | + Never, |
| 1720 | + Until { date: String }, |
| 1721 | + Count { count: u32 }, |
| 1086 | 1722 | } |
| 1087 | 1723 | |
| 1088 | | -fn parse_local_time(value: &str, path: &Path) -> Result<Time, LocalEventStoreError> { |
| 1724 | +impl LocalRecurrenceEndRecord { |
| 1725 | + fn from_rule(end: RecurrenceEnd) -> Self { |
| 1726 | + match end { |
| 1727 | + RecurrenceEnd::Never => Self::Never, |
| 1728 | + RecurrenceEnd::Until(date) => Self::Until { |
| 1729 | + date: date.to_string(), |
| 1730 | + }, |
| 1731 | + RecurrenceEnd::Count(count) => Self::Count { count }, |
| 1732 | + } |
| 1733 | + } |
| 1734 | + |
| 1735 | + fn into_rule(self, path: &Path) -> Result<RecurrenceEnd, LocalEventStoreError> { |
| 1736 | + match self { |
| 1737 | + Self::Never => Ok(RecurrenceEnd::Never), |
| 1738 | + Self::Until { date } => Ok(RecurrenceEnd::Until(parse_local_date(&date, path)?)), |
| 1739 | + Self::Count { count } => Ok(RecurrenceEnd::Count(count.max(1))), |
| 1740 | + } |
| 1741 | + } |
| 1742 | +} |
| 1743 | + |
| 1744 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 1745 | +#[serde(tag = "mode", rename_all = "snake_case")] |
| 1746 | +enum LocalRecurrenceMonthlyRecord { |
| 1747 | + DayOfMonth { |
| 1748 | + day: u8, |
| 1749 | + }, |
| 1750 | + WeekdayOrdinal { |
| 1751 | + ordinal: LocalRecurrenceOrdinalRecord, |
| 1752 | + weekday: LocalWeekdayRecord, |
| 1753 | + }, |
| 1754 | +} |
| 1755 | + |
| 1756 | +impl LocalRecurrenceMonthlyRecord { |
| 1757 | + fn from_rule(rule: RecurrenceMonthlyRule) -> Self { |
| 1758 | + match rule { |
| 1759 | + RecurrenceMonthlyRule::DayOfMonth(day) => Self::DayOfMonth { day }, |
| 1760 | + RecurrenceMonthlyRule::WeekdayOrdinal { ordinal, weekday } => Self::WeekdayOrdinal { |
| 1761 | + ordinal: LocalRecurrenceOrdinalRecord::from_rule(ordinal), |
| 1762 | + weekday: LocalWeekdayRecord::from_weekday(weekday), |
| 1763 | + }, |
| 1764 | + } |
| 1765 | + } |
| 1766 | + |
| 1767 | + fn into_rule(self, _path: &Path) -> Result<RecurrenceMonthlyRule, LocalEventStoreError> { |
| 1768 | + Ok(match self { |
| 1769 | + Self::DayOfMonth { day } => RecurrenceMonthlyRule::DayOfMonth(day), |
| 1770 | + Self::WeekdayOrdinal { ordinal, weekday } => RecurrenceMonthlyRule::WeekdayOrdinal { |
| 1771 | + ordinal: ordinal.into_rule(), |
| 1772 | + weekday: weekday.into_weekday(), |
| 1773 | + }, |
| 1774 | + }) |
| 1775 | + } |
| 1776 | +} |
| 1777 | + |
| 1778 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 1779 | +#[serde(tag = "mode", rename_all = "snake_case")] |
| 1780 | +enum LocalRecurrenceYearlyRecord { |
| 1781 | + Date { |
| 1782 | + month: u8, |
| 1783 | + day: u8, |
| 1784 | + }, |
| 1785 | + WeekdayOrdinal { |
| 1786 | + month: u8, |
| 1787 | + ordinal: LocalRecurrenceOrdinalRecord, |
| 1788 | + weekday: LocalWeekdayRecord, |
| 1789 | + }, |
| 1790 | +} |
| 1791 | + |
| 1792 | +impl LocalRecurrenceYearlyRecord { |
| 1793 | + fn from_rule(rule: RecurrenceYearlyRule) -> Self { |
| 1794 | + match rule { |
| 1795 | + RecurrenceYearlyRule::Date { month, day } => Self::Date { |
| 1796 | + month: u8::from(month), |
| 1797 | + day, |
| 1798 | + }, |
| 1799 | + RecurrenceYearlyRule::WeekdayOrdinal { |
| 1800 | + month, |
| 1801 | + ordinal, |
| 1802 | + weekday, |
| 1803 | + } => Self::WeekdayOrdinal { |
| 1804 | + month: u8::from(month), |
| 1805 | + ordinal: LocalRecurrenceOrdinalRecord::from_rule(ordinal), |
| 1806 | + weekday: LocalWeekdayRecord::from_weekday(weekday), |
| 1807 | + }, |
| 1808 | + } |
| 1809 | + } |
| 1810 | + |
| 1811 | + fn into_rule(self, path: &Path) -> Result<RecurrenceYearlyRule, LocalEventStoreError> { |
| 1812 | + Ok(match self { |
| 1813 | + Self::Date { month, day } => RecurrenceYearlyRule::Date { |
| 1814 | + month: parse_month_record(month, path)?, |
| 1815 | + day, |
| 1816 | + }, |
| 1817 | + Self::WeekdayOrdinal { |
| 1818 | + month, |
| 1819 | + ordinal, |
| 1820 | + weekday, |
| 1821 | + } => RecurrenceYearlyRule::WeekdayOrdinal { |
| 1822 | + month: parse_month_record(month, path)?, |
| 1823 | + ordinal: ordinal.into_rule(), |
| 1824 | + weekday: weekday.into_weekday(), |
| 1825 | + }, |
| 1826 | + }) |
| 1827 | + } |
| 1828 | +} |
| 1829 | + |
| 1830 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 1831 | +#[serde(rename_all = "snake_case")] |
| 1832 | +enum LocalRecurrenceOrdinalRecord { |
| 1833 | + First, |
| 1834 | + Second, |
| 1835 | + Third, |
| 1836 | + Fourth, |
| 1837 | + Last, |
| 1838 | +} |
| 1839 | + |
| 1840 | +impl LocalRecurrenceOrdinalRecord { |
| 1841 | + fn from_rule(ordinal: RecurrenceOrdinal) -> Self { |
| 1842 | + match ordinal { |
| 1843 | + RecurrenceOrdinal::Number(1) => Self::First, |
| 1844 | + RecurrenceOrdinal::Number(2) => Self::Second, |
| 1845 | + RecurrenceOrdinal::Number(3) => Self::Third, |
| 1846 | + RecurrenceOrdinal::Number(4) => Self::Fourth, |
| 1847 | + RecurrenceOrdinal::Last | RecurrenceOrdinal::Number(_) => Self::Last, |
| 1848 | + } |
| 1849 | + } |
| 1850 | + |
| 1851 | + const fn into_rule(self) -> RecurrenceOrdinal { |
| 1852 | + match self { |
| 1853 | + Self::First => RecurrenceOrdinal::Number(1), |
| 1854 | + Self::Second => RecurrenceOrdinal::Number(2), |
| 1855 | + Self::Third => RecurrenceOrdinal::Number(3), |
| 1856 | + Self::Fourth => RecurrenceOrdinal::Number(4), |
| 1857 | + Self::Last => RecurrenceOrdinal::Last, |
| 1858 | + } |
| 1859 | + } |
| 1860 | +} |
| 1861 | + |
| 1862 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 1863 | +#[serde(rename_all = "snake_case")] |
| 1864 | +enum LocalWeekdayRecord { |
| 1865 | + Sunday, |
| 1866 | + Monday, |
| 1867 | + Tuesday, |
| 1868 | + Wednesday, |
| 1869 | + Thursday, |
| 1870 | + Friday, |
| 1871 | + Saturday, |
| 1872 | +} |
| 1873 | + |
| 1874 | +impl LocalWeekdayRecord { |
| 1875 | + const fn from_weekday(weekday: Weekday) -> Self { |
| 1876 | + match weekday { |
| 1877 | + Weekday::Sunday => Self::Sunday, |
| 1878 | + Weekday::Monday => Self::Monday, |
| 1879 | + Weekday::Tuesday => Self::Tuesday, |
| 1880 | + Weekday::Wednesday => Self::Wednesday, |
| 1881 | + Weekday::Thursday => Self::Thursday, |
| 1882 | + Weekday::Friday => Self::Friday, |
| 1883 | + Weekday::Saturday => Self::Saturday, |
| 1884 | + } |
| 1885 | + } |
| 1886 | + |
| 1887 | + const fn into_weekday(self) -> Weekday { |
| 1888 | + match self { |
| 1889 | + Self::Sunday => Weekday::Sunday, |
| 1890 | + Self::Monday => Weekday::Monday, |
| 1891 | + Self::Tuesday => Weekday::Tuesday, |
| 1892 | + Self::Wednesday => Weekday::Wednesday, |
| 1893 | + Self::Thursday => Weekday::Thursday, |
| 1894 | + Self::Friday => Weekday::Friday, |
| 1895 | + Self::Saturday => Weekday::Saturday, |
| 1896 | + } |
| 1897 | + } |
| 1898 | +} |
| 1899 | + |
| 1900 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 1901 | +struct LocalOccurrenceOverrideRecord { |
| 1902 | + anchor: LocalOccurrenceAnchorRecord, |
| 1903 | + event: LocalEventDraftRecord, |
| 1904 | +} |
| 1905 | + |
| 1906 | +impl LocalOccurrenceOverrideRecord { |
| 1907 | + fn from_override(override_record: &OccurrenceOverride) -> Self { |
| 1908 | + Self { |
| 1909 | + anchor: LocalOccurrenceAnchorRecord::from_anchor(override_record.anchor), |
| 1910 | + event: LocalEventDraftRecord::from_draft(&override_record.draft), |
| 1911 | + } |
| 1912 | + } |
| 1913 | + |
| 1914 | + fn into_override(self, path: &Path) -> Result<OccurrenceOverride, LocalEventStoreError> { |
| 1915 | + Ok(OccurrenceOverride { |
| 1916 | + anchor: self.anchor.into_anchor(path)?, |
| 1917 | + draft: self.event.into_draft(path)?, |
| 1918 | + }) |
| 1919 | + } |
| 1920 | +} |
| 1921 | + |
| 1922 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 1923 | +#[serde(tag = "kind", rename_all = "snake_case")] |
| 1924 | +enum LocalOccurrenceAnchorRecord { |
| 1925 | + AllDay { date: String }, |
| 1926 | + Timed { date: String, time: String }, |
| 1927 | +} |
| 1928 | + |
| 1929 | +impl LocalOccurrenceAnchorRecord { |
| 1930 | + fn from_anchor(anchor: OccurrenceAnchor) -> Self { |
| 1931 | + match anchor { |
| 1932 | + OccurrenceAnchor::AllDay { date } => Self::AllDay { |
| 1933 | + date: date.to_string(), |
| 1934 | + }, |
| 1935 | + OccurrenceAnchor::Timed { start } => Self::Timed { |
| 1936 | + date: start.date.to_string(), |
| 1937 | + time: format_time(start.time), |
| 1938 | + }, |
| 1939 | + } |
| 1940 | + } |
| 1941 | + |
| 1942 | + fn into_anchor(self, path: &Path) -> Result<OccurrenceAnchor, LocalEventStoreError> { |
| 1943 | + Ok(match self { |
| 1944 | + Self::AllDay { date } => OccurrenceAnchor::AllDay { |
| 1945 | + date: parse_local_date(&date, path)?, |
| 1946 | + }, |
| 1947 | + Self::Timed { date, time } => OccurrenceAnchor::Timed { |
| 1948 | + start: EventDateTime::new( |
| 1949 | + parse_local_date(&date, path)?, |
| 1950 | + parse_local_time(&time, path)?, |
| 1951 | + ), |
| 1952 | + }, |
| 1953 | + }) |
| 1954 | + } |
| 1955 | +} |
| 1956 | + |
| 1957 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 1958 | +#[serde(tag = "timing", rename_all = "snake_case")] |
| 1959 | +enum LocalEventDraftRecord { |
| 1960 | + Timed { |
| 1961 | + title: String, |
| 1962 | + start_date: String, |
| 1963 | + start_time: String, |
| 1964 | + end_date: String, |
| 1965 | + end_time: String, |
| 1966 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1967 | + location: Option<String>, |
| 1968 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1969 | + notes: Option<String>, |
| 1970 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 1971 | + reminders_minutes_before: Vec<u16>, |
| 1972 | + }, |
| 1973 | + AllDay { |
| 1974 | + title: String, |
| 1975 | + date: String, |
| 1976 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1977 | + location: Option<String>, |
| 1978 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 1979 | + notes: Option<String>, |
| 1980 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 1981 | + reminders_minutes_before: Vec<u16>, |
| 1982 | + }, |
| 1983 | +} |
| 1984 | + |
| 1985 | +impl LocalEventDraftRecord { |
| 1986 | + fn from_draft(draft: &CreateEventDraft) -> Self { |
| 1987 | + let reminders_minutes_before = draft |
| 1988 | + .reminders |
| 1989 | + .iter() |
| 1990 | + .map(|reminder| reminder.minutes_before) |
| 1991 | + .collect::<Vec<_>>(); |
| 1992 | + match draft.timing { |
| 1993 | + CreateEventTiming::Timed { start, end } => Self::Timed { |
| 1994 | + title: draft.title.clone(), |
| 1995 | + start_date: start.date.to_string(), |
| 1996 | + start_time: format_time(start.time), |
| 1997 | + end_date: end.date.to_string(), |
| 1998 | + end_time: format_time(end.time), |
| 1999 | + location: draft.location.clone(), |
| 2000 | + notes: draft.notes.clone(), |
| 2001 | + reminders_minutes_before, |
| 2002 | + }, |
| 2003 | + CreateEventTiming::AllDay { date } => Self::AllDay { |
| 2004 | + title: draft.title.clone(), |
| 2005 | + date: date.to_string(), |
| 2006 | + location: draft.location.clone(), |
| 2007 | + notes: draft.notes.clone(), |
| 2008 | + reminders_minutes_before, |
| 2009 | + }, |
| 2010 | + } |
| 2011 | + } |
| 2012 | + |
| 2013 | + fn into_draft(self, path: &Path) -> Result<CreateEventDraft, LocalEventStoreError> { |
| 2014 | + Ok(match self { |
| 2015 | + Self::Timed { |
| 2016 | + title, |
| 2017 | + start_date, |
| 2018 | + start_time, |
| 2019 | + end_date, |
| 2020 | + end_time, |
| 2021 | + location, |
| 2022 | + notes, |
| 2023 | + reminders_minutes_before, |
| 2024 | + } => { |
| 2025 | + let start = EventDateTime::new( |
| 2026 | + parse_local_date(&start_date, path)?, |
| 2027 | + parse_local_time(&start_time, path)?, |
| 2028 | + ); |
| 2029 | + let end = EventDateTime::new( |
| 2030 | + parse_local_date(&end_date, path)?, |
| 2031 | + parse_local_time(&end_time, path)?, |
| 2032 | + ); |
| 2033 | + if start >= end { |
| 2034 | + return Err(LocalEventStoreError::Parse { |
| 2035 | + path: path.to_path_buf(), |
| 2036 | + reason: format!( |
| 2037 | + "invalid override range: start {start:?} must be before end {end:?}" |
| 2038 | + ), |
| 2039 | + }); |
| 2040 | + } |
| 2041 | + |
| 2042 | + CreateEventDraft { |
| 2043 | + title, |
| 2044 | + timing: CreateEventTiming::Timed { start, end }, |
| 2045 | + location: empty_to_none(location), |
| 2046 | + notes: empty_to_none(notes), |
| 2047 | + reminders: reminders_from_minutes(reminders_minutes_before), |
| 2048 | + recurrence: None, |
| 2049 | + } |
| 2050 | + } |
| 2051 | + Self::AllDay { |
| 2052 | + title, |
| 2053 | + date, |
| 2054 | + location, |
| 2055 | + notes, |
| 2056 | + reminders_minutes_before, |
| 2057 | + } => CreateEventDraft { |
| 2058 | + title, |
| 2059 | + timing: CreateEventTiming::AllDay { |
| 2060 | + date: parse_local_date(&date, path)?, |
| 2061 | + }, |
| 2062 | + location: empty_to_none(location), |
| 2063 | + notes: empty_to_none(notes), |
| 2064 | + reminders: reminders_from_minutes(reminders_minutes_before), |
| 2065 | + recurrence: None, |
| 2066 | + }, |
| 2067 | + }) |
| 2068 | + } |
| 2069 | +} |
| 2070 | + |
| 2071 | +fn reminders_from_minutes(minutes: Vec<u16>) -> Vec<Reminder> { |
| 2072 | + let mut reminders = minutes |
| 2073 | + .into_iter() |
| 2074 | + .map(Reminder::minutes_before) |
| 2075 | + .collect::<Vec<_>>(); |
| 2076 | + reminders.sort(); |
| 2077 | + reminders.dedup(); |
| 2078 | + reminders |
| 2079 | +} |
| 2080 | + |
| 2081 | +fn empty_to_none(value: Option<String>) -> Option<String> { |
| 2082 | + value.and_then(|value| { |
| 2083 | + let trimmed = value.trim(); |
| 2084 | + if trimmed.is_empty() { |
| 2085 | + None |
| 2086 | + } else { |
| 2087 | + Some(trimmed.to_string()) |
| 2088 | + } |
| 2089 | + }) |
| 2090 | +} |
| 2091 | + |
| 2092 | +fn weekday_name(weekday: Weekday) -> String { |
| 2093 | + match weekday { |
| 2094 | + Weekday::Sunday => "sunday", |
| 2095 | + Weekday::Monday => "monday", |
| 2096 | + Weekday::Tuesday => "tuesday", |
| 2097 | + Weekday::Wednesday => "wednesday", |
| 2098 | + Weekday::Thursday => "thursday", |
| 2099 | + Weekday::Friday => "friday", |
| 2100 | + Weekday::Saturday => "saturday", |
| 2101 | + } |
| 2102 | + .to_string() |
| 2103 | +} |
| 2104 | + |
| 2105 | +fn parse_weekday_record(value: &str, path: &Path) -> Result<Weekday, LocalEventStoreError> { |
| 2106 | + match value { |
| 2107 | + "sunday" => Ok(Weekday::Sunday), |
| 2108 | + "monday" => Ok(Weekday::Monday), |
| 2109 | + "tuesday" => Ok(Weekday::Tuesday), |
| 2110 | + "wednesday" => Ok(Weekday::Wednesday), |
| 2111 | + "thursday" => Ok(Weekday::Thursday), |
| 2112 | + "friday" => Ok(Weekday::Friday), |
| 2113 | + "saturday" => Ok(Weekday::Saturday), |
| 2114 | + _ => Err(LocalEventStoreError::Parse { |
| 2115 | + path: path.to_path_buf(), |
| 2116 | + reason: format!("invalid weekday '{value}'"), |
| 2117 | + }), |
| 2118 | + } |
| 2119 | +} |
| 2120 | + |
| 2121 | +fn parse_month_record(value: u8, path: &Path) -> Result<Month, LocalEventStoreError> { |
| 2122 | + Month::try_from(value).map_err(|_| LocalEventStoreError::Parse { |
| 2123 | + path: path.to_path_buf(), |
| 2124 | + reason: format!("invalid month '{value}'"), |
| 2125 | + }) |
| 2126 | +} |
| 2127 | + |
| 2128 | +fn parse_local_date(value: &str, path: &Path) -> Result<CalendarDate, LocalEventStoreError> { |
| 2129 | + parse_iso_date(value).ok_or_else(|| LocalEventStoreError::Parse { |
| 2130 | + path: path.to_path_buf(), |
| 2131 | + reason: format!("invalid date '{value}'"), |
| 2132 | + }) |
| 2133 | +} |
| 2134 | + |
| 2135 | +fn parse_local_time(value: &str, path: &Path) -> Result<Time, LocalEventStoreError> { |
| 1089 | 2136 | parse_hhmm_time(value).ok_or_else(|| LocalEventStoreError::Parse { |
| 1090 | 2137 | path: path.to_path_buf(), |
| 1091 | 2138 | reason: format!("invalid time '{value}'"), |
@@ -1639,6 +2686,212 @@ mod tests { |
| 1639 | 2686 | assert!(!range.contains_date(day.add_days(1))); |
| 1640 | 2687 | } |
| 1641 | 2688 | |
| 2689 | + #[test] |
| 2690 | + fn daily_recurrence_expands_with_interval_and_count() { |
| 2691 | + let start = date_ymd(2026, Month::April, 1); |
| 2692 | + let event = Event::all_day("daily", "Every other day", start, source()).with_recurrence( |
| 2693 | + RecurrenceRule { |
| 2694 | + frequency: RecurrenceFrequency::Daily, |
| 2695 | + interval: 2, |
| 2696 | + end: RecurrenceEnd::Count(3), |
| 2697 | + weekdays: Vec::new(), |
| 2698 | + monthly: None, |
| 2699 | + yearly: None, |
| 2700 | + }, |
| 2701 | + ); |
| 2702 | + let source = InMemoryAgendaSource::with_events_and_holidays(vec![event], Vec::new()); |
| 2703 | + let range = DateRange::new(start, date_ymd(2026, Month::April, 10)).expect("valid range"); |
| 2704 | + |
| 2705 | + let dates = source |
| 2706 | + .events_intersecting(range) |
| 2707 | + .into_iter() |
| 2708 | + .filter_map(|event| event.timing.date()) |
| 2709 | + .collect::<Vec<_>>(); |
| 2710 | + |
| 2711 | + assert_eq!(dates, [date_ymd(2026, Month::April, 1), date(3), date(5)]); |
| 2712 | + } |
| 2713 | + |
| 2714 | + #[test] |
| 2715 | + fn weekly_recurrence_supports_multiple_days_and_interval() { |
| 2716 | + let start = date_ymd(2026, Month::April, 5); |
| 2717 | + let event = |
| 2718 | + Event::all_day("weekly", "Workout", start, source()).with_recurrence(RecurrenceRule { |
| 2719 | + frequency: RecurrenceFrequency::Weekly, |
| 2720 | + interval: 2, |
| 2721 | + end: RecurrenceEnd::Never, |
| 2722 | + weekdays: vec![Weekday::Sunday, Weekday::Tuesday], |
| 2723 | + monthly: None, |
| 2724 | + yearly: None, |
| 2725 | + }); |
| 2726 | + let source = InMemoryAgendaSource::with_events_and_holidays(vec![event], Vec::new()); |
| 2727 | + let range = DateRange::new(start, date_ymd(2026, Month::April, 23)).expect("valid range"); |
| 2728 | + |
| 2729 | + let dates = source |
| 2730 | + .events_intersecting(range) |
| 2731 | + .into_iter() |
| 2732 | + .filter_map(|event| event.timing.date()) |
| 2733 | + .collect::<Vec<_>>(); |
| 2734 | + |
| 2735 | + assert_eq!(dates, [date(5), date(7), date(19), date(21)]); |
| 2736 | + } |
| 2737 | + |
| 2738 | + #[test] |
| 2739 | + fn monthly_recurrence_skips_invalid_day_of_month_dates() { |
| 2740 | + let start = date_ymd(2026, Month::January, 31); |
| 2741 | + let event = Event::all_day("month-day", "Month end", start, source()).with_recurrence( |
| 2742 | + RecurrenceRule { |
| 2743 | + frequency: RecurrenceFrequency::Monthly, |
| 2744 | + interval: 1, |
| 2745 | + end: RecurrenceEnd::Never, |
| 2746 | + weekdays: Vec::new(), |
| 2747 | + monthly: Some(RecurrenceMonthlyRule::DayOfMonth(31)), |
| 2748 | + yearly: None, |
| 2749 | + }, |
| 2750 | + ); |
| 2751 | + let source = InMemoryAgendaSource::with_events_and_holidays(vec![event], Vec::new()); |
| 2752 | + let range = DateRange::new(start, date_ymd(2026, Month::April, 1)).expect("valid range"); |
| 2753 | + |
| 2754 | + let dates = source |
| 2755 | + .events_intersecting(range) |
| 2756 | + .into_iter() |
| 2757 | + .filter_map(|event| event.timing.date()) |
| 2758 | + .collect::<Vec<_>>(); |
| 2759 | + |
| 2760 | + assert_eq!(dates, [start, date_ymd(2026, Month::March, 31)]); |
| 2761 | + } |
| 2762 | + |
| 2763 | + #[test] |
| 2764 | + fn monthly_recurrence_supports_last_weekday_rules() { |
| 2765 | + let start = date_ymd(2026, Month::April, 30); |
| 2766 | + let event = Event::all_day("last-thursday", "Review", start, source()).with_recurrence( |
| 2767 | + RecurrenceRule { |
| 2768 | + frequency: RecurrenceFrequency::Monthly, |
| 2769 | + interval: 1, |
| 2770 | + end: RecurrenceEnd::Count(3), |
| 2771 | + weekdays: Vec::new(), |
| 2772 | + monthly: Some(RecurrenceMonthlyRule::WeekdayOrdinal { |
| 2773 | + ordinal: RecurrenceOrdinal::Last, |
| 2774 | + weekday: Weekday::Thursday, |
| 2775 | + }), |
| 2776 | + yearly: None, |
| 2777 | + }, |
| 2778 | + ); |
| 2779 | + let source = InMemoryAgendaSource::with_events_and_holidays(vec![event], Vec::new()); |
| 2780 | + let range = DateRange::new(start, date_ymd(2026, Month::July, 1)).expect("valid range"); |
| 2781 | + |
| 2782 | + let dates = source |
| 2783 | + .events_intersecting(range) |
| 2784 | + .into_iter() |
| 2785 | + .filter_map(|event| event.timing.date()) |
| 2786 | + .collect::<Vec<_>>(); |
| 2787 | + |
| 2788 | + assert_eq!( |
| 2789 | + dates, |
| 2790 | + [ |
| 2791 | + date_ymd(2026, Month::April, 30), |
| 2792 | + date_ymd(2026, Month::May, 28), |
| 2793 | + date_ymd(2026, Month::June, 25) |
| 2794 | + ] |
| 2795 | + ); |
| 2796 | + } |
| 2797 | + |
| 2798 | + #[test] |
| 2799 | + fn yearly_recurrence_skips_invalid_dates_and_supports_weekday_ordinal() { |
| 2800 | + let leap_day = date_ymd(2024, Month::February, 29); |
| 2801 | + let leap = |
| 2802 | + Event::all_day("leap", "Leap", leap_day, source()).with_recurrence(RecurrenceRule { |
| 2803 | + frequency: RecurrenceFrequency::Yearly, |
| 2804 | + interval: 1, |
| 2805 | + end: RecurrenceEnd::Never, |
| 2806 | + weekdays: Vec::new(), |
| 2807 | + monthly: None, |
| 2808 | + yearly: Some(RecurrenceYearlyRule::Date { |
| 2809 | + month: Month::February, |
| 2810 | + day: 29, |
| 2811 | + }), |
| 2812 | + }); |
| 2813 | + let thanksgiving = Event::all_day( |
| 2814 | + "thanksgiving", |
| 2815 | + "Thanksgiving", |
| 2816 | + date_ymd(2026, Month::November, 26), |
| 2817 | + source(), |
| 2818 | + ) |
| 2819 | + .with_recurrence(RecurrenceRule { |
| 2820 | + frequency: RecurrenceFrequency::Yearly, |
| 2821 | + interval: 1, |
| 2822 | + end: RecurrenceEnd::Count(2), |
| 2823 | + weekdays: Vec::new(), |
| 2824 | + monthly: None, |
| 2825 | + yearly: Some(RecurrenceYearlyRule::WeekdayOrdinal { |
| 2826 | + month: Month::November, |
| 2827 | + ordinal: RecurrenceOrdinal::Number(4), |
| 2828 | + weekday: Weekday::Thursday, |
| 2829 | + }), |
| 2830 | + }); |
| 2831 | + let source = |
| 2832 | + InMemoryAgendaSource::with_events_and_holidays(vec![leap, thanksgiving], Vec::new()); |
| 2833 | + let range = DateRange::new( |
| 2834 | + date_ymd(2024, Month::January, 1), |
| 2835 | + date_ymd(2029, Month::January, 1), |
| 2836 | + ) |
| 2837 | + .expect("valid range"); |
| 2838 | + |
| 2839 | + let ids_and_dates = source |
| 2840 | + .events_intersecting(range) |
| 2841 | + .into_iter() |
| 2842 | + .map(|event| (event.id, event.timing.date().expect("all-day date"))) |
| 2843 | + .collect::<Vec<_>>(); |
| 2844 | + |
| 2845 | + assert!(ids_and_dates.contains(&("leap#2024-02-29".to_string(), leap_day))); |
| 2846 | + assert!(ids_and_dates.contains(&( |
| 2847 | + "leap#2028-02-29".to_string(), |
| 2848 | + date_ymd(2028, Month::February, 29) |
| 2849 | + ))); |
| 2850 | + assert!( |
| 2851 | + !ids_and_dates |
| 2852 | + .iter() |
| 2853 | + .any(|(_, date)| *date == date_ymd(2025, Month::February, 28)) |
| 2854 | + ); |
| 2855 | + assert!(ids_and_dates.contains(&( |
| 2856 | + "thanksgiving#2026-11-26".to_string(), |
| 2857 | + date_ymd(2026, Month::November, 26) |
| 2858 | + ))); |
| 2859 | + assert!(ids_and_dates.contains(&( |
| 2860 | + "thanksgiving#2027-11-25".to_string(), |
| 2861 | + date_ymd(2027, Month::November, 25) |
| 2862 | + ))); |
| 2863 | + } |
| 2864 | + |
| 2865 | + #[test] |
| 2866 | + fn recurring_cross_midnight_events_intersect_each_visible_day() { |
| 2867 | + let start = date(23); |
| 2868 | + let event = Event::timed( |
| 2869 | + "late", |
| 2870 | + "Late shift", |
| 2871 | + at(start, 23, 0), |
| 2872 | + at(start.add_days(1), 1, 0), |
| 2873 | + source(), |
| 2874 | + ) |
| 2875 | + .expect("valid recurring event") |
| 2876 | + .with_recurrence(RecurrenceRule { |
| 2877 | + frequency: RecurrenceFrequency::Daily, |
| 2878 | + interval: 1, |
| 2879 | + end: RecurrenceEnd::Count(2), |
| 2880 | + weekdays: Vec::new(), |
| 2881 | + monthly: None, |
| 2882 | + yearly: None, |
| 2883 | + }); |
| 2884 | + let source = InMemoryAgendaSource::with_events_and_holidays(vec![event], Vec::new()); |
| 2885 | + |
| 2886 | + let agenda = DayAgenda::from_source(start.add_days(1), &source); |
| 2887 | + |
| 2888 | + assert_eq!(agenda.timed_events.len(), 2); |
| 2889 | + assert!(agenda.timed_events[0].starts_before_day); |
| 2890 | + assert_eq!(agenda.timed_events[0].visible_end.as_minutes(), 60); |
| 2891 | + assert_eq!(agenda.timed_events[1].visible_start.as_minutes(), 23 * 60); |
| 2892 | + assert!(agenda.timed_events[1].ends_after_day); |
| 2893 | + } |
| 2894 | + |
| 1642 | 2895 | #[test] |
| 1643 | 2896 | fn invalid_ranges_are_rejected() { |
| 1644 | 2897 | let day = date(23); |
@@ -1680,6 +2933,7 @@ mod tests { |
| 1680 | 2933 | location: Some("War room".to_string()), |
| 1681 | 2934 | notes: Some("Bring notes".to_string()), |
| 1682 | 2935 | reminders: vec![Reminder::minutes_before(10), Reminder::minutes_before(60)], |
| 2936 | + recurrence: None, |
| 1683 | 2937 | }) |
| 1684 | 2938 | .expect("timed event saves"); |
| 1685 | 2939 | source |
@@ -1689,11 +2943,12 @@ mod tests { |
| 1689 | 2943 | location: None, |
| 1690 | 2944 | notes: None, |
| 1691 | 2945 | reminders: vec![Reminder::minutes_before(24 * 60)], |
| 2946 | + recurrence: None, |
| 1692 | 2947 | }) |
| 1693 | 2948 | .expect("all-day event saves"); |
| 1694 | 2949 | |
| 1695 | 2950 | let body = std::fs::read_to_string(&path).expect("event file exists"); |
| 1696 | | - assert!(body.contains(r#""version": 1"#)); |
| 2951 | + assert!(body.contains(r#""version": 2"#)); |
| 1697 | 2952 | assert!(body.contains(r#""reminders_minutes_before""#)); |
| 1698 | 2953 | |
| 1699 | 2954 | let reloaded = ConfiguredAgendaSource::from_events_file(&path, HolidayProvider::off()) |
@@ -1738,6 +2993,7 @@ mod tests { |
| 1738 | 2993 | location: None, |
| 1739 | 2994 | notes: None, |
| 1740 | 2995 | reminders: Vec::new(), |
| 2996 | + recurrence: None, |
| 1741 | 2997 | }) |
| 1742 | 2998 | .expect("event saves"); |
| 1743 | 2999 | |
@@ -1750,6 +3006,7 @@ mod tests { |
| 1750 | 3006 | location: Some("Room 2".to_string()), |
| 1751 | 3007 | notes: Some("Moved".to_string()), |
| 1752 | 3008 | reminders: vec![Reminder::minutes_before(5)], |
| 3009 | + recurrence: None, |
| 1753 | 3010 | }, |
| 1754 | 3011 | ) |
| 1755 | 3012 | .expect("event updates"); |
@@ -1770,6 +3027,192 @@ mod tests { |
| 1770 | 3027 | assert_eq!(agenda.all_day_events[0].location.as_deref(), Some("Room 2")); |
| 1771 | 3028 | } |
| 1772 | 3029 | |
| 3030 | + #[test] |
| 3031 | + fn local_event_store_loads_version_one_and_rewrites_version_two_on_save() { |
| 3032 | + let path = temp_events_path("version-one"); |
| 3033 | + let _ = std::fs::remove_dir_all(path.parent().expect("path has parent")); |
| 3034 | + std::fs::create_dir_all(path.parent().expect("path has parent")) |
| 3035 | + .expect("parent can be created"); |
| 3036 | + std::fs::write( |
| 3037 | + &path, |
| 3038 | + r#"{ |
| 3039 | + "version": 1, |
| 3040 | + "events": [ |
| 3041 | + { |
| 3042 | + "id": "old", |
| 3043 | + "title": "Old file", |
| 3044 | + "date": "2026-04-23" |
| 3045 | + } |
| 3046 | + ] |
| 3047 | +}"#, |
| 3048 | + ) |
| 3049 | + .expect("file can be written"); |
| 3050 | + let mut source = ConfiguredAgendaSource::from_events_file(&path, HolidayProvider::off()) |
| 3051 | + .expect("version one file loads"); |
| 3052 | + |
| 3053 | + source |
| 3054 | + .update_event( |
| 3055 | + "old", |
| 3056 | + CreateEventDraft { |
| 3057 | + title: "Rewritten".to_string(), |
| 3058 | + timing: CreateEventTiming::AllDay { date: date(23) }, |
| 3059 | + location: None, |
| 3060 | + notes: None, |
| 3061 | + reminders: Vec::new(), |
| 3062 | + recurrence: None, |
| 3063 | + }, |
| 3064 | + ) |
| 3065 | + .expect("event update saves"); |
| 3066 | + |
| 3067 | + let body = std::fs::read_to_string(&path).expect("event file exists"); |
| 3068 | + let _ = std::fs::remove_dir_all(path.parent().expect("test dir exists")); |
| 3069 | + |
| 3070 | + assert!(body.contains(r#""version": 2"#)); |
| 3071 | + assert!(body.contains("Rewritten")); |
| 3072 | + } |
| 3073 | + |
| 3074 | + #[test] |
| 3075 | + fn local_event_store_saves_recurring_series_and_occurrence_overrides() { |
| 3076 | + let path = temp_events_path("recurring-overrides"); |
| 3077 | + let _ = std::fs::remove_dir_all(path.parent().expect("path has parent")); |
| 3078 | + let day = date(23); |
| 3079 | + let mut source = ConfiguredAgendaSource::from_events_file(&path, HolidayProvider::off()) |
| 3080 | + .expect("missing event file is empty"); |
| 3081 | + let event = source |
| 3082 | + .create_event(CreateEventDraft { |
| 3083 | + title: "Standup".to_string(), |
| 3084 | + timing: CreateEventTiming::Timed { |
| 3085 | + start: at(day, 9, 0), |
| 3086 | + end: at(day, 9, 30), |
| 3087 | + }, |
| 3088 | + location: None, |
| 3089 | + notes: None, |
| 3090 | + reminders: Vec::new(), |
| 3091 | + recurrence: Some(RecurrenceRule { |
| 3092 | + frequency: RecurrenceFrequency::Daily, |
| 3093 | + interval: 1, |
| 3094 | + end: RecurrenceEnd::Count(3), |
| 3095 | + weekdays: Vec::new(), |
| 3096 | + monthly: None, |
| 3097 | + yearly: None, |
| 3098 | + }), |
| 3099 | + }) |
| 3100 | + .expect("recurring event saves"); |
| 3101 | + let anchor = OccurrenceAnchor::Timed { |
| 3102 | + start: at(day.add_days(1), 9, 0), |
| 3103 | + }; |
| 3104 | + |
| 3105 | + source |
| 3106 | + .update_occurrence( |
| 3107 | + &event.id, |
| 3108 | + anchor, |
| 3109 | + CreateEventDraft { |
| 3110 | + title: "Moved standup".to_string(), |
| 3111 | + timing: CreateEventTiming::Timed { |
| 3112 | + start: at(day.add_days(1), 10, 0), |
| 3113 | + end: at(day.add_days(1), 10, 30), |
| 3114 | + }, |
| 3115 | + location: Some("Room 2".to_string()), |
| 3116 | + notes: None, |
| 3117 | + reminders: vec![Reminder::minutes_before(5)], |
| 3118 | + recurrence: None, |
| 3119 | + }, |
| 3120 | + ) |
| 3121 | + .expect("occurrence override saves"); |
| 3122 | + |
| 3123 | + let reloaded = ConfiguredAgendaSource::from_events_file(&path, HolidayProvider::off()) |
| 3124 | + .expect("saved file reloads"); |
| 3125 | + let agenda = DayAgenda::from_source(day.add_days(1), &reloaded); |
| 3126 | + |
| 3127 | + let _ = std::fs::remove_dir_all(path.parent().expect("test dir exists")); |
| 3128 | + |
| 3129 | + assert_eq!(agenda.timed_events.len(), 1); |
| 3130 | + let overridden = &agenda.timed_events[0].event; |
| 3131 | + assert_eq!(overridden.title, "Moved standup"); |
| 3132 | + assert_eq!(overridden.location.as_deref(), Some("Room 2")); |
| 3133 | + assert_eq!( |
| 3134 | + overridden.occurrence().map(|occurrence| occurrence.anchor), |
| 3135 | + Some(anchor) |
| 3136 | + ); |
| 3137 | + } |
| 3138 | + |
| 3139 | + #[test] |
| 3140 | + fn series_edits_drop_overrides_whose_anchor_no_longer_generates() { |
| 3141 | + let path = temp_events_path("series-edit-overrides"); |
| 3142 | + let _ = std::fs::remove_dir_all(path.parent().expect("path has parent")); |
| 3143 | + let day = date(23); |
| 3144 | + let mut source = ConfiguredAgendaSource::from_events_file(&path, HolidayProvider::off()) |
| 3145 | + .expect("missing event file is empty"); |
| 3146 | + let event = source |
| 3147 | + .create_event(CreateEventDraft { |
| 3148 | + title: "Standup".to_string(), |
| 3149 | + timing: CreateEventTiming::Timed { |
| 3150 | + start: at(day, 9, 0), |
| 3151 | + end: at(day, 9, 30), |
| 3152 | + }, |
| 3153 | + location: None, |
| 3154 | + notes: None, |
| 3155 | + reminders: Vec::new(), |
| 3156 | + recurrence: Some(RecurrenceRule { |
| 3157 | + frequency: RecurrenceFrequency::Daily, |
| 3158 | + interval: 1, |
| 3159 | + end: RecurrenceEnd::Count(3), |
| 3160 | + weekdays: Vec::new(), |
| 3161 | + monthly: None, |
| 3162 | + yearly: None, |
| 3163 | + }), |
| 3164 | + }) |
| 3165 | + .expect("recurring event saves"); |
| 3166 | + let anchor = OccurrenceAnchor::Timed { |
| 3167 | + start: at(day.add_days(1), 9, 0), |
| 3168 | + }; |
| 3169 | + source |
| 3170 | + .update_occurrence( |
| 3171 | + &event.id, |
| 3172 | + anchor, |
| 3173 | + CreateEventDraft { |
| 3174 | + title: "Override".to_string(), |
| 3175 | + timing: CreateEventTiming::Timed { |
| 3176 | + start: at(day.add_days(1), 10, 0), |
| 3177 | + end: at(day.add_days(1), 10, 30), |
| 3178 | + }, |
| 3179 | + location: None, |
| 3180 | + notes: None, |
| 3181 | + reminders: Vec::new(), |
| 3182 | + recurrence: None, |
| 3183 | + }, |
| 3184 | + ) |
| 3185 | + .expect("override saves"); |
| 3186 | + |
| 3187 | + let updated = source |
| 3188 | + .update_event( |
| 3189 | + &event.id, |
| 3190 | + CreateEventDraft { |
| 3191 | + title: "Standup".to_string(), |
| 3192 | + timing: CreateEventTiming::Timed { |
| 3193 | + start: at(day, 9, 0), |
| 3194 | + end: at(day, 9, 30), |
| 3195 | + }, |
| 3196 | + location: None, |
| 3197 | + notes: None, |
| 3198 | + reminders: Vec::new(), |
| 3199 | + recurrence: Some(RecurrenceRule { |
| 3200 | + frequency: RecurrenceFrequency::Weekly, |
| 3201 | + interval: 1, |
| 3202 | + end: RecurrenceEnd::Never, |
| 3203 | + weekdays: vec![day.weekday()], |
| 3204 | + monthly: None, |
| 3205 | + yearly: None, |
| 3206 | + }), |
| 3207 | + }, |
| 3208 | + ) |
| 3209 | + .expect("series update saves"); |
| 3210 | + |
| 3211 | + let _ = std::fs::remove_dir_all(path.parent().expect("test dir exists")); |
| 3212 | + |
| 3213 | + assert!(updated.occurrence_overrides.is_empty()); |
| 3214 | + } |
| 3215 | + |
| 1773 | 3216 | #[test] |
| 1774 | 3217 | fn local_event_store_rejects_missing_and_non_local_updates() { |
| 1775 | 3218 | let day = date(23); |
@@ -1789,6 +3232,7 @@ mod tests { |
| 1789 | 3232 | location: None, |
| 1790 | 3233 | notes: None, |
| 1791 | 3234 | reminders: Vec::new(), |
| 3235 | + recurrence: None, |
| 1792 | 3236 | }; |
| 1793 | 3237 | |
| 1794 | 3238 | assert!(matches!( |