Add YearMonthRange helper to YearMonth module

This commit is contained in:
2025-09-16 22:23:29 +02:00
parent 2a725a1a90
commit 7c93c980bc

View File

@@ -104,3 +104,47 @@ impl PartialOrd for YearMonth {
.is_some_and(std::cmp::Ordering::is_ge) .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<YearMonthRange, &'static str> {
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<Self::Item> {
if self.current > self.stop {
None
} else {
let result = self.current.clone();
self.current = result.get_next_month();
Some(result)
}
}
}