From ed4cc3097c36ec90aaeff721c33b55008947aeb4 Mon Sep 17 00:00:00 2001 From: Nora Date: Wed, 27 Mar 2019 14:43:34 +0100 Subject: move duration handling to icalwrap/icalvcalendar --- src/icalwrap/icalvevent.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++-- src/khevent.rs | 26 ++++++++--------- src/testdata.rs | 30 ++++++++++++++++++++ 3 files changed, 111 insertions(+), 16 deletions(-) diff --git a/src/icalwrap/icalvevent.rs b/src/icalwrap/icalvevent.rs index 10d7102..12b1ab9 100644 --- a/src/icalwrap/icalvevent.rs +++ b/src/icalwrap/icalvevent.rs @@ -50,10 +50,12 @@ impl IcalVEvent { } } - pub fn get_duration(&self) -> Option { + fn get_duration_internal(&self) -> Option { unsafe { let duration = ical::icalcomponent_get_duration(self.ptr); - if ical::icaldurationtype_is_bad_duration(duration) == 0 { + if ical::icaldurationtype_is_bad_duration(duration) == 0 + && ical::icaldurationtype_is_null_duration(duration) == 0 + { Some(IcalDuration::from(duration)) } else { None @@ -61,6 +63,19 @@ impl IcalVEvent { } } + pub fn get_duration(&self) -> Option { + match self.get_duration_internal() { + Some(duration) => Some(duration), + None => { + if self.get_dtstart()?.is_date() { + Some(IcalDuration::from_seconds(24 * 60 * 60)) + } else { + Some(IcalDuration::from_seconds(0)) + } + } + } + } + pub fn get_dtstart(&self) -> Option { unsafe { let dtstart = ical::icalcomponent_get_dtstart(self.ptr); @@ -249,12 +264,61 @@ mod tests { } #[test] - fn test_get_duration() { + fn test_get_duration_internal_normal() { let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); let event = cal.get_principal_event(); assert_eq!( Some(IcalDuration::from_seconds(10 * 24 * 60 * 60 + 18 * 60 * 60)), + event.get_duration_internal() + ); + } + + #[test] + fn test_get_duration_normal() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); + let event = cal.get_principal_event(); + + assert_eq!( + Some(IcalDuration::from_seconds(10 * 24 * 60 * 60 + 18 * 60 * 60)), + event.get_duration() + ); + } + + #[test] + fn test_get_duration_inernal_startdate_only() { + let cal = IcalVCalendar::from_str(testdata::TEST_DTSTART_ONLY_DATE, None).unwrap(); + let event = cal.get_principal_event(); + + assert!(event.get_duration_internal().is_none()); + } + + #[test] + fn test_get_duration_startdate_only() { + let cal = IcalVCalendar::from_str(testdata::TEST_DTSTART_ONLY_DATE, None).unwrap(); + let event = cal.get_principal_event(); + + assert_eq!( + Some(IcalDuration::from_seconds(24 * 60 * 60)), + event.get_duration() + ); + } + + #[test] + fn test_get_duration_internal_startdatetime_only() { + let cal = IcalVCalendar::from_str(testdata::TEST_DTSTART_ONLY_DATETIME, None).unwrap(); + let event = cal.get_principal_event(); + + assert!(event.get_duration_internal().is_none()); + } + + #[test] + fn test_get_duration_startdatetime_only() { + let cal = IcalVCalendar::from_str(testdata::TEST_DTSTART_ONLY_DATETIME, None).unwrap(); + let event = cal.get_principal_event(); + + assert_eq!( + Some(IcalDuration::from_seconds(0)), event.get_duration() ); } @@ -296,4 +360,5 @@ mod tests { assert_eq!(None, event.get_location()); } + } diff --git a/src/khevent.rs b/src/khevent.rs index 0ad7f33..5f6d499 100644 --- a/src/khevent.rs +++ b/src/khevent.rs @@ -24,21 +24,11 @@ impl KhEvent { } pub fn get_end(&self) -> Option { + let dur = self.get_duration().unwrap(); if self.is_recur_instance() { - let dur = self.get_duration().unwrap(); - let dtend = self.instance_timestamp.clone().unwrap() + dur; - Some(dtend) + self.instance_timestamp.clone().map(|start| start + dur) } else { - match self.event.get_dtend() { - Some(dtend) => Some(dtend), - None => { - if self.get_start().unwrap().is_date() { - self.get_start().map(|start| start.succ()) - } else { - self.get_start() - } - } - } + self.get_start().clone().map(|start| start + dur) } } @@ -272,4 +262,14 @@ mod tests { assert_eq!(10, event.get_recur_instances().count()); } + #[test] + fn get_duration_test() { + let cal = IcalVCalendar::from_str(testdata::TEST_DTSTART_ONLY_DATE, None).unwrap(); + let event = cal.get_principal_khevent(); + assert!(event.is_allday()); + assert_eq!( + Some(IcalDuration::from_seconds(24 * 60 * 60)), + event.get_duration() + ); + } } diff --git a/src/testdata.rs b/src/testdata.rs index b975699..9fc9c7e 100644 --- a/src/testdata.rs +++ b/src/testdata.rs @@ -48,6 +48,36 @@ pub static TEST_EVENT_MULTIDAY_LASTMODIFIED: &str = indoc!(" END:VCALENDAR "); +pub static TEST_DTSTART_ONLY_DATE: &str = indoc!(" + BEGIN:VCALENDAR + VERSION:2.0 + PRODID:-//ABC Corporation//NONSGML My Product//EN + BEGIN:VEVENT + UID:20070423T123432Z-541111@example.com + DTSTAMP:20070423T123432Z + DTSTART;VALUE=DATE:20070628 + SUMMARY:Festival International de Jazz de Montreal + LOCATION:LDB Lobby + TRANSP:TRANSPARENT + END:VEVENT + END:VCALENDAR +"); + +pub static TEST_DTSTART_ONLY_DATETIME: &str = indoc!(" + BEGIN:VCALENDAR + VERSION:2.0 + PRODID:-//ABC Corporation//NONSGML My Product//EN + BEGIN:VEVENT + UID:20070423T123432Z-541111@example.com + DTSTAMP:20070423T123432Z + DTSTART:20070628T132900 + SUMMARY:Festival International de Jazz de Montreal + LOCATION:LDB Lobby + TRANSP:TRANSPARENT + END:VEVENT + END:VCALENDAR +"); + pub static TEST_EVENT_EMPTY_SUMMARY: &str = indoc!(" BEGIN:VCALENDAR VERSION:2.0 -- cgit v1.2.3