Skip to main content

eventkit/
structured_location.rs

1//! EventKit structured-location 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, Redacted};
8
9#[derive(Clone, PartialEq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11/// Represents the EventKit location coordinates used by structured locations.
12pub struct EKGeoLocation {
13    /// Mirrors the EventKit `latitude` property.
14    pub latitude: f64,
15    /// Mirrors the EventKit `longitude` property.
16    pub longitude: f64,
17    /// Mirrors the EventKit `altitude` property.
18    pub altitude: Option<f64>,
19    /// Mirrors the EventKit `horizontalAccuracy` property.
20    pub horizontal_accuracy: Option<f64>,
21    /// Mirrors the EventKit `verticalAccuracy` property.
22    pub vertical_accuracy: Option<f64>,
23}
24
25impl core::fmt::Debug for EKGeoLocation {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        f.debug_struct("EKGeoLocation")
28            .field("latitude", &Redacted)
29            .field("longitude", &Redacted)
30            .field("altitude", &redacted(self.altitude.as_ref()))
31            .field("horizontal_accuracy", &self.horizontal_accuracy)
32            .field("vertical_accuracy", &self.vertical_accuracy)
33            .finish()
34    }
35}
36
37impl EKGeoLocation {
38    /// Creates a new EventKit `EKGeoLocation` value.
39    pub const fn new(latitude: f64, longitude: f64) -> Self {
40        Self {
41            latitude,
42            longitude,
43            altitude: None,
44            horizontal_accuracy: None,
45            vertical_accuracy: None,
46        }
47    }
48}
49
50#[derive(Clone, PartialEq, Serialize, Deserialize, Default)]
51#[serde(rename_all = "camelCase")]
52/// Represents EventKit `EKStructuredLocation` data.
53pub struct EKStructuredLocation {
54    /// Mirrors the EventKit `title` property.
55    pub title: Option<String>,
56    /// Mirrors the EventKit `geoLocation` property.
57    pub geo_location: Option<EKGeoLocation>,
58    /// Mirrors the EventKit `radius` property.
59    pub radius: f64,
60}
61
62impl core::fmt::Debug for EKStructuredLocation {
63    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
64        f.debug_struct("EKStructuredLocation")
65            .field("title", &redacted(self.title.as_ref()))
66            .field("geo_location", &self.geo_location)
67            .field("radius", &self.radius)
68            .finish()
69    }
70}
71
72impl EKStructuredLocation {
73    /// Creates a new EventKit `EKStructuredLocation` value.
74    pub fn new(title: impl Into<String>) -> Self {
75        Self {
76            title: Some(title.into()),
77            ..Self::default()
78        }
79    }
80
81    /// Sets the EventKit `geoLocation` property on this `EKStructuredLocation` value.
82    pub fn with_geo_location(mut self, geo_location: EKGeoLocation) -> Self {
83        self.geo_location = Some(geo_location);
84        self
85    }
86
87    /// Sets the EventKit `radius` property on this `EKStructuredLocation` value.
88    pub fn with_radius(mut self, radius: f64) -> Self {
89        self.radius = radius;
90        self
91    }
92
93    /// Round-trips this EventKit `EKStructuredLocation` through the native bridge.
94    pub fn roundtrip(&self) -> Result<Self, EventKitError> {
95        let payload = json_cstring(self, "EKStructuredLocation")?;
96        let mut error = core::ptr::null_mut();
97        let json = unsafe {
98            ffi::structured_location::ek_structured_location_roundtrip_json(
99                payload.as_ptr(),
100                &raw mut error,
101            )
102        };
103        if json.is_null() {
104            Err(unsafe {
105                EventKitError::from_error_ptr(error, "structured location roundtrip failed")
106            })
107        } else {
108            unsafe { parse_json_ptr(json, "EKStructuredLocation") }
109        }
110    }
111}