mod windows_zones;
use core::{fmt, mem::take};
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
collections::{BTreeMap, BTreeSet},
format,
string::{String, ToString},
vec,
vec::Vec,
};
use ical::{
param::IcalParam,
prop::{IcalProp, IcalPropKind, IcalPropName, action::ACTION, trigger::TRIGGER},
tree::{
codec::Codec,
cst::{IcalCst, IcalItem},
error::IcalParseError,
line::IcalLine,
param::{
cn::CN, cutype::CUTYPE, fmttype::FMTTYPE, partstat::PARTSTAT, range::RANGE, role::ROLE,
tzid::TZID, value::VALUE,
},
},
tzdb,
value::{
IcalValue,
datetime::IcalDateTime,
recur::IcalRecur,
text::{IcalText, IcalTextList},
},
};
use jiff::{
Span, Timestamp,
civil::{Date, DateTime},
tz::TimeZone,
};
use serde_json::{from_value, to_value};
use crate::v1::{
field::MsgraphField,
rest::users::{
contacts::MsgraphSingleValueExtendedProperty,
events::{
MsgraphAttendee, MsgraphAttendeeType, MsgraphDateTimeTimeZone, MsgraphDayOfWeek,
MsgraphEvent, MsgraphFreeBusyStatus, MsgraphLocation, MsgraphPatternedRecurrence,
MsgraphRecurrencePattern, MsgraphRecurrencePatternType, MsgraphRecurrenceRange,
MsgraphRecurrenceRangeType, MsgraphResponseStatus, MsgraphResponseType,
MsgraphSensitivity, MsgraphWeekIndex,
},
messages::{MsgraphBodyType, MsgraphEmailAddress, MsgraphImportance, MsgraphItemBody},
},
};
pub const MSGRAPH_EVENT_STASH_ID: &str =
"String {c8e5e5cf-3f6c-4f0a-9d4e-52f1e7b2a9d3} Name pimalaya-ical";
pub const MSGRAPH_EVENT_STASH_EXPAND: &str = "singleValueExtendedProperties($filter=id eq \
'String {c8e5e5cf-3f6c-4f0a-9d4e-52f1e7b2a9d3} Name pimalaya-ical')";
pub const MSGRAPH_EVENT_ICAL_SELECT: &str = "id,iCalUId,changeKey,createdDateTime,\
lastModifiedDateTime,type,seriesMasterId,originalStart,originalStartTimeZone,\
originalEndTimeZone,subject,body,start,end,isAllDay,location,recurrence,attendees,\
organizer,categories,showAs,sensitivity,importance,isReminderOn,\
reminderMinutesBeforeStart,isCancelled,webLink,onlineMeeting,cancelledOccurrences";
pub const MSGRAPH_INSTANCES_MAX_YEARS: i16 = 5;
pub(crate) const PRODID: &str = "-//Pimalaya//io-msgraph//EN";
pub(crate) const MAX_STASH_LINE: usize = 8 * 1024;
const MINTED_PROPS: &[&str] = &["X-MSGRAPH-WEB-LINK", "CONFERENCE", TEAMS_MEETING_URL];
const TEAMS_MEETING_URL: &str = "X-MICROSOFT-SKYPETEAMSMEETINGURL";
const ONLINE_MEETING: &str = "X-PIMDIR-ONLINE-MEETING";
const BUSY_STATUS: &str = "X-MICROSOFT-CDO-BUSYSTATUS";
const ALT_DESC: &str = "X-ALT-DESC";
impl MsgraphEvent {
pub fn to_ical(&self) -> String {
self.to_ical_series(&[])
}
pub fn to_ical_series(&self, exceptions: &[&MsgraphEvent]) -> String {
let master = localized(self);
let master = &*master;
let exceptions: Vec<Cow<'_, MsgraphEvent>> = exceptions
.iter()
.map(|exception| localized(exception))
.collect();
let uid = master
.stashed_uid()
.or_else(|| master.ical_uid.as_deref().map(original_uid))
.unwrap_or_else(|| master.id.clone());
let mut zones = BTreeSet::new();
let mut events = vec![vevent(master, None, &uid, &mut zones)];
for exception in &exceptions {
events.push(vevent(exception, Some(master), &uid, &mut zones));
}
let mut calendar = IcalCst::v2();
calendar.push(IcalProp::text(IcalPropKind::ProdId, vec![], PRODID));
let anchor = anchor(master);
for zone in &zones {
if let Some(vtimezone) = tzdb::vtimezone(zone, anchor) {
calendar.push_component(vtimezone);
}
}
for event in events {
calendar.push_component(event);
}
String::from_utf8_lossy(&calendar.to_bytes()).into_owned()
}
pub fn from_ical(contents: &[u8]) -> Result<Self, MsgraphEventIcalError> {
let calendar = IcalCst::parse(contents).map_err(MsgraphEventIcalError::Parse)?;
project(find_vevent(&calendar, |vevent| {
!has_prop(vevent, "RECURRENCE-ID")
})?)
}
pub fn create_from_ical(contents: &[u8]) -> Result<Self, MsgraphEventIcalError> {
let mut event = Self::from_ical(contents)?;
if stash_lines(&event.single_value_extended_properties).is_empty() {
event.single_value_extended_properties = MsgraphField::Unset;
}
let mut body = to_value(&event).map_err(MsgraphEventIcalError::Json)?;
if let Some(object) = body.as_object_mut() {
object.retain(|_, value| {
!value.is_null() && value.as_array().is_none_or(|array| !array.is_empty())
});
}
from_value(body).map_err(MsgraphEventIcalError::Json)
}
pub fn update_from_ical(contents: &[u8], base: &[u8]) -> Result<Self, MsgraphEventIcalError> {
let mut event = Self::from_ical(contents)?;
event.unset_unchanged(&Self::from_ical(base)?);
Ok(event)
}
pub fn instance_update_from_ical(
contents: &[u8],
base: &[u8],
) -> Result<Option<Self>, MsgraphEventIcalError> {
let occurrence = |contents| {
let calendar = IcalCst::parse(contents).map_err(MsgraphEventIcalError::Parse)?;
project(find_vevent(&calendar, |_| true)?)
};
let event = occurrence(contents)?;
Ok(instance_patch(event, &occurrence(base)?))
}
fn unset_unchanged(&mut self, base: &Self) {
macro_rules! unset_unchanged {
($($field:ident),* $(,)?) => {$(
if self.$field == base.$field {
self.$field = MsgraphField::Unset;
}
)*};
}
if same_text(&body_text(&self.body), &body_text(&base.body)) {
self.body = MsgraphField::Unset;
}
unset_unchanged!(
subject,
start,
end,
is_all_day,
location,
recurrence,
attendees,
categories,
show_as,
sensitivity,
importance,
is_reminder_on,
reminder_minutes_before_start,
is_online_meeting,
single_value_extended_properties,
);
}
pub fn recurrence_id_of(&self, original_start: &str) -> Option<String> {
let master = localized(self);
let prop = recurrence_id(&master, original_start, &mut BTreeSet::new())?;
match prop.value {
IcalValue::Date(date) => Some(date.0.into_owned()),
IcalValue::DateTime(stamp) => Some(stamp.0.into_owned()),
_ => None,
}
}
pub fn stashed_uid(&self) -> Option<String> {
stash_lines(&self.single_value_extended_properties)
.iter()
.find_map(|line| {
let mut bytes = line.clone();
bytes.push_str("\r\n");
let (line, _) = IcalLine::take(bytes.as_bytes()).ok()?;
line.name
.get()
.eq_ignore_ascii_case("UID")
.then(|| line.raw_value_str().trim().to_string())
.filter(|uid| !uid.is_empty())
})
}
}
impl MsgraphPatternedRecurrence {
pub fn bounds(&self) -> Option<(Date, Option<Date>)> {
let range = &self.range;
let start = range.start_date.as_deref()?.parse::<Date>().ok()?;
let end = match range.range_type? {
MsgraphRecurrenceRangeType::EndDate => {
Some(range.end_date.as_deref()?.parse::<Date>().ok()?)
}
MsgraphRecurrenceRangeType::Numbered => {
let periods = i64::from(range.number_of_occurrences?)
* i64::from(self.pattern.interval.unwrap_or(1).max(1));
let span = match self.pattern.pattern_type? {
MsgraphRecurrencePatternType::Daily => Span::new().try_days(periods),
MsgraphRecurrencePatternType::Weekly => Span::new().try_weeks(periods),
MsgraphRecurrencePatternType::AbsoluteMonthly
| MsgraphRecurrencePatternType::RelativeMonthly => {
Span::new().try_months(periods)
}
MsgraphRecurrencePatternType::AbsoluteYearly
| MsgraphRecurrencePatternType::RelativeYearly => {
Span::new().try_years(periods)
}
};
Some(start.checked_add(span.ok()?).ok()?)
}
MsgraphRecurrenceRangeType::NoEnd => None,
};
Some((start, end))
}
pub fn instance_windows(&self, within: (Date, Date)) -> Option<Vec<(String, String)>> {
let (start, end) = self.bounds()?;
let end = match end {
Some(end) => end.checked_add(Span::new().days(1)).ok()?,
None => within.1,
};
let start = start.max(within.0);
let end = end.min(within.1);
let instant = |date: Date| -> Option<String> {
let timestamp: Timestamp = date.to_zoned(TimeZone::UTC).ok()?.timestamp();
Some(timestamp.strftime("%Y-%m-%dT%H:%M:%SZ").to_string())
};
let mut windows = Vec::new();
let mut from = start;
while from < end {
let longest = from
.checked_add(Span::new().years(MSGRAPH_INSTANCES_MAX_YEARS))
.ok()?
.checked_sub(Span::new().days(1))
.ok()?;
let to = longest.min(end);
windows.push((instant(from)?, instant(to)?));
from = to;
}
Some(windows)
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MsgraphEventSeries {
pub master: MsgraphEvent,
pub instances: Vec<MsgraphEventInstanceChange>,
pub left_out: Vec<String>,
}
impl MsgraphEventSeries {
pub fn create_from_ical(contents: &[u8]) -> Result<Self, MsgraphEventIcalError> {
let master = MsgraphEvent::create_from_ical(contents)?;
let (instances, left_out) = instance_changes(contents, None)?;
Ok(Self {
master,
instances,
left_out,
})
}
pub fn update_from_ical(contents: &[u8], base: &[u8]) -> Result<Self, MsgraphEventIcalError> {
let master = MsgraphEvent::update_from_ical(contents, base)?;
let moved = !master.start.is_unset()
|| !master.end.is_unset()
|| !master.is_all_day.is_unset()
|| !master.recurrence.is_unset();
let held = (!moved).then_some(base);
let (instances, left_out) = instance_changes(contents, held)?;
Ok(Self {
master,
instances,
left_out,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MsgraphEventInstanceChange {
pub original_start: String,
pub window: (String, String),
pub action: MsgraphEventInstanceAction,
}
impl MsgraphEventInstanceChange {
pub fn is_instance(&self, instance: &MsgraphEvent) -> bool {
let Some(original) = instance
.original_start
.as_deref()
.and_then(|start| start.parse::<Timestamp>().ok())
else {
return false;
};
match self.original_start.parse::<Timestamp>() {
Ok(start) => start == original,
Err(_) => self
.original_start
.parse::<Date>()
.is_ok_and(|date| original.to_zoned(TimeZone::UTC).date() == date),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MsgraphEventInstanceAction {
Delete,
Update(Box<MsgraphEvent>),
}
#[derive(Debug)]
pub enum MsgraphEventIcalError {
Parse(IcalParseError),
NoEvent,
NotAnEvent(String),
NoZonedStart,
NoEnd,
UnsupportedRecurrence(String),
Json(serde_json::Error),
}
impl fmt::Display for MsgraphEventIcalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse(err) => write!(f, "Invalid iCalendar: {err}"),
Self::NoEvent => write!(f, "The iCalendar contents carry no VEVENT"),
Self::NotAnEvent(name) => {
write!(
f,
"Microsoft Graph models no {name}: its calendars hold events only"
)
}
Self::NoZonedStart => write!(
f,
"Microsoft Graph needs a DTSTART that is a date, in UTC, or in a named zone"
),
Self::NoEnd => write!(f, "The VEVENT has a DTEND or a DURATION that gives no end"),
Self::UnsupportedRecurrence(rule) => {
write!(f, "Microsoft Graph cannot hold the recurrence `{rule}`")
}
Self::Json(err) => write!(f, "Cannot shape the Graph event body: {err}"),
}
}
}
impl core::error::Error for MsgraphEventIcalError {}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum OriginalStart {
Date(Date),
Instant(Timestamp),
}
impl OriginalStart {
fn read(value: &str, tzid: Option<&str>, series: &Boundary) -> Option<Self> {
let value = value.trim();
if value.len() == 8 {
let date = Date::strptime("%Y%m%d", value).ok()?;
return series.all_day.then_some(Self::Date(date));
}
let (local, utc) = match value.strip_suffix('Z') {
Some(local) => (local, true),
None => (value, false),
};
let local = DateTime::strptime("%Y%m%dT%H%M%S", local).ok()?;
if series.all_day {
return Some(Self::Date(local.date()));
}
let zone = if utc {
TimeZone::UTC
} else {
match zone(Some(tzid.unwrap_or(&series.zone))) {
Zone::Utc => TimeZone::UTC,
Zone::Iana(name) => TimeZone::get(&name).ok()?,
Zone::Unknown(_) => return None,
}
};
Some(Self::Instant(local.to_zoned(zone).ok()?.timestamp()))
}
fn day(self) -> Date {
match self {
Self::Date(date) => date,
Self::Instant(instant) => instant.to_zoned(TimeZone::UTC).date(),
}
}
fn to_graph(self) -> String {
match self {
Self::Date(date) => date.to_string(),
Self::Instant(instant) => instant.strftime("%Y-%m-%dT%H:%M:%SZ").to_string(),
}
}
}
#[derive(Default)]
struct Exceptions {
recurring: bool,
cancelled: BTreeMap<OriginalStart, String>,
overridden: BTreeMap<OriginalStart, Override>,
left_out: Vec<String>,
}
impl Exceptions {
fn read(calendar: &IcalCst<'_>, series: &Boundary) -> Result<Self, MsgraphEventIcalError> {
let master = find_vevent(calendar, |vevent| !has_prop(vevent, "RECURRENCE-ID"))?;
let mut exceptions = Self {
recurring: has_prop(master, "RRULE"),
..Default::default()
};
for item in &master.items {
let IcalItem::Prop(line) = item else {
continue;
};
let name = line.name.get();
if name.eq_ignore_ascii_case("RDATE") || name.eq_ignore_ascii_case("EXRULE") {
exceptions.left_out.push(raw_line(line));
continue;
}
if !name.eq_ignore_ascii_case("EXDATE") {
continue;
}
let raw = raw_line(line);
let values = line.raw_value_str();
let head = raw.strip_suffix(&*values).unwrap_or(&raw);
let tzid = line.param::<TZID>();
for value in values.split(',') {
let named = format!("{head}{}", value.trim());
match OriginalStart::read(value, tzid.as_deref(), series) {
Some(start) if exceptions.recurring => {
exceptions.cancelled.insert(start, named);
}
_ => exceptions.left_out.push(named),
}
}
}
let vevents = calendar.items.iter().filter_map(|item| match item {
IcalItem::Component(component) if component_name(component) == "VEVENT" => {
Some(component.as_ref())
}
_ => None,
});
for vevent in vevents {
let Some(line) = find_prop(vevent, "RECURRENCE-ID") else {
continue;
};
let named = raw_line(line);
let tzid = line.param::<TZID>();
let start = OriginalStart::read(&line.raw_value_str(), tzid.as_deref(), series)
.filter(|_| exceptions.recurring && line.param::<RANGE>().is_none());
let Some(start) = start else {
exceptions.left_out.push(named);
continue;
};
let cancelled = find_prop(vevent, "STATUS").is_some_and(|status| {
status
.raw_value_str()
.trim()
.eq_ignore_ascii_case("CANCELLED")
});
if cancelled {
exceptions.cancelled.insert(start, named);
continue;
}
let event = project(vevent)?;
exceptions.left_out.extend(
stash_lines(&event.single_value_extended_properties)
.into_iter()
.filter(|line| {
let name = line.split([';', ':']).next().unwrap_or_default();
!name.eq_ignore_ascii_case("UID")
&& !name.eq_ignore_ascii_case("RECURRENCE-ID")
}),
);
let moved_to = find_prop(vevent, "DTSTART").and_then(|line| {
let tzid = line.param::<TZID>();
OriginalStart::read(&line.raw_value_str(), tzid.as_deref(), series)
});
exceptions.overridden.insert(
start,
Override {
event,
moved_to,
line: named,
},
);
}
Ok(exceptions)
}
}
struct Override {
event: MsgraphEvent,
moved_to: Option<OriginalStart>,
line: String,
}
fn instance_changes(
contents: &[u8],
base: Option<&[u8]>,
) -> Result<(Vec<MsgraphEventInstanceChange>, Vec<String>), MsgraphEventIcalError> {
let calendar = IcalCst::parse(contents).map_err(MsgraphEventIcalError::Parse)?;
let master = find_vevent(&calendar, |vevent| !has_prop(vevent, "RECURRENCE-ID"))?;
let start = find_prop(master, "DTSTART")
.and_then(boundary)
.ok_or(MsgraphEventIcalError::NoZonedStart)?;
let series = project(master)?;
let mut document = Exceptions::read(&calendar, &start)?;
let held = match base {
Some(base) => {
let calendar = IcalCst::parse(base).map_err(MsgraphEventIcalError::Parse)?;
Exceptions::read(&calendar, &start)?
}
None => Exceptions::default(),
};
let mut left_out = take(&mut document.left_out);
if !document.recurring {
return Ok((Vec::new(), left_out));
}
let starts: BTreeSet<OriginalStart> = [&document, &held]
.into_iter()
.flat_map(|exceptions| {
exceptions
.cancelled
.keys()
.chain(exceptions.overridden.keys())
})
.copied()
.collect();
let mut changes = Vec::new();
for start in starts {
let overridden = document.overridden.get(&start);
let held_overridden = held.overridden.get(&start);
let action = if document.cancelled.contains_key(&start) {
if held.cancelled.contains_key(&start) {
continue;
}
MsgraphEventInstanceAction::Delete
} else if let Some(line) = held.cancelled.get(&start) {
left_out.push(line.clone());
continue;
} else {
let occurrence = match overridden {
Some(exception) => Some(exception.event.clone()),
None => carried(&series, start),
};
let held_occurrence = match held_overridden {
Some(exception) => Some(exception.event.clone()),
None => carried(&series, start),
};
let (Some(occurrence), Some(held_occurrence)) = (occurrence, held_occurrence) else {
let exception = overridden.or(held_overridden);
left_out.extend(exception.map(|exception| exception.line.clone()));
continue;
};
match instance_patch(occurrence, &held_occurrence) {
Some(patch) => MsgraphEventInstanceAction::Update(Box::new(patch)),
None => continue,
}
};
let moved_to = held_overridden.and_then(|exception| exception.moved_to);
let first = moved_to.map_or(start.day(), |moved| start.day().min(moved.day()));
let last = moved_to.map_or(start.day(), |moved| start.day().max(moved.day()));
let midnight = |date: Date| format!("{date}T00:00:00Z");
changes.push(MsgraphEventInstanceChange {
original_start: start.to_graph(),
window: (
midnight(first.saturating_sub(Span::new().days(1))),
midnight(last.saturating_add(Span::new().days(2))),
),
action,
});
}
Ok((changes, left_out))
}
fn carried(series: &MsgraphEvent, start: OriginalStart) -> Option<MsgraphEvent> {
let from = series.start.as_option()?;
let to = series.end.as_option()?;
let span = from
.date_time
.parse::<DateTime>()
.ok()?
.until(to.date_time.parse::<DateTime>().ok()?)
.ok()?;
let local = match start {
OriginalStart::Date(date) => date.at(0, 0, 0, 0),
OriginalStart::Instant(instant) => {
let zone = match zone(from.time_zone.as_deref()) {
Zone::Utc => TimeZone::UTC,
Zone::Iana(name) => TimeZone::get(&name).ok()?,
Zone::Unknown(_) => return None,
};
instant.to_zoned(zone).datetime()
}
};
let wall = |local: DateTime, boundary: &MsgraphDateTimeTimeZone| MsgraphDateTimeTimeZone {
date_time: local.strftime("%Y-%m-%dT%H:%M:%S").to_string(),
time_zone: boundary.time_zone.clone(),
};
let mut occurrence = series.clone();
occurrence.start = MsgraphField::Set(wall(local, from));
occurrence.end = MsgraphField::Set(wall(local.checked_add(span).ok()?, to));
Some(occurrence)
}
fn instance_patch(mut occurrence: MsgraphEvent, held: &MsgraphEvent) -> Option<MsgraphEvent> {
occurrence.unset_unchanged(held);
occurrence.recurrence = MsgraphField::Unset;
occurrence.single_value_extended_properties = MsgraphField::Unset;
(occurrence != MsgraphEvent::default()).then_some(occurrence)
}
fn project(vevent: &IcalCst<'_>) -> Result<MsgraphEvent, MsgraphEventIcalError> {
let mut event = MsgraphEvent {
subject: MsgraphField::Null,
body: MsgraphField::Null,
location: MsgraphField::Null,
recurrence: MsgraphField::Null,
attendees: MsgraphField::Set(Vec::new()),
categories: MsgraphField::Set(Vec::new()),
show_as: MsgraphField::Null,
sensitivity: MsgraphField::Null,
importance: MsgraphField::Null,
is_reminder_on: MsgraphField::Set(false),
..Default::default()
};
let mut description = None;
let mut html = None;
let mut start = None;
let mut end = None;
let mut duration = None;
let mut transparent = None;
let mut rrule = None;
let mut stash = Vec::new();
for item in &vevent.items {
match item {
IcalItem::Prop(line) => {
let name = line.name.get();
let consumed = if MINTED_PROPS.iter().any(|m| name.eq_ignore_ascii_case(m)) {
true
} else if name.eq_ignore_ascii_case(ONLINE_MEETING) {
if line.raw_value_str().trim().eq_ignore_ascii_case("TRUE") {
event.is_online_meeting = MsgraphField::Set(true);
}
true
} else if name.eq_ignore_ascii_case(BUSY_STATUS) {
event.show_as =
MsgraphField::set_or_null(busy_status_from_ical(&line.raw_value_str()));
true
} else if name.eq_ignore_ascii_case(ALT_DESC)
&& line
.param::<FMTTYPE>()
.is_some_and(|kind| kind.eq_ignore_ascii_case("text/html"))
{
html = Some(text(line));
true
} else {
match name.parse::<IcalPropKind>() {
Ok(IcalPropKind::Summary) => {
event.subject = MsgraphField::Set(text(line));
true
}
Ok(IcalPropKind::Description) => {
description = Some(text(line));
true
}
Ok(IcalPropKind::Location) => {
event.location = MsgraphField::Set(MsgraphLocation {
display_name: Some(text(line)),
..Default::default()
});
true
}
Ok(IcalPropKind::DtStart) => {
start = Some(boundary(line));
true
}
Ok(IcalPropKind::DtEnd) => {
end = Some(boundary(line));
true
}
Ok(IcalPropKind::Duration) => {
let span = line.raw_value_str().trim().parse::<Span>().ok();
duration = Some(span);
span.is_some()
}
Ok(IcalPropKind::RRule) => {
rrule = Some(IcalRecur::decode(&line.value).0.trim().to_owned());
true
}
Ok(IcalPropKind::ExDate) => true,
Ok(IcalPropKind::Categories) => {
let list = IcalTextList::decode(&line.value);
if let MsgraphField::Set(categories) = &mut event.categories {
categories.extend(list.0.into_iter().map(Cow::into_owned));
}
true
}
Ok(IcalPropKind::Class) => {
event.sensitivity = MsgraphField::set_or_null(sensitivity_from_ical(
&line.raw_value_str(),
));
true
}
Ok(IcalPropKind::Priority) => {
event.importance = MsgraphField::set_or_null(importance_from_ical(
&line.raw_value_str(),
));
true
}
Ok(IcalPropKind::Transp) => {
transparent = Some(
line.raw_value_str()
.trim()
.eq_ignore_ascii_case("TRANSPARENT"),
);
true
}
Ok(IcalPropKind::Attendee) => {
if let MsgraphField::Set(attendees) = &mut event.attendees {
attendees.push(attendee(line));
}
true
}
Ok(
IcalPropKind::Organizer
| IcalPropKind::Status
| IcalPropKind::DtStamp
| IcalPropKind::Created
| IcalPropKind::LastModified
| IcalPropKind::Sequence,
) => true,
_ => false,
}
};
if !consumed {
let raw = raw_line(line);
if raw.len() <= MAX_STASH_LINE {
stash.push(raw);
}
}
}
IcalItem::Component(alarm) => {
let reminder = reminder_minutes(alarm);
match (&event.reminder_minutes_before_start, reminder) {
(MsgraphField::Unset, Some(minutes)) => {
event.is_reminder_on = MsgraphField::Set(true);
event.reminder_minutes_before_start = MsgraphField::Set(minutes);
}
_ => stash.extend(raw_component(alarm)),
}
}
IcalItem::Opaque(bytes) => stash.push(String::from_utf8_lossy(bytes).into_owned()),
}
}
let Some(Some(start)) = start else {
return Err(MsgraphEventIcalError::NoZonedStart);
};
let end = match (end, duration) {
(Some(Some(end)), _) => end,
(_, Some(Some(duration))) => start
.shifted(duration)
.ok_or(MsgraphEventIcalError::NoEnd)?,
(None, None) => {
let implied = Span::new().days(i64::from(start.all_day));
start.shifted(implied).ok_or(MsgraphEventIcalError::NoEnd)?
}
_ => return Err(MsgraphEventIcalError::NoEnd),
};
event.is_all_day = MsgraphField::Set(start.all_day);
if let Some(rule) = &rrule {
event.recurrence = MsgraphField::Set(recurrence(rule, &start)?);
}
event.start = MsgraphField::Set(start.into_graph());
event.end = MsgraphField::Set(end.into_graph());
event.body = match (html, description) {
(Some(html), Some(content)) if !same_text(&strip_html(&html), &content) => {
MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Text),
content: Some(content),
})
}
(Some(content), _) => MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Html),
content: Some(content),
}),
(None, Some(content)) => MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Text),
content: Some(content),
}),
(None, None) => MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Text),
content: Some(String::new()),
}),
};
if matches!(event.show_as, MsgraphField::Null) {
event.show_as = match transparent {
Some(true) => MsgraphField::Set(MsgraphFreeBusyStatus::Free),
Some(false) => MsgraphField::Set(MsgraphFreeBusyStatus::Busy),
None => MsgraphField::Null,
};
}
event.single_value_extended_properties =
MsgraphField::Set(vec![MsgraphSingleValueExtendedProperty {
id: MSGRAPH_EVENT_STASH_ID.to_string(),
value: stash.join("\n"),
}]);
Ok(event)
}
fn vevent(
event: &MsgraphEvent,
master: Option<&MsgraphEvent>,
uid: &str,
zones: &mut BTreeSet<String>,
) -> IcalCst<'static> {
let mut vevent = IcalCst::empty("VEVENT");
vevent.push(IcalProp::text(IcalPropKind::Uid, vec![], uid.to_owned()));
let stamp = event
.last_modified_date_time
.as_deref()
.or(event.created_date_time.as_deref())
.and_then(utc_stamp);
if let Some(stamp) = &stamp {
vevent.push(stamp_prop(IcalPropKind::DtStamp, stamp.clone()));
}
let all_day = event.is_all_day.as_option().copied().unwrap_or(false);
if let Some(start) = event.start.as_option() {
vevent.push(boundary_prop(IcalPropKind::DtStart, start, all_day, zones));
}
if let Some(end) = event.end.as_option() {
vevent.push(boundary_prop(IcalPropKind::DtEnd, end, all_day, zones));
}
if let (Some(master), Some(original)) = (master, &event.original_start)
&& let Some(prop) = recurrence_id(master, original, zones)
{
vevent.push(prop);
}
if let Some(subject) = event.subject.as_option() {
vevent.push(IcalProp::text(
IcalPropKind::Summary,
vec![],
subject.clone(),
));
}
if let Some(body) = event.body.as_option() {
let content = body.content.clone().unwrap_or_default();
match body.content_type {
Some(MsgraphBodyType::Html) => {
vevent.push(IcalProp::text(
IcalPropKind::Description,
vec![],
strip_html(&content),
));
vevent.push(IcalProp::text(
ALT_DESC,
vec![IcalParam::FmtType("text/html".into())],
content,
));
}
_ if !content.is_empty() => {
vevent.push(IcalProp::text(IcalPropKind::Description, vec![], content));
}
_ => {}
}
}
if let Some(name) = event
.location
.as_option()
.and_then(|location| location.display_name.clone())
.filter(|name| !name.is_empty())
{
vevent.push(IcalProp::text(IcalPropKind::Location, vec![], name));
}
if event.is_cancelled == Some(true) {
vevent.push(IcalProp::text(IcalPropKind::Status, vec![], "CANCELLED"));
}
if let Some(show_as) = event.show_as.as_option() {
let transp = match show_as {
MsgraphFreeBusyStatus::Free => "TRANSPARENT",
_ => "OPAQUE",
};
vevent.push(IcalProp::text(IcalPropKind::Transp, vec![], transp));
vevent.push(IcalProp::text(
BUSY_STATUS,
vec![],
busy_status_to_ical(*show_as),
));
}
if let Some(class) = event
.sensitivity
.as_option()
.map(|s| sensitivity_to_ical(*s))
{
vevent.push(IcalProp::text(IcalPropKind::Class, vec![], class));
}
if let Some(importance) = event.importance.as_option() {
let priority = match importance {
MsgraphImportance::High => "1",
MsgraphImportance::Normal => "5",
MsgraphImportance::Low => "9",
};
vevent.push(IcalProp {
name: IcalPropName::Kind(IcalPropKind::Priority),
params: Vec::new(),
value: IcalValue::Integer(priority.into()),
});
}
let categories: Vec<Cow<'static, str>> = event
.categories
.as_option()
.map(Vec::as_slice)
.unwrap_or(&[])
.iter()
.filter(|category| !category.is_empty())
.map(|category| Cow::Owned(category.clone()))
.collect();
if !categories.is_empty() {
vevent.push(IcalProp {
name: IcalPropName::Kind(IcalPropKind::Categories),
params: Vec::new(),
value: IcalValue::TextList(IcalTextList(categories)),
});
}
for (kind, stamp) in [
(IcalPropKind::Created, &event.created_date_time),
(IcalPropKind::LastModified, &event.last_modified_date_time),
] {
if let Some(stamp) = stamp.as_deref().and_then(utc_stamp) {
vevent.push(stamp_prop(kind, stamp));
}
}
if let Some(organizer) = event
.organizer
.as_option()
.map(|organizer| &organizer.email_address)
{
vevent.push(person_prop(IcalPropKind::Organizer, organizer, Vec::new()));
}
for attendee in event
.attendees
.as_option()
.map(Vec::as_slice)
.unwrap_or(&[])
{
if let Some(prop) = attendee_prop(attendee) {
vevent.push(prop);
}
}
if master.is_none()
&& let Some(recurrence) = event.recurrence.as_option()
{
if let Some(rule) = rrule(recurrence, event.start.as_option(), all_day) {
vevent.push(IcalProp {
name: IcalPropName::Kind(IcalPropKind::RRule),
params: Vec::new(),
value: IcalValue::Recur(rule.into()),
});
}
for exdate in exdates(event, all_day, zones) {
vevent.push(exdate);
}
}
if let Some(link) = &event.web_link {
vevent.push(IcalProp::text("X-MSGRAPH-WEB-LINK", vec![], link.clone()));
}
if let Some(url) = event
.online_meeting
.as_ref()
.and_then(|meeting| meeting.join_url.as_deref())
.filter(|url| !url.is_empty())
{
vevent.push(IcalProp {
name: IcalPropName::Kind(IcalPropKind::Conference),
params: vec![
IcalParam::Value("URI".into()),
IcalParam::Feature(vec!["AUDIO".into(), "VIDEO".into()]),
],
value: IcalValue::Uri(url.to_owned().into()),
});
}
let stash: Vec<String> = stash_lines(&event.single_value_extended_properties)
.into_iter()
.filter(|line| !is_uid_line(line) && !is_minted_line(line))
.collect();
push_stash(&mut vevent, &stash);
if event.is_reminder_on.as_option() == Some(&true)
&& let Some(minutes) = event.reminder_minutes_before_start.as_option()
{
let mut alarm = IcalCst::empty("VALARM");
alarm.push(IcalProp::text(IcalPropKind::Action, vec![], "DISPLAY"));
alarm.push(IcalProp {
name: IcalPropName::Kind(IcalPropKind::Trigger),
params: Vec::new(),
value: IcalValue::Duration(format!("-PT{minutes}M").into()),
});
alarm.push(IcalProp::text(
IcalPropKind::Description,
vec![],
"Reminder",
));
vevent.push_component(alarm);
}
vevent
}
fn localized(event: &MsgraphEvent) -> Cow<'_, MsgraphEvent> {
if event.is_all_day.as_option() == Some(&true) {
return Cow::Borrowed(event);
}
let start = local_boundary(
event.start.as_option(),
event.original_start_time_zone.as_deref(),
);
let end = local_boundary(
event.end.as_option(),
event.original_end_time_zone.as_deref(),
);
if start.is_none() && end.is_none() {
return Cow::Borrowed(event);
}
let mut event = event.clone();
if let Some(start) = start {
event.start = MsgraphField::Set(start);
}
if let Some(end) = end {
event.end = MsgraphField::Set(end);
}
Cow::Owned(event)
}
fn local_boundary(
boundary: Option<&MsgraphDateTimeTimeZone>,
original: Option<&str>,
) -> Option<MsgraphDateTimeTimeZone> {
let boundary = boundary?;
if !matches!(zone(boundary.time_zone.as_deref()), Zone::Utc) {
return None;
}
let Zone::Iana(name) = zone(original) else {
return None;
};
let utc = boundary
.date_time
.split('.')
.next()?
.parse::<DateTime>()
.ok()?;
let local = utc
.to_zoned(TimeZone::UTC)
.ok()?
.with_time_zone(TimeZone::get(&name).ok()?)
.datetime();
Some(MsgraphDateTimeTimeZone {
date_time: local.strftime("%Y-%m-%dT%H:%M:%S").to_string(),
time_zone: Some(name),
})
}
pub(crate) enum Zone {
Utc,
Iana(String),
Unknown(String),
}
pub(crate) fn zone(name: Option<&str>) -> Zone {
let Some(name) = name.map(str::trim).filter(|name| !name.is_empty()) else {
return Zone::Utc;
};
if name.eq_ignore_ascii_case("UTC") || name.eq_ignore_ascii_case("Etc/UTC") {
return Zone::Utc;
}
if let Some(iana) = windows_zones::iana(name) {
return match iana {
"Etc/UTC" => Zone::Utc,
iana => Zone::Iana(iana.to_owned()),
};
}
if tzdb::is_known(name) {
return Zone::Iana(name.to_owned());
}
Zone::Unknown(name.to_owned())
}
fn boundary_prop(
kind: IcalPropKind,
boundary: &MsgraphDateTimeTimeZone,
all_day: bool,
zones: &mut BTreeSet<String>,
) -> IcalProp<'static> {
let digits = ical_digits(&boundary.date_time);
if all_day {
return IcalProp {
name: IcalPropName::Kind(kind),
params: vec![IcalParam::Value("DATE".into())],
value: IcalValue::Date(digits.get(..8).unwrap_or_default().to_owned().into()),
};
}
match zone(boundary.time_zone.as_deref()) {
Zone::Utc => stamp_prop(kind, format!("{digits}Z")),
Zone::Iana(zone) | Zone::Unknown(zone) => {
zones.insert(zone.clone());
IcalProp {
name: IcalPropName::Kind(kind),
params: vec![IcalParam::TzId(zone.into())],
value: IcalValue::DateTime(digits.into()),
}
}
}
}
fn recurrence_id(
master: &MsgraphEvent,
original: &str,
zones: &mut BTreeSet<String>,
) -> Option<IcalProp<'static>> {
let instant = original.parse::<Timestamp>().ok()?;
let all_day = master.is_all_day.as_option().copied().unwrap_or(false);
let start_zone = master
.start
.as_option()
.and_then(|start| start.time_zone.as_deref());
if all_day {
let date = instant.to_zoned(TimeZone::UTC).date();
return Some(IcalProp {
name: IcalPropName::Kind(IcalPropKind::RecurrenceId),
params: vec![IcalParam::Value("DATE".into())],
value: IcalValue::Date(date.strftime("%Y%m%d").to_string().into()),
});
}
match zone(start_zone) {
Zone::Iana(name) => {
let local = instant.to_zoned(TimeZone::get(&name).ok()?).datetime();
zones.insert(name.clone());
Some(IcalProp {
name: IcalPropName::Kind(IcalPropKind::RecurrenceId),
params: vec![IcalParam::TzId(name.into())],
value: IcalValue::DateTime(local.strftime("%Y%m%dT%H%M%S").to_string().into()),
})
}
_ => Some(stamp_prop(
IcalPropKind::RecurrenceId,
instant.strftime("%Y%m%dT%H%M%SZ").to_string(),
)),
}
}
pub(crate) fn rrule(
recurrence: &MsgraphPatternedRecurrence,
start: Option<&MsgraphDateTimeTimeZone>,
all_day: bool,
) -> Option<String> {
let pattern = &recurrence.pattern;
let mut parts = Vec::new();
let days = || -> String {
pattern
.days_of_week
.iter()
.map(|day| weekday_to_ical(*day))
.collect::<Vec<_>>()
.join(",")
};
match pattern.pattern_type? {
MsgraphRecurrencePatternType::Daily => parts.push(String::from("FREQ=DAILY")),
MsgraphRecurrencePatternType::Weekly => {
parts.push(String::from("FREQ=WEEKLY"));
if !pattern.days_of_week.is_empty() {
parts.push(format!("BYDAY={}", days()));
}
}
MsgraphRecurrencePatternType::AbsoluteMonthly => {
parts.push(String::from("FREQ=MONTHLY"));
parts.push(format!("BYMONTHDAY={}", pattern.day_of_month?));
}
MsgraphRecurrencePatternType::RelativeMonthly => {
parts.push(String::from("FREQ=MONTHLY"));
parts.push(format!("BYDAY={}", days()));
parts.push(format!("BYSETPOS={}", setpos(pattern.index)));
}
MsgraphRecurrencePatternType::AbsoluteYearly => {
parts.push(String::from("FREQ=YEARLY"));
parts.push(format!("BYMONTH={}", pattern.month?));
parts.push(format!("BYMONTHDAY={}", pattern.day_of_month?));
}
MsgraphRecurrencePatternType::RelativeYearly => {
parts.push(String::from("FREQ=YEARLY"));
parts.push(format!("BYMONTH={}", pattern.month?));
parts.push(format!("BYDAY={}", days()));
parts.push(format!("BYSETPOS={}", setpos(pattern.index)));
}
}
if let Some(interval) = pattern.interval.filter(|interval| *interval > 1) {
parts.push(format!("INTERVAL={interval}"));
}
if let Some(first) = pattern.first_day_of_week
&& pattern.pattern_type == Some(MsgraphRecurrencePatternType::Weekly)
{
parts.push(format!("WKST={}", weekday_to_ical(first)));
}
let range = &recurrence.range;
match range.range_type {
Some(MsgraphRecurrenceRangeType::Numbered) => {
parts.push(format!("COUNT={}", range.number_of_occurrences?));
}
Some(MsgraphRecurrenceRangeType::EndDate) => {
let end = range.end_date.as_deref()?.parse::<Date>().ok()?;
parts.push(format!("UNTIL={}", until(end, range, start, all_day)?));
}
Some(MsgraphRecurrenceRangeType::NoEnd) | None => {}
}
Some(parts.join(";"))
}
fn until(
end: Date,
range: &MsgraphRecurrenceRange,
start: Option<&MsgraphDateTimeTimeZone>,
all_day: bool,
) -> Option<String> {
if all_day {
return Some(end.strftime("%Y%m%d").to_string());
}
let name = range
.recurrence_time_zone
.as_deref()
.or(start.and_then(|start| start.time_zone.as_deref()));
let zone = match zone(name) {
Zone::Iana(name) => TimeZone::get(&name).ok()?,
_ => TimeZone::UTC,
};
let last = end.at(23, 59, 59, 0).to_zoned(zone).ok()?;
Some(last.timestamp().strftime("%Y%m%dT%H%M%SZ").to_string())
}
fn exdates(
master: &MsgraphEvent,
all_day: bool,
zones: &mut BTreeSet<String>,
) -> Vec<IcalProp<'static>> {
let Some(start) = master.start.as_option() else {
return Vec::new();
};
let time = ical_digits(&start.date_time);
let time = time.get(8..).unwrap_or("T000000").to_owned();
master
.cancelled_occurrences
.iter()
.filter_map(|occurrence| occurrence.rsplit('.').next())
.filter_map(|date| date.parse::<Date>().ok())
.map(|date| {
let day = date.strftime("%Y%m%d").to_string();
if all_day {
return IcalProp {
name: IcalPropName::Kind(IcalPropKind::ExDate),
params: vec![IcalParam::Value("DATE".into())],
value: IcalValue::Date(day.into()),
};
}
match zone(start.time_zone.as_deref()) {
Zone::Utc => stamp_prop(IcalPropKind::ExDate, format!("{day}{time}Z")),
Zone::Iana(name) | Zone::Unknown(name) => {
zones.insert(name.clone());
IcalProp {
name: IcalPropName::Kind(IcalPropKind::ExDate),
params: vec![IcalParam::TzId(name.into())],
value: IcalValue::DateTime(format!("{day}{time}").into()),
}
}
}
})
.collect()
}
pub(crate) struct Boundary {
pub(crate) local: DateTime,
pub(crate) all_day: bool,
pub(crate) zone: String,
}
impl Boundary {
fn shifted(&self, duration: Span) -> Option<Self> {
Some(Self {
local: self.local.checked_add(duration).ok()?,
all_day: self.all_day,
zone: self.zone.clone(),
})
}
fn into_graph(self) -> MsgraphDateTimeTimeZone {
MsgraphDateTimeTimeZone {
date_time: self.local.strftime("%Y-%m-%dT%H:%M:%S").to_string(),
time_zone: Some(self.zone),
}
}
}
pub(crate) fn boundary(line: &IcalLine<'_>) -> Option<Boundary> {
let value = line.raw_value_str();
let value = value.trim();
let is_date = line
.param::<VALUE>()
.is_some_and(|kind| kind.eq_ignore_ascii_case("DATE"))
|| (value.len() == 8 && value.bytes().all(|byte| byte.is_ascii_digit()));
if is_date {
let date = Date::strptime("%Y%m%d", value).ok()?;
return Some(Boundary {
local: date.at(0, 0, 0, 0),
all_day: true,
zone: String::from("UTC"),
});
}
let (local, zone) = match value.strip_suffix('Z') {
Some(local) => (local, String::from("UTC")),
None => (value, line.param::<TZID>()?.into_owned()),
};
Some(Boundary {
local: DateTime::strptime("%Y%m%dT%H%M%S", local).ok()?,
all_day: false,
zone,
})
}
pub(crate) fn recurrence(
rule: &str,
start: &Boundary,
) -> Result<MsgraphPatternedRecurrence, MsgraphEventIcalError> {
let unsupported = || MsgraphEventIcalError::UnsupportedRecurrence(rule.to_owned());
let mut freq = None;
let mut interval = None;
let mut count = None;
let mut until_value = None;
let mut by_day = Vec::new();
let mut ordinals = Vec::new();
let mut by_month_day = None;
let mut by_month = None;
let mut by_setpos = None;
let mut wkst = None;
for part in rule.split(';').filter(|part| !part.is_empty()) {
let (key, value) = part.split_once('=').ok_or_else(unsupported)?;
match key.to_ascii_uppercase().as_str() {
"FREQ" => freq = Some(value.to_ascii_uppercase()),
"INTERVAL" => interval = Some(value.parse::<u32>().map_err(|_| unsupported())?),
"COUNT" => count = Some(value.parse::<u32>().map_err(|_| unsupported())?),
"UNTIL" => until_value = Some(value.to_owned()),
"BYDAY" => {
for day in value.split(',') {
let (ordinal, name) = day
.split_at_checked(day.len().saturating_sub(2))
.ok_or_else(unsupported)?;
if !ordinal.is_empty() {
ordinals.push(ordinal.parse::<i32>().map_err(|_| unsupported())?);
}
let day = weekday_from_ical(name).ok_or_else(unsupported)?;
if by_day.contains(&day) {
return Err(unsupported());
}
by_day.push(day);
}
}
"BYMONTHDAY" => by_month_day = Some(value.parse::<u32>().map_err(|_| unsupported())?),
"BYMONTH" => by_month = Some(value.parse::<u32>().map_err(|_| unsupported())?),
"BYSETPOS" => by_setpos = Some(value.parse::<i32>().map_err(|_| unsupported())?),
"WKST" => wkst = Some(weekday_from_ical(value).ok_or_else(unsupported)?),
_ => return Err(unsupported()),
}
}
let position = match (ordinals.as_slice(), by_setpos) {
([], position) => position,
([ordinal], None) if by_day.len() == 1 => Some(*ordinal),
_ => return Err(unsupported()),
};
let index = match position {
None => None,
Some(1) => Some(MsgraphWeekIndex::First),
Some(2) => Some(MsgraphWeekIndex::Second),
Some(3) => Some(MsgraphWeekIndex::Third),
Some(4) => Some(MsgraphWeekIndex::Fourth),
Some(-1) => Some(MsgraphWeekIndex::Last),
Some(_) => return Err(unsupported()),
};
let pattern_type = match (freq.as_deref(), by_day.is_empty(), index) {
(Some("DAILY"), true, None) => MsgraphRecurrencePatternType::Daily,
(Some("WEEKLY"), _, None) => MsgraphRecurrencePatternType::Weekly,
(Some("MONTHLY"), true, None) => MsgraphRecurrencePatternType::AbsoluteMonthly,
(Some("MONTHLY"), false, Some(_)) => MsgraphRecurrencePatternType::RelativeMonthly,
(Some("YEARLY"), true, None) => MsgraphRecurrencePatternType::AbsoluteYearly,
(Some("YEARLY"), false, Some(_)) => MsgraphRecurrencePatternType::RelativeYearly,
_ => return Err(unsupported()),
};
let date = start.local.date();
let mut days_of_week = by_day;
if pattern_type == MsgraphRecurrencePatternType::Weekly && days_of_week.is_empty() {
days_of_week.push(weekday_of(date));
}
let (day_of_month, month) = match (pattern_type, by_month_day, by_month) {
(
MsgraphRecurrencePatternType::Daily
| MsgraphRecurrencePatternType::Weekly
| MsgraphRecurrencePatternType::RelativeMonthly,
None,
None,
) => (None, None),
(MsgraphRecurrencePatternType::AbsoluteMonthly, day, None) => {
(Some(day.unwrap_or(date.day() as u32)), None)
}
(MsgraphRecurrencePatternType::AbsoluteYearly, None, month) => (
Some(date.day() as u32),
Some(month.unwrap_or(date.month() as u32)),
),
(MsgraphRecurrencePatternType::AbsoluteYearly, Some(day), Some(month)) => {
(Some(day), Some(month))
}
(MsgraphRecurrencePatternType::RelativeYearly, None, Some(month)) => (None, Some(month)),
_ => return Err(unsupported()),
};
let zone_name = (!start.all_day).then(|| start.zone.clone());
let range = match (count, until_value) {
(Some(count), _) => MsgraphRecurrenceRange {
range_type: Some(MsgraphRecurrenceRangeType::Numbered),
number_of_occurrences: Some(count),
..Default::default()
},
(None, Some(until)) => {
let day = until.get(..8).ok_or_else(unsupported)?;
let mut end = Date::strptime("%Y%m%d", day).map_err(|_| unsupported())?;
if !start.all_day
&& let Some(local) = until.strip_suffix('Z')
&& let Zone::Iana(name) = zone(Some(&start.zone))
&& let Ok(series) = TimeZone::get(&name)
{
let instant = DateTime::strptime("%Y%m%dT%H%M%S", local)
.and_then(|local| local.to_zoned(TimeZone::UTC))
.map_err(|_| unsupported())?;
end = instant.with_time_zone(series).date();
}
MsgraphRecurrenceRange {
range_type: Some(MsgraphRecurrenceRangeType::EndDate),
end_date: Some(end.strftime("%Y-%m-%d").to_string()),
..Default::default()
}
}
(None, None) => MsgraphRecurrenceRange {
range_type: Some(MsgraphRecurrenceRangeType::NoEnd),
..Default::default()
},
};
Ok(MsgraphPatternedRecurrence {
pattern: MsgraphRecurrencePattern {
pattern_type: Some(pattern_type),
interval: Some(interval.unwrap_or(1)),
month,
day_of_month,
days_of_week,
first_day_of_week: wkst,
index,
},
range: MsgraphRecurrenceRange {
start_date: Some(date.strftime("%Y-%m-%d").to_string()),
recurrence_time_zone: zone_name,
..range
},
})
}
fn find_vevent<'a>(
calendar: &'a IcalCst<'a>,
pick: impl Fn(&IcalCst<'_>) -> bool,
) -> Result<&'a IcalCst<'a>, MsgraphEventIcalError> {
let components = || {
calendar.items.iter().filter_map(|item| match item {
IcalItem::Component(component) => Some(component.as_ref()),
_ => None,
})
};
if let Some(vevent) = components()
.filter(|component| component_name(component) == "VEVENT")
.find(|vevent| pick(vevent))
{
return Ok(vevent);
}
match components()
.map(component_name)
.find(|name| name != "VTIMEZONE" && name != "VEVENT")
{
Some(name) => Err(MsgraphEventIcalError::NotAnEvent(name)),
None => Err(MsgraphEventIcalError::NoEvent),
}
}
pub(crate) fn component_name(component: &IcalCst<'_>) -> String {
component
.begin
.as_ref()
.map(|begin| begin.raw_value_str().trim().to_uppercase())
.unwrap_or_default()
}
fn has_prop(component: &IcalCst<'_>, name: &str) -> bool {
component.items.iter().any(|item| match item {
IcalItem::Prop(line) => line.name.get().eq_ignore_ascii_case(name),
_ => false,
})
}
fn find_prop<'c, 'a>(component: &'c IcalCst<'a>, name: &str) -> Option<&'c IcalLine<'a>> {
component.items.iter().find_map(|item| match item {
IcalItem::Prop(line) if line.name.get().eq_ignore_ascii_case(name) => Some(line),
_ => None,
})
}
fn reminder_minutes(alarm: &IcalCst<'_>) -> Option<u32> {
if component_name(alarm) != "VALARM" {
return None;
}
let action = alarm.prop::<ACTION>()?;
if !matches!(action.0.trim().to_uppercase().as_str(), "DISPLAY" | "AUDIO") {
return None;
}
let trigger = alarm.prop::<TRIGGER>()?;
let lead = trigger.0.trim().strip_prefix('-')?.parse::<Span>().ok()?;
let minutes = lead.total(jiff::Unit::Minute).ok()?;
let whole = minutes as u32;
(minutes >= 0.0 && f64::from(whole) == minutes).then_some(whole)
}
fn person_prop(
kind: IcalPropKind,
address: &MsgraphEmailAddress,
mut params: Vec<IcalParam<'static>>,
) -> IcalProp<'static> {
if let Some(name) = address.name.clone().filter(|name| !name.is_empty()) {
params.insert(0, IcalParam::Cn(name.into()));
}
let email = address.address.clone().unwrap_or_default();
IcalProp {
name: IcalPropName::Kind(kind),
params,
value: IcalValue::CalAddress(format!("mailto:{email}").into()),
}
}
fn attendee_prop(attendee: &MsgraphAttendee) -> Option<IcalProp<'static>> {
let address = attendee.email_address.as_ref()?;
let mut params = Vec::new();
match attendee.attendee_type {
Some(MsgraphAttendeeType::Optional) => {
params.push(IcalParam::Role("OPT-PARTICIPANT".into()))
}
Some(MsgraphAttendeeType::Resource) => {
params.push(IcalParam::Role("NON-PARTICIPANT".into()));
params.push(IcalParam::CuType("RESOURCE".into()));
}
_ => params.push(IcalParam::Role("REQ-PARTICIPANT".into())),
}
let partstat = match attendee.status.as_ref().and_then(|status| status.response) {
Some(MsgraphResponseType::Accepted | MsgraphResponseType::Organizer) => "ACCEPTED",
Some(MsgraphResponseType::Declined) => "DECLINED",
Some(MsgraphResponseType::TentativelyAccepted) => "TENTATIVE",
_ => "NEEDS-ACTION",
};
params.push(IcalParam::PartStat(partstat.into()));
Some(person_prop(IcalPropKind::Attendee, address, params))
}
fn attendee(line: &IcalLine<'_>) -> MsgraphAttendee {
let role = line.param::<ROLE>().unwrap_or_default();
let cutype = line.param::<CUTYPE>().unwrap_or_default();
let attendee_type =
if cutype.eq_ignore_ascii_case("RESOURCE") || cutype.eq_ignore_ascii_case("ROOM") {
MsgraphAttendeeType::Resource
} else if role.eq_ignore_ascii_case("OPT-PARTICIPANT") {
MsgraphAttendeeType::Optional
} else {
MsgraphAttendeeType::Required
};
let response =
line.param::<PARTSTAT>()
.and_then(|status| match status.to_ascii_uppercase().as_str() {
"ACCEPTED" => Some(MsgraphResponseType::Accepted),
"DECLINED" => Some(MsgraphResponseType::Declined),
"TENTATIVE" => Some(MsgraphResponseType::TentativelyAccepted),
"NEEDS-ACTION" => Some(MsgraphResponseType::NotResponded),
_ => None,
});
let value = line.raw_value_str();
let value = value.trim();
let address = value
.strip_prefix("mailto:")
.or_else(|| value.strip_prefix("MAILTO:"))
.unwrap_or(value)
.to_owned();
MsgraphAttendee {
attendee_type: Some(attendee_type),
status: response.map(|response| MsgraphResponseStatus {
response: Some(response),
time: None,
}),
email_address: Some(MsgraphEmailAddress {
name: line.param::<CN>().map(Cow::into_owned),
address: Some(address),
}),
}
}
fn anchor(event: &MsgraphEvent) -> i64 {
event
.start
.as_option()
.and_then(|start| start.date_time.get(..10))
.and_then(|date| date.parse::<Date>().ok())
.and_then(|date| date.to_zoned(TimeZone::UTC).ok())
.map_or(0, |zoned| zoned.timestamp().as_second())
}
fn ical_digits(date_time: &str) -> String {
let base = date_time.split('.').next().unwrap_or(date_time);
base.chars()
.filter(|character| *character != '-' && *character != ':')
.take(15)
.collect()
}
pub(crate) fn utc_stamp(instant: &str) -> Option<String> {
let instant = instant.parse::<Timestamp>().ok()?;
Some(instant.strftime("%Y%m%dT%H%M%SZ").to_string())
}
pub(crate) fn stamp_prop(kind: IcalPropKind, stamp: String) -> IcalProp<'static> {
IcalProp {
name: IcalPropName::Kind(kind),
params: Vec::new(),
value: IcalValue::DateTime(IcalDateTime(stamp.into())),
}
}
pub(crate) fn body_text(body: &MsgraphField<MsgraphItemBody>) -> String {
let Some(body) = body.as_option() else {
return String::new();
};
let content = body.content.as_deref().unwrap_or_default();
match body.content_type {
Some(MsgraphBodyType::Html) => strip_html(content),
_ => content.to_owned(),
}
}
pub(crate) fn strip_html(html: &str) -> String {
let mut text = String::with_capacity(html.len());
let mut rest = html;
let mut skipped: Option<&str> = None;
while let Some(open) = rest.find('<') {
if skipped.is_none() {
push_unescaped(&mut text, &rest[..open]);
}
rest = &rest[open..];
let (tag, after) = match rest.strip_prefix("<!--") {
Some(comment) => ("", comment.split_once("-->").unwrap_or_default().1),
None => rest[1..].split_once('>').unwrap_or_default(),
};
rest = after;
let closing = tag.starts_with('/');
let name = tag.trim_start_matches('/');
let name = name
.split(|character: char| !character.is_ascii_alphanumeric())
.next()
.unwrap_or_default();
match skipped {
Some(element) if closing && name.eq_ignore_ascii_case(element) => skipped = None,
Some(_) => {}
None if closing => {
if name.eq_ignore_ascii_case("p") || name.eq_ignore_ascii_case("div") {
text.push('\n');
rest = after_break(rest);
}
}
None if name.eq_ignore_ascii_case("br") => {
text.push('\n');
rest = after_break(rest);
}
None => {
skipped = ["head", "style", "script"]
.into_iter()
.find(|element| name.eq_ignore_ascii_case(element));
}
}
}
if skipped.is_none() {
push_unescaped(&mut text, rest);
}
text.trim().to_owned()
}
fn after_break(html: &str) -> &str {
let html = html.strip_prefix('\r').unwrap_or(html);
html.strip_prefix('\n').unwrap_or(html)
}
fn push_unescaped(text: &mut String, mut html: &str) {
while let Some(at) = html.find('&') {
text.push_str(&html[..at]);
html = &html[at + 1..];
let end = html.bytes().take(9).position(|byte| byte == b';');
let reference = end.and_then(|end| {
let (name, after) = (&html[..end], &html[end + 1..]);
let character = match name {
"amp" => '&',
"lt" => '<',
"gt" => '>',
"quot" => '"',
"apos" => '\'',
"nbsp" => '\u{a0}',
_ => {
let number = name.strip_prefix('#')?;
let code = match number.strip_prefix(['x', 'X']) {
Some(hex) => u32::from_str_radix(hex, 16).ok()?,
None => number.parse().ok()?,
};
char::from_u32(code)?
}
};
Some((character, after))
});
match reference {
Some(('\u{a0}', after)) => {
text.push(' ');
html = after;
}
Some((character, after)) => {
text.push(character);
html = after;
}
None => text.push('&'),
}
}
text.push_str(html);
}
pub(crate) fn same_text(left: &str, right: &str) -> bool {
left.split_whitespace().eq(right.split_whitespace())
}
pub(crate) fn stash_lines(
props: &MsgraphField<Vec<MsgraphSingleValueExtendedProperty>>,
) -> Vec<String> {
props
.as_option()
.map(Vec::as_slice)
.unwrap_or(&[])
.iter()
.filter(|prop| prop.id.to_ascii_lowercase().contains("pimalaya-ical"))
.flat_map(MsgraphSingleValueExtendedProperty::stash_lines)
.collect()
}
pub(crate) fn push_stash(component: &mut IcalCst<'_>, lines: &[String]) {
let mut block: Vec<&str> = Vec::new();
let mut depth = 0_usize;
for line in lines {
let name = line.split([';', ':']).next().unwrap_or_default();
if name.eq_ignore_ascii_case("BEGIN") {
depth += 1;
}
if depth == 0 {
let _ = component.push_raw(line);
continue;
}
block.push(line);
if name.eq_ignore_ascii_case("END") {
depth -= 1;
if depth == 0 {
let _ = component.push_raw(&block.join("\r\n"));
block.clear();
}
}
}
}
fn is_uid_line(line: &str) -> bool {
let name = line.split([';', ':']).next().unwrap_or_default();
name.eq_ignore_ascii_case("UID")
}
fn is_minted_line(line: &str) -> bool {
let name = line.split([';', ':']).next().unwrap_or_default();
MINTED_PROPS
.iter()
.any(|minted| name.eq_ignore_ascii_case(minted))
}
pub(crate) fn text(line: &IcalLine<'_>) -> String {
IcalText::decode(&line.value).0.into_owned()
}
pub(crate) fn raw_line(line: &IcalLine<'_>) -> String {
let mut raw = String::from(line.name.get());
for param in &line.params {
raw.push(';');
raw.push_str(¶m.to_string());
}
raw.push(':');
raw.push_str(&line.raw_value_str());
raw
}
pub(crate) fn raw_component(component: &IcalCst<'_>) -> Vec<String> {
let mut lines: Vec<String> = component.begin.iter().map(raw_line).collect();
for item in &component.items {
match item {
IcalItem::Prop(line) => lines.push(raw_line(line)),
IcalItem::Component(child) => lines.extend(raw_component(child)),
IcalItem::Opaque(bytes) => lines.push(String::from_utf8_lossy(bytes).into_owned()),
}
}
lines.extend(component.end.iter().map(raw_line));
lines
}
fn setpos(index: Option<MsgraphWeekIndex>) -> i32 {
match index {
Some(MsgraphWeekIndex::Second) => 2,
Some(MsgraphWeekIndex::Third) => 3,
Some(MsgraphWeekIndex::Fourth) => 4,
Some(MsgraphWeekIndex::Last) => -1,
_ => 1,
}
}
fn weekday_to_ical(day: MsgraphDayOfWeek) -> &'static str {
match day {
MsgraphDayOfWeek::Sunday => "SU",
MsgraphDayOfWeek::Monday => "MO",
MsgraphDayOfWeek::Tuesday => "TU",
MsgraphDayOfWeek::Wednesday => "WE",
MsgraphDayOfWeek::Thursday => "TH",
MsgraphDayOfWeek::Friday => "FR",
MsgraphDayOfWeek::Saturday => "SA",
}
}
fn weekday_from_ical(day: &str) -> Option<MsgraphDayOfWeek> {
Some(match day.to_ascii_uppercase().as_str() {
"SU" => MsgraphDayOfWeek::Sunday,
"MO" => MsgraphDayOfWeek::Monday,
"TU" => MsgraphDayOfWeek::Tuesday,
"WE" => MsgraphDayOfWeek::Wednesday,
"TH" => MsgraphDayOfWeek::Thursday,
"FR" => MsgraphDayOfWeek::Friday,
"SA" => MsgraphDayOfWeek::Saturday,
_ => return None,
})
}
fn weekday_of(date: Date) -> MsgraphDayOfWeek {
use jiff::civil::Weekday;
match date.weekday() {
Weekday::Sunday => MsgraphDayOfWeek::Sunday,
Weekday::Monday => MsgraphDayOfWeek::Monday,
Weekday::Tuesday => MsgraphDayOfWeek::Tuesday,
Weekday::Wednesday => MsgraphDayOfWeek::Wednesday,
Weekday::Thursday => MsgraphDayOfWeek::Thursday,
Weekday::Friday => MsgraphDayOfWeek::Friday,
Weekday::Saturday => MsgraphDayOfWeek::Saturday,
}
}
fn busy_status_to_ical(show_as: MsgraphFreeBusyStatus) -> &'static str {
match show_as {
MsgraphFreeBusyStatus::Free => "FREE",
MsgraphFreeBusyStatus::Tentative => "TENTATIVE",
MsgraphFreeBusyStatus::Busy => "BUSY",
MsgraphFreeBusyStatus::Oof => "OOF",
MsgraphFreeBusyStatus::WorkingElsewhere => "WORKINGELSEWHERE",
MsgraphFreeBusyStatus::Unknown => "UNKNOWN",
}
}
fn busy_status_from_ical(value: &str) -> Option<MsgraphFreeBusyStatus> {
Some(match value.trim().to_ascii_uppercase().as_str() {
"FREE" => MsgraphFreeBusyStatus::Free,
"TENTATIVE" => MsgraphFreeBusyStatus::Tentative,
"BUSY" => MsgraphFreeBusyStatus::Busy,
"OOF" => MsgraphFreeBusyStatus::Oof,
"WORKINGELSEWHERE" => MsgraphFreeBusyStatus::WorkingElsewhere,
_ => return None,
})
}
fn sensitivity_to_ical(sensitivity: MsgraphSensitivity) -> &'static str {
match sensitivity {
MsgraphSensitivity::Normal => "PUBLIC",
MsgraphSensitivity::Personal | MsgraphSensitivity::Private => "PRIVATE",
MsgraphSensitivity::Confidential => "CONFIDENTIAL",
}
}
fn sensitivity_from_ical(value: &str) -> Option<MsgraphSensitivity> {
Some(match value.trim().to_ascii_uppercase().as_str() {
"PUBLIC" => MsgraphSensitivity::Normal,
"PRIVATE" => MsgraphSensitivity::Private,
"CONFIDENTIAL" => MsgraphSensitivity::Confidential,
_ => return None,
})
}
pub(crate) fn importance_from_ical(value: &str) -> Option<MsgraphImportance> {
match value.trim().parse::<u8>().ok()? {
1..=4 => Some(MsgraphImportance::High),
5 => Some(MsgraphImportance::Normal),
6..=9 => Some(MsgraphImportance::Low),
_ => None,
}
}
const GLOBAL_OBJECT_ID_CLASS: &str = "040000008200E00074C5B7101A82E008";
const VCAL_UID_MARKER: &[u8] = b"vCal-Uid\x01\x00\x00\x00";
pub fn original_uid(ical_uid: &str) -> String {
unwrap_global_object_id(ical_uid).unwrap_or_else(|| ical_uid.to_owned())
}
fn unwrap_global_object_id(ical_uid: &str) -> Option<String> {
let class = ical_uid.get(..GLOBAL_OBJECT_ID_CLASS.len())?;
if !class.eq_ignore_ascii_case(GLOBAL_OBJECT_ID_CLASS) {
return None;
}
let rest = &ical_uid.as_bytes()[GLOBAL_OBJECT_ID_CLASS.len()..];
if !rest.len().is_multiple_of(2) {
return None;
}
let bytes = rest
.chunks(2)
.map(|pair| u8::from_str_radix(core::str::from_utf8(pair).ok()?, 16).ok())
.collect::<Option<Vec<u8>>>()?;
let size = u32::from_le_bytes(bytes.get(20..24)?.try_into().ok()?) as usize;
let data = bytes.get(24..24usize.checked_add(size)?)?;
let uid = data.strip_prefix(VCAL_UID_MARKER)?;
let end = uid.iter().position(|byte| *byte == 0).unwrap_or(uid.len());
String::from_utf8(uid[..end].to_vec())
.ok()
.filter(|uid| !uid.is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::v1::rest::users::{contacts::tests::*, messages::MsgraphRecipient};
fn master() -> MsgraphEvent {
MsgraphEvent {
id: "SM1".into(),
ical_uid: Some("040000008200E0".into()),
last_modified_date_time: Some("2026-08-01T10:00:00Z".into()),
subject: MsgraphField::Set("Stand-up".into()),
start: MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2026-08-14T09:00:00.0000000".into(),
time_zone: Some("Romance Standard Time".into()),
}),
end: MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2026-08-14T09:15:00.0000000".into(),
time_zone: Some("Romance Standard Time".into()),
}),
is_all_day: MsgraphField::Set(false),
recurrence: MsgraphField::Set(MsgraphPatternedRecurrence {
pattern: MsgraphRecurrencePattern {
pattern_type: Some(MsgraphRecurrencePatternType::Weekly),
interval: Some(1),
days_of_week: vec![MsgraphDayOfWeek::Monday, MsgraphDayOfWeek::Friday],
first_day_of_week: Some(MsgraphDayOfWeek::Monday),
..Default::default()
},
range: MsgraphRecurrenceRange {
range_type: Some(MsgraphRecurrenceRangeType::Numbered),
start_date: Some("2026-08-14".into()),
number_of_occurrences: Some(10),
..Default::default()
},
}),
organizer: MsgraphField::Set(MsgraphRecipient {
email_address: MsgraphEmailAddress {
name: Some("Alice".into()),
address: Some("alice@x.org".into()),
},
}),
attendees: MsgraphField::Set(vec![MsgraphAttendee {
attendee_type: Some(MsgraphAttendeeType::Optional),
status: Some(MsgraphResponseStatus {
response: Some(MsgraphResponseType::Accepted),
time: None,
}),
email_address: Some(MsgraphEmailAddress {
name: Some("Bob".into()),
address: Some("bob@x.org".into()),
}),
}]),
show_as: MsgraphField::Set(MsgraphFreeBusyStatus::Tentative),
is_reminder_on: MsgraphField::Set(true),
reminder_minutes_before_start: MsgraphField::Set(15),
cancelled_occurrences: vec!["OID.SM1.2026-08-21".into()],
..Default::default()
}
}
#[test]
fn a_stashed_alarm_reads_back_whole() {
let doc = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:a\r\n\
DTSTART:20261120T090000Z\r\nDTEND:20261120T100000Z\r\nSUMMARY:Review\r\n\
BEGIN:VALARM\r\nACTION:DISPLAY\r\nTRIGGER:-PT15M\r\nEND:VALARM\r\n\
BEGIN:VALARM\r\nACTION:EMAIL\r\nTRIGGER:-PT1H\r\nSUMMARY:Soon\r\nEND:VALARM\r\n\
END:VEVENT\r\nEND:VCALENDAR\r\n";
let ical = MsgraphEvent::from_ical(doc.as_bytes()).unwrap().to_ical();
assert!(ical.contains(
"SUMMARY:Review\r\nBEGIN:VALARM\r\nACTION:EMAIL\r\nTRIGGER:-PT1H\r\nSUMMARY:Soon\r\n\
END:VALARM\r\n"
));
}
#[test]
fn a_windows_zoned_series_projects_with_its_rule_and_zone() {
let ical = master().to_ical();
assert!(ical.contains("UID:040000008200E0\r\n"), "{ical}");
assert!(
ical.contains("BEGIN:VTIMEZONE\r\nTZID:Europe/Paris\r\n"),
"{ical}"
);
assert!(
ical.contains("DTSTART;TZID=Europe/Paris:20260814T090000\r\n"),
"{ical}"
);
assert!(
ical.contains("DTEND;TZID=Europe/Paris:20260814T091500\r\n"),
"{ical}"
);
assert!(
ical.contains("RRULE:FREQ=WEEKLY;BYDAY=MO,FR;WKST=MO;COUNT=10\r\n"),
"{ical}"
);
assert!(
ical.contains("EXDATE;TZID=Europe/Paris:20260821T090000\r\n"),
"{ical}"
);
assert!(
ical.contains("ORGANIZER;CN=Alice:mailto:alice@x.org\r\n"),
"{ical}"
);
assert!(
ical.contains(
"ATTENDEE;CN=Bob;ROLE=OPT-PARTICIPANT;PARTSTAT=ACCEPTED:mailto:bob@x.org\r\n"
),
"{ical}"
);
assert!(
ical.contains("X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE\r\n"),
"{ical}"
);
assert!(
ical.contains("BEGIN:VALARM\r\nACTION:DISPLAY\r\nTRIGGER:-PT15M\r\n"),
"{ical}"
);
}
#[test]
fn an_exception_carries_the_series_uid_and_its_original_start() {
let exception = MsgraphEvent {
id: "EX1".into(),
ical_uid: Some("other".into()),
event_type: Some(crate::v1::rest::users::events::MsgraphEventType::Exception),
series_master_id: Some("SM1".into()),
original_start: Some("2026-08-17T07:00:00Z".into()),
subject: MsgraphField::Set("Stand-up, moved".into()),
start: MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2026-08-17T10:00:00.0000000".into(),
time_zone: Some("Romance Standard Time".into()),
}),
..Default::default()
};
let ical = master().to_ical_series(&[&exception]);
assert_eq!(ical.matches("BEGIN:VEVENT").count(), 2, "{ical}");
assert_eq!(ical.matches("UID:040000008200E0\r\n").count(), 2, "{ical}");
assert!(
ical.contains("RECURRENCE-ID;TZID=Europe/Paris:20260817T090000\r\n"),
"{ical}"
);
assert_eq!(ical.matches("RRULE:FREQ=WEEKLY").count(), 1, "{ical}");
}
const WRAPPED: &str = "040000008200E00074C5B7101A82E00800000000000000000000000000000000000000002D0000007643616C2D556964010000006D6F612D393839663030616435646236393839663030616435646236406D6F6100";
#[test]
fn a_global_object_id_gives_back_the_uid_it_wraps() {
let uid = "moa-989f00ad5db6989f00ad5db6@moa";
assert_eq!(original_uid(WRAPPED), uid);
assert_eq!(original_uid(&WRAPPED.to_lowercase()), uid);
let minted = "040000008200E00074C5B7101A82E0080000000088DCD2137A53DD01000000000000000010000000F70EDDABBC4DD5439803DF5489114F0B";
assert_eq!(original_uid(minted), minted);
assert_eq!(original_uid("040000008200E0"), "040000008200E0");
assert_eq!(original_uid("urn:uuid:4fbe8971"), "urn:uuid:4fbe8971");
assert_eq!(
original_uid(&WRAPPED[..WRAPPED.len() - 4]),
&WRAPPED[..WRAPPED.len() - 4]
);
}
#[test]
fn an_invitation_taken_in_by_mail_keeps_the_organizer_uid() {
let mut event = master();
event.ical_uid = Some(WRAPPED.into());
let exception = MsgraphEvent {
id: "EX1".into(),
ical_uid: Some(WRAPPED.into()),
event_type: Some(crate::v1::rest::users::events::MsgraphEventType::Exception),
series_master_id: Some("SM1".into()),
original_start: Some("2026-08-17T07:00:00Z".into()),
..Default::default()
};
let ical = event.to_ical_series(&[&exception]);
assert_eq!(
ical.matches("UID:moa-989f00ad5db6989f00ad5db6@moa\r\n")
.count(),
2,
"{ical}"
);
}
#[test]
fn an_occurrence_is_told_by_the_recurrence_id_the_projection_writes() {
assert_eq!(
master().recurrence_id_of("2026-08-17T07:00:00Z").as_deref(),
Some("20260817T090000")
);
assert_eq!(
master()
.recurrence_id_of("2026-08-17T07:00:00.0000000Z")
.as_deref(),
Some("20260817T090000")
);
let mut all_day = master();
all_day.is_all_day = MsgraphField::Set(true);
assert_eq!(
all_day.recurrence_id_of("2026-08-17T00:00:00Z").as_deref(),
Some("20260817")
);
assert_eq!(master().recurrence_id_of("not a time"), None);
}
#[test]
fn an_all_day_event_projects_as_dates_and_back() {
let event = MsgraphEvent {
id: "AD1".into(),
subject: MsgraphField::Set("Holiday".into()),
is_all_day: MsgraphField::Set(true),
start: MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2026-08-14T00:00:00.0000000".into(),
time_zone: Some("UTC".into()),
}),
end: MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2026-08-15T00:00:00.0000000".into(),
time_zone: Some("UTC".into()),
}),
..Default::default()
};
let ical = event.to_ical();
assert!(ical.contains("DTSTART;VALUE=DATE:20260814\r\n"), "{ical}");
assert!(ical.contains("DTEND;VALUE=DATE:20260815\r\n"), "{ical}");
let back = MsgraphEvent::from_ical(ical.as_bytes()).unwrap();
assert_eq!(back.is_all_day.as_option(), Some(&true));
assert_eq!(
back.start.as_option().map(|start| start.date_time.as_str()),
Some("2026-08-14T00:00:00")
);
}
#[test]
fn a_series_reads_back_with_its_recurrence() {
let event = MsgraphEvent::from_ical(master().to_ical().as_bytes()).unwrap();
assert_eq!(
event.subject.as_option().map(String::as_str),
Some("Stand-up")
);
let start = event.start.as_option().unwrap();
assert_eq!(start.date_time, "2026-08-14T09:00:00");
assert_eq!(start.time_zone.as_deref(), Some("Europe/Paris"));
let recurrence = event.recurrence.as_option().unwrap();
assert_eq!(
recurrence.pattern.pattern_type,
Some(MsgraphRecurrencePatternType::Weekly)
);
assert_eq!(
recurrence.pattern.days_of_week,
[MsgraphDayOfWeek::Monday, MsgraphDayOfWeek::Friday]
);
assert_eq!(recurrence.range.number_of_occurrences, Some(10));
assert_eq!(recurrence.range.start_date.as_deref(), Some("2026-08-14"));
assert_eq!(
event.show_as.as_option(),
Some(&MsgraphFreeBusyStatus::Tentative)
);
assert_eq!(event.reminder_minutes_before_start.as_option(), Some(&15));
assert_eq!(event.attendees.as_option().map(Vec::len), Some(1));
assert!(
!stash_lines(&event.single_value_extended_properties)
.iter()
.any(|line| line.starts_with("EXDATE")),
"{:?}",
stash_lines(&event.single_value_extended_properties)
);
}
#[test]
fn a_relative_monthly_rule_round_trips_through_its_index() {
let ical = concat!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:u1\r\n",
"DTSTART:20260827T120000Z\r\nDTEND:20260827T130000Z\r\n",
"RRULE:FREQ=MONTHLY;BYDAY=TH;BYSETPOS=-1\r\n",
"END:VEVENT\r\nEND:VCALENDAR\r\n",
);
let event = MsgraphEvent::from_ical(ical.as_bytes()).unwrap();
let recurrence = event.recurrence.as_option().unwrap();
assert_eq!(
recurrence.pattern.pattern_type,
Some(MsgraphRecurrencePatternType::RelativeMonthly)
);
assert_eq!(recurrence.pattern.index, Some(MsgraphWeekIndex::Last));
let back = event.to_ical();
assert!(
back.contains("RRULE:FREQ=MONTHLY;BYDAY=TH;BYSETPOS=-1\r\n"),
"{back}"
);
}
#[test]
fn an_end_date_ends_at_midnight_in_the_series_zone() {
let mut event = master();
if let MsgraphField::Set(recurrence) = &mut event.recurrence {
recurrence.range = MsgraphRecurrenceRange {
range_type: Some(MsgraphRecurrenceRangeType::EndDate),
start_date: Some("2026-08-14".into()),
end_date: Some("2026-09-30".into()),
recurrence_time_zone: Some("Romance Standard Time".into()),
..Default::default()
};
}
assert!(event.to_ical().contains(";UNTIL=20260930T215959Z\r\n"));
}
#[test]
fn a_series_is_bounded_by_its_range_type_not_the_sentinel_end() {
let bounds = |range_type, interval| {
let recurrence = MsgraphPatternedRecurrence {
pattern: MsgraphRecurrencePattern {
pattern_type: Some(MsgraphRecurrencePatternType::Weekly),
interval,
..Default::default()
},
range: MsgraphRecurrenceRange {
range_type: Some(range_type),
start_date: Some("2026-09-28".into()),
end_date: Some("0001-01-01".into()),
number_of_occurrences: Some(11),
..Default::default()
},
};
recurrence
.bounds()
.map(|(start, end)| (start.to_string(), end.map(|end| end.to_string())))
};
let start = String::from("2026-09-28");
assert_eq!(
bounds(MsgraphRecurrenceRangeType::Numbered, None),
Some((start.clone(), Some("2026-12-14".into()))),
);
assert_eq!(
bounds(MsgraphRecurrenceRangeType::Numbered, Some(2)),
Some((start.clone(), Some("2027-03-01".into()))),
);
assert_eq!(
bounds(MsgraphRecurrenceRangeType::NoEnd, None),
Some((start.clone(), None)),
);
assert_eq!(
bounds(MsgraphRecurrenceRangeType::EndDate, None),
Some((start, Some("0001-01-01".into()))),
);
}
fn weekly(
range_type: MsgraphRecurrenceRangeType,
start: &str,
end: &str,
) -> MsgraphPatternedRecurrence {
MsgraphPatternedRecurrence {
pattern: MsgraphRecurrencePattern {
pattern_type: Some(MsgraphRecurrencePatternType::Weekly),
..Default::default()
},
range: MsgraphRecurrenceRange {
range_type: Some(range_type),
start_date: Some(start.into()),
end_date: Some(end.into()),
number_of_occurrences: Some(11),
..Default::default()
},
}
}
fn windows(
recurrence: &MsgraphPatternedRecurrence,
from: &str,
to: &str,
) -> Vec<(String, String)> {
let within = (from.parse().unwrap(), to.parse().unwrap());
recurrence.instance_windows(within).unwrap()
}
fn window(from: &str, to: &str) -> (String, String) {
(format!("{from}T00:00:00Z"), format!("{to}T00:00:00Z"))
}
#[test]
fn a_bounded_series_inside_the_range_is_one_window() {
let series = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2026-08-14",
"2026-09-30",
);
assert_eq!(
windows(&series, "2021-11-15", "2031-11-15"),
[window("2026-08-14", "2026-10-01")],
);
}
#[test]
fn a_series_over_five_years_is_cut_in_windows_graph_accepts() {
let series = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2020-01-01",
"2031-12-31",
);
let windows = windows(&series, "2000-01-01", "2040-01-01");
assert_eq!(
windows,
[
window("2020-01-01", "2024-12-31"),
window("2024-12-31", "2029-12-30"),
window("2029-12-30", "2032-01-01"),
],
);
for (from, to) in &windows {
let from: Timestamp = from.parse().unwrap();
let to: Timestamp = to.parse().unwrap();
let limit = from
.to_zoned(TimeZone::UTC)
.checked_add(Span::new().years(MSGRAPH_INSTANCES_MAX_YEARS))
.unwrap()
.timestamp();
assert!(to < limit, "{from} to {to} spans five years or more");
}
}
#[test]
fn an_open_ended_series_ends_where_the_range_does() {
let series = weekly(
MsgraphRecurrenceRangeType::NoEnd,
"2019-01-07",
"0001-01-01",
);
assert_eq!(
windows(&series, "2021-11-15", "2031-11-15"),
[
window("2021-11-15", "2026-11-14"),
window("2026-11-14", "2031-11-13"),
window("2031-11-13", "2031-11-15"),
],
);
}
#[test]
fn a_series_the_range_does_not_meet_has_no_window() {
let past = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2010-01-04",
"2012-12-31",
);
assert_eq!(windows(&past, "2021-11-15", "2031-11-15"), []);
let ahead = weekly(
MsgraphRecurrenceRangeType::NoEnd,
"2031-11-15",
"0001-01-01",
);
assert_eq!(windows(&ahead, "2021-11-15", "2031-11-15"), []);
}
#[test]
fn the_end_date_of_a_series_is_read_whole() {
let series = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2020-01-06",
"2021-11-15",
);
assert_eq!(
windows(&series, "2021-11-15", "2031-11-15"),
[window("2021-11-15", "2021-11-16")],
);
let series = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2026-08-14",
"2026-09-30",
);
assert_eq!(
windows(&series, "2026-01-01", "2026-09-30"),
[window("2026-08-14", "2026-09-30")],
);
}
#[test]
fn a_numbered_series_is_bounded_by_its_occurrences() {
let series = weekly(
MsgraphRecurrenceRangeType::Numbered,
"2026-09-28",
"0001-01-01",
);
assert_eq!(
windows(&series, "2021-11-15", "2031-11-15"),
[window("2026-09-28", "2026-12-15")],
);
}
#[test]
fn a_series_whose_bounds_do_not_read_has_no_windows() {
let mut series = weekly(
MsgraphRecurrenceRangeType::EndDate,
"2026-08-14",
"2026-09-30",
);
series.range.start_date = None;
let within = ("2021-11-15".parse().unwrap(), "2031-11-15".parse().unwrap());
assert_eq!(series.instance_windows(within), None);
}
#[test]
fn what_graph_cannot_hold_is_refused_by_name() {
let with = |props: &str| {
format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:u1\r\n{props}END:VEVENT\r\nEND:VCALENDAR\r\n"
)
};
let hourly =
with("DTSTART:20260814T090000Z\r\nDTEND:20260814T100000Z\r\nRRULE:FREQ=HOURLY\r\n");
let floating = with("DTSTART:20260814T090000\r\nDTEND:20260814T100000\r\n");
let todo = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VTODO\r\nUID:t\r\nEND:VTODO\r\nEND:VCALENDAR\r\n";
assert!(matches!(
MsgraphEvent::from_ical(hourly.as_bytes()),
Err(MsgraphEventIcalError::UnsupportedRecurrence(_))
));
assert!(matches!(
MsgraphEvent::from_ical(floating.as_bytes()),
Err(MsgraphEventIcalError::NoZonedStart)
));
assert!(matches!(
MsgraphEvent::from_ical(todo.as_bytes()),
Err(MsgraphEventIcalError::NotAnEvent(name)) if name == "VTODO"
));
}
#[test]
fn a_duration_gives_the_end() {
let ical = concat!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:u1\r\n",
"DTSTART;TZID=Europe/Paris:20260814T090000\r\nDURATION:PT1H30M\r\n",
"END:VEVENT\r\nEND:VCALENDAR\r\n",
);
let event = MsgraphEvent::from_ical(ical.as_bytes()).unwrap();
let end = event.end.as_option().unwrap();
assert_eq!(end.date_time, "2026-08-14T10:30:00");
assert_eq!(end.time_zone.as_deref(), Some("Europe/Paris"));
}
#[test]
fn the_uid_and_the_remainder_ride_the_stash() {
let ical = concat!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:urn:uuid:4fbe8971\r\n",
"DTSTART:20260814T090000Z\r\nDTEND:20260814T100000Z\r\nSUMMARY:Lunch\r\n",
"X-CUSTOM;X-P=1:kept\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
);
let mut event = MsgraphEvent::create_from_ical(ical.as_bytes()).unwrap();
assert_eq!(event.stashed_uid().as_deref(), Some("urn:uuid:4fbe8971"));
event.id = "EV9".into();
event.ical_uid = Some("040000008200E0".into());
let back = event.to_ical();
assert!(back.contains("UID:urn:uuid:4fbe8971\r\n"), "{back}");
assert!(!back.contains("UID:040000008200E0"), "{back}");
assert!(back.contains("X-CUSTOM;X-P=1:kept\r\n"), "{back}");
}
fn stashing(extra: &[u8], eol: &str) -> Vec<u8> {
let head = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
"BEGIN:VEVENT",
"DTSTART:20261120T090000Z",
"DTEND:20261120T100000Z",
"",
]
.join(eol);
let tail = ["", "END:VEVENT", "END:VCALENDAR", ""].join(eol);
[head.as_bytes(), extra, tail.as_bytes()].concat()
}
const UNMANAGED: [&str; 3] = [
"X-CUSTOM",
"COMMENT;LANGUAGE=fr",
"X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-TITLE=\"Caf\u{e9}, Z\u{fc}rich\"",
];
#[test]
fn an_unmanaged_line_round_trips_whatever_its_folds() {
for head in UNMANAGED {
for len in 0..=160 {
let line = format!("{head}:{}", sample(len));
for width in [1, 2, 3, 7, 74, 75, 76, usize::MAX] {
for eol in ["\r\n", "\n"] {
let doc = stashing(folded(&line, width, eol).as_bytes(), eol);
let event = MsgraphEvent::from_ical(&doc).unwrap();
let stash = stash_lines(&event.single_value_extended_properties);
assert_eq!(stash, [line.as_str()], "width {width}");
let back = unfolded(&event.to_ical());
assert!(back.contains(&line), "width {width}: {back:?}");
}
}
}
}
}
#[test]
fn a_fold_inside_a_character_is_stashed_whole() {
let line = format!("X-CUSTOM:{}", sample(120));
for width in 1..=75 {
let chunks: Vec<&[u8]> = line.as_bytes().chunks(width).collect();
let doc = stashing(&chunks.join(b"\r\n ".as_slice()), "\r\n");
let event = MsgraphEvent::from_ical(&doc).unwrap();
let back = unfolded(&event.to_ical());
assert!(back.contains(&line), "width {width}: {back:?}");
}
}
#[test]
fn a_folded_uid_reads_back_whole() {
let uid = "040000008200E00074C5B7101A82".repeat(4);
let line = format!("UID:{uid}");
let doc = stashing(folded(&line, 75, "\r\n").as_bytes(), "\r\n");
let event = MsgraphEvent::from_ical(&doc).unwrap();
assert_eq!(event.stashed_uid().as_deref(), Some(uid.as_str()));
assert!(unfolded(&event.to_ical()).contains(&line));
}
#[test]
fn a_stash_stored_folded_reads_back_whole() {
let uid = format!("UID:{}", "040000008200E00074C5B7101A82".repeat(4));
for head in UNMANAGED {
for len in 0..=160 {
let line = format!("{head}:{}", sample(len));
for width in [1, 2, 3, 7, 74, 75, 76] {
for eol in ["\r\n", "\n"] {
let value = [
folded(&uid, 75, eol),
folded(&line, width, eol),
String::from("X-LAST:1"),
]
.join("\n");
let event = MsgraphEvent {
single_value_extended_properties: MsgraphField::Set(vec![
MsgraphSingleValueExtendedProperty {
id: MSGRAPH_EVENT_STASH_ID.into(),
value,
},
]),
..master()
};
assert_eq!(event.stashed_uid().as_deref(), Some(&uid[4..]));
let back = unfolded(&event.to_ical());
assert!(back.contains(&uid), "width {width}: {back:?}");
assert!(back.contains(&line), "width {width}: {back:?}");
assert!(back.contains(&String::from("X-LAST:1")), "{back:?}");
}
}
}
}
}
#[test]
fn a_stashed_alarm_keeps_its_folded_lines() {
let description = format!("DESCRIPTION:{}", sample(120));
let alarm = [
"BEGIN:VALARM\r\nACTION:DISPLAY\r\nTRIGGER:-PT15M\r\nEND:VALARM",
"BEGIN:VALARM\r\nACTION:EMAIL\r\nTRIGGER:-PT1H",
&folded(&description, 75, "\r\n"),
"END:VALARM",
]
.join("\r\n");
let event = MsgraphEvent::from_ical(&stashing(alarm.as_bytes(), "\r\n")).unwrap();
let stash = stash_lines(&event.single_value_extended_properties);
assert!(stash.contains(&description), "{stash:?}");
let back = unfolded(&event.to_ical()).join("\r\n");
let whole =
format!("BEGIN:VALARM\r\nACTION:EMAIL\r\nTRIGGER:-PT1H\r\n{description}\r\nEND:VALARM");
assert!(back.contains(&whole), "{back}");
}
#[test]
fn a_weekday_that_is_not_ascii_is_refused() {
for day in ["\u{e9}", "\u{e9}a", "\u{20ac}", "1\u{20ac}", "M\u{e9}"] {
let rule = format!("FREQ=WEEKLY;BYDAY={day}");
let doc = stashing(format!("RRULE:{rule}").as_bytes(), "\r\n");
assert!(matches!(
MsgraphEvent::from_ical(&doc),
Err(MsgraphEventIcalError::UnsupportedRecurrence(refused)) if refused == rule
));
}
}
fn starting(local: &str, zone: &str) -> Boundary {
Boundary {
local: local.parse().unwrap(),
all_day: false,
zone: zone.into(),
}
}
#[test]
fn an_until_ends_on_its_date_in_the_series_zone() {
let end = |start: &Boundary, until: &str| {
let rule = format!("FREQ=DAILY;UNTIL={until}");
recurrence(&rule, start).unwrap().range.end_date
};
for zone in ["America/New_York", "Eastern Standard Time"] {
let evening = starting("2026-12-01T20:00:00", zone);
assert_eq!(
end(&evening, "20270101T045959Z").as_deref(),
Some("2026-12-31"),
"{zone}"
);
}
let morning = starting("2026-01-01T01:00:00", "Asia/Tokyo");
assert_eq!(
end(&morning, "20260109T160000Z").as_deref(),
Some("2026-01-10")
);
let evening = starting("2026-12-01T20:00:00", "America/New_York");
assert_eq!(end(&evening, "20261231").as_deref(), Some("2026-12-31"));
assert_eq!(
end(&evening, "20261231T200000").as_deref(),
Some("2026-12-31")
);
let written = recurrence("FREQ=DAILY;UNTIL=20270101T045959Z", &evening).unwrap();
assert_eq!(
rrule(&written, None, false).as_deref(),
Some("FREQ=DAILY;UNTIL=20270101T045959Z")
);
}
#[test]
fn a_rule_graph_would_only_approximate_is_refused() {
let monday = starting("2026-03-02T09:00:00", "Europe/Paris");
for rule in [
"FREQ=DAILY;BYMONTH=1",
"FREQ=DAILY;BYMONTHDAY=1",
"FREQ=WEEKLY;BYMONTH=1",
"FREQ=WEEKLY;BYDAY=MO,MO",
"FREQ=MONTHLY;BYMONTH=3;BYMONTHDAY=1",
"FREQ=MONTHLY;BYDAY=1MO,3MO",
"FREQ=MONTHLY;BYDAY=MO,2TU",
"FREQ=MONTHLY;BYDAY=2TU,2WE",
"FREQ=MONTHLY;BYDAY=1MO;BYSETPOS=1",
"FREQ=MONTHLY;BYSETPOS=1;BYDAY=1MO",
"FREQ=MONTHLY;BYDAY=MO;BYSETPOS=1;BYMONTHDAY=2",
"FREQ=YEARLY;BYMONTHDAY=15",
"FREQ=YEARLY;BYDAY=1MO",
"FREQ=YEARLY;BYDAY=MO;BYSETPOS=1",
] {
assert!(
matches!(
recurrence(rule, &monday),
Err(MsgraphEventIcalError::UnsupportedRecurrence(refused)) if refused == rule
),
"{rule}"
);
}
}
#[test]
fn a_rule_graph_holds_exactly_is_taken() {
let monday = starting("2026-03-02T09:00:00", "Europe/Paris");
let pattern = |rule: &str| recurrence(rule, &monday).unwrap().pattern;
let relative = pattern("FREQ=MONTHLY;BYDAY=2TU");
assert_eq!(
relative.pattern_type,
Some(MsgraphRecurrencePatternType::RelativeMonthly)
);
assert_eq!(relative.index, Some(MsgraphWeekIndex::Second));
assert_eq!(relative.days_of_week, [MsgraphDayOfWeek::Tuesday]);
let last = pattern("FREQ=MONTHLY;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR");
assert_eq!(last.index, Some(MsgraphWeekIndex::Last));
assert_eq!(last.days_of_week.len(), 5);
let monthly = pattern("FREQ=MONTHLY");
assert_eq!((monthly.day_of_month, monthly.month), (Some(2), None));
for rule in ["FREQ=YEARLY", "FREQ=YEARLY;BYMONTH=3"] {
let yearly = pattern(rule);
assert_eq!(
yearly.pattern_type,
Some(MsgraphRecurrencePatternType::AbsoluteYearly)
);
assert_eq!((yearly.day_of_month, yearly.month), (Some(2), Some(3)));
}
let yearly = pattern("FREQ=YEARLY;BYMONTH=5;BYMONTHDAY=15");
assert_eq!((yearly.day_of_month, yearly.month), (Some(15), Some(5)));
let relative = pattern("FREQ=YEARLY;BYMONTH=5;BYDAY=2SU");
assert_eq!(
relative.pattern_type,
Some(MsgraphRecurrencePatternType::RelativeYearly)
);
assert_eq!((relative.day_of_month, relative.month), (None, Some(5)));
for rule in [
"FREQ=DAILY;INTERVAL=2",
"FREQ=WEEKLY;BYDAY=MO,FR;WKST=SU;COUNT=10",
"FREQ=MONTHLY;BYMONTHDAY=2",
"FREQ=MONTHLY;BYDAY=MO,TU;BYSETPOS=2",
"FREQ=YEARLY;BYMONTH=3;BYMONTHDAY=2",
"FREQ=YEARLY;BYMONTH=3;BYDAY=MO;BYSETPOS=-1",
] {
let read = recurrence(rule, &monday).unwrap();
assert_eq!(rrule(&read, None, false).as_deref(), Some(rule));
}
}
#[test]
fn an_event_without_an_end_takes_the_one_its_start_implies() {
let with = |props: &str| {
let doc = format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\n{props}END:VEVENT\r\nEND:VCALENDAR\r\n"
);
MsgraphEvent::from_ical(doc.as_bytes())
};
let day = with("DTSTART;VALUE=DATE:20261225\r\n").unwrap();
assert_eq!(day.is_all_day, MsgraphField::Set(true));
assert_eq!(
day.end.as_option().map(|end| end.date_time.as_str()),
Some("2026-12-26T00:00:00")
);
let instant = with("DTSTART;TZID=Europe/Paris:20261225T090000\r\n").unwrap();
assert_eq!(instant.end, instant.start);
assert_eq!(
instant.end.as_option().map(|end| end.date_time.as_str()),
Some("2026-12-25T09:00:00")
);
assert!(matches!(
with("DTSTART:20261225T090000Z\r\nDURATION:soon\r\n"),
Err(MsgraphEventIcalError::NoEnd)
));
assert!(matches!(
with("DTSTART:20261225T090000Z\r\nDTEND:20261225T100000\r\n"),
Err(MsgraphEventIcalError::NoEnd)
));
}
#[test]
fn the_join_link_of_an_online_meeting_is_minted_read_only() {
let mut event = master();
event.online_meeting = Some(crate::v1::rest::users::events::MsgraphOnlineMeetingInfo {
join_url: Some("https://teams.x.org/l/1".into()),
});
event.single_value_extended_properties =
MsgraphField::Set(vec![MsgraphSingleValueExtendedProperty {
id: MSGRAPH_EVENT_STASH_ID.into(),
value: "X-MICROSOFT-SKYPETEAMSMEETINGURL:https://teams.x.org/l/0".into(),
}]);
let ical = event.to_ical();
assert!(
ical.contains("CONFERENCE;VALUE=URI;FEATURE=AUDIO,VIDEO:https://teams.x.org/l/1\r\n"),
"{ical}"
);
assert!(!ical.contains("SKYPETEAMS"), "{ical}");
assert!(!ical.contains("teams.x.org/l/0"), "{ical}");
let back = MsgraphEvent::from_ical(ical.as_bytes()).unwrap();
assert!(back.online_meeting.is_none());
assert!(back.is_online_meeting.is_unset());
assert!(
!stash_lines(&back.single_value_extended_properties)
.iter()
.any(|line| line.contains("CONFERENCE") || line.contains("SKYPETEAMS")),
"{back:?}"
);
}
#[test]
fn an_online_meeting_asked_for_sets_is_online_meeting_once() {
let base = master().to_ical();
let asked = base.replace(
"SUMMARY:Stand-up\r\n",
"SUMMARY:Stand-up\r\nX-PIMDIR-ONLINE-MEETING:TRUE\r\n",
);
assert_ne!(asked, base);
let created = MsgraphEvent::from_ical(asked.as_bytes()).unwrap();
assert_eq!(created.is_online_meeting, MsgraphField::Set(true));
assert!(
!stash_lines(&created.single_value_extended_properties)
.iter()
.any(|line| line.contains("ONLINE-MEETING")),
"{created:?}"
);
let body = serde_json::to_value(&created).unwrap();
assert_eq!(body["isOnlineMeeting"], true);
let patch = MsgraphEvent::update_from_ical(asked.as_bytes(), base.as_bytes()).unwrap();
assert_eq!(patch.is_online_meeting, MsgraphField::Set(true));
let edited = asked.replace("SUMMARY:Stand-up", "SUMMARY:Daily");
let patch = MsgraphEvent::update_from_ical(edited.as_bytes(), asked.as_bytes()).unwrap();
assert!(patch.is_online_meeting.is_unset());
let declined = base.replace(
"SUMMARY:Stand-up\r\n",
"SUMMARY:Stand-up\r\nX-PIMDIR-ONLINE-MEETING:FALSE\r\n",
);
let event = MsgraphEvent::from_ical(declined.as_bytes()).unwrap();
assert!(event.is_online_meeting.is_unset());
let body = serde_json::to_value(&event).unwrap();
assert!(body.get("isOnlineMeeting").is_none());
}
#[test]
fn an_update_sends_only_what_changed() {
let base = master().to_ical();
let edited = base.replace("SUMMARY:Stand-up", "SUMMARY:Daily");
let patch = MsgraphEvent::update_from_ical(edited.as_bytes(), base.as_bytes()).unwrap();
assert_eq!(patch.subject.as_option().map(String::as_str), Some("Daily"));
assert!(patch.start.is_unset());
assert!(patch.recurrence.is_unset());
assert!(patch.attendees.is_unset());
assert!(patch.single_value_extended_properties.is_unset());
}
fn occurrence(hour: &str, summary: &str, extra: &str) -> String {
format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Test//EN\r\nBEGIN:VEVENT\r\n\
UID:040000008200E0\r\nRECURRENCE-ID;TZID=Europe/Paris:20260817T090000\r\n\
DTSTART;TZID=Europe/Paris:20260817T{hour}0000\r\n\
DTEND;TZID=Europe/Paris:20260817T{hour}1500\r\n\
SUMMARY:{summary}\r\n{extra}END:VEVENT\r\nEND:VCALENDAR\r\n"
)
}
#[test]
fn an_occurrence_moved_alone_sends_its_new_times() {
let base = occurrence("09", "Stand-up", "");
let moved = occurrence("10", "Stand-up", "");
let patch = MsgraphEvent::instance_update_from_ical(moved.as_bytes(), base.as_bytes())
.unwrap()
.unwrap();
let start = patch.start.as_option().unwrap();
assert_eq!(start.date_time, "2026-08-17T10:00:00");
assert_eq!(start.time_zone.as_deref(), Some("Europe/Paris"));
assert_eq!(
patch.end.as_option().unwrap().date_time,
"2026-08-17T10:15:00"
);
assert!(patch.subject.is_unset());
assert!(patch.body.is_unset());
assert!(patch.attendees.is_unset());
assert!(patch.recurrence.is_unset());
assert!(patch.single_value_extended_properties.is_unset());
}
#[test]
fn a_reverted_occurrence_takes_the_series_fields_back() {
let exception = occurrence("10", "Stand-up, moved", "");
let plain = occurrence("09", "Stand-up", "");
let patch = MsgraphEvent::instance_update_from_ical(plain.as_bytes(), exception.as_bytes())
.unwrap()
.unwrap();
assert_eq!(
patch.subject.as_option().map(String::as_str),
Some("Stand-up")
);
assert_eq!(
patch.start.as_option().unwrap().date_time,
"2026-08-17T09:00:00"
);
}
#[test]
fn an_occurrence_as_graph_holds_it_sends_nothing() {
let held = occurrence("09", "Stand-up", "");
let carried = occurrence("09", "Stand-up", "RRULE:FREQ=WEEKLY\r\nX-OWN:kept\r\n");
assert_eq!(
MsgraphEvent::instance_update_from_ical(held.as_bytes(), held.as_bytes()).unwrap(),
None
);
assert_eq!(
MsgraphEvent::instance_update_from_ical(carried.as_bytes(), held.as_bytes()).unwrap(),
None
);
}
const EXDATE: &str = "EXDATE;TZID=Europe/Paris:20261125T140000\r\n";
fn series(extra: &str, overrides: &str) -> String {
format!(
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Test//EN\r\nBEGIN:VEVENT\r\n\
UID:series@x.org\r\nDTSTAMP:20261115T090000Z\r\n\
DTSTART;TZID=Europe/Paris:20261123T140000\r\n\
DTEND;TZID=Europe/Paris:20261123T150000\r\n\
RRULE:FREQ=DAILY;COUNT=4\r\nSUMMARY:Review\r\n{extra}END:VEVENT\r\n\
{overrides}END:VCALENDAR\r\n"
)
}
fn moved(start: &str, end: &str, summary: &str, extra: &str) -> String {
format!(
"BEGIN:VEVENT\r\nUID:series@x.org\r\nDTSTAMP:20261115T090000Z\r\n\
RECURRENCE-ID;TZID=Europe/Paris:20261124T140000\r\n\
DTSTART;TZID=Europe/Paris:20261124T{start}0000\r\n\
DTEND;TZID=Europe/Paris:20261124T{end}0000\r\n\
SUMMARY:{summary}\r\n{extra}END:VEVENT\r\n"
)
}
fn patch_of(change: &MsgraphEventInstanceChange) -> &MsgraphEvent {
match &change.action {
MsgraphEventInstanceAction::Update(patch) => patch,
action => panic!("a patch, not {action:?}"),
}
}
fn times(patch: &MsgraphEvent) -> (&str, &str) {
let start = patch.start.as_option().unwrap();
let end = patch.end.as_option().unwrap();
assert_eq!(start.time_zone.as_deref(), Some("Europe/Paris"));
assert_eq!(end.time_zone.as_deref(), Some("Europe/Paris"));
(&start.date_time, &end.date_time)
}
#[test]
fn a_series_carries_its_exdate_and_override_to_their_instances() {
let document = series(EXDATE, &moved("16", "17", "Review", ""));
let series = MsgraphEventSeries::create_from_ical(document.as_bytes()).unwrap();
assert_eq!(
series.master,
MsgraphEvent::create_from_ical(document.as_bytes()).unwrap()
);
assert!(series.left_out.is_empty(), "{:?}", series.left_out);
let [moved, excluded] = series.instances.as_slice() else {
panic!("one patch and one delete: {:?}", series.instances);
};
assert_eq!(moved.original_start, "2026-11-24T13:00:00Z");
assert_eq!(moved.window, window("2026-11-23", "2026-11-26"));
let patch = patch_of(moved);
assert_eq!(times(patch), ("2026-11-24T16:00:00", "2026-11-24T17:00:00"));
assert_eq!(
patch,
&MsgraphEvent {
start: patch.start.clone(),
end: patch.end.clone(),
..Default::default()
},
"the times alone"
);
assert_eq!(excluded.original_start, "2026-11-25T13:00:00Z");
assert_eq!(excluded.window, window("2026-11-24", "2026-11-27"));
assert_eq!(excluded.action, MsgraphEventInstanceAction::Delete);
}
#[test]
fn an_occurrence_cancelled_twice_or_by_its_status_is_deleted() {
let excluded = series(
"EXDATE;TZID=Europe/Paris:20261124T140000\r\n",
&moved("16", "17", "Review", ""),
);
let cancelled = series("", &moved("14", "15", "Review", "STATUS:CANCELLED\r\n"));
for document in [excluded, cancelled] {
let series = MsgraphEventSeries::create_from_ical(document.as_bytes()).unwrap();
let [change] = series.instances.as_slice() else {
panic!("one delete: {:?}", series.instances);
};
assert_eq!(change.original_start, "2026-11-24T13:00:00Z");
assert_eq!(change.action, MsgraphEventInstanceAction::Delete);
}
}
#[test]
fn an_update_writes_what_changed_of_the_instances() {
let held = "EXDATE;TZID=Europe/Paris:20261126T140000\r\n";
let base = series(held, &moved("16", "17", "Review", ""));
let document = series(&format!("{held}{EXDATE}"), &moved("17", "18", "Review", ""));
let series =
MsgraphEventSeries::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap();
assert_eq!(
series.master,
MsgraphEvent::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap()
);
assert!(series.left_out.is_empty(), "{:?}", series.left_out);
let [moved, excluded] = series.instances.as_slice() else {
panic!("one patch and one delete: {:?}", series.instances);
};
assert_eq!(moved.original_start, "2026-11-24T13:00:00Z");
let patch = patch_of(moved);
assert_eq!(times(patch), ("2026-11-24T17:00:00", "2026-11-24T18:00:00"));
assert!(patch.subject.is_unset());
assert_eq!(excluded.original_start, "2026-11-25T13:00:00Z");
assert_eq!(excluded.action, MsgraphEventInstanceAction::Delete);
}
#[test]
fn a_dropped_override_takes_the_series_back() {
let base = series("", &moved("16", "17", "Review, moved", ""));
let document = series("", "");
let series =
MsgraphEventSeries::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap();
let [reverted] = series.instances.as_slice() else {
panic!("one patch: {:?}", series.instances);
};
assert_eq!(reverted.original_start, "2026-11-24T13:00:00Z");
let patch = patch_of(reverted);
assert_eq!(times(patch), ("2026-11-24T14:00:00", "2026-11-24T15:00:00"));
assert_eq!(
patch.subject.as_option().map(String::as_str),
Some("Review")
);
}
#[test]
fn an_update_moving_the_series_writes_its_instances_again() {
let base = series(EXDATE, &moved("16", "17", "Review", ""));
let document = series("EXDATE;TZID=Europe/Paris:20261125T150000\r\n", "")
.replace(
"DTEND;TZID=Europe/Paris:20261123T150000",
"DTEND;TZID=Europe/Paris:20261123T160000",
)
.replace(
"DTSTART;TZID=Europe/Paris:20261123T140000",
"DTSTART;TZID=Europe/Paris:20261123T150000",
);
let series =
MsgraphEventSeries::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap();
assert!(series.master.start.as_option().is_some());
assert!(series.left_out.is_empty(), "{:?}", series.left_out);
let [excluded] = series.instances.as_slice() else {
panic!("one delete: {:?}", series.instances);
};
assert_eq!(excluded.original_start, "2026-11-25T14:00:00Z");
assert_eq!(excluded.action, MsgraphEventInstanceAction::Delete);
}
#[test]
fn an_override_moved_far_is_listed_where_it_stands() {
let far = moved("16", "17", "Review", "").replace(
"DTSTART;TZID=Europe/Paris:20261124T160000",
"DTSTART;TZID=Europe/Paris:20261204T160000",
);
let far = far.replace(
"DTEND;TZID=Europe/Paris:20261124T170000",
"DTEND;TZID=Europe/Paris:20261204T170000",
);
let base = series("", &far);
let document = series("", &moved("16", "17", "Review", ""));
let series =
MsgraphEventSeries::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap();
let [moved] = series.instances.as_slice() else {
panic!("one patch: {:?}", series.instances);
};
assert_eq!(moved.window, window("2026-11-23", "2026-12-06"));
}
#[test]
fn what_graph_cannot_hold_of_a_series_is_named() {
let base = series("EXDATE;TZID=Europe/Paris:20261126T140000\r\n", "");
let ranged = "BEGIN:VEVENT\r\nUID:series@x.org\r\n\
RECURRENCE-ID;RANGE=THISANDFUTURE;TZID=Europe/Paris:20261125T140000\r\n\
DTSTART;TZID=Europe/Paris:20261125T090000\r\n\
DTEND;TZID=Europe/Paris:20261125T100000\r\n\
SUMMARY:Review\r\nEND:VEVENT\r\n";
let document = series(
"RDATE;TZID=Europe/Paris:20261201T140000\r\n\
EXDATE;TZID=Nowhere/Land:20261125T140000\r\n",
&format!("{}{ranged}", moved("16", "17", "Review", "X-OWN:kept\r\n")),
);
let series =
MsgraphEventSeries::update_from_ical(document.as_bytes(), base.as_bytes()).unwrap();
let mut left_out = series.left_out.clone();
left_out.sort();
assert_eq!(
left_out,
[
"EXDATE;TZID=Europe/Paris:20261126T140000",
"EXDATE;TZID=Nowhere/Land:20261125T140000",
"RDATE;TZID=Europe/Paris:20261201T140000",
"RECURRENCE-ID;RANGE=THISANDFUTURE;TZID=Europe/Paris:20261125T140000",
"X-OWN:kept",
]
);
let [moved] = series.instances.as_slice() else {
panic!("one patch: {:?}", series.instances);
};
assert_eq!(moved.original_start, "2026-11-24T13:00:00Z");
let lone = document.replace("RRULE:FREQ=DAILY;COUNT=4\r\n", "");
let lone = MsgraphEventSeries::create_from_ical(lone.as_bytes()).unwrap();
assert!(lone.instances.is_empty(), "{:?}", lone.instances);
assert!(
lone.left_out.contains(&String::from(
"RECURRENCE-ID;TZID=Europe/Paris:20261124T140000"
)),
"{:?}",
lone.left_out
);
}
#[test]
fn a_change_picks_its_instance_from_a_listing() {
let listed = |original: &str| MsgraphEvent {
original_start: Some(original.into()),
..Default::default()
};
let document = series(EXDATE, "");
let series = MsgraphEventSeries::create_from_ical(document.as_bytes()).unwrap();
let [excluded] = series.instances.as_slice() else {
panic!("one delete: {:?}", series.instances);
};
assert!(excluded.is_instance(&listed("2026-11-25T13:00:00Z")));
assert!(excluded.is_instance(&listed("2026-11-25T13:00:00.0000000Z")));
assert!(!excluded.is_instance(&listed("2026-11-24T13:00:00Z")));
assert!(!excluded.is_instance(&MsgraphEvent::default()));
let all_day = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:days@x.org\r\n\
DTSTART;VALUE=DATE:20261123\r\nDTEND;VALUE=DATE:20261124\r\n\
RRULE:FREQ=DAILY;COUNT=4\r\nEXDATE;VALUE=DATE:20261125\r\n\
SUMMARY:Off\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
let series = MsgraphEventSeries::create_from_ical(all_day.as_bytes()).unwrap();
let [excluded] = series.instances.as_slice() else {
panic!("one delete: {:?}", series.instances);
};
assert_eq!(excluded.original_start, "2026-11-25");
assert_eq!(excluded.window, window("2026-11-24", "2026-11-27"));
assert!(excluded.is_instance(&listed("2026-11-25T00:00:00Z")));
assert!(!excluded.is_instance(&listed("2026-11-24T00:00:00Z")));
}
fn utc_read() -> MsgraphEvent {
let utc = |date_time: &str| {
MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: date_time.into(),
time_zone: Some("UTC".into()),
})
};
MsgraphEvent {
id: "E1".into(),
start: utc("2030-01-10T09:00:00.0000000"),
end: utc("2030-01-10T10:00:00.0000000"),
is_all_day: MsgraphField::Set(false),
original_start_time_zone: Some("Romance Standard Time".into()),
original_end_time_zone: Some("Romance Standard Time".into()),
..Default::default()
}
}
#[test]
fn a_utc_read_is_told_in_its_original_zone() {
let ical = utc_read().to_ical();
assert!(
ical.contains("DTSTART;TZID=Europe/Paris:20300110T100000\r\n"),
"{ical}"
);
assert!(
ical.contains("DTEND;TZID=Europe/Paris:20300110T110000\r\n"),
"{ical}"
);
assert!(
ical.contains("BEGIN:VTIMEZONE\r\nTZID:Europe/Paris\r\n"),
"{ical}"
);
}
#[test]
fn an_unknown_original_zone_stays_utc() {
let mut event = utc_read();
event.original_start_time_zone = Some("tzone://Microsoft/Custom".into());
event.original_end_time_zone = Some("tzone://Microsoft/Custom".into());
let ical = event.to_ical();
assert!(ical.contains("DTSTART:20300110T090000Z\r\n"), "{ical}");
}
#[test]
fn an_all_day_read_keeps_its_dates() {
let mut event = utc_read();
event.is_all_day = MsgraphField::Set(true);
event.start = MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2030-01-10T00:00:00.0000000".into(),
time_zone: Some("UTC".into()),
});
event.end = MsgraphField::Set(MsgraphDateTimeTimeZone {
date_time: "2030-01-11T00:00:00.0000000".into(),
time_zone: Some("UTC".into()),
});
let ical = event.to_ical();
assert!(ical.contains("DTSTART;VALUE=DATE:20300110\r\n"), "{ical}");
}
#[test]
fn an_html_body_reads_as_the_text_it_shows() {
let styled = "<html><head><title>t</title><style>p {margin:0}</style></head>\
<body><script>var a = 1 > 0;</script><p>Hi</p><p>there</p></body></html>";
assert_eq!(strip_html(styled), "Hi\nthere");
assert_eq!(
strip_html("<div>line one<br>\r\nline two<BR/>line three</div>"),
"line one\nline two\nline three"
);
assert_eq!(strip_html("a<!-- b > c -->d"), "ad");
assert_eq!(strip_html("&lt; &amp; <b>"), "< & <b>");
assert_eq!(
strip_html(""café€' a b c"),
"\"caf\u{e9}\u{20ac}' a b c"
);
assert_eq!(
strip_html("R&D & co; &bogus; � &"),
"R&D & co; &bogus; � &"
);
}
fn html_read() -> String {
let mut event = utc_read();
event.body = MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Html),
content: Some(
"<html><body>\r\n<div class=\"PlainText\">old notes</div>\r\n</body></html>".into(),
),
});
event.to_ical()
}
#[test]
fn an_untouched_description_keeps_the_html() {
let read = html_read();
let event = MsgraphEvent::from_ical(read.as_bytes()).unwrap();
let body = event.body.as_option().unwrap();
assert_eq!(body.content_type, Some(MsgraphBodyType::Html));
}
#[test]
fn an_edited_description_beats_its_stale_html() {
let read = html_read();
let edited = read.replace("DESCRIPTION:old notes", "DESCRIPTION:new notes");
assert_ne!(edited, read, "the document carries the stripped text");
let patch = MsgraphEvent::update_from_ical(edited.as_bytes(), read.as_bytes()).unwrap();
let body = patch.body.as_option().expect("the edit reaches the patch");
assert_eq!(body.content_type, Some(MsgraphBodyType::Text));
assert_eq!(body.content.as_deref(), Some("new notes"));
}
fn without_description(ical: &str) -> String {
let mut kept = String::new();
let mut dropping = false;
for line in ical.split_inclusive("\r\n") {
if !line.starts_with(' ') {
dropping = line.starts_with("DESCRIPTION") || line.starts_with("X-ALT-DESC");
}
if !dropping {
kept.push_str(line);
}
}
kept
}
#[test]
fn an_empty_html_body_matches_a_document_without_description() {
let mut event = utc_read();
event.body = MsgraphField::Set(MsgraphItemBody {
content_type: Some(MsgraphBodyType::Html),
content: Some("<html><body></body></html>".into()),
});
let read = event.to_ical();
let edited = without_description(&read);
let patch = MsgraphEvent::update_from_ical(edited.as_bytes(), read.as_bytes()).unwrap();
assert_eq!(patch.body, MsgraphField::Unset, "no body change to send");
}
#[test]
fn a_cleared_description_sends_an_empty_text_body() {
let read = html_read();
let edited = without_description(&read);
let patch = MsgraphEvent::update_from_ical(edited.as_bytes(), read.as_bytes()).unwrap();
let body = serde_json::to_value(&patch).unwrap()["body"].clone();
assert_eq!(body["contentType"], "text", "Graph refuses a null body");
assert_eq!(body["content"], "");
}
#[test]
fn the_expand_clause_names_the_stash() {
let quoted = format!("'{MSGRAPH_EVENT_STASH_ID}'");
assert!(MSGRAPH_EVENT_STASH_EXPAND.contains("ed));
}
}