From: Scott Worley <scottworley@scottworley.com>
Date: Mon, 19 Aug 2024 18:17:26 +0000 (-0700)
Subject: Don't count multiple entries in a single row in column counts
X-Git-Tag: v0.2.0~15
X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/397ef957e293901c2945f815e585474608fe2c9d

Don't count multiple entries in a single row in column counts
---

diff --git a/src/lib.rs b/src/lib.rs
index e90f66b..c7271f3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
 #[cfg(test)]
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 #[cfg(test)]
 use std::io::BufRead;
 #[cfg(test)]
@@ -71,7 +71,7 @@ fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput,
 #[cfg(test)]
 fn column_counts(rows: &[RowInput]) -> HashMap<String, usize> {
     rows.iter()
-        .flat_map(|r| r.entries.iter())
+        .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter())
         .fold(HashMap::new(), |mut counts, e| {
             counts
                 .entry(String::from(e))
@@ -201,5 +201,13 @@ mod tests {
             ),
             HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)])
         );
+        assert_eq!(
+            column_counts(
+                &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
+                    .collect::<Result<Vec<_>, _>>()
+                    .unwrap()
+            ),
+            HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)])
+        );
     }
 }