openweather_async/
models.rs

1use serde::{Deserializer, Deserialize, Serialize};
2use serde::de::{self};
3use std::fmt;
4
5
6#[derive(Serialize, Deserialize, Debug)]
7pub struct Coord {
8    #[serde(rename = "Lon")]
9    pub lon: Option<f32>,
10    #[serde(rename = "Lat")]
11    pub lat: Option<f32>,
12}
13
14#[derive(Serialize, Deserialize, Debug)]
15#[serde(rename(deserialize = "Weather", serialize = "Weather"))]
16pub struct WeatherData {
17    pub id: u32,
18    pub main: String,
19    pub description: String,
20    pub icon: String,
21}
22
23#[derive(Serialize, Deserialize, Debug)]
24pub struct Main {
25    pub temp: f32,
26    pub feels_like: f32,
27    pub temp_min: f32,
28    pub temp_max: f32,
29    pub pressure: f32,
30    pub humidity: f32,
31    pub sea_level: Option<f32>,
32    pub grnd_level: Option<f32>,
33    pub temp_kf: Option<f32>,
34}
35
36#[derive(Serialize, Deserialize, Debug)]
37pub struct Wind {
38    pub speed: f32,
39    pub deg: f32,
40    pub gust: Option<f32>,
41}
42
43#[derive(Serialize, Deserialize, Debug)]
44pub struct Clouds {
45    pub all: Option<u32>,
46    pub today: Option<u32>,
47}
48
49#[derive(Serialize, Deserialize, Debug)]
50pub struct Sys {
51    #[serde(rename = "type")]
52    pub message_type: Option<u32>,
53    pub id: Option<u32>,
54    pub country: String,
55    pub sunrise: Option<u32>,
56    pub sunset: Option<u32>,
57}
58
59#[derive(Serialize, Deserialize, Debug)]
60pub struct Weather {
61    pub coord: Coord,
62    pub weather: Option<Vec<WeatherData>>,
63    pub base: Option<String>,
64    pub main: Main,
65    pub visibility: Option<u32>,
66    pub wind: Wind,
67    pub rain: Option<String>,
68    pub snow: Option<String>,
69    pub clouds: Clouds,
70    pub dt: u32,
71    pub sys: Option<Sys>,
72    pub timezone:Option<i32>,
73    pub id: i32,
74    pub name: String,
75    pub cod: Option<u32>,
76}
77
78#[derive(Serialize, Deserialize, Debug)]
79pub struct WeatherMultiple {
80    #[serde(deserialize_with = "derserialize_u32_or_string")]
81    cod: u32,
82    calctime: Option<f32>,
83    count: Option<f32>,
84    cnt: Option<u32>,
85    list: Vec<Weather>,
86}
87
88
89//https://stackoverflow.com/questions/37870428/convert-two-types-into-a-single-type-with-serde
90struct Deserializeu32orString;
91
92impl<'de> de::Visitor<'de> for Deserializeu32orString {
93    type Value = u32;
94
95    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
96        formatter.write_str("sdfaan integer or a string")
97    }
98
99    fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
100
101    where
102        E: de::Error,
103    {
104        Ok(v)
105    }
106
107    fn visit_str<E>(self, _v: &str) -> Result<Self::Value, E>
108    where
109        E: de::Error,
110    {
111        Ok(0)
112    }
113}
114
115fn derserialize_u32_or_string<'de, D>(deserializer: D) -> Result<u32, D::Error>
116where
117    D: Deserializer<'de>,
118{
119    deserializer.deserialize_any(Deserializeu32orString)
120}
121
122