1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
use chrono::{Duration, DateTime, Date, Utc, TimeZone, Local};
use std::ffi::CStr;
use super::IcalComponent;
use super::IcalVCalendar;
use ical;
pub struct IcalVEvent {
ptr: *mut ical::icalcomponent,
parent: Option<IcalVCalendar>,
instance_timestamp: Option<DateTime<Utc>>,
}
impl Drop for IcalVEvent {
fn drop(&mut self) {
unsafe {
// println!("free");
ical::icalcomponent_free(self.ptr);
}
}
}
impl IcalComponent for IcalVEvent {
fn get_ptr (&self) -> *mut ical::icalcomponent {
self.ptr
}
fn as_component(&self) -> &dyn IcalComponent {
self
}
}
impl IcalVEvent {
pub fn from_ptr_with_parent(
ptr: *mut ical::icalcomponent,
parent: &IcalVCalendar,
) -> IcalVEvent {
IcalVEvent {
ptr,
parent: Some(parent.shallow_copy()),
instance_timestamp: None,
}
}
pub fn get_dtend_unix(&self) -> Option<i64> {
match self.instance_timestamp {
Some(timestamp) => unsafe {
let icalduration = ical::icalcomponent_get_duration(self.ptr);
let duration = Duration::seconds(i64::from(ical::icaldurationtype_as_int(icalduration)));
Some(timestamp.checked_add_signed(duration)?.timestamp())
},
None =>
unsafe {
let dtend = ical::icalcomponent_get_dtend(self.ptr);
trace!("{:?}", dtend);
if ical::icaltime_is_null_time(dtend) == 1 {
None
} else {
Some(ical::icaltime_as_timet_with_zone(dtend, dtend.zone))
}
}
}
}
pub fn get_dtstart_unix(&self) -> Option<i64> {
match self.instance_timestamp {
Some(timestamp) => Some(timestamp.timestamp()),
None => unsafe {
let dtstart = ical::icalcomponent_get_dtstart(self.ptr);
if ical::icaltime_is_null_time(dtstart) == 1 {
None
} else {
Some(ical::icaltime_as_timet_with_zone(dtstart, dtstart.zone))
}
}
}
}
pub fn get_dtend(&self) -> Option<DateTime<Local>> {
let dtend = self.get_dtend_unix()?;
Some(Utc.timestamp(dtend, 0).with_timezone(&Local))
}
pub fn get_dtstart(&self) -> Option<DateTime<Local>> {
let dtstart = self.get_dtstart_unix()?;
Some(Utc.timestamp(dtstart, 0).with_timezone(&Local))
}
pub fn get_dtstart_date(&self) -> Option<Date<Local>> {
Some(self.get_dtstart()?.date())
}
pub fn get_dtend_date(&self) -> Option<Date<Local>> {
Some(self.get_dtend()?.date())
}
pub fn get_last_relevant_date(&self) -> Option<Date<Local>> {
if self.is_allday() {
self.get_dtend().map( |dtend| dtend.date().pred())
} else {
self.get_dtend().map( |dtend| dtend.date())
}
}
pub fn has_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>> {
let mut result = vec!();
let result_ptr: *mut ::std::os::raw::c_void = &mut result as *mut _ as *mut ::std::os::raw::c_void;
unsafe {
let dtstart = ical::icalcomponent_get_dtstart(self.ptr);
let mut dtend = ical::icalcomponent_get_dtend(self.ptr);
//unroll up to 1 year in the future
dtend.year += 1;
ical::icalcomponent_foreach_recurrence(self.ptr, dtstart, dtend, Some(recur_callback), result_ptr);
}
result
}
pub fn with_internal_timestamp(&self, datetime: DateTime<Utc>) -> IcalVEvent {
IcalVEvent {
ptr: self.ptr,
parent: self.parent.as_ref().map(|parent| parent.shallow_copy()),
instance_timestamp: Some(datetime),
}
}
pub fn get_recur_instances(&self) -> impl Iterator<Item = IcalVEvent> + '_ {
self.get_recur_datetimes().into_iter().map(move |rec| self.with_internal_timestamp(rec))
}
pub fn get_parent(&self) -> Option<&IcalVCalendar> {
self.parent.as_ref()
}
pub fn get_summary(&self) -> Option<String> {
unsafe {
let ptr = ical::icalcomponent_get_summary(self.ptr);
if !ptr.is_null() {
Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
} else {
None
}
}
}
pub fn get_description(&self) -> Option<String> {
unsafe {
let ptr = ical::icalcomponent_get_description(self.ptr);
if !ptr.is_null() {
Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
} else {
None
}
}
}
pub fn get_location(&self) -> Option<String> {
unsafe {
let ptr = ical::icalcomponent_get_location(self.ptr);
if !ptr.is_null() {
Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
} else {
None
}
}
}
pub fn get_uid(&self) -> String {
unsafe {
let cstr = CStr::from_ptr(ical::icalcomponent_get_uid(self.ptr));
cstr.to_string_lossy().into_owned()
}
}
pub fn is_allday(&self) -> bool {
unsafe {
let dtstart = ical::icalcomponent_get_dtstart(self.ptr);
dtstart.is_date == 1
}
}
}
extern "C" fn recur_callback(
_comp: *mut ical::icalcomponent,
span: *mut ical::icaltime_span,
data: *mut ::std::os::raw::c_void) {
let data: &mut Vec<DateTime<Utc>> = unsafe { &mut *(data as *mut Vec<DateTime<Utc>>) };
let spanstart = unsafe {
trace!("callback!, {:?}", *span);
let start = (*span).start;
Utc.timestamp(start, 0)
};
data.push(spanstart);
}
#[cfg(test)]
mod tests {
use super::*;
use testdata;
use chrono::NaiveDate;
#[test]
fn recur_iterator_test() {
testdata::setup();
let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
let event = cal.get_principal_event();
assert_eq!(Local.ymd(2018, 10, 11), event.get_dtstart_date().unwrap());
assert_eq!(Local.ymd(2018, 10, 13), event.get_dtend_date().unwrap());
assert_eq!("RRULE:FREQ=WEEKLY;COUNT=10", event.get_property(ical::icalproperty_kind_ICAL_RRULE_PROPERTY).unwrap().as_ical_string());
assert_eq!(10, event.get_recur_datetimes().len());
assert_eq!(10, event.get_recur_instances().count());
}
#[test]
fn test_get_all_properties() {
let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap();
let event = cal.get_principal_event();
let props = event.get_properties_all();
assert_eq!(7, props.len());
}
#[test]
fn test_get_property_get_value() {
let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, None).unwrap();
let event = cal.get_principal_event();
let prop = event
|