use itertools to group entries by sheet

This commit is contained in:
Abraham Toriz 2021-06-22 11:27:50 -05:00
parent 79afcaed89
commit 06ed433e15
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
3 changed files with 31 additions and 5 deletions

16
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<W: Write>(&self, entries: Vec<Entry>, 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<W: Write>(&self, entries: Vec<Entry>, 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 {