diff --git a/src/free_api_v25/current/main.rs b/src/free_api_v25/current/main.rs new file mode 100644 index 0000000..2eb1542 --- /dev/null +++ b/src/free_api_v25/current/main.rs @@ -0,0 +1,58 @@ +use serde::{Deserialize, Serialize}; + +/// Main weather parameters including temperature, pressure, and humidity +/// +/// All temperature values are in the units specified in the API request +/// (Kelvin, Celsius, or Fahrenheit). +#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)] +pub struct Main { + /// Temperature in requested units (Kelvin, Celsius, or Fahrenheit) + #[serde(default)] + pub temp: Option, + /// "Feels like" temperature, accounting for human perception of weather + #[serde(default)] + pub feels_like: Option, + /// Minimum observed temperature (within large urban areas) + #[serde(default)] + pub temp_min: Option, + /// Maximum observed temperature (within large urban areas) + #[serde(default)] + pub temp_max: Option, + /// Atmospheric pressure at sea level (hPa) + #[serde(default)] + pub pressure: Option, + /// Humidity percentage (0-100) + #[serde(default)] + pub humidity: Option, + /// Atmospheric pressure at sea level (hPa) + #[serde(default)] + pub sea_level: Option, + /// Atmospheric pressure at ground level (hPa) + #[serde(default)] + pub grnd_level: Option, +} + +impl Main { + /// Returns the temperature difference between max and min + /// + /// # Example + /// ``` + /// use owm_api25::current::Main; + /// + /// let main = Main { + /// temp: Some(20.0), + /// temp_min: Some(15.0), + /// temp_max: Some(25.0), + /// ..Default::default() + /// }; + /// + /// assert_eq!(main.temp_range(), Some(10.0)); + /// ``` + #[must_use] + pub fn temp_range(&self) -> Option { + match (self.temp_min, self.temp_max) { + (Some(min), Some(max)) => Some(max - min), + _ => None, + } + } +} diff --git a/src/free_api_v25/current/mod.rs b/src/free_api_v25/current/mod.rs index b4cf5f4..826f92b 100644 --- a/src/free_api_v25/current/mod.rs +++ b/src/free_api_v25/current/mod.rs @@ -18,4 +18,5 @@ //! - [`query`](crate::query) for building request URLs pub mod coord; +pub mod main; pub mod weather;