diff options
author | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-13 12:01:15 +0100 |
---|---|---|
committer | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-13 12:08:23 +0100 |
commit | d81b3e164fba4040fc751d632be04214483a8761 (patch) | |
tree | 1e02ccfde5b3709ef09251d6bfd161786f44ec49 /src/icalwrap | |
parent | 1224aa4f3543025996fa2122dae2e6a365b861bc (diff) |
icalwrap: add is_recur_valid method
Diffstat (limited to 'src/icalwrap')
-rw-r--r-- | src/icalwrap/icalvevent.rs | 78 |
1 files changed, 74 insertions, 4 deletions
diff --git a/src/icalwrap/icalvevent.rs b/src/icalwrap/icalvevent.rs index 278d737..b05167a 100644 --- a/src/icalwrap/icalvevent.rs +++ b/src/icalwrap/icalvevent.rs @@ -101,9 +101,12 @@ impl IcalVEvent { } } - pub fn has_recur(&self) -> bool { + pub fn is_recur_master(&self) -> bool { + self.is_recur() && self.instance_timestamp.is_none() + } + + pub fn is_recur(&self) -> bool { !self.get_properties(ical::icalproperty_kind_ICAL_RRULE_PROPERTY).is_empty() - & self.instance_timestamp.is_none() } pub fn get_recur_datetimes(&self) -> Vec<DateTime<Utc>> { @@ -123,6 +126,21 @@ impl IcalVEvent { result } + pub fn is_recur_valid(&self) -> bool { + if self.instance_timestamp.is_none() { + return true; + } + + let timestamp = self.instance_timestamp.unwrap(); + + if self.is_recur() { + let recur_times = self.get_recur_datetimes(); + return recur_times.contains(×tamp.with_timezone(&Utc)); + } + + self.get_dtstart() == Some(timestamp) + } + pub fn with_internal_timestamp(&self, datetime: DateTime<Local>) -> IcalVEvent { IcalVEvent { ptr: self.ptr, @@ -302,9 +320,23 @@ mod tests { } #[test] - fn has_recur_test() { + fn test_is_recur_master() { let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); - assert!(cal.get_principal_event().has_recur()); + assert!(cal.get_principal_event().is_recur_master()); + } + + #[test] + fn test_is_recur_master_instance() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); + let event = cal.get_principal_event(); + let event = event.with_internal_timestamp(Local.ymd(2018, 01, 01).and_hms(0, 0, 0)); + assert!(!event.is_recur_master()); + } + + #[test] + fn test_is_recur_master_invalid() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_ONE_MEETING, None).unwrap(); + assert!(!cal.get_principal_event().is_recur_master()); } #[test] @@ -317,5 +349,43 @@ mod tests { assert_eq!(Utc.ymd(2018, 10, 18).and_hms(0, 0, 0).with_timezone(&Local), recur_instances.next().unwrap().get_dtstart().unwrap()); } + #[test] + fn test_is_recur_valid_trivial() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); + let event = cal.get_principal_event(); + + assert!(event.is_recur_valid()); + } + + #[test] + fn test_is_recur_valid_dtstart() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); + let event = cal.get_principal_event(); + + let event = event.with_internal_timestamp(event.get_dtstart().unwrap()); + + assert!(event.is_recur_valid()); + } + + #[test] + fn test_is_recur_valid_incorrect() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); + let event = cal.get_principal_event(); + + let event = event.with_internal_timestamp(Utc.ymd(2010, 01, 01).and_hms(0, 0, 0).with_timezone(&Local)); + + assert!(!event.is_recur_valid()); + } + + #[test] + fn test_is_recur_valid_other() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap(); + let event = cal.get_principal_event(); + + let event = event.with_internal_timestamp(Utc.ymd(2018, 10, 25).and_hms(0, 0, 0).with_timezone(&Local)); + + assert!(event.is_recur_valid()); + } + } |