From ecaf30c889ac772f0f1f9aedd7d80f01e40a840a Mon Sep 17 00:00:00 2001 From: Isto Nikula Date: Thu, 10 Jul 2025 16:36:19 +0300 Subject: [PATCH] cleanup summarize_durations --- src/lib.rs | 24 +++++++++--------------- src/main.rs | 6 ------ 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 10358a5..ffac067 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,23 +104,17 @@ fn process_line( pub fn summarize_durations( durations_by_tag: &HashMap>, ) -> (Vec<(String, Duration)>, Duration) { - let mut durations_by_tag: Vec<_> = durations_by_tag.iter().collect(); - durations_by_tag.sort_by(|a, b| a.0.cmp(b.0)); - - let mut duration_total = Duration::minutes(0); - let mut summary = Vec::new(); - for (tag, durations) in durations_by_tag { - let mut duration = Duration::minutes(0); - for d in durations { - duration = duration + *d; - } - duration_total = duration_total + duration; - summary.push((tag.clone(), duration)); - } - (summary, duration_total) -} + let mut summary: Vec<(String, Duration)> = durations_by_tag + .iter() + .map(|(tag, durations)| (tag.clone(), durations.iter().sum())) + .collect(); + summary.sort_by(|a, b| a.0.cmp(&b.0)); + let total = summary.iter().map(|(_, d)| *d).sum(); + + (summary, total) +} #[cfg(test)] mod tests { diff --git a/src/main.rs b/src/main.rs index beaddad..f76f65e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,3 @@ fn main() -> Result<()> { Ok(()) } - - - - - -