fix(calendar): exclude a CalDAV occurrence on its own EXDATE line
- a zoned EXDATE;TZID line no longer gets a UTC value appended - all-day series exclude with EXDATE;VALUE=DATE Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -70,7 +70,6 @@ pub fn build_vevent(draft: &CalDavEventDraft, stamp: DateTime<Utc>) -> String {
70
70
/// Add EXDATE for one occurrence on the series master and bump SEQUENCE.
71
71
/// Leaves overrides and other VEVENT blocks untouched.
72
72
pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Result<String, String> {
73
let stamp = format_utc(occurrence);
74
73
let mut out = Vec::new();
75
74
let mut in_event = false;
76
75
let mut is_master = false;
@@ -93,7 +92,7 @@ pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Resu
93
92
if upper == "END:VEVENT" {
94
93
event_lines.push(line);
95
94
if is_master {
96
out.extend(patch_master_exdate(event_lines, &stamp)?);
95
out.extend(patch_master_exdate(event_lines, occurrence)?);
97
96
patched = true;
98
97
} else {
99
98
out.extend(event_lines);
@@ -113,45 +112,54 @@ pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Resu
113
112
Ok(out.join("\r\n") + "\r\n")
114
113
}
115
114
116
fn patch_master_exdate(mut lines: Vec<String>, stamp: &str) -> Result<Vec<String>, String> {
115
fn patch_master_exdate(
116
mut lines: Vec<String>,
117
occurrence: DateTime<Utc>,
118
) -> Result<Vec<String>, String> {
119
let all_day = lines.iter().any(|line| {
120
let upper = line.to_ascii_uppercase();
121
upper.starts_with("DTSTART") && upper.contains("VALUE=DATE") && !upper.contains("DATE-TIME")
122
});
123
let (exdate, value) = if all_day {
124
let value = occurrence.format("%Y%m%d").to_string();
125
(format!("EXDATE;VALUE=DATE:{value}"), value)
126
} else {
127
let value = format_utc(occurrence);
128
(format!("EXDATE:{value}"), value)
129
};
117
130
let mut sequence_bumped = false;
118
let mut exdate_added = false;
131
let mut excluded = false;
119
132
for line in &mut lines {
120
133
let upper = line.to_ascii_uppercase();
121
134
if upper.starts_with("SEQUENCE:") {
122
let value = line.split_once(':').map(|(_, v)| v).unwrap_or("0");
123
let next = value.trim().parse::<u32>().unwrap_or(0).saturating_add(1);
135
let current = line.split_once(':').map(|(_, v)| v).unwrap_or("0");
136
let next = current.trim().parse::<u32>().unwrap_or(0).saturating_add(1);
124
137
*line = format!("SEQUENCE:{next}");
125
138
sequence_bumped = true;
126
139
}
127
if upper.starts_with("EXDATE") {
128
if line.contains(stamp) {
129
exdate_added = true;
130
} else if let Some((name, value)) = line.split_once(':') {
131
*line = format!("{name}:{value},{stamp}");
132
exdate_added = true;
133
}
140
if upper.starts_with("EXDATE")
141
&& line
142
.split_once(':')
143
.is_some_and(|(_, values)| values.split(',').any(|item| item == value))
144
{
145
excluded = true;
134
146
}
135
147
}
136
148
if !sequence_bumped {
137
if let Some(idx) = lines
149
let at = lines
138
150
.iter()
139
151
.position(|l| l.to_ascii_uppercase().starts_with("UID:"))
140
{
141
lines.insert(idx + 1, "SEQUENCE:1".to_owned());
142
} else {
143
lines.insert(1, "SEQUENCE:1".to_owned());
144
}
152
.map_or(1, |idx| idx + 1);
153
lines.insert(at, "SEQUENCE:1".to_owned());
145
154
}
146
if !exdate_added {
147
if let Some(idx) = lines.iter().position(|l| {
148
l.to_ascii_uppercase().starts_with("RRULE")
149
|| l.to_ascii_uppercase().starts_with("RDATE")
150
}) {
151
lines.insert(idx + 1, format!("EXDATE:{stamp}"));
152
} else {
155
if !excluded {
156
let Some(idx) = lines.iter().position(|l| {
157
let upper = l.to_ascii_uppercase();
158
upper.starts_with("RRULE") || upper.starts_with("RDATE")
159
}) else {
153
160
return Err("CalDAV series master missing RRULE/RDATE".into());
154
}
161
};
162
lines.insert(idx + 1, exdate);
155
163
}
156
164
Ok(lines)
157
165
}
@@ -197,6 +205,25 @@ mod tests {
197
205
assert!(ics.contains("ATTENDEE:mailto:a@b.c"));
198
206
}
199
207
208
#[test]
209
fn exclude_occurrence_keeps_a_zoned_exdate_and_adds_its_own_line() {
210
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nDTSTART;TZID=Europe/Berlin:20260105T100000\r\nRRULE:FREQ=WEEKLY\r\nEXDATE;TZID=Europe/Berlin:20260105T100000\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
211
let patched =
212
exclude_occurrence_from_ics(ics, Utc.with_ymd_and_hms(2026, 1, 12, 9, 0, 0).unwrap())
213
.unwrap();
214
assert!(patched.contains("EXDATE;TZID=Europe/Berlin:20260105T100000\r\n"));
215
assert!(patched.contains("EXDATE:20260112T090000Z\r\n"));
216
}
217
218
#[test]
219
fn exclude_occurrence_of_an_all_day_series_uses_a_date() {
220
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nDTSTART;VALUE=DATE:20260105\r\nRRULE:FREQ=WEEKLY\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
221
let patched =
222
exclude_occurrence_from_ics(ics, Utc.with_ymd_and_hms(2026, 1, 12, 0, 0, 0).unwrap())
223
.unwrap();
224
assert!(patched.contains("EXDATE;VALUE=DATE:20260112\r\n"));
225
}
226
200
227
#[test]
201
228
fn exclude_occurrence_adds_exdate_and_keeps_rrule() {
202
229
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nSEQUENCE:2\r\nDTSTART:20260105T090000Z\r\nDTEND:20260105T100000Z\r\nRRULE:FREQ=WEEKLY\r\nSUMMARY:Standup\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
@@ -70,7 +70,6 @@ pub fn build_vevent(draft: &CalDavEventDraft, stamp: DateTime<Utc>) -> String {70
/// Add EXDATE for one occurrence on the series master and bump SEQUENCE.70
/// Add EXDATE for one occurrence on the series master and bump SEQUENCE.71
/// Leaves overrides and other VEVENT blocks untouched.71
/// Leaves overrides and other VEVENT blocks untouched.72
pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Result<String, String> {72
pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Result<String, String> {73
let stamp = format_utc(occurrence);74
let mut out = Vec::new();73
let mut out = Vec::new();75
let mut in_event = false;74
let mut in_event = false;76
let mut is_master = false;75
let mut is_master = false;@@ -93,7 +92,7 @@ pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Resu93
if upper == "END:VEVENT" {92
if upper == "END:VEVENT" {94
event_lines.push(line);93
event_lines.push(line);95
if is_master {94
if is_master {96
out.extend(patch_master_exdate(event_lines, &stamp)?);95
out.extend(patch_master_exdate(event_lines, occurrence)?);97
patched = true;96
patched = true;98
} else {97
} else {99
out.extend(event_lines);98
out.extend(event_lines);@@ -113,45 +112,54 @@ pub fn exclude_occurrence_from_ics(ics: &str, occurrence: DateTime<Utc>) -> Resu113
Ok(out.join("\r\n") + "\r\n")112
Ok(out.join("\r\n") + "\r\n")114
}113
}115
114
116
fn patch_master_exdate(mut lines: Vec<String>, stamp: &str) -> Result<Vec<String>, String> {115
fn patch_master_exdate(116
mut lines: Vec<String>,117
occurrence: DateTime<Utc>,118
) -> Result<Vec<String>, String> {119
let all_day = lines.iter().any(|line| {120
let upper = line.to_ascii_uppercase();121
upper.starts_with("DTSTART") && upper.contains("VALUE=DATE") && !upper.contains("DATE-TIME")122
});123
let (exdate, value) = if all_day {124
let value = occurrence.format("%Y%m%d").to_string();125
(format!("EXDATE;VALUE=DATE:{value}"), value)126
} else {127
let value = format_utc(occurrence);128
(format!("EXDATE:{value}"), value)129
};117
let mut sequence_bumped = false;130
let mut sequence_bumped = false;118
let mut exdate_added = false;131
let mut excluded = false;119
for line in &mut lines {132
for line in &mut lines {120
let upper = line.to_ascii_uppercase();133
let upper = line.to_ascii_uppercase();121
if upper.starts_with("SEQUENCE:") {134
if upper.starts_with("SEQUENCE:") {122
let value = line.split_once(':').map(|(_, v)| v).unwrap_or("0");135
let current = line.split_once(':').map(|(_, v)| v).unwrap_or("0");123
let next = value.trim().parse::<u32>().unwrap_or(0).saturating_add(1);136
let next = current.trim().parse::<u32>().unwrap_or(0).saturating_add(1);124
*line = format!("SEQUENCE:{next}");137
*line = format!("SEQUENCE:{next}");125
sequence_bumped = true;138
sequence_bumped = true;126
}139
}127
if upper.starts_with("EXDATE") {140
if upper.starts_with("EXDATE")128
if line.contains(stamp) {141
&& line129
exdate_added = true;142
.split_once(':')130
} else if let Some((name, value)) = line.split_once(':') {143
.is_some_and(|(_, values)| values.split(',').any(|item| item == value))131
*line = format!("{name}:{value},{stamp}");144
{132
exdate_added = true;145
excluded = true;133
}134
}146
}135
}147
}136
if !sequence_bumped {148
if !sequence_bumped {137
if let Some(idx) = lines149
let at = lines138
.iter()150
.iter()139
.position(|l| l.to_ascii_uppercase().starts_with("UID:"))151
.position(|l| l.to_ascii_uppercase().starts_with("UID:"))140
{152
.map_or(1, |idx| idx + 1);141
lines.insert(idx + 1, "SEQUENCE:1".to_owned());153
lines.insert(at, "SEQUENCE:1".to_owned());142
} else {143
lines.insert(1, "SEQUENCE:1".to_owned());144
}145
}154
}146
if !exdate_added {155
if !excluded {147
if let Some(idx) = lines.iter().position(|l| {156
let Some(idx) = lines.iter().position(|l| {148
l.to_ascii_uppercase().starts_with("RRULE")157
let upper = l.to_ascii_uppercase();149
|| l.to_ascii_uppercase().starts_with("RDATE")158
upper.starts_with("RRULE") || upper.starts_with("RDATE")150
}) {159
}) else {151
lines.insert(idx + 1, format!("EXDATE:{stamp}"));152
} else {153
return Err("CalDAV series master missing RRULE/RDATE".into());160
return Err("CalDAV series master missing RRULE/RDATE".into());154
}161
};162
lines.insert(idx + 1, exdate);155
}163
}156
Ok(lines)164
Ok(lines)157
}165
}@@ -197,6 +205,25 @@ mod tests {197
assert!(ics.contains("ATTENDEE:mailto:a@b.c"));205
assert!(ics.contains("ATTENDEE:mailto:a@b.c"));198
}206
}199
207
208
#[test]209
fn exclude_occurrence_keeps_a_zoned_exdate_and_adds_its_own_line() {210
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nDTSTART;TZID=Europe/Berlin:20260105T100000\r\nRRULE:FREQ=WEEKLY\r\nEXDATE;TZID=Europe/Berlin:20260105T100000\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";211
let patched =212
exclude_occurrence_from_ics(ics, Utc.with_ymd_and_hms(2026, 1, 12, 9, 0, 0).unwrap())213
.unwrap();214
assert!(patched.contains("EXDATE;TZID=Europe/Berlin:20260105T100000\r\n"));215
assert!(patched.contains("EXDATE:20260112T090000Z\r\n"));216
}217
218
#[test]219
fn exclude_occurrence_of_an_all_day_series_uses_a_date() {220
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nDTSTART;VALUE=DATE:20260105\r\nRRULE:FREQ=WEEKLY\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";221
let patched =222
exclude_occurrence_from_ics(ics, Utc.with_ymd_and_hms(2026, 1, 12, 0, 0, 0).unwrap())223
.unwrap();224
assert!(patched.contains("EXDATE;VALUE=DATE:20260112\r\n"));225
}226
200
#[test]227
#[test]201
fn exclude_occurrence_adds_exdate_and_keeps_rrule() {228
fn exclude_occurrence_adds_exdate_and_keeps_rrule() {202
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nSEQUENCE:2\r\nDTSTART:20260105T090000Z\r\nDTEND:20260105T100000Z\r\nRRULE:FREQ=WEEKLY\r\nSUMMARY:Standup\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";229
let ics = "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:series\r\nSEQUENCE:2\r\nDTSTART:20260105T090000Z\r\nDTEND:20260105T100000Z\r\nRRULE:FREQ=WEEKLY\r\nSUMMARY:Standup\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";