eventkit/
structured_location.rs1use 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")]
11pub struct EKGeoLocation {
13 pub latitude: f64,
15 pub longitude: f64,
17 pub altitude: Option<f64>,
19 pub horizontal_accuracy: Option<f64>,
21 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 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")]
52pub struct EKStructuredLocation {
54 pub title: Option<String>,
56 pub geo_location: Option<EKGeoLocation>,
58 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 pub fn new(title: impl Into<String>) -> Self {
75 Self {
76 title: Some(title.into()),
77 ..Self::default()
78 }
79 }
80
81 pub fn with_geo_location(mut self, geo_location: EKGeoLocation) -> Self {
83 self.geo_location = Some(geo_location);
84 self
85 }
86
87 pub fn with_radius(mut self, radius: f64) -> Self {
89 self.radius = radius;
90 self
91 }
92
93 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}