]> git.scottworley.com Git - tablify/blame - src/lib.rs
Don't be O(n^2) unnecessarily
[tablify] / src / lib.rs
CommitLineData
88a08162
SW
1use std::borrow::ToOwned;
2use std::collections::HashMap;
7067975b 3use std::fmt::Write;
9dfa98b7 4use std::io::BufRead;
75bb888a
SW
5use std::iter::Iterator;
6
cc2378d5
SW
7const HEADER: &str = "<!DOCTYPE html>
8<html>
9<head>
10 <meta charset=\"utf-8\">
11 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
12 <style>
b8b365ce 13 td { text-align: center; }
cc2378d5
SW
14 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
15 th, td { white-space: nowrap; }
16 th { text-align: left; font-weight: normal; }
17 table { border-collapse: collapse }
3bc643e9 18 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
cc2378d5
SW
19 tr.key > th > div { width: 1em; }
20 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
21 td { border: thin solid gray; }
1dda21e6 22 td.yes { border: thin solid gray; background-color: #ddd; }
cc2378d5
SW
23 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
24 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
cc2378d5
SW
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>
76638ea1
SW
35 <tbody>
36";
cc2378d5
SW
37const FOOTER: &str = " </tbody>
38 </table>
39</body>
40</html>";
41
70436f23
SW
42#[derive(PartialEq, Eq, Debug)]
43pub struct HTML(String);
44impl HTML {
45 fn escape(value: &str) -> HTML {
46 let mut escaped: String = String::new();
47 for c in value.chars() {
48 match c {
49 '>' => escaped.push_str("&gt;"),
50 '<' => escaped.push_str("&lt;"),
51 '\'' => escaped.push_str("&#39;"),
52 '"' => escaped.push_str("&quot;"),
53 '&' => escaped.push_str("&amp;"),
54 ok_c => escaped.push(ok_c),
55 }
56 }
57 HTML(escaped)
58 }
59}
60impl From<&str> for HTML {
61 fn from(value: &str) -> HTML {
62 HTML(String::from(value))
63 }
64}
65impl FromIterator<HTML> for HTML {
66 fn from_iter<T>(iter: T) -> HTML
67 where
68 T: IntoIterator<Item = HTML>,
69 {
70 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
71 }
72}
73impl std::fmt::Display for HTML {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 write!(f, "{}", self.0)
76 }
77}
78
88a08162
SW
79#[derive(Debug, PartialEq, Eq)]
80enum InputLine<'a> {
81 Blank,
82 RowHeader(&'a str),
83 Entry(&'a str, Option<&'a str>),
e8657dff 84}
88a08162
SW
85impl<'a> From<&'a str> for InputLine<'a> {
86 fn from(value: &'a str) -> InputLine<'a> {
87 let trimmed = value.trim_end();
88 if trimmed.is_empty() {
89 InputLine::Blank
90 } else if !trimmed.starts_with(' ') {
91 InputLine::RowHeader(value.trim())
92 } else {
93 match value.split_once(':') {
94 None => InputLine::Entry(value.trim(), None),
95 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
96 }
e8657dff
SW
97 }
98 }
99}
14e9852b 100
75bb888a 101#[derive(Debug, PartialEq, Eq)]
88a08162
SW
102struct Row {
103 label: String,
104 entries: HashMap<String, Vec<Option<String>>>,
75bb888a
SW
105}
106
88a08162 107struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
8110b492 108 input: std::iter::Enumerate<Input>,
88a08162 109 row: Option<Row>,
201b9ef3 110}
88a08162 111impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
201b9ef3 112 fn new(input: Input) -> Self {
8110b492
SW
113 Self {
114 input: input.enumerate(),
115 row: None,
116 }
201b9ef3
SW
117 }
118}
88a08162
SW
119impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
120 type Item = Result<Row, std::io::Error>;
201b9ef3
SW
121 fn next(&mut self) -> Option<Self::Item> {
122 loop {
8bf0d5b1 123 match self.input.next() {
201b9ef3 124 None => return Ok(std::mem::take(&mut self.row)).transpose(),
8110b492 125 Some((_, Err(e))) => return Some(Err(e)),
88a08162
SW
126 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
127 InputLine::Blank if self.row.is_some() => {
128 return Ok(std::mem::take(&mut self.row)).transpose()
8110b492 129 }
88a08162
SW
130 InputLine::Blank => {}
131 InputLine::Entry(col, instance) => match &mut self.row {
132 None => {
133 return Some(Err(std::io::Error::other(format!(
134 "{}: Entry with no header",
135 n + 1
136 ))))
137 }
138 Some(ref mut row) => {
139 row.entries
140 .entry(col.to_owned())
141 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
142 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
143 }
144 },
145 InputLine::RowHeader(row) => {
146 let prev = std::mem::take(&mut self.row);
147 self.row = Some(Row {
148 label: row.to_owned(),
149 entries: HashMap::new(),
150 });
151 if prev.is_some() {
152 return Ok(prev).transpose();
153 }
201b9ef3 154 }
88a08162 155 },
201b9ef3
SW
156 }
157 }
158 }
159}
160
88a08162 161fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<Row, std::io::Error>> {
201b9ef3 162 Reader::new(std::io::BufReader::new(input).lines())
75bb888a
SW
163}
164
88a08162 165fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
58b5f36d
SW
166 let mut counts: Vec<_> = rows
167 .iter()
88a08162 168 .flat_map(|r| r.entries.keys())
b8907770 169 .fold(HashMap::new(), |mut cs, col| {
88a08162 170 cs.entry(col.to_owned())
58b5f36d 171 .and_modify(|n| *n += 1)
f272e502 172 .or_insert(1);
58b5f36d 173 cs
f272e502 174 })
58b5f36d
SW
175 .into_iter()
176 .map(|(col, n)| (n, col))
177 .collect();
38d1167a 178 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
58b5f36d 179 counts
f272e502 180}
88a08162 181fn column_order(rows: &[Row]) -> Vec<String> {
d22b2e05
SW
182 column_counts(rows)
183 .into_iter()
184 .map(|(_, col)| col)
185 .collect()
186}
f272e502 187
88a08162
SW
188fn render_instance(instance: &Option<String>) -> HTML {
189 match instance {
70436f23
SW
190 None => HTML::from("✓"),
191 Some(instance) => HTML::escape(instance.as_ref()),
de408c29
SW
192 }
193}
194
88a08162 195fn render_cell(col: &str, row: &Row) -> HTML {
70436f23
SW
196 let row_label = HTML::escape(row.label.as_ref());
197 let col_label = HTML::escape(col);
88a08162
SW
198 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
199 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
200 let all_empty = instances
201 .iter()
202 .flat_map(|is| is.iter())
203 .all(Option::is_none);
204 let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) {
70436f23 205 HTML::from("")
de408c29 206 } else if all_empty {
88a08162 207 HTML(format!("{}", instances.unwrap().len()))
de408c29 208 } else {
70436f23 209 HTML(
88a08162
SW
210 instances
211 .unwrap()
70436f23 212 .iter()
88a08162 213 .map(render_instance)
70436f23
SW
214 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
215 .collect::<Vec<_>>()
216 .join(" "),
217 )
de408c29 218 };
70436f23 219 HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
de408c29
SW
220}
221
88a08162 222fn render_row(columns: &[String], row: &Row) -> HTML {
70436f23
SW
223 let row_label = HTML::escape(row.label.as_ref());
224 HTML(format!(
92476edc 225 "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
de408c29
SW
226 &columns
227 .iter()
228 .map(|col| render_cell(col, row))
70436f23
SW
229 .collect::<HTML>()
230 ))
de408c29
SW
231}
232
70436f23
SW
233fn render_column_headers(columns: &[String]) -> HTML {
234 HTML(
235 String::from("<tr class=\"key\"><th></th>")
236 + &columns.iter().fold(String::new(), |mut acc, col| {
237 let col_header = HTML::escape(col.as_ref());
238 write!(
239 &mut acc,
240 "<th id=\"{col_header}\"><div><div>{col_header}</div></div></th>"
241 )
242 .unwrap();
243 acc
244 })
245 + "</tr>\n",
246 )
76638ea1
SW
247}
248
4b99fb70
SW
249/// # Errors
250///
251/// Will return `Err` if
252/// * there's an i/o error while reading `input`
253/// * the log has invalid syntax:
254/// * an indented line with no preceding non-indented line
70436f23 255pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
4b99fb70 256 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
de408c29 257 let columns = column_order(&rows);
70436f23
SW
258 Ok(HTML(format!(
259 "{HEADER}{}{}{FOOTER}",
260 render_column_headers(&columns),
261 rows.into_iter()
de408c29 262 .map(|r| render_row(&columns, &r))
70436f23
SW
263 .collect::<HTML>()
264 )))
ece97615 265}
75bb888a
SW
266
267#[cfg(test)]
268mod tests {
269 use super::*;
270
b8907770 271 #[test]
88a08162
SW
272 fn test_parse_line() {
273 assert_eq!(InputLine::from(""), InputLine::Blank);
274 assert_eq!(InputLine::from(" "), InputLine::Blank);
275 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
276 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
277 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
b8907770 278 assert_eq!(
88a08162
SW
279 InputLine::from(" foo:bar"),
280 InputLine::Entry("foo", Some("bar"))
b8907770
SW
281 );
282 assert_eq!(
88a08162
SW
283 InputLine::from(" foo: bar"),
284 InputLine::Entry("foo", Some("bar"))
b8907770 285 );
0d999bc3 286 assert_eq!(
88a08162
SW
287 InputLine::from(" foo: bar "),
288 InputLine::Entry("foo", Some("bar"))
289 );
290 assert_eq!(
291 InputLine::from(" foo: bar "),
292 InputLine::Entry("foo", Some("bar"))
293 );
294 assert_eq!(
295 InputLine::from(" foo : bar "),
296 InputLine::Entry("foo", Some("bar"))
0d999bc3 297 );
b8907770
SW
298 }
299
75bb888a
SW
300 #[test]
301 fn test_read_rows() {
302 assert_eq!(
201b9ef3 303 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
88a08162
SW
304 vec![Row {
305 label: "foo".to_owned(),
306 entries: HashMap::new(),
75bb888a
SW
307 }]
308 );
9dfa98b7 309 assert_eq!(
201b9ef3 310 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
88a08162
SW
311 vec![Row {
312 label: "bar".to_owned(),
313 entries: HashMap::new(),
9dfa98b7
SW
314 }]
315 );
2aa9ef94 316 assert_eq!(
201b9ef3 317 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
2aa9ef94 318 vec![
88a08162
SW
319 Row {
320 label: "foo".to_owned(),
321 entries: HashMap::new(),
2aa9ef94 322 },
88a08162
SW
323 Row {
324 label: "bar".to_owned(),
325 entries: HashMap::new(),
2aa9ef94
SW
326 }
327 ]
328 );
201b9ef3
SW
329 assert_eq!(
330 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
88a08162
SW
331 vec![Row {
332 label: "foo".to_owned(),
333 entries: HashMap::from([("bar".to_owned(), vec![None])]),
201b9ef3
SW
334 }]
335 );
336 assert_eq!(
337 read_rows(&b"foo\n bar\n baz\n"[..])
338 .flatten()
339 .collect::<Vec<_>>(),
88a08162
SW
340 vec![Row {
341 label: "foo".to_owned(),
342 entries: HashMap::from([
343 ("bar".to_owned(), vec![None]),
344 ("baz".to_owned(), vec![None])
345 ]),
201b9ef3
SW
346 }]
347 );
348 assert_eq!(
349 read_rows(&b"foo\n\nbar\n"[..])
350 .flatten()
351 .collect::<Vec<_>>(),
352 vec![
88a08162
SW
353 Row {
354 label: "foo".to_owned(),
355 entries: HashMap::new(),
201b9ef3 356 },
88a08162
SW
357 Row {
358 label: "bar".to_owned(),
359 entries: HashMap::new(),
201b9ef3
SW
360 }
361 ]
362 );
1f6bd845
SW
363 assert_eq!(
364 read_rows(&b"foo\n \nbar\n"[..])
365 .flatten()
366 .collect::<Vec<_>>(),
367 vec![
88a08162
SW
368 Row {
369 label: "foo".to_owned(),
370 entries: HashMap::new(),
1f6bd845 371 },
88a08162
SW
372 Row {
373 label: "bar".to_owned(),
374 entries: HashMap::new(),
1f6bd845
SW
375 }
376 ]
377 );
378 assert_eq!(
379 read_rows(&b"foo \n bar \n"[..])
380 .flatten()
381 .collect::<Vec<_>>(),
88a08162
SW
382 vec![Row {
383 label: "foo".to_owned(),
384 entries: HashMap::from([("bar".to_owned(), vec![None])]),
1f6bd845
SW
385 }]
386 );
201b9ef3
SW
387
388 let bad = read_rows(&b" foo"[..]).next().unwrap();
389 assert!(bad.is_err());
8110b492 390 assert!(format!("{bad:?}").contains("1: Entry with no header"));
201b9ef3
SW
391
392 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
393 assert!(bad2.is_err());
8110b492 394 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
75bb888a 395 }
f272e502
SW
396
397 #[test]
398 fn test_column_counts() {
399 assert_eq!(
400 column_counts(
401 &read_rows(&b"foo\n bar\n baz\n"[..])
402 .collect::<Result<Vec<_>, _>>()
403 .unwrap()
404 ),
58b5f36d 405 vec![(1, String::from("bar")), (1, String::from("baz"))]
f272e502
SW
406 );
407 assert_eq!(
408 column_counts(
409 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
410 .collect::<Result<Vec<_>, _>>()
411 .unwrap()
412 ),
38d1167a 413 vec![(2, String::from("baz")), (1, String::from("bar"))]
f272e502 414 );
397ef957
SW
415 assert_eq!(
416 column_counts(
417 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
418 .collect::<Result<Vec<_>, _>>()
419 .unwrap()
420 ),
38d1167a 421 vec![(2, String::from("baz")), (1, String::from("bar"))]
397ef957 422 );
b8907770
SW
423 assert_eq!(
424 column_counts(
425 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
426 .collect::<Result<Vec<_>, _>>()
427 .unwrap()
428 ),
38d1167a 429 vec![(2, String::from("baz")), (1, String::from("bar"))]
b8907770 430 );
f272e502 431 }
de408c29
SW
432
433 #[test]
434 fn test_render_cell() {
435 assert_eq!(
436 render_cell(
437 "foo",
88a08162
SW
438 &Row {
439 label: "nope".to_owned(),
440 entries: HashMap::new(),
de408c29
SW
441 }
442 ),
70436f23 443 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
de408c29
SW
444 );
445 assert_eq!(
446 render_cell(
447 "foo",
88a08162
SW
448 &Row {
449 label: "nope".to_owned(),
450 entries: HashMap::from([("bar".to_owned(), vec![None])]),
de408c29
SW
451 }
452 ),
70436f23 453 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
de408c29
SW
454 );
455 assert_eq!(
456 render_cell(
457 "foo",
88a08162
SW
458 &Row {
459 label: "nope".to_owned(),
460 entries: HashMap::from([("foo".to_owned(), vec![None])]),
de408c29
SW
461 }
462 ),
70436f23 463 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
de408c29
SW
464 );
465 assert_eq!(
466 render_cell(
467 "foo",
88a08162
SW
468 &Row {
469 label: "nope".to_owned(),
470 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
de408c29
SW
471 }
472 ),
70436f23 473 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>")
de408c29
SW
474 );
475 assert_eq!(
476 render_cell(
477 "foo",
88a08162
SW
478 &Row {
479 label: "nope".to_owned(),
480 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
de408c29
SW
481 }
482 ),
70436f23 483 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>")
de408c29
SW
484 );
485 assert_eq!(
486 render_cell(
487 "foo",
88a08162
SW
488 &Row {
489 label: "nope".to_owned(),
490 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
de408c29
SW
491 }
492 ),
70436f23
SW
493 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 ✓</td>")
494 );
495 assert_eq!(
496 render_cell(
497 "heart",
88a08162
SW
498 &Row {
499 label: "nope".to_owned(),
500 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
70436f23
SW
501 }
502 ),
503 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','heart')\" onmouseout=\"ch2('nope','heart')\">&lt;3</td>")
504 );
505 assert_eq!(
506 render_cell(
507 "foo",
88a08162
SW
508 &Row {
509 label: "bob's".to_owned(),
510 entries: HashMap::from([("foo".to_owned(), vec![None])]),
70436f23
SW
511 }
512 ),
513 HTML::from("<td class=\"yes\" onmouseover=\"h2('bob&#39;s','foo')\" onmouseout=\"ch2('bob&#39;s','foo')\"></td>")
de408c29
SW
514 );
515 }
75bb888a 516}