ical-rs 0.5.0

iCalendar parser, validator, editor, merger and builder library
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! # Diffing
//!
//! What one side changed relative to the base, as one [`Op`] per observed
//! change.
//!
//! Inside a matched component, a property is matched down one ladder: a
//! synchronisation identity, which iCalendar does not define for a property so
//! the rung is empty here, then a natural identity, then equality, then
//! position.
//!
//! A property that may occur more than once and whose value names a thing
//! outside the calendar is identified by that value: `ATTENDEE` by its
//! calendar user address, `ATTACH` by its URI or inline binary, `RELATED-TO`
//! by the `UID` it points at, `CONFERENCE` and `IMAGE` by their URI.
//!
//! A different calendar address is a different person, so two properties
//! carrying different identities are never matched with each other, and a
//! value two siblings share tells neither of them apart, so both of those fall
//! back to their positions.
//!
//! An identity is compared lowercased and written back exactly, so an address
//! meets the other case it was written in while the line keeps its own bytes.
//!
//! ## What counts as a change
//!
//! A whole property added or removed, a value changed, one item of a list
//! value added or removed, a parameter added, removed or changed. List items
//! merge as a set, both sides' additions and removals applying, so they never
//! collide.

use alloc::{
    borrow::{Cow, ToOwned},
    string::{String, ToString},
    vec,
    vec::Vec,
};

use crate::{
    param::IcalParam,
    prop::{IcalPropKind, IcalPropName},
    tree::{
        line::IcalLine,
        merge::{IcalComponentPath, IcalMergeAction, IcalPropPath, Op, Slot, node::Node},
    },
    value::IcalValue,
    version::IcalVersion,
};

/// One side against the base, as the calendars they walk to.
pub(super) struct Diff<'n, 'c, 'a> {
    pub(super) base: &'n [Node<'c, 'a>],
    pub(super) side: &'n [Node<'c, 'a>],
    pub(super) version: IcalVersion,
}

impl<'a> Diff<'_, '_, 'a> {
    /// Every change the side made relative to the base.
    pub(super) fn run(&self) -> Vec<Op<'a>> {
        let mut ops = Vec::new();

        for node in self.base {
            if !self.side.iter().any(|held| held.path == node.path)
                && !self.removed_above(&node.path)
            {
                ops.push(Op {
                    action: IcalMergeAction::ComponentRemoved {
                        at: node.path.clone(),
                    },
                    source: None,
                    slot: Slot::Component,
                });
            }
        }

        for node in self.side {
            if !self.base.iter().any(|held| held.path == node.path) && !self.added_above(&node.path)
            {
                ops.push(Op {
                    action: IcalMergeAction::ComponentAdded {
                        at: node.path.clone(),
                    },
                    source: None,
                    slot: Slot::Component,
                });
            }
        }

        // NOTE: A calendar may hold two components at one path, a `UID`
        // written twice with no `RECURRENCE-ID` telling them apart, so each
        // side component is matched once: matching both base components
        // against the same one would report the difference between them as a
        // change either side made.
        let mut taken = vec![false; self.side.len()];

        for node in self.base {
            let Some((at, held)) = self
                .side
                .iter()
                .enumerate()
                .find(|(at, held)| !taken[*at] && held.path == node.path)
            else {
                continue;
            };

            taken[at] = true;

            self.component(node, held, &mut ops);
        }

        ops
    }

    /// Whether an ancestor of this path is itself missing from the side, so
    /// the removal is already reported one level up.
    fn removed_above(&self, path: &IcalComponentPath<'_>) -> bool {
        path.ancestors().any(|above| {
            self.base.iter().any(|node| node.path == above)
                && !self.side.iter().any(|node| node.path == above)
        })
    }

    /// The mirror of [`Diff::removed_above`] for an addition.
    fn added_above(&self, path: &IcalComponentPath<'_>) -> bool {
        path.ancestors().any(|above| {
            self.side.iter().any(|node| node.path == above)
                && !self.base.iter().any(|node| node.path == above)
        })
    }

    /// Diff the properties of one matched component pair.
    fn component(&self, base: &Node<'_, 'a>, side: &Node<'_, 'a>, ops: &mut Vec<Op<'a>>) {
        let base_props: Vec<&IcalLine<'a>> = base.cst.prop_lines().collect();
        let side_props: Vec<&IcalLine<'a>> = side.cst.prop_lines().collect();

        let mut names: Vec<String> = Vec::new();
        for line in base_props.iter().chain(&side_props) {
            let name = line.name.get().to_ascii_uppercase();
            if !names.contains(&name) {
                names.push(name);
            }
        }

        for name in names {
            let of = |lines: &[&IcalLine<'a>]| -> Vec<usize> {
                lines
                    .iter()
                    .enumerate()
                    .filter(|(_, line)| line.name.get().eq_ignore_ascii_case(&name))
                    .map(|(index, _)| index)
                    .collect()
            };

            let mut base_free = of(&base_props);
            let mut side_free = of(&side_props);
            let mut pairs = Vec::new();

            let mut b = 0;
            while b < base_free.len() {
                let held = identity_in(&base_props, base_free[b]);
                let same = held.and_then(|held| {
                    side_free
                        .iter()
                        .position(|&s| identity_in(&side_props, s).is_some_and(|side| side == held))
                });

                match same {
                    Some(s) => pairs.push((base_free.remove(b), side_free.remove(s))),
                    None => b += 1,
                }
            }

            // NOTE: An untouched property pairs with itself before position is
            // consulted, so adding one line does not renumber every line after
            // it.
            let mut b = 0;
            while b < base_free.len() {
                let same = side_free.iter().position(|&s| {
                    base_props[base_free[b]].decode(self.version)
                        == side_props[s].decode(self.version)
                });

                match same {
                    Some(s) => pairs.push((base_free.remove(b), side_free.remove(s))),
                    None => b += 1,
                }
            }

            // NOTE: Position only tells apart properties iCalendar gives no
            // identity of their own. A calendar address that matched nothing
            // names a person who left, never a person the other side renamed.
            let mut b = 0;
            while b < base_free.len() {
                if identity_in(&base_props, base_free[b]).is_some() {
                    b += 1;
                    continue;
                }

                let same = side_free
                    .iter()
                    .position(|&s| identity_in(&side_props, s).is_none());

                match same {
                    Some(s) => pairs.push((base_free.remove(b), side_free.remove(s))),
                    None => break,
                }
            }

            for index in base_free {
                let line = base_props[index];
                let at = IcalPropPath::of(&base.path, &base_props, index);

                ops.push(Op {
                    action: IcalMergeAction::PropRemoved {
                        value: line.decode(self.version).value.into_owned(),
                        at,
                    },
                    source: None,
                    slot: Slot::Prop,
                });
            }

            for index in side_free {
                let line = side_props[index];
                let at = IcalPropPath::of(&side.path, &side_props, index);

                ops.push(Op {
                    action: IcalMergeAction::PropAdded {
                        value: line.decode(self.version).value.into_owned(),
                        at: at.clone(),
                    },
                    source: Some(at),
                    slot: Slot::Prop,
                });
            }

            for (b, s) in pairs {
                self.prop(&base.path, &base_props, b, &side_props, s, ops);
            }
        }
    }

    /// Diff one matched property pair: its parameters, then its value.
    fn prop(
        &self,
        component: &IcalComponentPath<'a>,
        lines: &[&IcalLine<'a>],
        at: usize,
        side_lines: &[&IcalLine<'a>],
        side_at: usize,
        ops: &mut Vec<Op<'a>>,
    ) {
        let base = lines[at];
        let side = side_lines[side_at];
        let at = IcalPropPath::of(component, lines, at);
        let source = IcalPropPath::of(component, side_lines, side_at);

        let base_prop = base.decode(self.version);
        let side_prop = side.decode(self.version);

        for (index, param) in base_prop.params.iter().enumerate() {
            let name = param.merge_name();
            let ordinal = ordinal_of(&base_prop.params, index, &name);
            let held = nth_param(&side_prop.params, &name, ordinal);

            // NOTE: the raw nodes are what is compared, not the decoded
            // parameters: a single-valued parameter decodes its first value
            // alone, so two parameters differing past the first `,` decode
            // alike and the edit is never seen.
            let action = match held {
                None => IcalMergeAction::ParamRemoved {
                    at: at.clone(),
                    param: param.clone().into_owned(),
                },
                Some(held) if !base.params[index].same_param_as(&side.params[held]) => {
                    IcalMergeAction::ParamChanged {
                        at: at.clone(),
                        old: param.clone().into_owned(),
                        new: side_prop.params[held].clone().into_owned(),
                    }
                }
                Some(_) => continue,
            };

            ops.push(Op {
                action,
                source: Some(source.clone()),
                slot: Slot::Param { name, at: ordinal },
            });
        }

        for (index, param) in side_prop.params.iter().enumerate() {
            let name = param.merge_name();
            let ordinal = ordinal_of(&side_prop.params, index, &name);

            if nth_param(&base_prop.params, &name, ordinal).is_some() {
                continue;
            }

            ops.push(Op {
                action: IcalMergeAction::ParamAdded {
                    at: at.clone(),
                    param: param.clone().into_owned(),
                },
                source: Some(source.clone()),
                slot: Slot::Param { name, at: ordinal },
            });
        }

        if base.value.same_value_as(&side.value) {
            return;
        }

        match (&base_prop.value, &side_prop.value) {
            // NOTE: A list is a set: both sides' additions and both sides'
            // removals apply, so two sides editing one list never collide.
            (IcalValue::TextList(old), IcalValue::TextList(new)) => {
                list_ops(&at, &source, &old.0, &new.0, ops)
            }
            (IcalValue::DateTimeList(old), IcalValue::DateTimeList(new)) => {
                list_ops(&at, &source, &old.0, &new.0, ops)
            }
            (old, new) => ops.push(Op {
                action: IcalMergeAction::ValueChanged {
                    at,
                    old: old.clone().into_owned(),
                    new: new.clone().into_owned(),
                },
                source: Some(source),
                slot: Slot::Value,
            }),
        }
    }
}

impl<'a> IcalLine<'a> {
    /// The identity a property name carries, read off one line.
    fn prop_identity(&self) -> Option<Cow<'a, str>> {
        let name = IcalPropName::from(Cow::Owned(self.name.get().to_owned()));
        let identified = matches!(
            name,
            IcalPropName::Kind(
                IcalPropKind::Attendee
                    | IcalPropKind::Attach
                    | IcalPropKind::RelatedTo
                    | IcalPropKind::Conference
                    | IcalPropKind::Image
            )
        );

        identified.then(|| Cow::Owned(self.value_key()))
    }
}

/// The value that tells a property from its same-named siblings.
///
/// A property that may occur more than once and whose value names a thing
/// outside the calendar is that thing: an `ATTENDEE` a calendar user address
/// (RFC 5545 3.8.4.1), an `ATTACH` a URI or inline binary (3.8.1.1), a
/// `RELATED-TO` a `UID` (3.8.4.5), a `CONFERENCE` or `IMAGE` a URI (RFC 7986).
///
/// Every other property has none: either it may occur only once, so its name
/// already tells it apart, or its value is the datum being edited, and keying
/// on it would make every edit a replacement.
///
/// An identity that does not tell a property from its siblings is no identity:
/// a value written twice in one component names both, so those fall back to
/// their positions, and a sibling still alone with its value keeps its own.
pub(super) fn identity_in<'a>(lines: &[&IcalLine<'a>], at: usize) -> Option<Cow<'a, str>> {
    let held = lines[at].prop_identity()?;
    let name = lines[at].name.get();

    let twice = lines.iter().enumerate().any(|(index, line)| {
        index != at
            && line.name.get().eq_ignore_ascii_case(name)
            && line.prop_identity().is_some_and(|line| line == held)
    });

    (!twice).then_some(held)
}

/// The item-by-item difference between two list values.
fn list_ops<'a>(
    at: &IcalPropPath<'a>,
    source: &IcalPropPath<'a>,
    old: &[Cow<'_, str>],
    new: &[Cow<'_, str>],
    ops: &mut Vec<Op<'a>>,
) {
    let (added, removed) = list_diff(old, new);

    for item in removed {
        ops.push(Op {
            action: IcalMergeAction::ValueItemRemoved {
                at: at.clone(),
                item: Cow::Owned(item.to_string()),
            },
            source: Some(source.clone()),
            slot: Slot::Items,
        });
    }

    for item in added {
        ops.push(Op {
            action: IcalMergeAction::ValueItemAdded {
                at: at.clone(),
                item: Cow::Owned(item.to_string()),
            },
            source: Some(source.clone()),
            slot: Slot::Items,
        });
    }
}

/// Diff two value lists as multisets, so a repeated item is matched one for
/// one rather than by mere membership.
///
/// Set membership loses a duplicate: dropping one `a` from `a,a,b` leaves an
/// `a` in the new list, and a set diff then reports no removal at all.
fn list_diff<'a>(
    old: &[Cow<'a, str>],
    new: &[Cow<'a, str>],
) -> (Vec<Cow<'a, str>>, Vec<Cow<'a, str>>) {
    let mut removed: Vec<Option<&Cow<'a, str>>> = old.iter().map(Some).collect();
    let mut added = Vec::new();

    for item in new {
        let kept = removed
            .iter()
            .position(|old| old.is_some_and(|old| old.as_ref() == item.as_ref()));

        match kept {
            Some(i) => removed[i] = None,
            None => added.push(item.clone()),
        }
    }

    let removed = removed.into_iter().flatten().cloned().collect();

    (added, removed)
}

/// Where a parameter sits among its property's parameters of that name.
fn ordinal_of(params: &[IcalParam<'_>], at: usize, name: &str) -> usize {
    params[..at]
        .iter()
        .filter(|held| held.merge_name() == name)
        .count()
}

/// The position of the parameter of that name at that ordinal, if the property
/// has one. The index addresses the decoded list and its raw parameter nodes
/// alike, which a decode maps one for one.
fn nth_param(params: &[IcalParam<'_>], name: &str, at: usize) -> Option<usize> {
    params
        .iter()
        .enumerate()
        .filter(|(_, held)| held.merge_name() == name)
        .map(|(index, _)| index)
        .nth(at)
}