openmeteo-rs 1.0.0

Rust client for the Open-Meteo weather API.
Documentation
use std::borrow::Cow;

use crate::query::AsApiStr;

/// Hourly variables for satellite radiation requests.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SatelliteRadiationHourlyVar {
    /// Shortwave solar radiation.
    ShortwaveRadiation,
    /// Direct solar radiation.
    DirectRadiation,
    /// Diffuse solar radiation.
    DiffuseRadiation,
    /// Direct normal irradiance.
    DirectNormalIrradiance,
    /// Global tilted irradiance.
    GlobalTiltedIrradiance,
    /// Clear-sky shortwave radiation.
    ShortwaveRadiationClearSky,
    /// Terrestrial radiation.
    TerrestrialRadiation,
    /// Instantaneous shortwave solar radiation.
    ShortwaveRadiationInstant,
    /// Instantaneous direct solar radiation.
    DirectRadiationInstant,
    /// Instantaneous diffuse solar radiation.
    DiffuseRadiationInstant,
    /// Instantaneous direct normal irradiance.
    DirectNormalIrradianceInstant,
    /// Instantaneous global tilted irradiance.
    GlobalTiltedIrradianceInstant,
    /// Instantaneous terrestrial radiation.
    TerrestrialRadiationInstant,
    /// Exact Open-Meteo satellite-radiation variable token not yet represented by this enum.
    ///
    /// The token is passed through unchanged and is not validated by the crate.
    Other(Cow<'static, str>),
}

impl SatelliteRadiationHourlyVar {
    /// Creates an exact Open-Meteo satellite-radiation variable token not yet represented by this enum.
    ///
    /// The token is passed through unchanged and is not validated by the crate.
    pub fn other(token: impl Into<Cow<'static, str>>) -> Self {
        Self::Other(token.into())
    }
}

impl AsApiStr for SatelliteRadiationHourlyVar {
    fn as_api_str(&self) -> Cow<'static, str> {
        match self {
            Self::ShortwaveRadiation => Cow::Borrowed("shortwave_radiation"),
            Self::DirectRadiation => Cow::Borrowed("direct_radiation"),
            Self::DiffuseRadiation => Cow::Borrowed("diffuse_radiation"),
            Self::DirectNormalIrradiance => Cow::Borrowed("direct_normal_irradiance"),
            Self::GlobalTiltedIrradiance => Cow::Borrowed("global_tilted_irradiance"),
            Self::ShortwaveRadiationClearSky => Cow::Borrowed("shortwave_radiation_clear_sky"),
            Self::TerrestrialRadiation => Cow::Borrowed("terrestrial_radiation"),
            Self::ShortwaveRadiationInstant => Cow::Borrowed("shortwave_radiation_instant"),
            Self::DirectRadiationInstant => Cow::Borrowed("direct_radiation_instant"),
            Self::DiffuseRadiationInstant => Cow::Borrowed("diffuse_radiation_instant"),
            Self::DirectNormalIrradianceInstant => {
                Cow::Borrowed("direct_normal_irradiance_instant")
            }
            Self::GlobalTiltedIrradianceInstant => {
                Cow::Borrowed("global_tilted_irradiance_instant")
            }
            Self::TerrestrialRadiationInstant => Cow::Borrowed("terrestrial_radiation_instant"),
            Self::Other(token) => token.clone(),
        }
    }
}

/// Satellite radiation model selector.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SatelliteRadiationModel {
    /// Open-Meteo seamless satellite radiation model.
    SatelliteRadiationSeamless,
    /// DWD SIS Europe/Africa v4 satellite radiation source.
    DwdSisEuropeAfricaV4,
    /// EUMETSAT LSA SAF MSG satellite radiation source.
    EumetsatLsaSafMsg,
    /// EUMETSAT LSA SAF IODC satellite radiation source.
    EumetsatLsaSafIodc,
    /// EUMETSAT CM SAF SARAH3 satellite radiation source.
    EumetsatSarah3,
    /// JMA/JAXA Himawari satellite radiation source.
    JmaJaxaHimawari,
    /// JMA/JAXA MTG FCI satellite radiation source.
    JmaJaxaMtgFci,
    /// NCEP HGEFS 0.25 degree ensemble-mean comparison model.
    NcepHgefs025EnsembleMean,
    /// Exact Open-Meteo satellite-radiation model token not yet represented by this enum.
    ///
    /// The token is passed through unchanged and is not validated by the crate.
    Other(Cow<'static, str>),
}

impl SatelliteRadiationModel {
    /// Creates an exact Open-Meteo satellite-radiation model token not yet represented by this enum.
    ///
    /// The token is passed through unchanged and is not validated by the crate.
    pub fn other(token: impl Into<Cow<'static, str>>) -> Self {
        Self::Other(token.into())
    }
}

impl AsApiStr for SatelliteRadiationModel {
    fn as_api_str(&self) -> Cow<'static, str> {
        match self {
            Self::SatelliteRadiationSeamless => Cow::Borrowed("satellite_radiation_seamless"),
            Self::DwdSisEuropeAfricaV4 => Cow::Borrowed("dwd_sis_europe_africa_v4"),
            Self::EumetsatLsaSafMsg => Cow::Borrowed("eumetsat_lsa_saf_msg"),
            Self::EumetsatLsaSafIodc => Cow::Borrowed("eumetsat_lsa_saf_iodc"),
            Self::EumetsatSarah3 => Cow::Borrowed("eumetsat_sarah3"),
            Self::JmaJaxaHimawari => Cow::Borrowed("jma_jaxa_himawari"),
            Self::JmaJaxaMtgFci => Cow::Borrowed("jma_jaxa_mtg_fci"),
            Self::NcepHgefs025EnsembleMean => Cow::Borrowed("ncep_hgefs025_ensemble_mean"),
            Self::Other(token) => token.clone(),
        }
    }
}

/// Temporal resolution for satellite radiation data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SatelliteRadiationTemporalResolution {
    /// Native 10/15/30-minute satellite resolution.
    Native,
    /// Interpolated hourly data.
    Hourly,
}

impl AsApiStr for SatelliteRadiationTemporalResolution {
    fn as_api_str(&self) -> Cow<'static, str> {
        Cow::Borrowed(match self {
            Self::Native => "native",
            Self::Hourly => "hourly",
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn satellite_radiation_tokens_are_formatted() {
        assert_eq!(
            SatelliteRadiationHourlyVar::ShortwaveRadiation.as_api_str(),
            "shortwave_radiation"
        );
        assert_eq!(
            SatelliteRadiationHourlyVar::DirectNormalIrradianceInstant.as_api_str(),
            "direct_normal_irradiance_instant"
        );
        assert_eq!(
            SatelliteRadiationHourlyVar::other("custom_satellite_var").as_api_str(),
            "custom_satellite_var"
        );
        assert_eq!(
            SatelliteRadiationModel::SatelliteRadiationSeamless.as_api_str(),
            "satellite_radiation_seamless"
        );
        assert_eq!(
            SatelliteRadiationModel::DwdSisEuropeAfricaV4.as_api_str(),
            "dwd_sis_europe_africa_v4"
        );
        assert_eq!(
            SatelliteRadiationModel::EumetsatLsaSafMsg.as_api_str(),
            "eumetsat_lsa_saf_msg"
        );
        assert_eq!(
            SatelliteRadiationModel::EumetsatLsaSafIodc.as_api_str(),
            "eumetsat_lsa_saf_iodc"
        );
        assert_eq!(
            SatelliteRadiationModel::EumetsatSarah3.as_api_str(),
            "eumetsat_sarah3"
        );
        assert_eq!(
            SatelliteRadiationModel::JmaJaxaHimawari.as_api_str(),
            "jma_jaxa_himawari"
        );
        assert_eq!(
            SatelliteRadiationModel::JmaJaxaMtgFci.as_api_str(),
            "jma_jaxa_mtg_fci"
        );
        assert_eq!(
            SatelliteRadiationModel::NcepHgefs025EnsembleMean.as_api_str(),
            "ncep_hgefs025_ensemble_mean"
        );
        assert_eq!(
            SatelliteRadiationTemporalResolution::Native.as_api_str(),
            "native"
        );
    }
}