2025-10-08 23:59:43 +05:30

98 lines
2.2 KiB
Rust

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Deserialize, Serialize)]
pub struct Coord {
pub lon: f64,
pub lat: f64,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Weather {
pub id: u32,
pub main: String,
pub description: String,
pub icon: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Main {
pub temp: f32,
pub feels_like: Option<f32>,
pub temp_min: Option<f32>,
pub temp_max: Option<f32>,
pub pressure: Option<u32>,
pub humidity: Option<u8>,
pub sea_level: Option<u32>,
pub grnd_level: Option<u32>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Wind {
pub speed: Option<f32>,
pub deg: Option<u16>,
pub gust: Option<f32>,
}
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Clouds {
pub all: Option<u8>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Precipitation {
#[serde(rename = "1h")]
pub one_hour: Option<f32>,
#[serde(rename = "3h")]
pub three_hour: Option<f32>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Sys {
pub r#type: Option<u8>,
pub id: Option<u32>,
pub country: Option<String>,
pub sunrise: Option<u64>,
pub sunset: Option<u64>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct WeatherResponse {
pub coord: Option<Coord>,
#[serde(default)]
pub weather: Vec<Weather>,
pub base: Option<String>,
pub main: Main,
pub visibility: Option<u32>,
pub wind: Option<Wind>,
pub clouds: Option<Clouds>,
/// Optional precipitation data
/// "rain": { "1h": f32 } or "3h": f32
#[serde(default)]
pub rain: Option<Precipitation>,
/// "snow": { "1h": f32 } or "3h": f32
#[serde(default)]
pub snow: Option<Precipitation>,
#[serde(with = "chrono::serde::ts_seconds_option")]
pub dt: Option<DateTime<Utc>>,
pub sys: Sys,
pub timezone: Option<i32>,
pub id: Option<u64>,
pub name: String,
pub cod: Option<u16>,
}
impl WeatherResponse {
pub fn primary_weather(&self) -> Option<&Weather> {
self.weather.first()
}
pub fn is_success(&self) -> bool {
self.cod.map_or(true, |code| code == 200)
}
}