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
// Copyright 2023-2024 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2

use std::{borrow::Cow, collections::HashMap};

use vparser::{ContentLine, Parser};

/// A simple component model that only cares about the basic structure.
///
/// This is used to split components and other simple operations. However, this
/// is not a full parser. It won't validate much beyond `BEGIN` and `END`
/// properly matching. The intent of this parser is not to be validating, but
/// to be very tolerant with inputs, so as to allow operating on somewhat
/// invalid inputs.
///
/// # Known Issues
///
/// Works only with iCalendar, not with vCard.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Component<'a> {
    kind: Cow<'a, str>,
    lines: Vec<ContentLine<'a>>,
    subcomponents: Vec<Component<'a>>,
    uid: Option<Cow<'a, str>>,
}

#[derive(Debug, thiserror::Error, PartialEq)]
pub(crate) enum ComponentError {
    #[error("unknown (or unimplemented) component: {0}")]
    UnknownComponent(String),
    #[error("found data after END of root component")]
    DataAfterEnd,
    #[error("reached end of file while parsing data")]
    UnexpectedEof,
    #[error("unbalanced BEGIN and END lines")]
    WrongEnd,
    #[error("END line had no matching BEGIN line")]
    EndWithoutBegin,
    #[error("found data after last END: line")]
    DataOutsideBeginEnd,
}

impl<'a> Component<'a> {
    fn new(kind: Cow<'a, str>) -> Self {
        Component {
            kind,
            lines: Vec::new(),
            subcomponents: Vec::new(),
            uid: None,
        }
    }

    /// Parse a component from a raw string input.
    pub(crate) fn parse(input: &str) -> Result<Component, ComponentError> {
        let mut stack = Vec::new();
        let mut current: Option<Component> = None;

        let mut parser = Parser::new(input);
        while let Some(line) = parser.next() {
            if line.name() == "BEGIN" {
                let new = Component::new(line.value());
                if let Some(previous) = current.replace(new) {
                    stack.push(previous);
                }
            } else if line.name() == "END" {
                let ending = current.take().ok_or(ComponentError::EndWithoutBegin)?;
                if line.value() != ending.kind {
                    return Err(ComponentError::WrongEnd);
                }
                match stack.pop() {
                    Some(mut previous) => {
                        previous.subcomponents.push(ending);
                        current = Some(previous);
                    }
                    None => {
                        return if parser.next().is_some_and(|line| !line.raw().is_empty()) {
                            Err(ComponentError::DataAfterEnd)
                        } else {
                            Ok(ending)
                        };
                    }
                }
            } else if let Some(ref mut current) = current {
                if line.name() == "UID" {
                    current.uid = Some(line.value());
                }
                current.lines.push(line);
            } else {
                return Err(ComponentError::DataOutsideBeginEnd);
            };
        }

        Err(ComponentError::UnexpectedEof)
    }

    // Breaks up a component collection into individual components.
    //
    // For a calendar with multiple `VEVENT`s and `VTIMEZONE`, it will return individual `VEVENT`
    // with the `VTIMEZONE` duplicated into each one, making them fully standalone components.
    pub(crate) fn into_split_collection(
        self: Component<'a>,
    ) -> Result<Vec<Component<'a>>, ComponentError> {
        let mut timezones = Vec::new();
        let mut items_with_uid = HashMap::new();
        let mut items_without_uid = Vec::new();

        self.split_inner(&mut timezones, &mut items_with_uid, &mut items_without_uid)?;

        let items_with_timezones = items_with_uid
            .into_values()
            .map(|mut calendar| {
                for entry in &mut *calendar.subcomponents {
                    // Clone here because `append` empties the passed input.
                    entry.subcomponents.append(&mut (timezones.clone()));
                    // FIXME: this copies all timezones into all components. I can do better.
                }
                calendar
            })
            .collect();

        Ok(items_with_timezones)
    }

    /// Split components inside this one recursively.
    ///
    /// Subcomponents are split into three groups:
    ///
    /// - `timezones`: `VTIMEZONE`, which must be copied inline.
    /// - `items`: items with a UID (which is the key for the `HashMap`.
    /// - `without_uid`: items which as missing a UID.
    ///
    /// Both `items` and `without_uid` are free-standing items for calendar [`Collection`]s.
    ///
    /// Calendar components will be put inside their own wrapper (e.g.: a `VEVENT` will be wrapped
    /// inside its own `VCALENDAR`.
    ///
    /// [`Collection`]: crate::base::Collection
    fn split_inner(
        self: Component<'a>,
        timezones: &mut Vec<Component<'a>>,
        items: &mut HashMap<Cow<'a, str>, Component<'a>>,
        without_uid: &mut Vec<Component<'a>>,
    ) -> Result<(), ComponentError> {
        match self.kind.as_ref() {
            "VTIMEZONE" => {
                timezones.push(self);
            }
            "VTODO" | "VJOURNAL" | "VEVENT" => {
                // Hint: we don't recurse into these, so VALARM components remain untouched.
                match &self.uid {
                    Some(uid) => {
                        items
                            .entry(uid.clone())
                            .or_insert(Component::new(Cow::Borrowed("VCALENDAR")))
                            .subcomponents
                            .push(self);
                    }
                    None => {
                        without_uid.push(self);
                    }
                }
            }
            "VCALENDAR" => {
                for component in self.subcomponents {
                    component.split_inner(timezones, items, without_uid)?;
                }
            }
            kind => return Err(ComponentError::UnknownComponent(kind.to_string())),
        }

        Ok(())
    }
}

impl std::fmt::Display for Component<'_> {
    /// Write a fully encoded representation of this item.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BEGIN:{}\r\n", self.kind)?;
        for line in &self.lines {
            write!(f, "{}\r\n", line.raw())?;
        }
        for component in &self.subcomponents {
            f.write_str(&component.to_string())?;
        }
        write!(f, "END:{}\r\n", self.kind)
    }
}

#[cfg(test)]
mod test {
    use std::borrow::Cow;

    use crate::simple_component::ComponentError;

    #[test]
    #[allow(clippy::too_many_lines)]
    fn test_parse_and_split_collection() {
        use super::Component;

        let calendar = vec![
            "BEGIN:VCALENDAR",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "X-LIC-LOCATION:Europe/Rome",
            "BEGIN:DAYLIGHT",
            "TZOFFSETFROM:+0100",
            "TZOFFSETTO:+0200",
            "TZNAME:CEST",
            "DTSTART:19700329T020000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3",
            "END:DAYLIGHT",
            "BEGIN:STANDARD",
            "TZOFFSETFROM:+0200",
            "TZOFFSETTO:+0100",
            "TZNAME:CET",
            "DTSTART:19701025T030000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10",
            "END:STANDARD",
            "END:VTIMEZONE",
            "BEGIN:VEVENT",
            "DTSTART:19970714T170000Z",
            "DTEND:19970715T035959Z",
            "SUMMARY:Bastille Day Party",
            "X-SOMETHING:r",
            "UID:11bb6bed-c29b-4999-a627-12dee35f8395",
            "END:VEVENT",
            "BEGIN:VEVENT",
            "DTSTART:19970714T170000Z",
            "DTEND:19970715T035959Z",
            "SUMMARY:Bastille Day Party (copy)",
            "X-SOMETHING:s",
            "UID:b8d52b8b-dd6b-4ef9-9249-0ad7c28f9e5a",
            "END:VEVENT",
            "END:VCALENDAR",
        ]
        .join("\r\n");

        let component = Component::parse(&calendar).unwrap();
        assert_eq!(component.kind, Cow::Borrowed("VCALENDAR"));

        let serialised_split = Component::into_split_collection(component)
            .unwrap()
            .iter()
            .map(Component::to_string)
            .collect::<Vec<_>>();

        let expected_first = vec![
            "BEGIN:VCALENDAR",
            "BEGIN:VEVENT",
            "DTSTART:19970714T170000Z",
            "DTEND:19970715T035959Z",
            "SUMMARY:Bastille Day Party (copy)",
            "X-SOMETHING:s",
            "UID:b8d52b8b-dd6b-4ef9-9249-0ad7c28f9e5a",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "X-LIC-LOCATION:Europe/Rome",
            "BEGIN:DAYLIGHT",
            "TZOFFSETFROM:+0100",
            "TZOFFSETTO:+0200",
            "TZNAME:CEST",
            "DTSTART:19700329T020000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3",
            "END:DAYLIGHT",
            "BEGIN:STANDARD",
            "TZOFFSETFROM:+0200",
            "TZOFFSETTO:+0100",
            "TZNAME:CET",
            "DTSTART:19701025T030000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10",
            "END:STANDARD",
            "END:VTIMEZONE",
            "END:VEVENT",
            "END:VCALENDAR",
            "",
        ]
        .join("\r\n");
        let expected_second = vec![
            "BEGIN:VCALENDAR",
            "BEGIN:VEVENT",
            "DTSTART:19970714T170000Z",
            "DTEND:19970715T035959Z",
            "SUMMARY:Bastille Day Party",
            "X-SOMETHING:r",
            "UID:11bb6bed-c29b-4999-a627-12dee35f8395",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "X-LIC-LOCATION:Europe/Rome",
            "BEGIN:DAYLIGHT",
            "TZOFFSETFROM:+0100",
            "TZOFFSETTO:+0200",
            "TZNAME:CEST",
            "DTSTART:19700329T020000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3",
            "END:DAYLIGHT",
            "BEGIN:STANDARD",
            "TZOFFSETFROM:+0200",
            "TZOFFSETTO:+0100",
            "TZNAME:CET",
            "DTSTART:19701025T030000",
            "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10",
            "END:STANDARD",
            "END:VTIMEZONE",
            "END:VEVENT",
            "END:VCALENDAR",
            "",
        ]
        .join("\r\n");

        // Comparing like this since the order is not deterministic.
        assert!(serialised_split.iter().any(|c| **c == expected_first));
        assert!(serialised_split.iter().any(|c| **c == expected_second));
    }

    #[test]
    fn test_missing_end() {
        use super::Component;

        let calendar = [
            "BEGIN:VCALENDAR",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "END:VTIMEZONE",
            "BEGIN:VEVENT",
            "SUMMARY:This event is probably invalid due to missing fields",
            "UID:11bb6bed-c29b-4999-a627-12dee35f8395",
            "END:VEVENT",
        ]
        .join("\r\n");

        assert_eq!(
            Component::parse(&calendar),
            Err(ComponentError::UnexpectedEof)
        );
    }

    #[test]
    fn test_unknown_kind() {
        use super::Component;

        let calendar = [
            "BEGIN:VCALENDAR",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "END:VTIMEZONE",
            "BEGIN:VEVENT",
            "SUMMARY:This event is probably invalid due to missing fields",
            "UID:11bb6bed-c29b-4999-a627-12dee35f8395",
            "END:VEVENT",
            "BEGIN:VAUTOMOBILE",
            "END:VAUTOMOBILE",
            "END:VCALENDAR",
        ]
        .join("\r\n");

        assert_eq!(
            Component::parse(&calendar).unwrap().into_split_collection(),
            Err(ComponentError::UnknownComponent("VAUTOMOBILE".to_string()))
        );
    }

    #[test]
    fn test_multiline_uid() {
        use super::Component;

        let calendar = [
            "BEGIN:VCALENDAR",
            "BEGIN:VTIMEZONE",
            "TZID:Europe/Rome",
            "END:VTIMEZONE",
            "BEGIN:VEVENT",
            "SUMMARY:This event is probably invalid due to missing fields",
            "UID:horrible-",
            " example",
            "END:VEVENT",
            "END:VCALENDAR",
        ]
        .join("\r\n");

        let calendar = Component::parse(&calendar)
            .unwrap()
            .into_split_collection()
            .unwrap()
            .pop()
            .unwrap();

        assert_eq!(
            calendar.subcomponents[0].uid.as_ref().unwrap(),
            "horrible-example"
        );
    }
}