diff --git a/src/shared_api_lib/src/year_month.rs b/src/shared_api_lib/src/year_month.rs index 820a8b0..da276f0 100644 --- a/src/shared_api_lib/src/year_month.rs +++ b/src/shared_api_lib/src/year_month.rs @@ -104,3 +104,47 @@ impl PartialOrd for YearMonth { .is_some_and(std::cmp::Ordering::is_ge) } } + +// Bounds are inclusive +pub struct YearMonthRange { + start: YearMonth, + current: YearMonth, + stop: YearMonth, +} + +impl YearMonthRange { + // Bounds are inclusive + pub fn new(start: &YearMonth, stop: &YearMonth) -> Result { + if *start > *stop { + return Err("Start cannot be greater than stop"); + } + + return Ok(YearMonthRange { + start: start.clone(), + current: start.clone(), + stop: stop.clone(), + }); + } + + pub fn start(&self) -> YearMonth { + self.start.clone() + } + + pub fn stop(&self) -> YearMonth { + self.stop.clone() + } +} + +impl Iterator for YearMonthRange { + type Item = YearMonth; + + fn next(&mut self) -> Option { + if self.current > self.stop { + None + } else { + let result = self.current.clone(); + self.current = result.get_next_month(); + Some(result) + } + } +}