Add module src/free_api_v25/current/precipitation.rs

This commit is contained in:
Candifloss 2025-10-10 00:56:40 +05:30
parent bbec51dbe6
commit 6ae6fb57cb
2 changed files with 37 additions and 0 deletions

View File

@ -20,5 +20,6 @@
pub mod clouds;
pub mod coord;
pub mod main;
pub mod precipitation;
pub mod weather;
pub mod wind;

View File

@ -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<f32>,
/// Precipitation volume over the last 3 hours (mm)
#[serde(rename = "3h", default)]
pub three_hour: Option<f32>,
}
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")
}
}
}