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
//! EventKit structured-location types and helpers.
use serde::{Deserialize, Serialize};
use crate::error::EventKitError;
use crate::ffi;
use crate::private::{json_cstring, parse_json_ptr};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Represents the EventKit location coordinates used by structured locations.
pub struct EKGeoLocation {
/// Mirrors the EventKit `latitude` property.
pub latitude: f64,
/// Mirrors the EventKit `longitude` property.
pub longitude: f64,
/// Mirrors the EventKit `altitude` property.
pub altitude: Option<f64>,
/// Mirrors the EventKit `horizontalAccuracy` property.
pub horizontal_accuracy: Option<f64>,
/// Mirrors the EventKit `verticalAccuracy` property.
pub vertical_accuracy: Option<f64>,
}
impl EKGeoLocation {
/// Creates a new EventKit `EKGeoLocation` value.
pub const fn new(latitude: f64, longitude: f64) -> Self {
Self {
latitude,
longitude,
altitude: None,
horizontal_accuracy: None,
vertical_accuracy: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
/// Represents EventKit `EKStructuredLocation` data.
pub struct EKStructuredLocation {
/// Mirrors the EventKit `title` property.
pub title: Option<String>,
/// Mirrors the EventKit `geoLocation` property.
pub geo_location: Option<EKGeoLocation>,
/// Mirrors the EventKit `radius` property.
pub radius: f64,
}
impl EKStructuredLocation {
/// Creates a new EventKit `EKStructuredLocation` value.
pub fn new(title: impl Into<String>) -> Self {
Self {
title: Some(title.into()),
..Self::default()
}
}
/// Sets the EventKit `geoLocation` property on this `EKStructuredLocation` value.
pub fn with_geo_location(mut self, geo_location: EKGeoLocation) -> Self {
self.geo_location = Some(geo_location);
self
}
/// Sets the EventKit `radius` property on this `EKStructuredLocation` value.
pub fn with_radius(mut self, radius: f64) -> Self {
self.radius = radius;
self
}
/// Round-trips this EventKit `EKStructuredLocation` through the native bridge.
pub fn roundtrip(&self) -> Result<Self, EventKitError> {
let payload = json_cstring(self, "EKStructuredLocation")?;
let mut error = core::ptr::null_mut();
let json = unsafe {
ffi::structured_location::ek_structured_location_roundtrip_json(
payload.as_ptr(),
&mut error,
)
};
if json.is_null() {
Err(unsafe {
EventKitError::from_error_ptr(error, "structured location roundtrip failed")
})
} else {
unsafe { parse_json_ptr(json, "EKStructuredLocation") }
}
}
}