1 | mod current; |
2 | pub use current::*; |
3 | |
4 | mod forecast; |
5 | pub use forecast::*; |
6 | |
7 | use serde::{Serialize, Deserialize}; |
8 | use chrono::{DateTime, TimeZone, Utc, FixedOffset, ParseResult}; |
9 | use std::fmt::{self, Display}; |
10 | |
11 | |
12 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
13 | pub struct History { |
14 | pub location: Location, |
15 | pub forecast: _Forecast, |
16 | } |
17 | |
18 | pub type Future = History; |
19 | |
20 | |
21 | pub trait Date { |
22 | fn _date_from_str(&self, s: &str) -> ParseResult<DateTime<Utc>> { |
23 | Utc.datetime_from_str(s, fmt:"%Y-%m-%d" ) |
24 | } |
25 | |
26 | fn date(&self) -> DateTime<Utc>; |
27 | fn date_epoch(&self) -> DateTime<Utc>; |
28 | } |
29 | |
30 | |
31 | pub trait Time { |
32 | fn _time_from_str(&self, s: &str) -> ParseResult<DateTime<Utc>> { |
33 | Utc.datetime_from_str(s, fmt:"%Y-%m-%d %H:%M" ) |
34 | } |
35 | |
36 | fn time(&self) -> DateTime<Utc>; |
37 | fn time_epoch(&self) -> DateTime<Utc>; |
38 | } |
39 | |
40 | |
41 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
42 | pub struct AirQuality { |
43 | #[serde(rename = "co" )] |
44 | pub carbon_monoxide: String, |
45 | #[serde(rename = "o3" )] |
46 | pub ozone: String, |
47 | #[serde(rename = "no2" )] |
48 | pub nitrogen_dioxide: String, |
49 | #[serde(rename = "so2" )] |
50 | pub sulphur_dioxide: String, |
51 | pub pm2_5: String, |
52 | pub pm10: String, |
53 | #[serde(rename = "us-epa-index" )] |
54 | pub us_epa_index: u32, |
55 | #[serde(rename = "gb-defra-index" )] |
56 | pub gb_defra_index: u32 |
57 | } |
58 | |
59 | |
60 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
61 | pub struct Condition { |
62 | pub text: String, |
63 | pub icon: String, |
64 | pub code: u32 |
65 | } |
66 | |
67 | |
68 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
69 | pub struct WeatherAlert { |
70 | #[serde(rename = "headline" )] |
71 | pub head_line: String, |
72 | #[serde(rename = "msgType" )] |
73 | pub msg_type: String, |
74 | pub severity: String, |
75 | pub urgency: String, |
76 | pub areas: String, |
77 | pub category: String, |
78 | pub certainty: String, |
79 | pub event: String, |
80 | pub note: String, |
81 | pub effective: String, |
82 | pub expires: String, |
83 | #[serde(rename = "desc" )] |
84 | pub description: String, |
85 | pub instruction: String |
86 | } |
87 | |
88 | impl WeatherAlert { |
89 | pub fn effective(&self) -> DateTime<FixedOffset> { |
90 | self.effective.parse().unwrap() |
91 | } |
92 | |
93 | pub fn expires(&self) -> DateTime<FixedOffset> { |
94 | self.expires.parse().unwrap() |
95 | } |
96 | } |
97 | |
98 | |
99 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
100 | pub struct Alerts { |
101 | pub alert: Vec<WeatherAlert> |
102 | } |
103 | |
104 | |
105 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
106 | pub struct Coords { |
107 | pub lat: f32, |
108 | pub lon: f32 |
109 | } |
110 | |
111 | impl Display for Coords { |
112 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
113 | write!(f, " {}, {}" , self.lat, self.lon) |
114 | } |
115 | } |
116 | |
117 | |
118 | #[derive (Debug, Serialize, Deserialize, PartialEq)] |
119 | pub struct Location { |
120 | #[serde(flatten)] |
121 | pub coords: Coords, |
122 | pub name: String, |
123 | pub region: String, |
124 | pub country: String, |
125 | pub id: Option<u32>, |
126 | pub url: Option<String>, |
127 | pub tz_id: Option<String>, |
128 | pub localtime_epoch: Option<i64>, |
129 | pub localtime: Option<String> |
130 | } |
131 | |
132 | impl Time for Location { |
133 | fn time(&self) -> DateTime<Utc> { |
134 | self._time_from_str(&self.localtime.as_ref().unwrap()).unwrap() |
135 | } |
136 | |
137 | fn time_epoch(&self) -> DateTime<Utc> { |
138 | Utc.timestamp(self.localtime_epoch.unwrap(), nsecs:0) |
139 | } |
140 | } |
141 | |