Skip to main content

openmeteo_rs/variables/
climate.rs

1use std::borrow::Cow;
2
3use crate::query::AsApiStr;
4
5/// Model selector for climate projection requests.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum ClimateModel {
9    /// CMCC CM2 VHR4 high-resolution climate model.
10    CmccCm2Vhr4,
11    /// EC-Earth3P HR high-resolution climate model.
12    EcEarth3PHr,
13    /// FGOALS f3 H high-resolution climate model.
14    FgoalsF3H,
15    /// HiRAM SIT HR high-resolution climate model.
16    HiramSitHr,
17    /// MPI ESM1.2 XR high-resolution climate model.
18    MpiEsm12Xr,
19    /// MRI AGCM3.2 S high-resolution climate model.
20    MriAgcm32S,
21    /// NICAM16 8S high-resolution climate model.
22    Nicam168S,
23    /// Exact Open-Meteo climate model token not yet represented by this enum.
24    ///
25    /// The token is passed through unchanged and is not validated by the crate.
26    Other(Cow<'static, str>),
27}
28
29impl ClimateModel {
30    /// Creates an exact Open-Meteo climate model token not yet represented by this enum.
31    ///
32    /// The token is passed through unchanged and is not validated by the crate.
33    pub fn other(token: impl Into<Cow<'static, str>>) -> Self {
34        Self::Other(token.into())
35    }
36}
37
38impl AsApiStr for ClimateModel {
39    fn as_api_str(&self) -> Cow<'static, str> {
40        match self {
41            Self::CmccCm2Vhr4 => Cow::Borrowed("CMCC_CM2_VHR4"),
42            Self::EcEarth3PHr => Cow::Borrowed("EC_Earth3P_HR"),
43            Self::FgoalsF3H => Cow::Borrowed("FGOALS_f3_H"),
44            Self::HiramSitHr => Cow::Borrowed("HiRAM_SIT_HR"),
45            Self::MpiEsm12Xr => Cow::Borrowed("MPI_ESM1_2_XR"),
46            Self::MriAgcm32S => Cow::Borrowed("MRI_AGCM3_2_S"),
47            Self::Nicam168S => Cow::Borrowed("NICAM16_8S"),
48            Self::Other(token) => token.clone(),
49        }
50    }
51}
52
53/// Daily variables for climate projection requests.
54#[derive(Debug, Clone, PartialEq, Eq, Hash)]
55#[non_exhaustive]
56pub enum ClimateDailyVar {
57    /// Maximum 2 m temperature.
58    Temperature2mMax,
59    /// Minimum 2 m temperature.
60    Temperature2mMin,
61    /// Mean 2 m temperature.
62    Temperature2mMean,
63    /// Mean cloud cover.
64    CloudCoverMean,
65    /// Maximum 2 m relative humidity.
66    RelativeHumidity2mMax,
67    /// Minimum 2 m relative humidity.
68    RelativeHumidity2mMin,
69    /// Mean 2 m relative humidity.
70    RelativeHumidity2mMean,
71    /// Mean soil moisture fraction in the 0-10 cm layer.
72    SoilMoisture0To10cmMean,
73    /// Daily precipitation sum.
74    PrecipitationSum,
75    /// Daily rain sum.
76    RainSum,
77    /// Daily snowfall sum.
78    SnowfallSum,
79    /// Mean 10 m wind speed.
80    WindSpeed10mMean,
81    /// Maximum 10 m wind speed.
82    WindSpeed10mMax,
83    /// Mean sea-level pressure.
84    PressureMslMean,
85    /// Daily shortwave radiation sum.
86    ShortwaveRadiationSum,
87    /// Exact Open-Meteo climate daily variable token not yet represented by this enum.
88    ///
89    /// The token is passed through unchanged and is not validated by the crate.
90    Other(Cow<'static, str>),
91}
92
93impl ClimateDailyVar {
94    /// Creates an exact Open-Meteo climate daily variable token not yet represented by this enum.
95    ///
96    /// The token is passed through unchanged and is not validated by the crate.
97    pub fn other(token: impl Into<Cow<'static, str>>) -> Self {
98        Self::Other(token.into())
99    }
100}
101
102impl AsApiStr for ClimateDailyVar {
103    fn as_api_str(&self) -> Cow<'static, str> {
104        match self {
105            Self::Temperature2mMax => Cow::Borrowed("temperature_2m_max"),
106            Self::Temperature2mMin => Cow::Borrowed("temperature_2m_min"),
107            Self::Temperature2mMean => Cow::Borrowed("temperature_2m_mean"),
108            Self::CloudCoverMean => Cow::Borrowed("cloud_cover_mean"),
109            Self::RelativeHumidity2mMax => Cow::Borrowed("relative_humidity_2m_max"),
110            Self::RelativeHumidity2mMin => Cow::Borrowed("relative_humidity_2m_min"),
111            Self::RelativeHumidity2mMean => Cow::Borrowed("relative_humidity_2m_mean"),
112            Self::SoilMoisture0To10cmMean => Cow::Borrowed("soil_moisture_0_to_10cm_mean"),
113            Self::PrecipitationSum => Cow::Borrowed("precipitation_sum"),
114            Self::RainSum => Cow::Borrowed("rain_sum"),
115            Self::SnowfallSum => Cow::Borrowed("snowfall_sum"),
116            Self::WindSpeed10mMean => Cow::Borrowed("wind_speed_10m_mean"),
117            Self::WindSpeed10mMax => Cow::Borrowed("wind_speed_10m_max"),
118            Self::PressureMslMean => Cow::Borrowed("pressure_msl_mean"),
119            Self::ShortwaveRadiationSum => Cow::Borrowed("shortwave_radiation_sum"),
120            Self::Other(token) => token.clone(),
121        }
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn climate_tokens_are_formatted() {
131        assert_eq!(ClimateModel::CmccCm2Vhr4.as_api_str(), "CMCC_CM2_VHR4");
132        assert_eq!(ClimateModel::EcEarth3PHr.as_api_str(), "EC_Earth3P_HR");
133        assert_eq!(ClimateModel::FgoalsF3H.as_api_str(), "FGOALS_f3_H");
134        assert_eq!(ClimateModel::HiramSitHr.as_api_str(), "HiRAM_SIT_HR");
135        assert_eq!(ClimateModel::MpiEsm12Xr.as_api_str(), "MPI_ESM1_2_XR");
136        assert_eq!(ClimateModel::MriAgcm32S.as_api_str(), "MRI_AGCM3_2_S");
137        assert_eq!(ClimateModel::Nicam168S.as_api_str(), "NICAM16_8S");
138        assert_eq!(
139            ClimateModel::other("custom_climate_model").as_api_str(),
140            "custom_climate_model"
141        );
142        assert_eq!(
143            ClimateDailyVar::SoilMoisture0To10cmMean.as_api_str(),
144            "soil_moisture_0_to_10cm_mean"
145        );
146        assert_eq!(
147            ClimateDailyVar::other("custom_daily_var").as_api_str(),
148            "custom_daily_var"
149        );
150    }
151}