From 6959ec1e67568390dd2269d19bd404ea4e1a0f7d Mon Sep 17 00:00:00 2001 From: Candifloss Date: Wed, 8 Oct 2025 23:59:43 +0530 Subject: [PATCH] Update library - More complete --- owm_api25/Cargo.toml | 1 + owm_api25/src/weather.rs | 83 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/owm_api25/Cargo.toml b/owm_api25/Cargo.toml index dfd4bc1..8bb02cf 100644 --- a/owm_api25/Cargo.toml +++ b/owm_api25/Cargo.toml @@ -8,3 +8,4 @@ toml = "0.9.6" dirs = "6.0.0" serde = { version = "1.0.225", features = ["derive"] } serde_json = "1.0.145" +chrono = "0.4.42" diff --git a/owm_api25/src/weather.rs b/owm_api25/src/weather.rs index eca61d7..2ea2fa8 100644 --- a/owm_api25/src/weather.rs +++ b/owm_api25/src/weather.rs @@ -1,30 +1,97 @@ +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 icon: String, pub description: String, + pub icon: String, } #[derive(Debug, Deserialize, Serialize)] pub struct Main { pub temp: f32, - pub pressure: u32, - pub humidity: u8, + pub feels_like: Option, + pub temp_min: Option, + pub temp_max: Option, + pub pressure: Option, + pub humidity: Option, + pub sea_level: Option, + pub grnd_level: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct Wind { + pub speed: Option, + pub deg: Option, + pub gust: Option, +} + +#[derive(Debug, Deserialize, Serialize, Default)] +pub struct Clouds { + pub all: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct Precipitation { + #[serde(rename = "1h")] + pub one_hour: Option, + #[serde(rename = "3h")] + pub three_hour: Option, } #[derive(Debug, Deserialize, Serialize)] pub struct Sys { - pub country: String, - pub sunrise: u64, - pub sunset: u64, + pub r#type: Option, + pub id: Option, + pub country: Option, + pub sunrise: Option, + pub sunset: Option, } #[derive(Debug, Deserialize, Serialize)] pub struct WeatherResponse { - pub name: String, - pub sys: Sys, + pub coord: Option, + #[serde(default)] pub weather: Vec, + pub base: Option, pub main: Main, + pub visibility: Option, + pub wind: Option, + pub clouds: Option, + + /// Optional precipitation data + /// "rain": { "1h": f32 } or "3h": f32 + #[serde(default)] + pub rain: Option, + + /// "snow": { "1h": f32 } or "3h": f32 + #[serde(default)] + pub snow: Option, + + #[serde(with = "chrono::serde::ts_seconds_option")] + pub dt: Option>, + pub sys: Sys, + pub timezone: Option, + pub id: Option, + pub name: String, + pub cod: Option, +} + +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) + } }