1 use std::collections::{HashMap, HashSet};
3 use std::iter::Iterator;
5 #[derive(Debug, PartialEq, Eq)]
11 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
12 input: std::iter::Enumerate<Input>,
13 row: Option<RowInput>,
15 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
16 fn new(input: Input) -> Self {
18 input: input.enumerate(),
23 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
24 type Item = Result<RowInput, std::io::Error>;
25 fn next(&mut self) -> Option<Self::Item> {
30 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
32 None => return Ok(std::mem::take(&mut self.row)).transpose(),
33 Some((_, Err(e))) => return Some(Err(e)),
34 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
35 return Ok(std::mem::take(&mut self.row)).transpose()
37 Some((_, Ok(line))) if line.is_empty() => {}
38 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
40 return Some(Err(std::io::Error::other(format!(
41 "{}: Entry with no header",
45 Some(ref mut row) => row.entries.push(String::from(line.trim())),
47 Some((_, Ok(line))) => {
48 let prev = std::mem::take(&mut self.row);
49 self.row = Some(RowInput {
54 return Ok(prev).transpose();
62 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
63 Reader::new(std::io::BufReader::new(input).lines())
66 fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
67 let mut counts: Vec<_> = rows
69 .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter())
70 .fold(HashMap::new(), |mut cs, e| {
71 cs.entry(String::from(e))
72 .and_modify(|n| *n += 1)
77 .map(|(col, n)| (n, col))
85 /// Will return `Err` if
86 /// * there's an i/o error while reading `input`
87 /// * the log has invalid syntax:
88 /// * an indented line with no preceding non-indented line
89 pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> {
90 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
91 let _columns = column_counts(&rows);
92 Ok(String::from("Hello, world!"))
100 fn test_read_rows() {
102 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
104 label: String::from("foo"),
109 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
111 label: String::from("bar"),
116 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
119 label: String::from("foo"),
123 label: String::from("bar"),
129 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
131 label: String::from("foo"),
132 entries: vec![String::from("bar")]
136 read_rows(&b"foo\n bar\n baz\n"[..])
138 .collect::<Vec<_>>(),
140 label: String::from("foo"),
141 entries: vec![String::from("bar"), String::from("baz")]
145 read_rows(&b"foo\n\nbar\n"[..])
147 .collect::<Vec<_>>(),
150 label: String::from("foo"),
154 label: String::from("bar"),
160 read_rows(&b"foo\n \nbar\n"[..])
162 .collect::<Vec<_>>(),
165 label: String::from("foo"),
169 label: String::from("bar"),
175 read_rows(&b"foo \n bar \n"[..])
177 .collect::<Vec<_>>(),
179 label: String::from("foo"),
180 entries: vec![String::from("bar")]
184 let bad = read_rows(&b" foo"[..]).next().unwrap();
185 assert!(bad.is_err());
186 assert!(format!("{bad:?}").contains("1: Entry with no header"));
188 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
189 assert!(bad2.is_err());
190 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
194 fn test_column_counts() {
197 &read_rows(&b"foo\n bar\n baz\n"[..])
198 .collect::<Result<Vec<_>, _>>()
201 vec![(1, String::from("bar")), (1, String::from("baz"))]
205 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
206 .collect::<Result<Vec<_>, _>>()
209 vec![(1, String::from("bar")), (2, String::from("baz"))]
213 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
214 .collect::<Result<Vec<_>, _>>()
217 vec![(1, String::from("bar")), (2, String::from("baz"))]