]>
Commit | Line | Data |
---|---|---|
1 | use std::collections::{HashMap, HashSet}; | |
2 | use std::io::BufRead; | |
3 | use std::iter::Iterator; | |
4 | ||
5 | const HEADER: &str = "<!DOCTYPE html> | |
6 | <html> | |
7 | <head> | |
8 | <meta charset=\"utf-8\"> | |
9 | <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> | |
10 | <style> | |
11 | /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */ | |
12 | th, td { white-space: nowrap; } | |
13 | th { text-align: left; font-weight: normal; } | |
14 | table { border-collapse: collapse } | |
15 | tr.key > th { height: 8em; vertical-align: bottom; line-height: 1 } | |
16 | tr.key > th > div { width: 1em; } | |
17 | tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) } | |
18 | td { border: thin solid gray; } | |
19 | td.numeric { text-align: right; } | |
20 | td.yes { border: thin solid gray; background-color: gray; } | |
21 | td.spacer { border: none; } | |
22 | /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */ | |
23 | .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; } | |
24 | img { height: 1.2em; } | |
25 | </style> | |
26 | <script> | |
27 | function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( \"highlight\"); } } | |
28 | function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } } | |
29 | function h2(a, b) { highlight(a); highlight(b); } | |
30 | function ch2(a, b) { clear_highlight(a); clear_highlight(b); } | |
31 | </script> | |
32 | </head> | |
33 | <body> | |
34 | <table> | |
35 | <tbody>"; | |
36 | const FOOTER: &str = " </tbody> | |
37 | </table> | |
38 | </body> | |
39 | </html>"; | |
40 | ||
41 | #[derive(Debug, PartialEq, Eq)] | |
42 | struct RowInput { | |
43 | label: String, | |
44 | entries: Vec<String>, | |
45 | } | |
46 | ||
47 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { | |
48 | input: std::iter::Enumerate<Input>, | |
49 | row: Option<RowInput>, | |
50 | } | |
51 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> { | |
52 | fn new(input: Input) -> Self { | |
53 | Self { | |
54 | input: input.enumerate(), | |
55 | row: None, | |
56 | } | |
57 | } | |
58 | } | |
59 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> { | |
60 | type Item = Result<RowInput, std::io::Error>; | |
61 | fn next(&mut self) -> Option<Self::Item> { | |
62 | loop { | |
63 | match self | |
64 | .input | |
65 | .next() | |
66 | .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end())))) | |
67 | { | |
68 | None => return Ok(std::mem::take(&mut self.row)).transpose(), | |
69 | Some((_, Err(e))) => return Some(Err(e)), | |
70 | Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => { | |
71 | return Ok(std::mem::take(&mut self.row)).transpose() | |
72 | } | |
73 | Some((_, Ok(line))) if line.is_empty() => {} | |
74 | Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row { | |
75 | None => { | |
76 | return Some(Err(std::io::Error::other(format!( | |
77 | "{}: Entry with no header", | |
78 | n + 1 | |
79 | )))) | |
80 | } | |
81 | Some(ref mut row) => row.entries.push(String::from(line.trim())), | |
82 | }, | |
83 | Some((_, Ok(line))) => { | |
84 | let prev = std::mem::take(&mut self.row); | |
85 | self.row = Some(RowInput { | |
86 | label: line, | |
87 | entries: vec![], | |
88 | }); | |
89 | if prev.is_some() { | |
90 | return Ok(prev).transpose(); | |
91 | } | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> { | |
99 | Reader::new(std::io::BufReader::new(input).lines()) | |
100 | } | |
101 | ||
102 | fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> { | |
103 | let mut counts: Vec<_> = rows | |
104 | .iter() | |
105 | .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter()) | |
106 | .fold(HashMap::new(), |mut cs, e| { | |
107 | cs.entry(String::from(e)) | |
108 | .and_modify(|n| *n += 1) | |
109 | .or_insert(1); | |
110 | cs | |
111 | }) | |
112 | .into_iter() | |
113 | .map(|(col, n)| (n, col)) | |
114 | .collect(); | |
115 | counts.sort(); | |
116 | counts | |
117 | } | |
118 | ||
119 | /// # Errors | |
120 | /// | |
121 | /// Will return `Err` if | |
122 | /// * there's an i/o error while reading `input` | |
123 | /// * the log has invalid syntax: | |
124 | /// * an indented line with no preceding non-indented line | |
125 | pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> { | |
126 | let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?; | |
127 | let _columns = column_counts(&rows); | |
128 | Ok(String::from(HEADER) + "Hello, world!" + FOOTER) | |
129 | } | |
130 | ||
131 | #[cfg(test)] | |
132 | mod tests { | |
133 | use super::*; | |
134 | ||
135 | #[test] | |
136 | fn test_read_rows() { | |
137 | assert_eq!( | |
138 | read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(), | |
139 | vec![RowInput { | |
140 | label: String::from("foo"), | |
141 | entries: vec![] | |
142 | }] | |
143 | ); | |
144 | assert_eq!( | |
145 | read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(), | |
146 | vec![RowInput { | |
147 | label: String::from("bar"), | |
148 | entries: vec![] | |
149 | }] | |
150 | ); | |
151 | assert_eq!( | |
152 | read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(), | |
153 | vec![ | |
154 | RowInput { | |
155 | label: String::from("foo"), | |
156 | entries: vec![] | |
157 | }, | |
158 | RowInput { | |
159 | label: String::from("bar"), | |
160 | entries: vec![] | |
161 | } | |
162 | ] | |
163 | ); | |
164 | assert_eq!( | |
165 | read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(), | |
166 | vec![RowInput { | |
167 | label: String::from("foo"), | |
168 | entries: vec![String::from("bar")] | |
169 | }] | |
170 | ); | |
171 | assert_eq!( | |
172 | read_rows(&b"foo\n bar\n baz\n"[..]) | |
173 | .flatten() | |
174 | .collect::<Vec<_>>(), | |
175 | vec![RowInput { | |
176 | label: String::from("foo"), | |
177 | entries: vec![String::from("bar"), String::from("baz")] | |
178 | }] | |
179 | ); | |
180 | assert_eq!( | |
181 | read_rows(&b"foo\n\nbar\n"[..]) | |
182 | .flatten() | |
183 | .collect::<Vec<_>>(), | |
184 | vec![ | |
185 | RowInput { | |
186 | label: String::from("foo"), | |
187 | entries: vec![] | |
188 | }, | |
189 | RowInput { | |
190 | label: String::from("bar"), | |
191 | entries: vec![] | |
192 | } | |
193 | ] | |
194 | ); | |
195 | assert_eq!( | |
196 | read_rows(&b"foo\n \nbar\n"[..]) | |
197 | .flatten() | |
198 | .collect::<Vec<_>>(), | |
199 | vec![ | |
200 | RowInput { | |
201 | label: String::from("foo"), | |
202 | entries: vec![] | |
203 | }, | |
204 | RowInput { | |
205 | label: String::from("bar"), | |
206 | entries: vec![] | |
207 | } | |
208 | ] | |
209 | ); | |
210 | assert_eq!( | |
211 | read_rows(&b"foo \n bar \n"[..]) | |
212 | .flatten() | |
213 | .collect::<Vec<_>>(), | |
214 | vec![RowInput { | |
215 | label: String::from("foo"), | |
216 | entries: vec![String::from("bar")] | |
217 | }] | |
218 | ); | |
219 | ||
220 | let bad = read_rows(&b" foo"[..]).next().unwrap(); | |
221 | assert!(bad.is_err()); | |
222 | assert!(format!("{bad:?}").contains("1: Entry with no header")); | |
223 | ||
224 | let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap(); | |
225 | assert!(bad2.is_err()); | |
226 | assert!(format!("{bad2:?}").contains("3: Entry with no header")); | |
227 | } | |
228 | ||
229 | #[test] | |
230 | fn test_column_counts() { | |
231 | assert_eq!( | |
232 | column_counts( | |
233 | &read_rows(&b"foo\n bar\n baz\n"[..]) | |
234 | .collect::<Result<Vec<_>, _>>() | |
235 | .unwrap() | |
236 | ), | |
237 | vec![(1, String::from("bar")), (1, String::from("baz"))] | |
238 | ); | |
239 | assert_eq!( | |
240 | column_counts( | |
241 | &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]) | |
242 | .collect::<Result<Vec<_>, _>>() | |
243 | .unwrap() | |
244 | ), | |
245 | vec![(1, String::from("bar")), (2, String::from("baz"))] | |
246 | ); | |
247 | assert_eq!( | |
248 | column_counts( | |
249 | &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]) | |
250 | .collect::<Result<Vec<_>, _>>() | |
251 | .unwrap() | |
252 | ), | |
253 | vec![(1, String::from("bar")), (2, String::from("baz"))] | |
254 | ); | |
255 | } | |
256 | } |