use chrono::{DateTime, Utc};
use crate::{Window, WindowSource};
pub struct NextWindows<'a, S>
where
S: WindowSource,
{
source: &'a S,
cursor: DateTime<Utc>,
}
impl<'a, S> NextWindows<'a, S>
where
S: WindowSource,
{
pub fn new(source: &'a S, from: DateTime<Utc>) -> Self {
Self {
source,
cursor: from,
}
}
}
impl<'a, S> Iterator for NextWindows<'a, S>
where
S: WindowSource,
{
type Item = Window<S::Meta>;
fn next(&mut self) -> Option<Self::Item> {
let window = self.source.next_window(self.cursor)?;
if window.start <= self.cursor {
return None;
}
self.cursor = window.start;
Some(window)
}
}
pub trait WindowSourceExt: WindowSource {
fn next_windows_from(&self, from: DateTime<Utc>) -> NextWindows<'_, Self>
where
Self: Sized,
{
NextWindows::new(self, from)
}
}
impl<T: WindowSource> WindowSourceExt for T {}