Skip to main content

eventkit/
alarm.rs

1//! EventKit alarm types and helpers.
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::EventKitError;
6use crate::ffi;
7use crate::private::{json_cstring, parse_json_ptr, redacted};
8use crate::structured_location::EKStructuredLocation;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12/// Represents the EventKit `EKAlarmProximity` value.
13pub enum EKAlarmProximity {
14    /// Matches the EventKit `none` case.
15    None,
16    /// Matches the EventKit `enter` case.
17    Enter,
18    /// Matches the EventKit `leave` case.
19    Leave,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24/// Represents the EventKit `EKAlarmType` value.
25pub enum EKAlarmType {
26    /// Matches the EventKit `display` case.
27    Display,
28    /// Matches the EventKit `audio` case.
29    Audio,
30    /// Matches the EventKit `procedure` case.
31    Procedure,
32    /// Matches the EventKit `email` case.
33    Email,
34}
35
36#[derive(Clone, PartialEq, Serialize, Deserialize, Default)]
37#[serde(rename_all = "camelCase")]
38/// Represents EventKit `EKAlarm` data.
39pub struct EKAlarm {
40    /// Mirrors the EventKit `absoluteDate` property.
41    pub absolute_date: Option<String>,
42    /// Mirrors the EventKit `relativeOffset` property.
43    pub relative_offset: Option<f64>,
44    /// Mirrors the EventKit `structuredLocation` property.
45    pub structured_location: Option<EKStructuredLocation>,
46    /// Mirrors the EventKit `proximity` property.
47    pub proximity: Option<EKAlarmProximity>,
48    /// Mirrors the EventKit `alarmType` property.
49    pub alarm_type: Option<EKAlarmType>,
50    /// Mirrors the EventKit `emailAddress` property.
51    pub email_address: Option<String>,
52    /// Mirrors the EventKit `soundName` property.
53    pub sound_name: Option<String>,
54    /// Mirrors the EventKit `url` property.
55    pub url: Option<String>,
56}
57
58impl core::fmt::Debug for EKAlarm {
59    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60        f.debug_struct("EKAlarm")
61            .field("absolute_date", &self.absolute_date)
62            .field("relative_offset", &self.relative_offset)
63            .field("structured_location", &self.structured_location)
64            .field("proximity", &self.proximity)
65            .field("alarm_type", &self.alarm_type)
66            .field("email_address", &redacted(self.email_address.as_ref()))
67            .field("sound_name", &self.sound_name)
68            .field("url", &redacted(self.url.as_ref()))
69            .finish()
70    }
71}
72
73impl EKAlarm {
74    /// Creates an EventKit alarm with a relative offset.
75    pub fn relative(relative_offset: f64) -> Self {
76        Self {
77            relative_offset: Some(relative_offset),
78            ..Self::default()
79        }
80    }
81
82    /// Creates an EventKit alarm with an absolute date.
83    pub fn absolute(absolute_date: impl Into<String>) -> Self {
84        Self {
85            absolute_date: Some(absolute_date.into()),
86            ..Self::default()
87        }
88    }
89
90    /// Sets the EventKit `structuredLocation` property on this `EKAlarm` value.
91    pub fn with_structured_location(mut self, structured_location: EKStructuredLocation) -> Self {
92        self.structured_location = Some(structured_location);
93        self
94    }
95
96    /// Round-trips this EventKit `EKAlarm` through the native bridge.
97    pub fn roundtrip(&self) -> Result<Self, EventKitError> {
98        let payload = json_cstring(self, "EKAlarm")?;
99        let mut error = core::ptr::null_mut();
100        let json = unsafe { ffi::alarm::ek_alarm_roundtrip_json(payload.as_ptr(), &raw mut error) };
101        if json.is_null() {
102            Err(unsafe { EventKitError::from_error_ptr(error, "alarm roundtrip failed") })
103        } else {
104            unsafe { parse_json_ptr(json, "EKAlarm") }
105        }
106    }
107}