From 397ef957e293901c2945f815e585474608fe2c9d Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Mon, 19 Aug 2024 11:17:26 -0700 Subject: [PATCH] Don't count multiple entries in a single row in column counts --- src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 HashMap { rows.iter() - .flat_map(|r| r.entries.iter()) + .flat_map(|r| r.entries.iter().collect::>().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::, _>>() + .unwrap() + ), + HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)]) + ); } } -- 2.44.1