Skip to main content

weatherkit/
current_weather.rs

1use core::ffi::c_void;
2
3use serde::Deserialize;
4
5use crate::error::WeatherKitError;
6use crate::ffi;
7use crate::pressure::{deserialize_pressure_trend, Pressure, PressureTrend};
8use crate::private::parse_json_from_handle;
9use crate::service::WeatherMetadata;
10use crate::weather_condition::{deserialize_weather_condition, WeatherCondition};
11
12#[derive(Debug, Clone, PartialEq, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct CloudCoverByAltitude {
15    pub low: f64,
16    pub medium: f64,
17    pub high: f64,
18}
19
20#[derive(Debug, Clone, PartialEq, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct Wind {
23    pub speed: f64,
24    pub direction: f64,
25    pub compass_direction: String,
26    pub gust: Option<f64>,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub struct UVIndex {
32    pub value: i64,
33    pub category: String,
34}
35
36#[derive(Debug, Clone, PartialEq, Deserialize)]
37#[serde(rename_all = "camelCase")]
38pub struct CurrentWeather {
39    pub date: String,
40    pub temperature: f64,
41    pub feels_like: f64,
42    pub humidity: f64,
43    pub dew_point: f64,
44    pub pressure: f64,
45    #[serde(deserialize_with = "deserialize_pressure_trend")]
46    pub pressure_trend: PressureTrend,
47    #[serde(deserialize_with = "deserialize_weather_condition")]
48    pub condition: WeatherCondition,
49    pub symbol_name: String,
50    pub wind: Wind,
51    pub uv_index: UVIndex,
52    pub visibility: f64,
53    pub cloud_cover: f64,
54    #[serde(default)]
55    pub cloud_cover_by_altitude: Option<CloudCoverByAltitude>,
56    pub is_daylight: bool,
57    pub precipitation_intensity: f64,
58    pub metadata: WeatherMetadata,
59}
60
61impl CurrentWeather {
62    pub(crate) fn from_owned_ptr(ptr: *mut c_void) -> Result<Self, WeatherKitError> {
63        parse_json_from_handle(
64            ptr,
65            ffi::current_weather::wk_current_weather_release,
66            ffi::current_weather::wk_current_weather_copy_json,
67            "current weather",
68        )
69    }
70
71    pub fn pressure_reading(&self) -> Pressure {
72        Pressure::new(self.pressure, self.pressure_trend.clone())
73    }
74}