weatherkit/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3pub mod auth;
4pub mod error;
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8
9pub const WEATHERKIT_API_BASE_URL: &str = "https://weatherkit.apple.com/api/v1";
10
11/// The system of units that the weather data is reported in.
12#[derive(Deserialize, Debug)]
13#[serde(rename_all = "camelCase")]
14pub enum UnitsSystem {
15    /// Metric
16    M,
17}
18
19/// Descriptive information about the weather data.
20#[derive(Deserialize, Debug)]
21#[serde(rename_all = "camelCase")]
22pub struct Metadata {
23    /// The URL of the legal attribution for the data source.
24    pub attribution_url: Option<String>,
25    /// The time when the weather data is no longer valid.
26    pub expire_time: DateTime<Utc>,
27    /// The ISO language code for localizable fields.
28    pub language: Option<String>,
29    /// The latitude of the relevant location.
30    pub latitude: f64,
31    /// The longitude of the relevant location.
32    pub longitude: f64,
33    /// The longitude of the relevant location.
34    pub provider_logo: Option<String>,
35    /// The longitude of the relevant location.
36    pub provider_name: Option<String>,
37    /// The time the weather data was procured.
38    pub read_time: DateTime<Utc>,
39    /// The time the provider reported the weather data.
40    pub reported_time: Option<DateTime<Utc>>,
41    /// Whether the weather data is temporarily unavailable from the provider.
42    pub temporarily_unavailable: Option<bool>,
43    /// The system of units that the weather data is reported in.
44    /// This is always set to metric.
45    pub units: Option<UnitsSystem>,
46    /// The data format version.
47    pub version: i64,
48}
49
50/// The direction of change of the sea level air pressure.
51#[derive(Eq, PartialEq, Deserialize, Debug)]
52#[serde(rename_all = "camelCase")]
53pub enum PressureTrend {
54    /// The sea level air pressure is increasing.
55    Rising,
56    /// The sea level air pressure is decreasing.
57    Falling,
58    /// The sea level air pressure is remaining about the same..
59    Steady,
60}
61
62#[derive(Deserialize, Debug)]
63#[serde(rename_all = "camelCase")]
64pub struct CurrentWeather {
65    /// Descriptive information about the weather data.
66    pub metadata: Metadata,
67    /// The date and time.
68    pub as_of: DateTime<Utc>,
69    /// The percentage of the sky covered with clouds during the period, from 0 to 1.
70    pub cloud_cover: Option<f64>,
71    /// An enumeration value indicating the condition at the time.
72    pub condition_code: String,
73    /// A Boolean value indicating whether there is daylight.
74    pub daylight: Option<bool>,
75    /// The relative humidity, from 0 to 1.
76    pub humidity: f64,
77    /// The precipitation intensity, in millimeters per hour.
78    pub precipitation_intensity: f64,
79    /// The sea level air pressure, in millibars.
80    pub pressure: f64,
81    /// The direction of change of the sea level air pressure.
82    pub pressure_trend: PressureTrend,
83    /// The current temperature, in degrees Celsius.
84    pub temperature: f64,
85    /// The feels-like temperature when factoring wind and humidity, in degrees Celsius.
86    pub temperature_apparent: f64,
87    /// The temperature at which relative humidity is 100%, in Celsius.
88    pub temperature_dew_point: f64,
89    /// The level of ultraviolet radiation.
90    pub uv_index: u8,
91    /// The distance at which terrain is visible, in meters.
92    pub visibility: f64,
93    /// The direction of the wind, in degrees.
94    pub wind_direction: Option<u16>,
95    /// The maximum wind gust speed, in kilometers per hour.
96    pub wind_gust: Option<f64>,
97    /// The wind speed, in kilometers per hour.
98    pub wind_speed: f64,
99}
100
101/// The type of precipitation forecasted to occur during the day.
102#[derive(Eq, PartialEq, Deserialize, Debug)]
103#[serde(rename_all = "camelCase")]
104pub enum PrecipitationType {
105    /// No precipitation is occurring.
106    Clear,
107    /// An unknown type of precipitation is occurring.
108    Precipitation,
109    /// Rain or freezing rain is falling.
110    Rain,
111    /// Snow is falling.
112    Snow,
113    /// Sleet or ice pellets are falling.
114    Sleet,
115    /// Hail is falling
116    Hail,
117    /// Winter weather (wintery mix or wintery showers) is falling.
118    Mixed,
119}
120
121/// THe shape of the moon as seen by the observer on the ground at a given time.
122#[derive(Eq, PartialEq, Deserialize, Debug)]
123#[serde(rename_all = "camelCase")]
124pub enum MoonPhase {
125    /// The moon isn't visible.
126    New,
127    /// A crescent-shaped sliver of the moon is visible, and increasing in size.
128    WaxingCrescent,
129    /// Approximately half of the moon is visible, and increasing in size.,
130    FirstQuarter,
131    /// The entire disc of the moon is visible.
132    Full,
133    /// More than half of the moon is visible, and increasing in size.
134    WaxingGibbous,
135    /// More than half of the moon is visible, and decreasing in size.
136    WaningGibbous,
137    /// Approximately half of the moon is visible, and decreasing in size.
138    ThirdQuarter,
139    /// A crescent-shaped sliver of the moon is visible, and decreasing in size.
140    WaningCrescent,
141}
142
143/// A summary forecast for a daytime or overnight period.
144#[derive(Deserialize, Debug)]
145#[serde(rename_all = "camelCase")]
146pub struct DayPartForecast {
147    /// The percentage of the sky covered with clouds during the period, from 0 to
148    pub cloud_cover: f64,
149    /// An enumeration value indicating the condition at the time.
150    pub condition_code: String,
151    /// The ending date and time of the forecast.
152    pub forecast_end: DateTime<Utc>,
153    /// The starting date and time of the forecast.
154    pub forecast_start: DateTime<Utc>,
155    /// The relative humidity during the period, from 0 to 1.
156    pub humidity: f64,
157    /// The amount of precipitation forecasted to occur during the period, in millimeters.
158    pub precipitation_amount: f64,
159    /// The chance of precipitation forecasted to occur during the period.
160    pub precipitation_chance: f64,
161    /// The type of precipitation forecasted to occur during the period.
162    pub precipitation_type: PrecipitationType,
163    /// The depth of snow as ice crystals forecasted to occur during the period, in millimeters.
164    pub snowfall_amount: f64,
165    /// The direction the wind is forecasted to come from during the period, in degrees.
166    pub wind_direction: Option<u16>,
167    /// The average speed the wind is forecasted to be during the period, in kilometers per hour.
168    pub wind_speed: f64,
169}
170
171/// The historical or forecasted weather conditions for a specified day.
172#[derive(Deserialize, Debug)]
173#[serde(rename_all = "camelCase")]
174pub struct DayWeatherConditions {
175    /// An enumeration value indicating the condition at the time.
176    pub condition_code: String,
177    /// The forecast between 7 AM and 7 PM for the day.
178    pub daytime_forecast: Option<DayPartForecast>,
179    /// The ending date and time of the day.
180    pub forecast_end: DateTime<Utc>,
181    /// The starting date and time of the day.
182    pub forecast_start: DateTime<Utc>,
183    /// The maximum ultraviolet index value during the day.
184    pub max_uv_index: u8,
185    /// The phase of the moon on the specified day.
186    pub moon_phase: MoonPhase,
187    /// The time of moonrise on the specified day.
188    pub moonrise: Option<DateTime<Utc>>,
189    /// The time of moonset on the specified day.
190    pub moonset: Option<DateTime<Utc>>,
191    /// The day part forecast between 7 PM and 7 AM for the overnight.
192    pub overnight_forecast: Option<DayPartForecast>,
193    /// The amount of precipitation forecasted to occur during the day, in millimeters.
194    pub precipitation_amount: f64,
195    /// The chance of precipitation forecasted to occur during the day.
196    pub precipitation_chance: f64,
197    /// The type of precipitation forecasted to occur during the day.
198    pub precipitation_type: PrecipitationType,
199    /// The depth of snow as ice crystals forecasted to occur during the day, in millimeters.
200    pub snowfall_amount: f64,
201    /// The time when the sun is lowest in the sky.
202    pub solar_midnight: Option<DateTime<Utc>>,
203    /// The time when the sun is highest in the sky.
204    pub solar_noon: Option<DateTime<Utc>>,
205    /// The time when the top edge of the sun reaches the horizon in the morning.
206    pub sunrise: Option<DateTime<Utc>>,
207    /// The time when the sun is 18 degrees below the horizon in the morning.
208    pub sunrise_astronomical: Option<DateTime<Utc>>,
209    /// The time when the sun is 6 degrees below the horizon in the morning.
210    pub sunrise_civil: Option<DateTime<Utc>>,
211    /// The time when the sun is 12 degrees below the horizon in the morning.
212    pub sunrise_nautical: Option<DateTime<Utc>>,
213    /// The time when the top edge of the sun reaches the horizon in the evening.
214    pub sunset: Option<DateTime<Utc>>,
215    /// The time when the sun is 18 degrees below the horizon in the evening.
216    pub sunset_astronomical: Option<DateTime<Utc>>,
217    /// The time when the sun is 6 degrees below the horizon in the evening.
218    pub sunset_civil: Option<DateTime<Utc>>,
219    /// The time when the sun is 12 degrees below the horizon in the evening.
220    pub sunset_nautical: Option<DateTime<Utc>>,
221    /// The maximum temperature forecasted to occur during the day, in degrees Celsius.
222    pub temperature_max: f64,
223    /// The minimum temperature forecasted to occur during the day, in degrees Celsius.
224    pub temperature_min: f64,
225}
226
227/// A collection of day forecasts for a specified range of days.
228#[derive(Deserialize, Debug)]
229#[serde(rename_all = "camelCase")]
230pub struct DailyForecast {
231    /// Descriptive information about the weather data.
232    pub metadata: Metadata,
233    /// An array of the day forecast weather conditions.
234    pub days: Vec<DayWeatherConditions>,
235    /// A URL that provides more information about the forecast.
236    pub learn_more_url: Option<String>,
237}
238
239/// The historical or forecasted weather conditions for a specified hour.
240#[derive(Deserialize, Debug)]
241#[serde(rename_all = "camelCase")]
242pub struct HourWeatherConditions {
243    /// The percentage of the sky covered with clouds during the period, from 0 to 1.
244    pub cloud_cover: f64,
245    /// An enumeration value indicating the condition at the time.
246    pub condition_code: String,
247    /// Indicates whether the hour starts during the day or night.
248    pub daylight: Option<bool>,
249    /// The starting date and time of the forecast.
250    pub forecast_start: DateTime<Utc>,
251    /// The relative humidity at the start of the hour, from 0 to 1.
252    pub humidity: f64,
253    /// The chance of precipitation forecasted to occur during the hour, from 0 to 1.
254    pub precipitation_chance: f64,
255    /// The type of precipitation forecasted to occur during the period.
256    pub precipitation_type: PrecipitationType,
257    /// The sea-level air pressure, in millibars.
258    pub pressure: f64,
259    /// The direction of change of the sea-level air pressure.
260    pub pressure_trend: Option<PressureTrend>,
261    /// The rate at which snow crystals are falling, in millimeters per hour.
262    pub snowfall_intensity: Option<f64>,
263    /// The temperature at the start of the hour, in degrees Celsius.
264    pub temperature: f64,
265    /// The temperature at the start of the hour, in degrees Celsius.
266    pub temperature_apparent: f64,
267    /// The temperature at which relative humidity is 100% at the top of the hour, in degrees Celsius.
268    pub temperature_dew_point: Option<f64>,
269    /// The level of ultraviolet radiation at the start of the hour.
270    pub uv_index: u8,
271    /// The distance at which terrain is visible at the start of the hour, in meters.
272    pub visibility: f64,
273    /// The direction of the wind at the start of the hour, in degrees.
274    pub wind_direction: Option<u16>,
275    /// The maximum wind gust speed during the hour, in kilometers per hour.
276    pub wind_gust: Option<f64>,
277    /// The wind speed at the start of the hour, in kilometers per hour.
278    pub wind_speed: f64,
279    /// The amount of precipitation forecasted to occur during period, in millimeters.
280    pub precipitation_amount: Option<f64>,
281}
282
283/// A collection of hour forecasts for a specified range of hours.
284#[derive(Deserialize, Debug)]
285#[serde(rename_all = "camelCase")]
286pub struct HourlyForecast {
287    /// Descriptive information about the weather data.
288    pub metadata: Metadata,
289    /// An array of hourly forecasts.
290    pub hours: Vec<HourWeatherConditions>,
291}
292
293/// The precipitation forecast for a specified minute.
294#[derive(Deserialize, Debug)]
295#[serde(rename_all = "camelCase")]
296pub struct ForecastMinute {
297    /// The probability of precipitation during this minute.
298    pub precipitation_chance: f64,
299    /// The precipitation intensity in millimeters per hour.
300    pub precipitation_intensity: f64,
301    /// The start time of the minute.
302    pub start_time: DateTime<Utc>,
303}
304
305/// The summary for a specified period in the minute forecast.
306#[derive(Deserialize, Debug)]
307#[serde(rename_all = "camelCase")]
308pub struct ForecastPeriodSummary {
309    /// The type of precipitation forecasted.
310    pub condition: PrecipitationType,
311    /// The end time of the forecast.
312    pub end_time: Option<DateTime<Utc>>,
313    /// The probability of precipitation during this period.
314    pub precipitation_chance: f64,
315    /// The precipitation intensity in millimeters per hour.
316    pub precipitation_intensity: f64,
317    /// The start time of the forecast.
318    pub start_time: DateTime<Utc>,
319}
320
321/// The next hour forecast information.
322#[derive(Deserialize, Debug)]
323#[serde(rename_all = "camelCase")]
324pub struct NextHourForecast {
325    /// Descriptive information about the weather data.
326    pub metadata: Metadata,
327    /// The time the forecast ends.
328    pub forecast_end: Option<DateTime<Utc>>,
329    /// The time the forecast starts.
330    pub forecast_start: Option<DateTime<Utc>>,
331    /// The time the forecast ends.
332    pub minutes: Vec<ForecastMinute>,
333    /// An array of the forecast summaries.
334    pub summary: Vec<ForecastPeriodSummary>,
335}
336
337/// How likely the event is to occur.
338#[derive(Deserialize, Debug)]
339#[serde(rename_all = "camelCase")]
340pub enum Certainty {
341    /// The event has already occurred or is ongoing.
342    Observed,
343    /// The event is likely to occur (greater than 50% probability).
344    Likely,
345    /// The event is unlikely to occur (less than 50% probability).
346    Possible,
347    /// The event is not expected to occur (approximately 0% probability)..
348    Unlikely,
349    /// It is unknown if the event will occur.
350    Unknown,
351}
352
353/// The recommended action from a reporting agency.
354#[derive(Deserialize, Debug)]
355#[serde(rename_all = "camelCase")]
356pub enum ResponseType {
357    /// Take shelter in place.
358    Shelter,
359    /// Relocate.
360    Evacuate,
361    /// Make preparations
362    Prepare,
363    /// Execute a pre-planned activity.
364    Execute,
365    /// Avoid the event.
366    Avoid,
367    /// Monitor the situation.
368    Monitor,
369    /// Assess the situation.
370    Assess,
371    /// The event no longer poses a threat.
372    AllClear,
373    /// No action recommended.
374    None,
375}
376
377/// The level of danger to life and property.
378#[derive(Deserialize, Debug)]
379#[serde(rename_all = "camelCase")]
380pub enum Severity {
381    /// Extraordinary threat.
382    Extreme,
383    /// Significant threat.
384    Severe,
385    /// Possible threat.
386    Moderate,
387    /// Minimal or no known threat.
388    Minor,
389    /// Unknown threat.
390    Unknown,
391}
392
393/// An indication of urgency of action from the reporting agency.
394#[derive(Deserialize, Debug)]
395#[serde(rename_all = "camelCase")]
396pub enum Urgency {
397    /// Take responsive action immediately.
398    Immediate,
399    /// Take responsive action in the next hour.
400    Expected,
401    /// Take responsive action in the near future.
402    Future,
403    /// Responsive action is no longer required.
404    Past,
405    /// The urgency is unknown.
406    Unknown,
407}
408
409/// Detailed information about the weather alert.
410#[derive(Deserialize, Debug)]
411#[serde(rename_all = "camelCase")]
412pub struct WeatherAlertSummary {
413    /// An official designation of the affected area.
414    pub area_id: Option<String>,
415    /// A human-readable name of the affected area.
416    pub area_name: Option<String>,
417    /// How likely the event is to occur.
418    pub certainty: Certainty,
419    /// The ISO code of the reporting country.
420    pub country_code: String,
421    /// A human-readable description of the event.
422    pub description: String,
423    /// The URL to a page containing detailed information about the event.
424    pub details_url: Option<String>,
425    /// The time the event went into effect.
426    pub effective_time: DateTime<Utc>,
427    /// The time when the underlying weather event is projected to end.
428    pub event_end_time: Option<DateTime<Utc>>,
429    /// The time when the underlying weather event is projected to start.
430    pub event_onset_time: Option<DateTime<Utc>>,
431    /// The time when the event expires.
432    pub expire_time: DateTime<Utc>,
433    /// A unique identifier of the event.
434    pub id: String,
435    /// The time that event was issued by the reporting agency.
436    pub issued_time: DateTime<Utc>,
437    /// An array of recommended actions from the reporting agency.
438    pub responses: Vec<ResponseType>,
439    /// The level of danger to life and property.
440    pub severity: Severity,
441    /// The name of the reporting agency.
442    pub source: String,
443    /// An indication of urgency of action from the reporting agency.
444    pub urgency: Option<Urgency>,
445}
446
447/// A collecton of weather alerts.
448#[derive(Deserialize, Debug)]
449#[serde(rename_all = "camelCase")]
450pub struct WeatherAlertsCollection {
451    /// An array of weather alert summaries.
452    pub alerts: Vec<WeatherAlertSummary>,
453    /// A URL that provides more information about the alerts.
454    pub details_url: Option<String>,
455}
456
457/// The collection of all requested weather data.
458#[derive(Deserialize, Debug)]
459#[serde(rename_all = "camelCase")]
460pub struct Weather {
461    /// The current weather for the requested location.
462    pub current_weather: Option<CurrentWeather>,
463    /// The daily forecast for the requested location.
464    pub forecast_daily: Option<DailyForecast>,
465    /// The hourly forecast for the requested location.
466    pub forecast_hourly: Option<HourlyForecast>,
467    /// The next hour forecast for the requested location.
468    pub forecast_next_hour: Option<NextHourForecast>,
469    /// Weather alerts for the requested location.
470    pub weather_alerts: Option<WeatherAlertsCollection>,
471}
472
473/// The collection of weather information for a location.
474#[derive(Eq, PartialEq, Serialize, Deserialize, Debug)]
475#[serde(rename_all = "camelCase")]
476pub enum DataSet {
477    /// The current weather for the requested location.
478    CurrentWeather,
479    /// The daily forecast for the requested location.
480    ForecastDaily,
481    /// The hourly forecast for the requested location.
482    ForecastHourly,
483    /// The next hour forecast for the requested location.
484    ForecastNextHour,
485    /// The next hour forecast for the requested location.
486    WeatherAlerts,
487}