Add YearMonthRange helper to YearMonth module
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user