use serde::{Deserialize, Serialize};
use crate::error::EventKitError;
use crate::ffi;
use crate::private::{json_cstring, parse_json_ptr, redacted, Redacted};
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EKGeoLocation {
pub latitude: f64,
pub longitude: f64,
pub altitude: Option<f64>,
pub horizontal_accuracy: Option<f64>,
pub vertical_accuracy: Option<f64>,
}
impl core::fmt::Debug for EKGeoLocation {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EKGeoLocation")
.field("latitude", &Redacted)
.field("longitude", &Redacted)
.field("altitude", &redacted(self.altitude.as_ref()))
.field("horizontal_accuracy", &self.horizontal_accuracy)
.field("vertical_accuracy", &self.vertical_accuracy)
.finish()
}
}
impl EKGeoLocation {
pub const fn new(latitude: f64, longitude: f64) -> Self {
Self {
latitude,
longitude,
altitude: None,
horizontal_accuracy: None,
vertical_accuracy: None,
}
}
}
#[derive(Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EKStructuredLocation {
pub title: Option<String>,
pub geo_location: Option<EKGeoLocation>,
pub radius: f64,
}
impl core::fmt::Debug for EKStructuredLocation {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EKStructuredLocation")
.field("title", &redacted(self.title.as_ref()))
.field("geo_location", &self.geo_location)
.field("radius", &self.radius)
.finish()
}
}
impl EKStructuredLocation {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: Some(title.into()),
..Self::default()
}
}
pub fn with_geo_location(mut self, geo_location: EKGeoLocation) -> Self {
self.geo_location = Some(geo_location);
self
}
pub fn with_radius(mut self, radius: f64) -> Self {
self.radius = radius;
self
}
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(),
&raw mut error,
)
};
if json.is_null() {
Err(unsafe {
EventKitError::from_error_ptr(error, "structured location roundtrip failed")
})
} else {
unsafe { parse_json_ptr(json, "EKStructuredLocation") }
}
}
}