Skip to main content

weatherkit/
weather_condition.rs

1//! WeatherKit condition and precipitation enums.
2
3use serde::{Deserialize, Deserializer};
4
5use crate::error::WeatherKitError;
6use crate::ffi;
7use crate::private::parse_json_from_static;
8
9/// Represents the WeatherKit `WeatherCondition` value.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum WeatherCondition {
13    /// Matches the WeatherKit `Blizzard` case.
14    Blizzard,
15    /// Matches the WeatherKit `BlowingDust` case.
16    BlowingDust,
17    /// Matches the WeatherKit `BlowingSnow` case.
18    BlowingSnow,
19    /// Matches the WeatherKit `Breezy` case.
20    Breezy,
21    /// Matches the WeatherKit `Clear` case.
22    Clear,
23    /// Matches the WeatherKit `Cloudy` case.
24    Cloudy,
25    /// Matches the WeatherKit `Drizzle` case.
26    Drizzle,
27    /// Matches the WeatherKit `Flurries` case.
28    Flurries,
29    /// Matches the WeatherKit `Foggy` case.
30    Foggy,
31    /// Matches the WeatherKit `FreezingDrizzle` case.
32    FreezingDrizzle,
33    /// Matches the WeatherKit `FreezingRain` case.
34    FreezingRain,
35    /// Matches the WeatherKit `Frigid` case.
36    Frigid,
37    /// Matches the WeatherKit `Hail` case.
38    Hail,
39    /// Matches the WeatherKit `Haze` case.
40    Haze,
41    /// Matches the WeatherKit `HeavyRain` case.
42    HeavyRain,
43    /// Matches the WeatherKit `HeavySnow` case.
44    HeavySnow,
45    /// Matches the WeatherKit `Hot` case.
46    Hot,
47    /// Matches the WeatherKit `Hurricane` case.
48    Hurricane,
49    /// Matches the WeatherKit `IsolatedThunderstorms` case.
50    IsolatedThunderstorms,
51    /// Matches the WeatherKit `MostlyClear` case.
52    MostlyClear,
53    /// Matches the WeatherKit `MostlyCloudy` case.
54    MostlyCloudy,
55    /// Matches the WeatherKit `PartlyCloudy` case.
56    PartlyCloudy,
57    /// Matches the WeatherKit `Rain` case.
58    Rain,
59    /// Matches the WeatherKit `ScatteredThunderstorms` case.
60    ScatteredThunderstorms,
61    /// Matches the WeatherKit `Sleet` case.
62    Sleet,
63    /// Matches the WeatherKit `Smoky` case.
64    Smoky,
65    /// Matches the WeatherKit `Snow` case.
66    Snow,
67    /// Matches the WeatherKit `StrongStorms` case.
68    StrongStorms,
69    /// Matches the WeatherKit `SunFlurries` case.
70    SunFlurries,
71    /// Matches the WeatherKit `SunShowers` case.
72    SunShowers,
73    /// Matches the WeatherKit `Thunderstorms` case.
74    Thunderstorms,
75    /// Matches the WeatherKit `TropicalStorm` case.
76    TropicalStorm,
77    /// Matches the WeatherKit `Windy` case.
78    Windy,
79    /// Matches the WeatherKit `WintryMix` case.
80    WintryMix,
81    /// Stores an unrecognized WeatherKit case name.
82    Unknown(String),
83}
84
85impl WeatherCondition {
86    pub(crate) fn from_raw(value: String) -> Self {
87        match value.as_str() {
88            "blizzard" => Self::Blizzard,
89            "blowingDust" => Self::BlowingDust,
90            "blowingSnow" => Self::BlowingSnow,
91            "breezy" => Self::Breezy,
92            "clear" => Self::Clear,
93            "cloudy" => Self::Cloudy,
94            "drizzle" => Self::Drizzle,
95            "flurries" => Self::Flurries,
96            "foggy" => Self::Foggy,
97            "freezingDrizzle" => Self::FreezingDrizzle,
98            "freezingRain" => Self::FreezingRain,
99            "frigid" => Self::Frigid,
100            "hail" => Self::Hail,
101            "haze" => Self::Haze,
102            "heavyRain" => Self::HeavyRain,
103            "heavySnow" => Self::HeavySnow,
104            "hot" => Self::Hot,
105            "hurricane" => Self::Hurricane,
106            "isolatedThunderstorms" => Self::IsolatedThunderstorms,
107            "mostlyClear" => Self::MostlyClear,
108            "mostlyCloudy" => Self::MostlyCloudy,
109            "partlyCloudy" => Self::PartlyCloudy,
110            "rain" => Self::Rain,
111            "scatteredThunderstorms" => Self::ScatteredThunderstorms,
112            "sleet" => Self::Sleet,
113            "smoky" => Self::Smoky,
114            "snow" => Self::Snow,
115            "strongStorms" => Self::StrongStorms,
116            "sunFlurries" => Self::SunFlurries,
117            "sunShowers" => Self::SunShowers,
118            "thunderstorms" => Self::Thunderstorms,
119            "tropicalStorm" => Self::TropicalStorm,
120            "windy" => Self::Windy,
121            "wintryMix" => Self::WintryMix,
122            other => Self::Unknown(other.to_owned()),
123        }
124    }
125
126    /// Returns the WeatherKit raw value for this case.
127    pub fn raw_value(&self) -> &str {
128        match self {
129            Self::Blizzard => "blizzard",
130            Self::BlowingDust => "blowingDust",
131            Self::BlowingSnow => "blowingSnow",
132            Self::Breezy => "breezy",
133            Self::Clear => "clear",
134            Self::Cloudy => "cloudy",
135            Self::Drizzle => "drizzle",
136            Self::Flurries => "flurries",
137            Self::Foggy => "foggy",
138            Self::FreezingDrizzle => "freezingDrizzle",
139            Self::FreezingRain => "freezingRain",
140            Self::Frigid => "frigid",
141            Self::Hail => "hail",
142            Self::Haze => "haze",
143            Self::HeavyRain => "heavyRain",
144            Self::HeavySnow => "heavySnow",
145            Self::Hot => "hot",
146            Self::Hurricane => "hurricane",
147            Self::IsolatedThunderstorms => "isolatedThunderstorms",
148            Self::MostlyClear => "mostlyClear",
149            Self::MostlyCloudy => "mostlyCloudy",
150            Self::PartlyCloudy => "partlyCloudy",
151            Self::Rain => "rain",
152            Self::ScatteredThunderstorms => "scatteredThunderstorms",
153            Self::Sleet => "sleet",
154            Self::Smoky => "smoky",
155            Self::Snow => "snow",
156            Self::StrongStorms => "strongStorms",
157            Self::SunFlurries => "sunFlurries",
158            Self::SunShowers => "sunShowers",
159            Self::Thunderstorms => "thunderstorms",
160            Self::TropicalStorm => "tropicalStorm",
161            Self::Windy => "windy",
162            Self::WintryMix => "wintryMix",
163            Self::Unknown(value) => value.as_str(),
164        }
165    }
166
167    /// Returns the WeatherKit descriptor catalog for this enum.
168    pub fn descriptors() -> Result<Vec<WeatherConditionDescriptor>, WeatherKitError> {
169        parse_json_from_static(
170            ffi::weather_condition::wk_weather_condition_copy_descriptors_json,
171            "weather condition descriptors",
172        )
173    }
174}
175
176/// Represents the WeatherKit `Precipitation` value.
177#[derive(Debug, Clone, PartialEq, Eq)]
178#[non_exhaustive]
179pub enum Precipitation {
180    /// Matches the WeatherKit `None` case.
181    None,
182    /// Matches the WeatherKit `Hail` case.
183    Hail,
184    /// Matches the WeatherKit `Mixed` case.
185    Mixed,
186    /// Matches the WeatherKit `Rain` case.
187    Rain,
188    /// Matches the WeatherKit `Sleet` case.
189    Sleet,
190    /// Matches the WeatherKit `Snow` case.
191    Snow,
192    /// Stores an unrecognized WeatherKit case name.
193    Unknown(String),
194}
195
196impl Precipitation {
197    pub(crate) fn from_raw(value: String) -> Self {
198        match value.as_str() {
199            "none" => Self::None,
200            "hail" => Self::Hail,
201            "mixed" => Self::Mixed,
202            "rain" => Self::Rain,
203            "sleet" => Self::Sleet,
204            "snow" => Self::Snow,
205            other => Self::Unknown(other.to_owned()),
206        }
207    }
208
209    /// Returns the WeatherKit raw value for this case.
210    pub fn raw_value(&self) -> &str {
211        match self {
212            Self::None => "none",
213            Self::Hail => "hail",
214            Self::Mixed => "mixed",
215            Self::Rain => "rain",
216            Self::Sleet => "sleet",
217            Self::Snow => "snow",
218            Self::Unknown(value) => value.as_str(),
219        }
220    }
221
222    /// Returns the WeatherKit descriptor catalog for this enum.
223    pub fn descriptors() -> Result<Vec<PrecipitationDescriptor>, WeatherKitError> {
224        parse_json_from_static(
225            ffi::weather_condition::wk_precipitation_copy_descriptors_json,
226            "precipitation descriptors",
227        )
228    }
229}
230
231/// Describes a WeatherKit precipitation case.
232#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
233#[serde(rename_all = "camelCase")]
234pub struct PrecipitationDescriptor {
235    /// Matches the WeatherKit raw value value.
236    pub raw_value: String,
237    /// Matches the WeatherKit description value.
238    pub description: String,
239    /// Matches the WeatherKit accessibility description value.
240    pub accessibility_description: String,
241}
242
243/// Describes a WeatherKit weather condition case.
244#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
245#[serde(rename_all = "camelCase")]
246pub struct WeatherConditionDescriptor {
247    /// Matches the WeatherKit raw value value.
248    pub raw_value: String,
249    /// Matches the WeatherKit description value.
250    pub description: String,
251    /// Matches the WeatherKit accessibility description value.
252    pub accessibility_description: String,
253}
254
255pub(crate) fn deserialize_weather_condition<'de, D>(
256    deserializer: D,
257) -> Result<WeatherCondition, D::Error>
258where
259    D: Deserializer<'de>,
260{
261    let raw = String::deserialize(deserializer)?;
262    Ok(WeatherCondition::from_raw(raw))
263}
264
265pub(crate) fn deserialize_precipitation<'de, D>(deserializer: D) -> Result<Precipitation, D::Error>
266where
267    D: Deserializer<'de>,
268{
269    let raw = String::deserialize(deserializer)?;
270    Ok(Precipitation::from_raw(raw))
271}