From 6ae6fb57cbcf0e652748634cf0f8dd2cb5168d8b Mon Sep 17 00:00:00 2001 From: Candifloss Date: Fri, 10 Oct 2025 00:56:40 +0530 Subject: [PATCH] Add module `src/free_api_v25/current/precipitation.rs` --- src/free_api_v25/current/mod.rs | 1 + src/free_api_v25/current/precipitation.rs | 36 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/free_api_v25/current/precipitation.rs diff --git a/src/free_api_v25/current/mod.rs b/src/free_api_v25/current/mod.rs index 81176ab..d37d286 100644 --- a/src/free_api_v25/current/mod.rs +++ b/src/free_api_v25/current/mod.rs @@ -20,5 +20,6 @@ pub mod clouds; pub mod coord; pub mod main; +pub mod precipitation; pub mod weather; pub mod wind; diff --git a/src/free_api_v25/current/precipitation.rs b/src/free_api_v25/current/precipitation.rs new file mode 100644 index 0000000..1654066 --- /dev/null +++ b/src/free_api_v25/current/precipitation.rs @@ -0,0 +1,36 @@ +use serde::{Deserialize, Serialize}; + +/// Precipitation information for rain or snow +/// +/// Contains precipitation volumes over different time periods. +#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)] +pub struct Precipitation { + /// Precipitation volume over the last 1 hour (mm) + #[serde(rename = "1h", default)] + pub one_hour: Option, + + /// Precipitation volume over the last 3 hours (mm) + #[serde(rename = "3h", default)] + pub three_hour: Option, +} + +impl Precipitation { + /// Returns the precipitation intensity category + /// + /// - None: No precipitation data + /// - "Light": < 2.5 mm/h + /// - "Moderate": 2.5 - 7.5 mm/h + /// - "Heavy": > 7.5 mm/h + #[must_use] + pub fn intensity(&self) -> Option<&'static str> { + let rate = self.one_hour.or(self.three_hour.map(|v| v / 3.0))?; + + if rate < 2.5 { + Some("Light") + } else if rate <= 7.5 { + Some("Moderate") + } else { + Some("Heavy") + } + } +}