From f5fd93547576f9410146e3910dc5c95ec7747cac Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Wed, 14 May 2025 23:51:36 +0100 Subject: [PATCH] Refactor groups method in DatesList to use a helper function for modularity --- src/utils/dateutils/dates.rs | 59 ++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/utils/dateutils/dates.rs b/src/utils/dateutils/dates.rs index 77946c2..d004cc3 100644 --- a/src/utils/dateutils/dates.rs +++ b/src/utils/dateutils/dates.rs @@ -340,32 +340,7 @@ impl DatesList { /// Returns an error if the start or end date strings cannot be parsed. pub fn groups(&self) -> Result>, Box> { let dates = self.list()?; - let mut groups: HashMap> = HashMap::new(); - - for date in dates { - let key = match self.freq { - DateFreq::Daily => GroupKey::Daily(date), - DateFreq::WeeklyMonday | DateFreq::WeeklyFriday => { - let iso_week = date.iso_week(); - GroupKey::Weekly(iso_week.year(), iso_week.week()) - } - DateFreq::MonthStart | DateFreq::MonthEnd => { - GroupKey::Monthly(date.year(), date.month()) - } - DateFreq::QuarterStart | DateFreq::QuarterEnd => { - GroupKey::Quarterly(date.year(), month_to_quarter(date.month())) - } - DateFreq::YearStart | DateFreq::YearEnd => GroupKey::Yearly(date.year()), - }; - groups.entry(key).or_insert_with(Vec::new).push(date); - } - - let mut sorted_groups: Vec<(GroupKey, Vec)> = groups.into_iter().collect(); - sorted_groups.sort_by(|(k1, _), (k2, _)| k1.cmp(k2)); - - // Dates within groups are already sorted because they came from the sorted `self.list()`. - let result_groups = sorted_groups.into_iter().map(|(_, dates)| dates).collect(); - Ok(result_groups) + group_dates_helper(dates, self.freq) } /// Returns the start date parsed as a `NaiveDate`. @@ -563,6 +538,38 @@ impl Iterator for DatesGenerator { // --- Internal helper functions --- +pub fn group_dates_helper( + dates: Vec, + freq: DateFreq, +) -> Result>, Box> { + let mut groups: HashMap> = HashMap::new(); + + for date in dates { + let key = match freq { + DateFreq::Daily => GroupKey::Daily(date), + DateFreq::WeeklyMonday | DateFreq::WeeklyFriday => { + let iso_week = date.iso_week(); + GroupKey::Weekly(iso_week.year(), iso_week.week()) + } + DateFreq::MonthStart | DateFreq::MonthEnd => { + GroupKey::Monthly(date.year(), date.month()) + } + DateFreq::QuarterStart | DateFreq::QuarterEnd => { + GroupKey::Quarterly(date.year(), month_to_quarter(date.month())) + } + DateFreq::YearStart | DateFreq::YearEnd => GroupKey::Yearly(date.year()), + }; + groups.entry(key).or_insert_with(Vec::new).push(date); + } + + let mut sorted_groups: Vec<(GroupKey, Vec)> = groups.into_iter().collect(); + sorted_groups.sort_by(|(k1, _), (k2, _)| k1.cmp(k2)); + + // Dates within groups are already sorted because they came from the sorted `self.list()`. + let result_groups = sorted_groups.into_iter().map(|(_, dates)| dates).collect(); + Ok(result_groups) +} + /// Generates the flat list of dates for the given range and frequency. /// Assumes the `collect_*` functions return sorted dates. pub fn get_dates_list_with_freq(