1use 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")]
12pub enum EKAlarmProximity {
14 None,
16 Enter,
18 Leave,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub enum EKAlarmType {
26 Display,
28 Audio,
30 Procedure,
32 Email,
34}
35
36#[derive(Clone, PartialEq, Serialize, Deserialize, Default)]
37#[serde(rename_all = "camelCase")]
38pub struct EKAlarm {
40 pub absolute_date: Option<String>,
42 pub relative_offset: Option<f64>,
44 pub structured_location: Option<EKStructuredLocation>,
46 pub proximity: Option<EKAlarmProximity>,
48 pub alarm_type: Option<EKAlarmType>,
50 pub email_address: Option<String>,
52 pub sound_name: Option<String>,
54 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 pub fn relative(relative_offset: f64) -> Self {
76 Self {
77 relative_offset: Some(relative_offset),
78 ..Self::default()
79 }
80 }
81
82 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 pub fn with_structured_location(mut self, structured_location: EKStructuredLocation) -> Self {
92 self.structured_location = Some(structured_location);
93 self
94 }
95
96 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}