]>
Commit | Line | Data |
---|---|---|
397ef957 | 1 | use std::collections::{HashMap, HashSet}; |
7067975b | 2 | use std::fmt::Write; |
9dfa98b7 | 3 | use std::io::BufRead; |
75bb888a SW |
4 | use std::iter::Iterator; |
5 | ||
cc2378d5 SW |
6 | const HEADER: &str = "<!DOCTYPE html> |
7 | <html> | |
8 | <head> | |
9 | <meta charset=\"utf-8\"> | |
10 | <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> | |
11 | <style> | |
b8b365ce | 12 | td { text-align: center; } |
cc2378d5 SW |
13 | /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */ |
14 | th, td { white-space: nowrap; } | |
15 | th { text-align: left; font-weight: normal; } | |
16 | table { border-collapse: collapse } | |
3bc643e9 | 17 | tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 } |
cc2378d5 SW |
18 | tr.key > th > div { width: 1em; } |
19 | tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) } | |
20 | td { border: thin solid gray; } | |
1dda21e6 | 21 | td.yes { border: thin solid gray; background-color: #ddd; } |
cc2378d5 SW |
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; } | |
cc2378d5 SW |
24 | </style> |
25 | <script> | |
26 | function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( \"highlight\"); } } | |
27 | function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } } | |
28 | function h2(a, b) { highlight(a); highlight(b); } | |
29 | function ch2(a, b) { clear_highlight(a); clear_highlight(b); } | |
30 | </script> | |
31 | </head> | |
32 | <body> | |
33 | <table> | |
76638ea1 SW |
34 | <tbody> |
35 | "; | |
cc2378d5 SW |
36 | const FOOTER: &str = " </tbody> |
37 | </table> | |
38 | </body> | |
39 | </html>"; | |
40 | ||
14e9852b | 41 | #[derive(Debug, PartialEq, Eq, Hash)] |
e8657dff SW |
42 | struct Entry { |
43 | col: String, | |
b8907770 | 44 | instance: Option<String>, |
e8657dff SW |
45 | } |
46 | impl From<&str> for Entry { | |
47 | fn from(value: &str) -> Entry { | |
b8907770 SW |
48 | match value.split_once(':') { |
49 | None => Entry { | |
50 | col: String::from(value), | |
51 | instance: None, | |
52 | }, | |
53 | Some((col, instance)) => Entry { | |
0d999bc3 SW |
54 | col: String::from(col.trim()), |
55 | instance: Some(String::from(instance.trim())), | |
b8907770 | 56 | }, |
e8657dff SW |
57 | } |
58 | } | |
59 | } | |
14e9852b | 60 | |
75bb888a SW |
61 | #[derive(Debug, PartialEq, Eq)] |
62 | struct RowInput { | |
63 | label: String, | |
14e9852b | 64 | entries: Vec<Entry>, |
75bb888a SW |
65 | } |
66 | ||
201b9ef3 | 67 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { |
8110b492 | 68 | input: std::iter::Enumerate<Input>, |
201b9ef3 SW |
69 | row: Option<RowInput>, |
70 | } | |
71 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> { | |
201b9ef3 | 72 | fn new(input: Input) -> Self { |
8110b492 SW |
73 | Self { |
74 | input: input.enumerate(), | |
75 | row: None, | |
76 | } | |
201b9ef3 SW |
77 | } |
78 | } | |
79 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> { | |
80 | type Item = Result<RowInput, std::io::Error>; | |
81 | fn next(&mut self) -> Option<Self::Item> { | |
82 | loop { | |
1f6bd845 SW |
83 | match self |
84 | .input | |
85 | .next() | |
8110b492 | 86 | .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end())))) |
1f6bd845 | 87 | { |
201b9ef3 | 88 | None => return Ok(std::mem::take(&mut self.row)).transpose(), |
8110b492 SW |
89 | Some((_, Err(e))) => return Some(Err(e)), |
90 | Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => { | |
201b9ef3 SW |
91 | return Ok(std::mem::take(&mut self.row)).transpose() |
92 | } | |
8110b492 SW |
93 | Some((_, Ok(line))) if line.is_empty() => {} |
94 | Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row { | |
95 | None => { | |
96 | return Some(Err(std::io::Error::other(format!( | |
97 | "{}: Entry with no header", | |
98 | n + 1 | |
99 | )))) | |
100 | } | |
e8657dff | 101 | Some(ref mut row) => row.entries.push(Entry::from(line.trim())), |
201b9ef3 | 102 | }, |
8110b492 | 103 | Some((_, Ok(line))) => { |
201b9ef3 SW |
104 | let prev = std::mem::take(&mut self.row); |
105 | self.row = Some(RowInput { | |
106 | label: line, | |
107 | entries: vec![], | |
108 | }); | |
109 | if prev.is_some() { | |
110 | return Ok(prev).transpose(); | |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
201b9ef3 SW |
118 | fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> { |
119 | Reader::new(std::io::BufReader::new(input).lines()) | |
75bb888a SW |
120 | } |
121 | ||
58b5f36d SW |
122 | fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> { |
123 | let mut counts: Vec<_> = rows | |
124 | .iter() | |
b8907770 SW |
125 | .flat_map(|r| { |
126 | r.entries | |
127 | .iter() | |
128 | .map(|e| &e.col) | |
129 | .collect::<HashSet<_>>() | |
130 | .into_iter() | |
131 | }) | |
132 | .fold(HashMap::new(), |mut cs, col| { | |
133 | cs.entry(String::from(col)) | |
58b5f36d | 134 | .and_modify(|n| *n += 1) |
f272e502 | 135 | .or_insert(1); |
58b5f36d | 136 | cs |
f272e502 | 137 | }) |
58b5f36d SW |
138 | .into_iter() |
139 | .map(|(col, n)| (n, col)) | |
140 | .collect(); | |
38d1167a | 141 | counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol))); |
58b5f36d | 142 | counts |
f272e502 | 143 | } |
d22b2e05 SW |
144 | fn column_order(rows: &[RowInput]) -> Vec<String> { |
145 | column_counts(rows) | |
146 | .into_iter() | |
147 | .map(|(_, col)| col) | |
148 | .collect() | |
149 | } | |
f272e502 | 150 | |
de408c29 SW |
151 | fn render_instance(entry: &Entry) -> String { |
152 | match &entry.instance { | |
153 | None => String::from("✓ "), | |
154 | Some(instance) => String::from(instance) + " ", | |
155 | } | |
156 | } | |
157 | ||
158 | fn render_cell(col: &str, row: &RowInput) -> String { | |
159 | // TODO: Escape HTML special characters | |
92476edc | 160 | let row_label = &row.label; |
de408c29 SW |
161 | let entries: Vec<&Entry> = row.entries.iter().filter(|e| e.col == col).collect(); |
162 | let class = if entries.is_empty() { "" } else { "yes" }; | |
163 | let all_empty = entries.iter().all(|e| e.instance.is_none()); | |
164 | let contents = if entries.is_empty() || (all_empty && entries.len() == 1) { | |
165 | String::new() | |
166 | } else if all_empty { | |
167 | format!("{}", entries.len()) | |
168 | } else { | |
169 | entries | |
170 | .iter() | |
171 | .map(|i| render_instance(i)) | |
172 | .collect::<String>() | |
173 | }; | |
92476edc | 174 | format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col}')\" onmouseout=\"ch2('{row_label}','{col}')\">{}</td>", contents.trim()) |
de408c29 SW |
175 | } |
176 | ||
177 | fn render_row(columns: &[String], row: &RowInput) -> String { | |
178 | // This is O(n^2) & doesn't need to be | |
179 | // TODO: Escape HTML special characters | |
92476edc | 180 | let row_label = &row.label; |
de408c29 | 181 | format!( |
92476edc | 182 | "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n", |
de408c29 SW |
183 | &columns |
184 | .iter() | |
185 | .map(|col| render_cell(col, row)) | |
186 | .collect::<String>() | |
187 | ) | |
188 | } | |
189 | ||
76638ea1 SW |
190 | fn render_column_headers(columns: &[String]) -> String { |
191 | // TODO: Escape HTML special characters | |
92476edc | 192 | String::from("<tr class=\"key\"><th></th>") |
7067975b | 193 | + &columns.iter().fold(String::new(), |mut acc, c| { |
92476edc | 194 | write!(&mut acc, "<th id=\"{c}\"><div><div>{c}</div></div></th>").unwrap(); |
7067975b SW |
195 | acc |
196 | }) | |
92476edc | 197 | + "</tr>\n" |
76638ea1 SW |
198 | } |
199 | ||
4b99fb70 SW |
200 | /// # Errors |
201 | /// | |
202 | /// Will return `Err` if | |
203 | /// * there's an i/o error while reading `input` | |
204 | /// * the log has invalid syntax: | |
205 | /// * an indented line with no preceding non-indented line | |
206 | pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> { | |
207 | let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?; | |
de408c29 SW |
208 | let columns = column_order(&rows); |
209 | Ok(String::from(HEADER) | |
76638ea1 | 210 | + &render_column_headers(&columns) |
de408c29 SW |
211 | + &rows |
212 | .into_iter() | |
213 | .map(|r| render_row(&columns, &r)) | |
214 | .collect::<String>() | |
215 | + FOOTER) | |
ece97615 | 216 | } |
75bb888a SW |
217 | |
218 | #[cfg(test)] | |
219 | mod tests { | |
220 | use super::*; | |
221 | ||
b8907770 SW |
222 | #[test] |
223 | fn test_parse_entry() { | |
224 | assert_eq!( | |
225 | Entry::from("foo"), | |
226 | Entry { | |
227 | col: String::from("foo"), | |
228 | instance: None | |
229 | } | |
230 | ); | |
231 | assert_eq!( | |
232 | Entry::from("foo:bar"), | |
233 | Entry { | |
234 | col: String::from("foo"), | |
235 | instance: Some(String::from("bar")) | |
236 | } | |
237 | ); | |
0d999bc3 SW |
238 | assert_eq!( |
239 | Entry::from("foo: bar"), | |
240 | Entry { | |
241 | col: String::from("foo"), | |
242 | instance: Some(String::from("bar")) | |
243 | } | |
244 | ); | |
b8907770 SW |
245 | } |
246 | ||
75bb888a SW |
247 | #[test] |
248 | fn test_read_rows() { | |
249 | assert_eq!( | |
201b9ef3 | 250 | read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(), |
75bb888a SW |
251 | vec![RowInput { |
252 | label: String::from("foo"), | |
253 | entries: vec![] | |
254 | }] | |
255 | ); | |
9dfa98b7 | 256 | assert_eq!( |
201b9ef3 | 257 | read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(), |
9dfa98b7 SW |
258 | vec![RowInput { |
259 | label: String::from("bar"), | |
260 | entries: vec![] | |
261 | }] | |
262 | ); | |
2aa9ef94 | 263 | assert_eq!( |
201b9ef3 | 264 | read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(), |
2aa9ef94 SW |
265 | vec![ |
266 | RowInput { | |
267 | label: String::from("foo"), | |
268 | entries: vec![] | |
269 | }, | |
270 | RowInput { | |
271 | label: String::from("bar"), | |
272 | entries: vec![] | |
273 | } | |
274 | ] | |
275 | ); | |
201b9ef3 SW |
276 | assert_eq!( |
277 | read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(), | |
278 | vec![RowInput { | |
279 | label: String::from("foo"), | |
e8657dff | 280 | entries: vec![Entry::from("bar")] |
201b9ef3 SW |
281 | }] |
282 | ); | |
283 | assert_eq!( | |
284 | read_rows(&b"foo\n bar\n baz\n"[..]) | |
285 | .flatten() | |
286 | .collect::<Vec<_>>(), | |
287 | vec![RowInput { | |
288 | label: String::from("foo"), | |
e8657dff | 289 | entries: vec![Entry::from("bar"), Entry::from("baz")] |
201b9ef3 SW |
290 | }] |
291 | ); | |
292 | assert_eq!( | |
293 | read_rows(&b"foo\n\nbar\n"[..]) | |
294 | .flatten() | |
295 | .collect::<Vec<_>>(), | |
296 | vec![ | |
297 | RowInput { | |
298 | label: String::from("foo"), | |
299 | entries: vec![] | |
300 | }, | |
301 | RowInput { | |
302 | label: String::from("bar"), | |
303 | entries: vec![] | |
304 | } | |
305 | ] | |
306 | ); | |
1f6bd845 SW |
307 | assert_eq!( |
308 | read_rows(&b"foo\n \nbar\n"[..]) | |
309 | .flatten() | |
310 | .collect::<Vec<_>>(), | |
311 | vec![ | |
312 | RowInput { | |
313 | label: String::from("foo"), | |
314 | entries: vec![] | |
315 | }, | |
316 | RowInput { | |
317 | label: String::from("bar"), | |
318 | entries: vec![] | |
319 | } | |
320 | ] | |
321 | ); | |
322 | assert_eq!( | |
323 | read_rows(&b"foo \n bar \n"[..]) | |
324 | .flatten() | |
325 | .collect::<Vec<_>>(), | |
326 | vec![RowInput { | |
327 | label: String::from("foo"), | |
e8657dff | 328 | entries: vec![Entry::from("bar")] |
1f6bd845 SW |
329 | }] |
330 | ); | |
201b9ef3 SW |
331 | |
332 | let bad = read_rows(&b" foo"[..]).next().unwrap(); | |
333 | assert!(bad.is_err()); | |
8110b492 | 334 | assert!(format!("{bad:?}").contains("1: Entry with no header")); |
201b9ef3 SW |
335 | |
336 | let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap(); | |
337 | assert!(bad2.is_err()); | |
8110b492 | 338 | assert!(format!("{bad2:?}").contains("3: Entry with no header")); |
75bb888a | 339 | } |
f272e502 SW |
340 | |
341 | #[test] | |
342 | fn test_column_counts() { | |
343 | assert_eq!( | |
344 | column_counts( | |
345 | &read_rows(&b"foo\n bar\n baz\n"[..]) | |
346 | .collect::<Result<Vec<_>, _>>() | |
347 | .unwrap() | |
348 | ), | |
58b5f36d | 349 | vec![(1, String::from("bar")), (1, String::from("baz"))] |
f272e502 SW |
350 | ); |
351 | assert_eq!( | |
352 | column_counts( | |
353 | &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]) | |
354 | .collect::<Result<Vec<_>, _>>() | |
355 | .unwrap() | |
356 | ), | |
38d1167a | 357 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
f272e502 | 358 | ); |
397ef957 SW |
359 | assert_eq!( |
360 | column_counts( | |
361 | &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]) | |
362 | .collect::<Result<Vec<_>, _>>() | |
363 | .unwrap() | |
364 | ), | |
38d1167a | 365 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
397ef957 | 366 | ); |
b8907770 SW |
367 | assert_eq!( |
368 | column_counts( | |
369 | &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]) | |
370 | .collect::<Result<Vec<_>, _>>() | |
371 | .unwrap() | |
372 | ), | |
38d1167a | 373 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
b8907770 | 374 | ); |
f272e502 | 375 | } |
de408c29 SW |
376 | |
377 | #[test] | |
378 | fn test_render_cell() { | |
379 | assert_eq!( | |
380 | render_cell( | |
381 | "foo", | |
382 | &RowInput { | |
383 | label: String::from("nope"), | |
384 | entries: vec![] | |
385 | } | |
386 | ), | |
92476edc | 387 | String::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>") |
de408c29 SW |
388 | ); |
389 | assert_eq!( | |
390 | render_cell( | |
391 | "foo", | |
392 | &RowInput { | |
393 | label: String::from("nope"), | |
394 | entries: vec![Entry::from("bar")] | |
395 | } | |
396 | ), | |
92476edc | 397 | String::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>") |
de408c29 SW |
398 | ); |
399 | assert_eq!( | |
400 | render_cell( | |
401 | "foo", | |
402 | &RowInput { | |
403 | label: String::from("nope"), | |
404 | entries: vec![Entry::from("foo")] | |
405 | } | |
406 | ), | |
92476edc | 407 | String::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>") |
de408c29 SW |
408 | ); |
409 | assert_eq!( | |
410 | render_cell( | |
411 | "foo", | |
412 | &RowInput { | |
413 | label: String::from("nope"), | |
414 | entries: vec![Entry::from("foo"), Entry::from("foo")] | |
415 | } | |
416 | ), | |
92476edc | 417 | String::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>") |
de408c29 SW |
418 | ); |
419 | assert_eq!( | |
420 | render_cell( | |
421 | "foo", | |
422 | &RowInput { | |
423 | label: String::from("nope"), | |
424 | entries: vec![Entry::from("foo: 5"), Entry::from("foo: 10")] | |
425 | } | |
426 | ), | |
92476edc | 427 | String::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>") |
de408c29 SW |
428 | ); |
429 | assert_eq!( | |
430 | render_cell( | |
431 | "foo", | |
432 | &RowInput { | |
433 | label: String::from("nope"), | |
434 | entries: vec![Entry::from("foo: 5"), Entry::from("foo")] | |
435 | } | |
436 | ), | |
92476edc | 437 | String::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 ✓</td>") |
de408c29 SW |
438 | ); |
439 | } | |
75bb888a | 440 | } |