Skip to content

Commit

Permalink
refactor: replace DateRange iterator with while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 9, 2024
1 parent 41d9b27 commit edcc69d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/events/ics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// SPDX-License-Identifier: MIT

use crate::events::{Event, EventDateTime, Events};
use crate::utils::DateRange;
use chrono::Duration;
use icalendar::{Calendar, CalendarDateTime, Component, DatePerhapsTime, Event as IcalendarEvent};
use rrule::RRuleSet;
use std::cmp::max;
use std::path::{Path, PathBuf};

impl From<icalendar::DatePerhapsTime> for EventDateTime {
Expand Down Expand Up @@ -47,14 +48,16 @@ impl TryFrom<&IcalendarEvent> for Event {
};
let mut rrulesets: Vec<RRuleSet> = vec![];
if !rrulestring.is_empty() {
for date in DateRange(start.date(), end.date()) {
let mut date = start.date();
while date < max(start.date() + Duration::days(1), end.date()) {
let rrule = format!(
"DTSTART;VALUE=DATE:{}\n{rrulestring}",
date.format("%Y%m%d")
);
if let Ok(x) = rrule.parse() {
rrulesets.push(x);
}
date += Duration::days(1);
}
}
Ok(Event {
Expand Down
15 changes: 0 additions & 15 deletions src/utils/date_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,6 @@ impl Iterator for MonthFullWeeksIter {
}
}

pub struct DateRange(pub NaiveDate, pub NaiveDate);

impl Iterator for DateRange {
type Item = NaiveDate;

fn next(&mut self) -> Option<Self::Item> {
if self.0 < self.1 {
self.0 += Duration::days(1);
Some(self.0)
} else {
None
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
mod date_extensions;
mod helpers;

pub use date_extensions::{DateExtensions, DateRange, MonthFullWeeksIter};
pub use date_extensions::{DateExtensions, MonthFullWeeksIter};
pub use helpers::convertstyle;

0 comments on commit edcc69d

Please sign in to comment.