1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! EventKit calendar snapshots and draft helpers.
use std::ptr;
use serde::{Deserialize, Serialize};
use crate::error::EventKitError;
use crate::event_store::{EKEntityType, EKEventStore};
use crate::ffi;
use crate::object::EKObject;
use crate::private::{json_cstring, parse_json_ptr};
use crate::source::EKSource;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
/// Represents the EventKit `EKCalendarType` value.
pub enum EKCalendarType {
#[default]
/// Matches the EventKit `local` case.
Local,
/// Matches the EventKit `calDav` case.
CalDav,
/// Matches the EventKit `exchange` case.
Exchange,
/// Matches the EventKit `subscription` case.
Subscription,
/// Matches the EventKit `birthday` case.
Birthday,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Represents the EventKit availability values supported by a calendar.
pub enum EKCalendarEventAvailability {
/// Matches the EventKit `busy` case.
Busy,
/// Matches the EventKit `free` case.
Free,
/// Matches the EventKit `tentative` case.
Tentative,
/// Matches the EventKit `unavailable` case.
Unavailable,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
/// Represents EventKit `EKCalendar` data.
pub struct EKCalendar {
/// Mirrors the EventKit `identifier` property.
pub identifier: String,
/// Mirrors the EventKit `title` property.
pub title: String,
/// Mirrors the EventKit `calendarType` property.
pub calendar_type: EKCalendarType,
#[serde(default)]
/// Mirrors the EventKit `allowedEntityTypes` property.
pub allowed_entity_types: Vec<EKEntityType>,
/// Mirrors the EventKit `color` property.
pub color: Option<String>,
/// Mirrors the EventKit `source` property.
pub source: Option<EKSource>,
/// Mirrors the EventKit `allowsContentModifications` property.
pub allows_content_modifications: bool,
/// Mirrors the EventKit `isSubscribed` property.
pub is_subscribed: bool,
/// Mirrors the EventKit `isImmutable` property.
pub is_immutable: bool,
#[serde(default)]
/// Mirrors the EventKit `supportedEventAvailabilities` property.
pub supported_event_availabilities: Vec<EKCalendarEventAvailability>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Represents editable EventKit `EKCalendar` input data.
pub struct EKCalendarDraft {
/// Mirrors the EventKit `identifier` property.
pub identifier: Option<String>,
/// Mirrors the EventKit `entityType` property.
pub entity_type: EKEntityType,
/// Mirrors the EventKit `sourceIdentifier` property.
pub source_identifier: Option<String>,
/// Mirrors the EventKit `title` property.
pub title: String,
/// Mirrors the EventKit `color` property.
pub color: Option<String>,
}
impl EKCalendarDraft {
/// Creates a new EventKit `EKCalendarDraft` value.
pub fn new(entity_type: EKEntityType, title: impl Into<String>) -> Self {
Self {
identifier: None,
entity_type,
source_identifier: None,
title: title.into(),
color: None,
}
}
/// Sets the EventKit `identifier` property on this `EKCalendarDraft` value.
pub fn with_identifier(mut self, identifier: impl Into<String>) -> Self {
self.identifier = Some(identifier.into());
self
}
/// Sets the EventKit `sourceIdentifier` property on this `EKCalendarDraft` value.
pub fn with_source_identifier(mut self, source_identifier: impl Into<String>) -> Self {
self.source_identifier = Some(source_identifier.into());
self
}
/// Sets the EventKit `color` property on this `EKCalendarDraft` value.
pub fn with_color(mut self, color: impl Into<String>) -> Self {
self.color = Some(color.into());
self
}
/// Round-trips this EventKit calendar draft through the native bridge.
pub fn roundtrip(&self, store: &EKEventStore) -> Result<EKCalendar, EventKitError> {
let payload = json_cstring(self, "EKCalendarDraft")?;
let mut error = ptr::null_mut();
let json = unsafe {
ffi::calendar::ek_calendar_roundtrip_json(
store.as_raw_ptr(),
payload.as_ptr(),
&mut error,
)
};
if json.is_null() {
Err(unsafe { EventKitError::from_error_ptr(error, "calendar roundtrip failed") })
} else {
unsafe { parse_json_ptr(json, "EKCalendar") }
}
}
/// Wraps this EventKit calendar draft as a live `EKObject` in the given store.
pub fn as_object_in(&self, store: &EKEventStore) -> Result<EKObject, EventKitError> {
EKObject::from_calendar_draft(store, self)
}
}