Add module src/free_api_v25/current/wind.rs
This commit is contained in:
parent
d715302a8f
commit
0955e96aef
@ -20,3 +20,4 @@
|
||||
pub mod coord;
|
||||
pub mod main;
|
||||
pub mod weather;
|
||||
pub mod wind;
|
47
src/free_api_v25/current/wind.rs
Normal file
47
src/free_api_v25/current/wind.rs
Normal file
@ -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<f32>,
|
||||
|
||||
/// Wind direction in degrees (meteorological, 0-360)
|
||||
///
|
||||
/// - 0°: North
|
||||
/// - 90°: East
|
||||
/// - 180°: South
|
||||
/// - 270°: West
|
||||
#[serde(default)]
|
||||
pub deg: Option<u16>,
|
||||
|
||||
/// Wind gust speed in chosen units
|
||||
#[serde(default)]
|
||||
pub gust: Option<f32>,
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user