aboutsummaryrefslogtreecommitdiff
path: root/aero-dav/src/caldecoder.rs
blob: 49d1c9e691aad022a9350a0259c47fbd6ddf0c83 (plain) (blame)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use quick_xml::events::Event;
use chrono::NaiveDateTime;

use super::types as dav;
use super::caltypes::*;
use super::xml::{QRead, IRead, Reader, Node, CAL_URN};
use super::error::ParsingError;

// ---- ROOT ELEMENTS ---
impl<E: dav::Extension> QRead<MkCalendar<E>> for MkCalendar<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "mkcalendar").await?;
        let set = xml.find().await?;
        xml.close().await?;
        Ok(MkCalendar(set))
    }
}

impl<E: dav::Extension, N: Node<N>> QRead<MkCalendarResponse<E,N>> for MkCalendarResponse<E,N> {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl<E: dav::Extension> QRead<CalendarQuery<E>> for CalendarQuery<E> {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl<E: dav::Extension> QRead<CalendarMultiget<E>> for CalendarMultiget<E> {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<FreeBusyQuery> for FreeBusyQuery {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}


// ---- EXTENSIONS ---
impl QRead<Violation> for Violation {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<Property> for Property {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        if xml.maybe_open(CAL_URN, "calendar-description").await?.is_some() {
            let lang = xml.prev_attr("xml:lang");
            let text = xml.tag_string().await?;
            xml.close().await?;
            return Ok(Property::CalendarDescription { lang, text })
        }

        if xml.maybe_open(CAL_URN, "calendar-timezone").await?.is_some() {
            let tz = xml.tag_string().await?;
            xml.close().await?;
            return Ok(Property::CalendarTimezone(tz))
        }

        if xml.maybe_open(CAL_URN, "supported-calendar-component-set").await?.is_some() {
            let comp = xml.collect().await?;
            xml.close().await?;
            return Ok(Property::SupportedCalendarComponentSet(comp))
        }

        if xml.maybe_open(CAL_URN, "supported-calendar-data").await?.is_some() {
            let mime = xml.collect().await?;
            xml.close().await?;
            return Ok(Property::SupportedCalendarData(mime))
        }

        if xml.maybe_open(CAL_URN, "max-resource-size").await?.is_some() {
            let sz = xml.tag_string().await?.parse::<u64>()?;
            xml.close().await?;
            return Ok(Property::MaxResourceSize(sz))
        }

        if xml.maybe_open(CAL_URN, "max-date-time").await?.is_some() {
            let dtstr = xml.tag_string().await?;
            let dt = NaiveDateTime::parse_from_str(dtstr.as_str(), ICAL_DATETIME_FMT)?.and_utc();
            xml.close().await?;
            return Ok(Property::MaxDateTime(dt))
        }

        if xml.maybe_open(CAL_URN, "max-instances").await?.is_some() {
            let sz = xml.tag_string().await?.parse::<u64>()?;
            xml.close().await?;
            return Ok(Property::MaxInstances(sz))
        }

        if xml.maybe_open(CAL_URN, "max-attendees-per-instance").await?.is_some() {
            let sz = xml.tag_string().await?.parse::<u64>()?;
            xml.close().await?;
            return Ok(Property::MaxAttendeesPerInstance(sz))
        }

        if xml.maybe_open(CAL_URN, "supported-collation-set").await?.is_some() {
            let cols = xml.collect().await?;
            xml.close().await?;
            return Ok(Property::SupportedCollationSet(cols))
        }

        let mut dirty = false;
        let mut caldata: Option<CalendarDataPayload> = None;
        xml.maybe_read(&mut caldata, &mut dirty).await?;
        if let Some(cal) = caldata {
            return Ok(Property::CalendarData(cal))
        }

        Err(ParsingError::Recoverable)
    }
}

impl QRead<PropertyRequest> for PropertyRequest {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<ResourceType> for ResourceType {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

// ---- INNER XML ----
impl QRead<SupportedCollation> for SupportedCollation {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "supported-collation").await?;
        let col = Collation::new(xml.tag_string().await?);
        xml.close().await?;
        Ok(SupportedCollation(col))
    }
}

impl QRead<CalendarDataPayload> for CalendarDataPayload {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "calendar-data").await?;
        let mime = CalendarDataSupport::qread(xml).await.ok();
        let payload = xml.tag_string().await?;
        xml.close().await?;
        Ok(CalendarDataPayload { mime, payload })
    }
}

impl QRead<CalendarDataSupport> for CalendarDataSupport {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        let ct = xml.prev_attr("content-type");
        let vs = xml.prev_attr("version");
        match (ct, vs) {
            (Some(content_type), Some(version)) => Ok(Self { content_type, version }),
            _ => Err(ParsingError::Recoverable),
        }
    }
}

impl QRead<CalendarDataRequest> for CalendarDataRequest {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "calendar-data").await?;
        let mime = CalendarDataSupport::qread(xml).await.ok();

        let (mut comp, mut recurrence, mut limit_freebusy_set) = (None, None, None);

        loop {
            let mut dirty = false;
            xml.maybe_read(&mut comp, &mut dirty).await?;
            xml.maybe_read(&mut recurrence, &mut dirty).await?;
            xml.maybe_read(&mut limit_freebusy_set, &mut dirty).await?;

            if !dirty {
                match xml.peek() {
                    Event::End(_) => break,
                    _ => xml.skip().await?,
                };
            }

        }

        xml.close().await?;
        Ok(Self { mime, comp, recurrence, limit_freebusy_set })
    }
}

impl QRead<CalendarDataEmpty> for CalendarDataEmpty {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "calendar-data").await?;
        let mime = CalendarDataSupport::qread(xml).await.ok();
        xml.close().await?;
        Ok(Self(mime))
    }
}

impl QRead<Comp> for Comp {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "comp").await?;
        let name = Component::new(xml.prev_attr("name").ok_or(ParsingError::MissingAttribute)?);
        let additional_rules = Box::pin(xml.maybe_find()).await?;
        xml.close().await?;
        Ok(Self { name, additional_rules })
    }
}

impl QRead<CompInner> for CompInner {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        let (mut prop_kind, mut comp_kind) = (None, None);

        loop {
            let mut dirty = false; 
          
            xml.maybe_read(&mut prop_kind, &mut dirty).await?;
            xml.maybe_read(&mut comp_kind, &mut dirty).await?;

           if !dirty {
                match xml.peek() {
                    Event::End(_) => break,
                    _ => xml.skip().await?,
                };
            }
        };

        match (prop_kind, comp_kind) {
            (Some(prop_kind), Some(comp_kind)) => Ok(Self { prop_kind, comp_kind }),
            _ => Err(ParsingError::MissingChild),
        }
    }
}

impl QRead<CompSupport> for CompSupport {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(CAL_URN, "comp").await?;
        let inner = Component::new(xml.prev_attr("name").ok_or(ParsingError::MissingAttribute)?);
        xml.close().await?;
        Ok(Self(inner))
    }
}

impl QRead<CompKind> for CompKind {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        let mut comp = Vec::new();
        loop {
            let mut dirty = false;

            if xml.maybe_open(CAL_URN, "allcomp").await?.is_some() {
                xml.close().await?;
                return Ok(CompKind::AllComp)
            }

            xml.maybe_push(&mut comp, &mut dirty).await?;

            if !dirty {
                match xml.peek() {
                    Event::End(_) => break,
                    _ => xml.skip().await?,
                };
            }
        }
        Ok(CompKind::Comp(comp))
    }
}

impl QRead<PropKind> for PropKind {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        let mut prop = Vec::new();
        loop {
            let mut dirty = false;

            if xml.maybe_open(CAL_URN, "allprop").await?.is_some() {
                xml.close().await?;
                return Ok(PropKind::AllProp)
            }

            xml.maybe_push(&mut prop, &mut dirty).await?;

            if !dirty {
                match xml.peek() {
                    Event::End(_) => break,
                    _ => xml.skip().await?,
                };
            }
        }
        Ok(PropKind::Prop(prop))
    }
}

impl QRead<RecurrenceModifier> for RecurrenceModifier {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<Expand> for Expand {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<LimitRecurrenceSet> for LimitRecurrenceSet {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<LimitFreebusySet> for LimitFreebusySet {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl<E: dav::Extension> QRead<CalendarSelector<E>> for CalendarSelector<E> {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<CompFilter> for CompFilter {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<CompFilterRules> for CompFilterRules {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<CompFilterMatch> for CompFilterMatch {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<PropFilter> for PropFilter {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<PropFilterRules> for PropFilterRules {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<PropFilterMatch> for PropFilterMatch {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<TimeOrText> for TimeOrText {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<TextMatch> for TextMatch {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<ParamFilterMatch> for ParamFilterMatch {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<TimeZone> for TimeZone {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<Filter> for Filter {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<TimeRange> for TimeRange {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

impl QRead<CalProp> for CalProp {
    async fn qread(_xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        unreachable!();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    //use chrono::{FixedOffset, TimeZone};
    use crate::realization::Calendar;
    //use quick_reader::NsReader;

    async fn deserialize<T: Node<T>>(src: &str) -> T {
        let mut rdr = Reader::new(quick_xml::NsReader::from_reader(src.as_bytes())).await.unwrap();
        rdr.find().await.unwrap()
    }

    #[tokio::test]
    async fn basic_mkcalendar() {
        let expected = MkCalendar(dav::Set(dav::PropValue(vec![
            dav::Property::DisplayName("Lisa's Events".into()),
        ])));

        let src = r#"
<?xml version="1.0" encoding="utf-8" ?>
<C:mkcalendar xmlns:D="DAV:"
             xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:set>
    <D:prop>
      <D:displayname>Lisa's Events</D:displayname>
    </D:prop>
   </D:set>
 </C:mkcalendar>
"#;
        let got = deserialize::<MkCalendar<Calendar>>(src).await;
        assert_eq!(got, expected)
    }
}