2 use std::collections::HashMap;
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]) -> HashMap<String, usize> {
74 .flat_map(|r| r.entries.iter())
75 .fold(HashMap::new(), |mut counts, e| {
77 .entry(String::from(e))
78 .and_modify(|c| *c += 1)
84 pub fn tablify(_input: &impl std::io::Read) -> String {
85 String::from("Hello, world!")
95 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
97 label: String::from("foo"),
102 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
104 label: String::from("bar"),
109 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
112 label: String::from("foo"),
116 label: String::from("bar"),
122 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
124 label: String::from("foo"),
125 entries: vec![String::from("bar")]
129 read_rows(&b"foo\n bar\n baz\n"[..])
131 .collect::<Vec<_>>(),
133 label: String::from("foo"),
134 entries: vec![String::from("bar"), String::from("baz")]
138 read_rows(&b"foo\n\nbar\n"[..])
140 .collect::<Vec<_>>(),
143 label: String::from("foo"),
147 label: String::from("bar"),
153 read_rows(&b"foo\n \nbar\n"[..])
155 .collect::<Vec<_>>(),
158 label: String::from("foo"),
162 label: String::from("bar"),
168 read_rows(&b"foo \n bar \n"[..])
170 .collect::<Vec<_>>(),
172 label: String::from("foo"),
173 entries: vec![String::from("bar")]
177 let bad = read_rows(&b" foo"[..]).next().unwrap();
178 assert!(bad.is_err());
179 assert!(format!("{bad:?}").contains("1: Entry with no header"));
181 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
182 assert!(bad2.is_err());
183 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
187 fn test_column_counts() {
190 &read_rows(&b"foo\n bar\n baz\n"[..])
191 .collect::<Result<Vec<_>, _>>()
194 HashMap::from([(String::from("bar"), 1), (String::from("baz"), 1)])
198 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
199 .collect::<Result<Vec<_>, _>>()
202 HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)])