2 use std::collections::{HashMap, HashSet};
6 use std::iter::Iterator;
8 #[derive(Debug, PartialEq, Eq)]
14 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
15 input: std::iter::Enumerate<Input>,
16 row: Option<RowInput>,
18 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
20 fn new(input: Input) -> Self {
22 input: input.enumerate(),
27 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
28 type Item = Result<RowInput, std::io::Error>;
29 fn next(&mut self) -> Option<Self::Item> {
34 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
36 None => return Ok(std::mem::take(&mut self.row)).transpose(),
37 Some((_, Err(e))) => return Some(Err(e)),
38 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
39 return Ok(std::mem::take(&mut self.row)).transpose()
41 Some((_, Ok(line))) if line.is_empty() => {}
42 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
44 return Some(Err(std::io::Error::other(format!(
45 "{}: Entry with no header",
49 Some(ref mut row) => row.entries.push(String::from(line.trim())),
51 Some((_, Ok(line))) => {
52 let prev = std::mem::take(&mut self.row);
53 self.row = Some(RowInput {
58 return Ok(prev).transpose();
67 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
68 Reader::new(std::io::BufReader::new(input).lines())
72 fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
73 let mut counts: Vec<_> = rows
75 .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter())
76 .fold(HashMap::new(), |mut cs, e| {
77 cs.entry(String::from(e))
78 .and_modify(|n| *n += 1)
83 .map(|(col, n)| (n, col))
89 pub fn tablify(_input: &impl std::io::Read) -> String {
90 String::from("Hello, world!")
100 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
102 label: String::from("foo"),
107 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
109 label: String::from("bar"),
114 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
117 label: String::from("foo"),
121 label: String::from("bar"),
127 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
129 label: String::from("foo"),
130 entries: vec![String::from("bar")]
134 read_rows(&b"foo\n bar\n baz\n"[..])
136 .collect::<Vec<_>>(),
138 label: String::from("foo"),
139 entries: vec![String::from("bar"), String::from("baz")]
143 read_rows(&b"foo\n\nbar\n"[..])
145 .collect::<Vec<_>>(),
148 label: String::from("foo"),
152 label: String::from("bar"),
158 read_rows(&b"foo\n \nbar\n"[..])
160 .collect::<Vec<_>>(),
163 label: String::from("foo"),
167 label: String::from("bar"),
173 read_rows(&b"foo \n bar \n"[..])
175 .collect::<Vec<_>>(),
177 label: String::from("foo"),
178 entries: vec![String::from("bar")]
182 let bad = read_rows(&b" foo"[..]).next().unwrap();
183 assert!(bad.is_err());
184 assert!(format!("{bad:?}").contains("1: Entry with no header"));
186 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
187 assert!(bad2.is_err());
188 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
192 fn test_column_counts() {
195 &read_rows(&b"foo\n bar\n baz\n"[..])
196 .collect::<Result<Vec<_>, _>>()
199 vec![(1, String::from("bar")), (1, String::from("baz"))]
203 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
204 .collect::<Result<Vec<_>, _>>()
207 vec![(1, String::from("bar")), (2, String::from("baz"))]
211 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
212 .collect::<Result<Vec<_>, _>>()
215 vec![(1, String::from("bar")), (2, String::from("baz"))]