From 06ed433e15521d01251aa03e89efdd3c6d684d19 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Tue, 22 Jun 2021 11:27:50 -0500 Subject: [PATCH] use itertools to group entries by sheet --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/formatters.rs | 19 ++++++++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index caf57fc..31560dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,6 +130,12 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -180,6 +186,15 @@ dependencies = [ "libc", ] +[[package]] +name = "itertools" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +dependencies = [ + "either", +] + [[package]] name = "libc" version = "0.2.81" @@ -404,6 +419,7 @@ dependencies = [ "chrono", "clap", "dirs", + "itertools", "pretty_assertions", "rusqlite", "serde", diff --git a/Cargo.toml b/Cargo.toml index b16d6d4..7ae8f1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ dirs = "*" serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.8" toml = "0.5" +itertools = "0.10" [dev-dependencies] pretty_assertions = "0.7.2" diff --git a/src/formatters.rs b/src/formatters.rs index 71307db..2894540 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -2,6 +2,7 @@ use std::str::FromStr; use std::io::Write; use serde::{Serialize, Deserialize}; +use itertools::Itertools; use crate::error; use crate::models::Entry; @@ -16,17 +17,25 @@ pub enum Formatter { impl Formatter { pub fn print_formatted(&self, entries: Vec, out: &mut W) -> error::Result<()> { match &self { - Formatter::Text => { - for entry in entries { - writeln!(out, "{:?}", entry)?; - } - } + Formatter::Text => self.print_formatted_text(entries, out)?, Formatter::Custom(name) => { } } Ok(()) } + + /// Print in the default text format. Assume entries are sorted by sheet and + /// then by start + fn print_formatted_text(&self, entries: Vec, out: &mut W) -> error::Result<()> { + let grouped_entries = entries.into_iter().group_by(|e| e.sheet.to_string()); + + for (key, group) in grouped_entries.into_iter() { + writeln!(out, "Timesheet: {}", key)?; + } + + Ok(()) + } } impl FromStr for Formatter {