Skip to main content

weatherkit/
availability_kind.rs

1//! WeatherKit availability types.
2
3use core::ffi::c_void;
4
5use serde::{Deserialize, Deserializer};
6
7use crate::error::WeatherKitError;
8use crate::ffi;
9use crate::private::{parse_json_from_handle, parse_json_from_static};
10
11/// Represents the WeatherKit `AvailabilityKind` value.
12#[derive(Debug, Clone, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum AvailabilityKind {
15    /// Matches the WeatherKit `Available` case.
16    Available,
17    /// Matches the WeatherKit `TemporarilyUnavailable` case.
18    TemporarilyUnavailable,
19    /// Matches the WeatherKit `Unsupported` case.
20    Unsupported,
21    /// Stores an unrecognized WeatherKit case name.
22    Unknown(String),
23}
24
25impl AvailabilityKind {
26    pub(crate) fn from_raw(value: String) -> Self {
27        match value.as_str() {
28            "available" => Self::Available,
29            "temporarilyUnavailable" => Self::TemporarilyUnavailable,
30            "unsupported" => Self::Unsupported,
31            "unknown" => Self::Unknown(value),
32            other => Self::Unknown(other.to_owned()),
33        }
34    }
35
36    /// Returns the WeatherKit raw value for this case.
37    pub fn raw_value(&self) -> &str {
38        match self {
39            Self::Available => "available",
40            Self::TemporarilyUnavailable => "temporarilyUnavailable",
41            Self::Unsupported => "unsupported",
42            Self::Unknown(value) => value.as_str(),
43        }
44    }
45
46    /// Returns the WeatherKit descriptor catalog for this enum.
47    pub fn descriptors() -> Result<Vec<AvailabilityKindDescriptor>, WeatherKitError> {
48        parse_json_from_static(
49            ffi::availability_kind::wk_availability_kind_copy_descriptors_json,
50            "availability kind descriptors",
51        )
52    }
53}
54
55/// Represents the WeatherKit `AvailabilityKindDescriptor` payload.
56#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
57#[serde(rename_all = "camelCase")]
58pub struct AvailabilityKindDescriptor {
59    /// Matches the WeatherKit raw value value.
60    pub raw_value: String,
61}
62
63/// Represents the WeatherKit `WeatherAvailability` payload.
64#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
65#[serde(rename_all = "camelCase")]
66pub struct WeatherAvailability {
67    /// Matches the WeatherKit minute availability value.
68    #[serde(deserialize_with = "deserialize_availability_kind")]
69    pub minute_availability: AvailabilityKind,
70    /// Matches the WeatherKit alert availability value.
71    #[serde(deserialize_with = "deserialize_availability_kind")]
72    pub alert_availability: AvailabilityKind,
73}
74
75impl WeatherAvailability {
76    pub(crate) fn from_owned_ptr(ptr: *mut c_void) -> Result<Self, WeatherKitError> {
77        parse_json_from_handle(
78            ptr,
79            ffi::availability_kind::wk_weather_availability_release,
80            ffi::availability_kind::wk_weather_availability_copy_json,
81            "weather availability",
82        )
83    }
84}
85
86pub(crate) fn deserialize_availability_kind<'de, D>(
87    deserializer: D,
88) -> Result<AvailabilityKind, D::Error>
89where
90    D: Deserializer<'de>,
91{
92    let raw = String::deserialize(deserializer)?;
93    Ok(AvailabilityKind::from_raw(raw))
94}