openmeteo-rs 1.0.0

Rust client for the Open-Meteo weather API.
Documentation
use time::OffsetDateTime;

use super::{HourlyData, VariableSeries};

/// Lazy hourly row iterator.
#[derive(Debug, Clone)]
pub struct HourlyRowIter<'a> {
    pub(crate) data: &'a HourlyData,
    pub(crate) index: usize,
}

impl<'a> Iterator for HourlyRowIter<'a> {
    type Item = HourlyRow<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.data.time.len() {
            return None;
        }
        let row = HourlyRow {
            data: self.data,
            index: self.index,
        };
        self.index += 1;
        Some(row)
    }
}

/// One lazy row view over hourly column data.
#[derive(Debug, Clone, Copy)]
pub struct HourlyRow<'a> {
    data: &'a HourlyData,
    index: usize,
}

impl HourlyRow<'_> {
    /// Row timestamp.
    pub fn time(&self) -> OffsetDateTime {
        self.data.time[self.index]
    }

    /// 2 m temperature at this row, if present.
    pub fn temperature_2m(&self) -> Option<f32> {
        self.get_f32(crate::HourlyVar::Temperature2m)
    }

    /// Precipitation at this row, if present.
    pub fn precipitation(&self) -> Option<f32> {
        self.get_f32(crate::HourlyVar::Precipitation)
    }

    /// Day/night flag at this row, if present.
    pub fn is_day(&self) -> Option<bool> {
        self.data.is_day_at(self.index)
    }

    /// Numeric value for a typed hourly variable at this row, if present.
    pub fn get_f32(&self, var: crate::HourlyVar) -> Option<f32> {
        self.data
            .get_var(var)
            .and_then(VariableSeries::values_f32)
            .and_then(|values| values.get(self.index).copied().flatten())
    }

    /// String value for a typed hourly variable at this row, if present.
    pub fn get_str(&self, var: crate::HourlyVar) -> Option<&str> {
        self.data
            .get_var(var)
            .and_then(VariableSeries::values_str)
            .and_then(|values| values.get(self.index))
            .and_then(Option::as_deref)
    }
}