diff --git a/src/free_api_v25/current/mod.rs b/src/free_api_v25/current/mod.rs index 826f92b..7c1abfa 100644 --- a/src/free_api_v25/current/mod.rs +++ b/src/free_api_v25/current/mod.rs @@ -20,3 +20,4 @@ pub mod coord; pub mod main; pub mod weather; +pub mod wind; \ No newline at end of file diff --git a/src/free_api_v25/current/wind.rs b/src/free_api_v25/current/wind.rs new file mode 100644 index 0000000..8199307 --- /dev/null +++ b/src/free_api_v25/current/wind.rs @@ -0,0 +1,47 @@ +use serde::{Deserialize, Serialize}; + +/// Wind information including speed, direction, and gusts +#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)] +pub struct Wind { + /// Wind speed in chosen units (m/s by default) + #[serde(default)] + pub speed: Option, + + /// Wind direction in degrees (meteorological, 0-360) + /// + /// - 0°: North + /// - 90°: East + /// - 180°: South + /// - 270°: West + #[serde(default)] + pub deg: Option, + + /// Wind gust speed in chosen units + #[serde(default)] + pub gust: Option, +} + +impl Wind { + /// Returns wind direction as a cardinal direction (N, NE, E, etc.) + /// + /// # Example + /// ``` + /// use owm_api25::current::Wind; + /// + /// let north_wind = Wind { deg: Some(0), ..Default::default() }; + /// assert_eq!(north_wind.direction(), Some("N")); + /// + /// let east_wind = Wind { deg: Some(90), ..Default::default() }; + /// assert_eq!(east_wind.direction(), Some("E")); + /// ``` + #[must_use] + pub fn direction(&self) -> Option<&'static str> { + let deg = self.deg?; + let directions = [ + "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", + "NW", "NNW", + ]; + let index = (((u32::from(deg) * 16 + 11) / 22) % 16) as usize; + Some(directions[index]) + } +} \ No newline at end of file