]> git.scottworley.com Git - tablify/commitdiff
Don't count multiple entries in a single row in column counts
authorScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 18:17:26 +0000 (11:17 -0700)
committerScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 18:52:01 +0000 (11:52 -0700)
src/lib.rs

index e90f66b37f2359542bad445064f86ea1ee2ec6ad..c7271f3932965c7a2f64712764307af83e1ec593 100644 (file)
@@ -1,5 +1,5 @@
 #[cfg(test)]
 #[cfg(test)]
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
 #[cfg(test)]
 use std::io::BufRead;
 #[cfg(test)]
 #[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()
 #[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))
         .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)])
         );
             ),
             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)])
+        );
     }
 }
     }
 }