Skip to main content

ibridge_core/
calendar.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Calendar and reminders values and provider traits.
4
5use crate::error::CoreError;
6use crate::ids::{ETag, ResourcePath};
7
8/// A calendar collection.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct Calendar {
11    /// Path relative to the account's calendar home.
12    pub path: ResourcePath,
13    /// Display name.
14    pub name: String,
15    /// The collection holds events.
16    pub holds_events: bool,
17    /// The collection holds tasks (reminders).
18    pub holds_tasks: bool,
19}
20
21/// A time range over one or more calendars.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub struct EventQuery {
24    /// Calendars to read. Empty means every calendar.
25    pub calendars: Vec<ResourcePath>,
26    /// Inclusive range start, as an ISO-8601 timestamp.
27    pub start: String,
28    /// Exclusive range end, as an ISO-8601 timestamp.
29    pub end: String,
30}
31
32/// One event, or one expanded occurrence of a recurring event.
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct Event {
35    /// Path relative to the calendar home.
36    pub path: ResourcePath,
37    /// `ETag` for a conditional write.
38    pub etag: ETag,
39    /// The iCalendar `UID`.
40    pub uid: String,
41    /// Summary.
42    pub summary: String,
43    /// Description.
44    pub description: String,
45    /// Start, as an ISO-8601 timestamp.
46    pub start: String,
47    /// End, as an ISO-8601 timestamp.
48    pub end: String,
49    /// IANA zone name of the start, when the event has a named zone.
50    pub time_zone: Option<String>,
51    /// The event covers whole days.
52    pub all_day: bool,
53    /// The event repeats. Each expanded occurrence carries the same path.
54    pub recurring: bool,
55    /// Original start of this occurrence, as an ISO-8601 timestamp, for a
56    /// recurring event. Pass it back to change only this occurrence.
57    pub recurrence_id: Option<String>,
58    /// Attendee addresses.
59    pub attendees: Vec<String>,
60    /// Alarm triggers, such as `-PT15M`.
61    pub alarms: Vec<String>,
62}
63
64/// Events from a query, with a count of items that were skipped.
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub struct EventList {
67    /// Occurrences in start order.
68    pub events: Vec<Event>,
69    /// Items that could not be read and were left out.
70    pub warnings: u32,
71}
72
73/// Fields written by create and update.
74#[derive(Clone, Debug, PartialEq, Eq)]
75pub struct EventDraft {
76    /// Summary. On update, empty keeps the event's own.
77    pub summary: String,
78    /// Description. `None` writes none on create and keeps the event's own on
79    /// update; `Some("")` clears it.
80    pub description: Option<String>,
81    /// Start, as an ISO-8601 timestamp. On update, empty (with an empty `end`)
82    /// keeps the event's own times, which is how a series keeps its first start.
83    pub start: String,
84    /// End, as an ISO-8601 timestamp. Writers always store this.
85    pub end: String,
86    /// IANA zone name for `start` and `end`. `None` writes UTC or a date; on
87    /// update it falls back to the zone the event is already in.
88    pub time_zone: Option<String>,
89    /// Guest addresses. `None` writes none on create and leaves the event's
90    /// guests alone on update; `Some` on update is the whole new guest list
91    /// (`Some(vec![])` removes every guest). iCloud emails each guest an
92    /// invitation, an update or a cancellation when the list or the event changes.
93    pub attendees: Option<Vec<String>>,
94}
95
96/// Which part of a recurring event an update changes.
97#[derive(Clone, Debug, PartialEq, Eq)]
98#[non_exhaustive]
99pub enum UpdateScope {
100    /// The whole event, or the whole series.
101    Series,
102    /// One occurrence, named by its original start.
103    Occurrence {
104        /// The occurrence's [`Event::recurrence_id`].
105        recurrence_id: String,
106    },
107}
108
109/// Calendar read and write operations.
110#[trait_variant::make(Send)]
111pub trait CalendarProvider {
112    /// Lists calendars.
113    ///
114    /// # Errors
115    ///
116    /// Returns [`CoreError`] when the calendar home cannot be read.
117    async fn list_calendars(&self) -> Result<Vec<Calendar>, CoreError>;
118
119    /// Lists events in a time range.
120    ///
121    /// # Errors
122    ///
123    /// Returns [`CoreError`] when the query cannot be run.
124    async fn list_events(&self, query: &EventQuery) -> Result<EventList, CoreError>;
125
126    /// Fetches one event.
127    ///
128    /// # Errors
129    ///
130    /// Returns [`CoreError`] when the event cannot be read.
131    async fn get_event(&self, path: &ResourcePath) -> Result<Event, CoreError>;
132
133    /// Creates an event.
134    ///
135    /// # Errors
136    ///
137    /// Returns [`CoreError`] when the event cannot be written.
138    async fn create_event(
139        &self,
140        calendar: &ResourcePath,
141        draft: &EventDraft,
142    ) -> Result<ResourcePath, CoreError>;
143
144    /// Changes an event when the `ETag` still matches.
145    ///
146    /// # Errors
147    ///
148    /// Returns [`CoreError::Conflict`] when the `ETag` does not match.
149    async fn update_event(
150        &self,
151        path: &ResourcePath,
152        etag: &ETag,
153        draft: &EventDraft,
154        scope: &UpdateScope,
155    ) -> Result<ETag, CoreError>;
156
157    /// Deletes an event when the `ETag` still matches.
158    ///
159    /// # Errors
160    ///
161    /// Returns [`CoreError::Conflict`] when the `ETag` does not match.
162    async fn delete_event(&self, path: &ResourcePath, etag: &ETag) -> Result<(), CoreError>;
163}
164
165/// A reminders list.
166#[derive(Clone, Debug, PartialEq, Eq)]
167pub struct ReminderList {
168    /// Path relative to the calendar home.
169    pub path: ResourcePath,
170    /// Display name.
171    pub name: String,
172}
173
174/// Shown with the reminder lists: iCloud's `CalDAV` does not expose lists the
175/// Reminders app has upgraded, so they are missing from the result.
176pub const UPGRADED_LISTS_NOTE: &str = "Only old-style reminder lists are shown. Lists that the Reminders app upgraded to the newer format can't be reached through iCloud's calendar service, so they are missing here and can't be read or changed.";
177
178/// Whether completed reminders are included.
179#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
180#[non_exhaustive]
181pub enum CompletedFilter {
182    /// Open and completed reminders.
183    All,
184    /// Open reminders only.
185    Open,
186}
187
188/// One reminder.
189#[derive(Clone, Debug, PartialEq, Eq)]
190pub struct Reminder {
191    /// Path relative to the calendar home.
192    pub path: ResourcePath,
193    /// `ETag` for a conditional write.
194    pub etag: ETag,
195    /// Title.
196    pub title: String,
197    /// Notes.
198    pub notes: String,
199    /// Due date, when the reminder has one.
200    pub due: Option<String>,
201    /// Priority from 1 (highest) to 9, when set.
202    pub priority: Option<u8>,
203    /// The reminder is completed.
204    pub completed: bool,
205}
206
207/// Reminders from a query, with a count of items that were skipped.
208#[derive(Clone, Debug, PartialEq, Eq)]
209pub struct ReminderPage {
210    /// The reminders, in the order the lists returned them.
211    pub reminders: Vec<Reminder>,
212    /// Items that could not be read and were left out.
213    pub warnings: u32,
214}
215
216/// Fields written by create and update. An update replaces all of them, so a
217/// `None` removes the property.
218#[derive(Clone, Debug, PartialEq, Eq)]
219pub struct ReminderDraft {
220    /// Title.
221    pub title: String,
222    /// Notes.
223    pub notes: String,
224    /// Due date, when the reminder has one.
225    pub due: Option<String>,
226    /// Priority from 1 (highest) to 9, when set.
227    pub priority: Option<u8>,
228    /// The reminder is completed.
229    pub completed: bool,
230}
231
232/// Reminders read and write operations.
233#[trait_variant::make(Send)]
234pub trait RemindersProvider {
235    /// Lists reminder lists.
236    ///
237    /// # Errors
238    ///
239    /// Returns [`CoreError`] when the lists cannot be read.
240    async fn list_lists(&self) -> Result<Vec<ReminderList>, CoreError>;
241
242    /// Lists reminders.
243    ///
244    /// # Errors
245    ///
246    /// Returns [`CoreError`] when the list cannot be read.
247    async fn list(
248        &self,
249        list: Option<&ResourcePath>,
250        completed: CompletedFilter,
251    ) -> Result<ReminderPage, CoreError>;
252
253    /// Fetches one reminder.
254    ///
255    /// # Errors
256    ///
257    /// Returns [`CoreError::NotFound`] when the reminder does not exist.
258    async fn get(&self, path: &ResourcePath) -> Result<Reminder, CoreError>;
259
260    /// Creates a reminder.
261    ///
262    /// # Errors
263    ///
264    /// Returns [`CoreError`] when the reminder cannot be written.
265    async fn create(
266        &self,
267        list: &ResourcePath,
268        draft: &ReminderDraft,
269    ) -> Result<ResourcePath, CoreError>;
270
271    /// Replaces a reminder when the `ETag` still matches.
272    ///
273    /// # Errors
274    ///
275    /// Returns [`CoreError::Conflict`] when the `ETag` does not match.
276    async fn update(
277        &self,
278        path: &ResourcePath,
279        etag: &ETag,
280        draft: &ReminderDraft,
281    ) -> Result<ETag, CoreError>;
282
283    /// Deletes a reminder when the `ETag` still matches.
284    ///
285    /// # Errors
286    ///
287    /// Returns [`CoreError::Conflict`] when the `ETag` does not match.
288    async fn delete(&self, path: &ResourcePath, etag: &ETag) -> Result<(), CoreError>;
289}