]> git.scottworley.com Git - tablify/blob - src/lib.rs
b52806a744df217691d50d43d471f01cdbd15e0d
[tablify] / src / lib.rs
1 use std::borrow::ToOwned;
2 use std::collections::HashMap;
3 use std::fmt::Write;
4 use std::io::BufRead;
5 use std::iter::Iterator;
6
7 #[derive(PartialEq, Eq, Debug)]
8 struct Config {
9 column_threshold: usize,
10 }
11 impl Config {
12 fn apply_command(&mut self, cmd: &str) -> Result<(), std::io::Error> {
13 if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
14 self.column_threshold = threshold
15 .parse()
16 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
17 }
18 Ok(())
19 }
20 }
21
22 const HEADER: &str = r#"<!DOCTYPE html>
23 <html>
24 <head>
25 <meta charset="utf-8">
26 <meta name="viewport" content="width=device-width, initial-scale=1">
27 <style>
28 td { text-align: center; }
29 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
30 th, td { white-space: nowrap; }
31 th { text-align: left; font-weight: normal; }
32 th.spacer_row { height: .3em; }
33 table { border-collapse: collapse }
34 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
35 tr.key > th > div { width: 1em; }
36 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
37 td { border: thin solid gray; }
38 td.leftover { text-align: left; border: none; padding-left: .4em; }
39 td.yes { border: thin solid gray; background-color: #ddd; }
40 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
41 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
42 </style>
43 <script>
44 function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( "highlight"); } }
45 function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
46 function h2(a, b) { highlight(a); highlight(b); }
47 function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
48 </script>
49 </head>
50 <body>
51 <table>
52 <tbody>
53 "#;
54 const FOOTER: &str = " </tbody>
55 </table>
56 </body>
57 </html>";
58
59 #[derive(PartialEq, Eq, Debug)]
60 pub struct HTML(String);
61 impl HTML {
62 fn escape(value: &str) -> HTML {
63 let mut escaped: String = String::new();
64 for c in value.chars() {
65 match c {
66 '>' => escaped.push_str("&gt;"),
67 '<' => escaped.push_str("&lt;"),
68 '\'' => escaped.push_str("&#39;"),
69 '"' => escaped.push_str("&quot;"),
70 '&' => escaped.push_str("&amp;"),
71 ok_c => escaped.push(ok_c),
72 }
73 }
74 HTML(escaped)
75 }
76 }
77 impl From<&str> for HTML {
78 fn from(value: &str) -> HTML {
79 HTML(String::from(value))
80 }
81 }
82 impl FromIterator<HTML> for HTML {
83 fn from_iter<T>(iter: T) -> HTML
84 where
85 T: IntoIterator<Item = HTML>,
86 {
87 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
88 }
89 }
90 impl std::fmt::Display for HTML {
91 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92 write!(f, "{}", self.0)
93 }
94 }
95
96 #[derive(Debug, PartialEq, Eq)]
97 enum InputLine<'a> {
98 Blank,
99 RowHeader(&'a str),
100 Entry(&'a str, Option<&'a str>),
101 Command(&'a str),
102 }
103 impl<'a> From<&'a str> for InputLine<'a> {
104 fn from(value: &'a str) -> InputLine<'a> {
105 let trimmed = value.trim_end();
106 if trimmed.is_empty() {
107 InputLine::Blank
108 } else if let Some(cmd) = trimmed.strip_prefix('!') {
109 InputLine::Command(cmd)
110 } else if !trimmed.starts_with(' ') {
111 InputLine::RowHeader(value.trim())
112 } else {
113 match value.split_once(':') {
114 None => InputLine::Entry(value.trim(), None),
115 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
116 }
117 }
118 }
119 }
120
121 #[derive(Debug, PartialEq, Eq)]
122 struct Row {
123 label: String,
124 entries: HashMap<String, Vec<Option<String>>>,
125 }
126
127 #[derive(Debug, PartialEq, Eq)]
128 enum Rowlike {
129 Row(Row),
130 Spacer,
131 }
132
133 struct Reader<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> {
134 input: std::iter::Enumerate<Input>,
135 row: Option<Row>,
136 config: &'cfg mut Config,
137 }
138 impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Reader<'cfg, Input> {
139 fn new(config: &'cfg mut Config, input: Input) -> Self {
140 Self {
141 input: input.enumerate(),
142 row: None,
143 config,
144 }
145 }
146 }
147 impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
148 for Reader<'cfg, Input>
149 {
150 type Item = Result<Rowlike, std::io::Error>;
151 fn next(&mut self) -> Option<Self::Item> {
152 loop {
153 match self.input.next() {
154 None => return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose(),
155 Some((_, Err(e))) => return Some(Err(e)),
156 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
157 InputLine::Command(cmd) => {
158 if let Err(e) = self.config.apply_command(cmd) {
159 return Some(Err(e));
160 }
161 }
162 InputLine::Blank if self.row.is_some() => {
163 return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose()
164 }
165 InputLine::Blank => return Some(Ok(Rowlike::Spacer)),
166 InputLine::Entry(col, instance) => match &mut self.row {
167 None => {
168 return Some(Err(std::io::Error::other(format!(
169 "{}: Entry with no header",
170 n + 1
171 ))))
172 }
173 Some(ref mut row) => {
174 row.entries
175 .entry(col.to_owned())
176 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
177 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
178 }
179 },
180 InputLine::RowHeader(row) => {
181 let prev = std::mem::take(&mut self.row);
182 self.row = Some(Row {
183 label: row.to_owned(),
184 entries: HashMap::new(),
185 });
186 if prev.is_some() {
187 return Ok(prev.map(Rowlike::Row)).transpose();
188 }
189 }
190 },
191 }
192 }
193 }
194 }
195
196 fn read_input(input: impl std::io::Read) -> Result<(Vec<Rowlike>, Config), std::io::Error> {
197 let mut config = Config {
198 column_threshold: 2,
199 };
200 let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines());
201 reader
202 .collect::<Result<Vec<_>, _>>()
203 .map(|rows| (rows, config))
204 }
205
206 fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> {
207 let empty = HashMap::new();
208 let mut counts: Vec<_> = rows
209 .iter()
210 .flat_map(|rl| match rl {
211 Rowlike::Row(r) => r.entries.keys(),
212 Rowlike::Spacer => empty.keys(),
213 })
214 .fold(HashMap::new(), |mut cs, col| {
215 cs.entry(col.to_owned())
216 .and_modify(|n| *n += 1)
217 .or_insert(1);
218 cs
219 })
220 .into_iter()
221 .map(|(col, n)| (n, col))
222 .collect();
223 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
224 counts
225 }
226 fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
227 column_counts(rows)
228 .into_iter()
229 .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
230 .collect()
231 }
232
233 fn render_one_instance(instance: &Option<String>) -> HTML {
234 match instance {
235 None => HTML::from("✓"),
236 Some(instance) => HTML::escape(instance.as_ref()),
237 }
238 }
239
240 fn render_instances(instances: &[Option<String>]) -> HTML {
241 let all_empty = instances.iter().all(Option::is_none);
242 if all_empty && instances.len() == 1 {
243 HTML::from("")
244 } else if all_empty {
245 HTML(format!("{}", instances.len()))
246 } else {
247 HTML(
248 instances
249 .iter()
250 .map(render_one_instance)
251 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
252 .collect::<Vec<_>>()
253 .join(" "),
254 )
255 }
256 }
257
258 fn render_cell(col: &str, row: &mut Row) -> HTML {
259 let row_label = HTML::escape(row.label.as_ref());
260 let col_label = HTML::escape(col);
261 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
262 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
263 let contents = match instances {
264 None => HTML::from(""),
265 Some(is) => render_instances(is),
266 };
267 row.entries.remove(col);
268 HTML(format!(
269 r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"#
270 ))
271 }
272
273 fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML {
274 let label = HTML::escape(notcol);
275 let rest = render_instances(instances);
276 if rest == HTML::from("") {
277 HTML(format!("{label}"))
278 } else {
279 HTML(format!("{label}: {rest}"))
280 }
281 }
282
283 fn render_all_leftovers(row: &Row) -> HTML {
284 let mut order: Vec<_> = row.entries.keys().collect();
285 order.sort_unstable();
286 HTML(
287 order
288 .into_iter()
289 .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!")))
290 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
291 .collect::<Vec<_>>()
292 .join(", "),
293 )
294 }
295
296 fn render_row(columns: &[String], rowlike: &mut Rowlike) -> HTML {
297 match rowlike {
298 Rowlike::Spacer => HTML::from("<tr><th class=\"spacer_row\"></th></tr>\n"),
299 Rowlike::Row(row) => {
300 let row_label = HTML::escape(row.label.as_ref());
301 let cells = columns
302 .iter()
303 .map(|col| render_cell(col, row))
304 .collect::<HTML>();
305 let leftovers = render_all_leftovers(row);
306 HTML(format!(
307 "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td class=\"leftover\" onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
308 ))
309 }
310 }
311 }
312
313 fn render_column_headers(columns: &[String]) -> HTML {
314 HTML(
315 String::from(r#"<tr class="key"><th></th>"#)
316 + &columns.iter().fold(String::new(), |mut acc, col| {
317 let col_header = HTML::escape(col.as_ref());
318 write!(
319 &mut acc,
320 r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
321 )
322 .unwrap();
323 acc
324 })
325 + "</tr>\n",
326 )
327 }
328
329 /// # Errors
330 ///
331 /// Will return `Err` if
332 /// * there's an i/o error while reading `input`
333 /// * the log has invalid syntax:
334 /// * an indented line with no preceding non-indented line
335 pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
336 let (rows, config) = read_input(input)?;
337 let columns = column_order(&config, &rows);
338 Ok(HTML(format!(
339 "{HEADER}{}{}{FOOTER}",
340 render_column_headers(&columns),
341 rows.into_iter()
342 .map(|mut r| render_row(&columns, &mut r))
343 .collect::<HTML>()
344 )))
345 }
346
347 #[cfg(test)]
348 mod tests {
349 use super::*;
350
351 #[test]
352 fn test_parse_line() {
353 assert_eq!(InputLine::from(""), InputLine::Blank);
354 assert_eq!(InputLine::from(" "), InputLine::Blank);
355 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
356 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
357 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
358 assert_eq!(
359 InputLine::from(" foo:bar"),
360 InputLine::Entry("foo", Some("bar"))
361 );
362 assert_eq!(
363 InputLine::from(" foo: bar"),
364 InputLine::Entry("foo", Some("bar"))
365 );
366 assert_eq!(
367 InputLine::from(" foo: bar "),
368 InputLine::Entry("foo", Some("bar"))
369 );
370 assert_eq!(
371 InputLine::from(" foo: bar "),
372 InputLine::Entry("foo", Some("bar"))
373 );
374 assert_eq!(
375 InputLine::from(" foo : bar "),
376 InputLine::Entry("foo", Some("bar"))
377 );
378 }
379
380 fn read_rows(input: impl std::io::Read) -> Result<Vec<Rowlike>, std::io::Error> {
381 read_input(input).map(|(rows, _)| rows)
382 }
383 fn read_config(input: impl std::io::Read) -> Result<Config, std::io::Error> {
384 read_input(input).map(|(_, config)| config)
385 }
386 #[test]
387 fn test_read_rows() {
388 assert_eq!(
389 read_rows(&b"foo"[..]).unwrap(),
390 vec![Rowlike::Row(Row {
391 label: "foo".to_owned(),
392 entries: HashMap::new(),
393 })]
394 );
395 assert_eq!(
396 read_rows(&b"bar"[..]).unwrap(),
397 vec![Rowlike::Row(Row {
398 label: "bar".to_owned(),
399 entries: HashMap::new(),
400 })]
401 );
402 assert_eq!(
403 read_rows(&b"foo\nbar\n"[..]).unwrap(),
404 vec![
405 Rowlike::Row(Row {
406 label: "foo".to_owned(),
407 entries: HashMap::new(),
408 }),
409 Rowlike::Row(Row {
410 label: "bar".to_owned(),
411 entries: HashMap::new(),
412 })
413 ]
414 );
415 assert_eq!(
416 read_rows(&b"foo\n bar\n"[..]).unwrap(),
417 vec![Rowlike::Row(Row {
418 label: "foo".to_owned(),
419 entries: HashMap::from([("bar".to_owned(), vec![None])]),
420 })]
421 );
422 assert_eq!(
423 read_rows(&b"foo\n bar\n baz\n"[..]).unwrap(),
424 vec![Rowlike::Row(Row {
425 label: "foo".to_owned(),
426 entries: HashMap::from([
427 ("bar".to_owned(), vec![None]),
428 ("baz".to_owned(), vec![None])
429 ]),
430 })]
431 );
432 assert_eq!(
433 read_rows(&b"foo\n\nbar\n"[..]).unwrap(),
434 vec![
435 Rowlike::Row(Row {
436 label: "foo".to_owned(),
437 entries: HashMap::new(),
438 }),
439 Rowlike::Row(Row {
440 label: "bar".to_owned(),
441 entries: HashMap::new(),
442 })
443 ]
444 );
445 assert_eq!(
446 read_rows(&b"foo\n\n\nbar\n"[..]).unwrap(),
447 vec![
448 Rowlike::Row(Row {
449 label: "foo".to_owned(),
450 entries: HashMap::new(),
451 }),
452 Rowlike::Spacer,
453 Rowlike::Row(Row {
454 label: "bar".to_owned(),
455 entries: HashMap::new(),
456 })
457 ]
458 );
459 assert_eq!(
460 read_rows(&b"foo\n \nbar\n"[..]).unwrap(),
461 vec![
462 Rowlike::Row(Row {
463 label: "foo".to_owned(),
464 entries: HashMap::new(),
465 }),
466 Rowlike::Row(Row {
467 label: "bar".to_owned(),
468 entries: HashMap::new(),
469 })
470 ]
471 );
472 assert_eq!(
473 read_rows(&b"foo \n bar \n"[..]).unwrap(),
474 vec![Rowlike::Row(Row {
475 label: "foo".to_owned(),
476 entries: HashMap::from([("bar".to_owned(), vec![None])]),
477 })]
478 );
479
480 let bad = read_rows(&b" foo"[..]);
481 assert!(bad.is_err());
482 assert!(format!("{bad:?}").contains("1: Entry with no header"));
483
484 let bad2 = read_rows(&b"foo\n\n bar"[..]);
485 assert!(bad2.is_err());
486 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
487 }
488
489 #[test]
490 fn test_read_config() {
491 assert_eq!(
492 read_config(&b"!col_threshold 10"[..]).unwrap(),
493 Config {
494 column_threshold: 10
495 }
496 );
497
498 let bad_num = read_config(&b"!col_threshold foo"[..]);
499 assert!(bad_num.is_err());
500 assert!(format!("{bad_num:?}").contains("Parse"));
501 }
502
503 #[test]
504 fn test_column_counts() {
505 assert_eq!(
506 column_counts(&read_rows(&b"foo\n bar\n baz\n"[..]).unwrap()),
507 vec![(1, String::from("bar")), (1, String::from("baz"))]
508 );
509 assert_eq!(
510 column_counts(&read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]).unwrap()),
511 vec![(2, String::from("baz")), (1, String::from("bar"))]
512 );
513 assert_eq!(
514 column_counts(&read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]).unwrap()),
515 vec![(2, String::from("baz")), (1, String::from("bar"))]
516 );
517 assert_eq!(
518 column_counts(
519 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]).unwrap()
520 ),
521 vec![(2, String::from("baz")), (1, String::from("bar"))]
522 );
523 }
524
525 #[test]
526 fn test_render_cell() {
527 assert_eq!(
528 render_cell(
529 "foo",
530 &mut Row {
531 label: "nope".to_owned(),
532 entries: HashMap::new(),
533 }
534 ),
535 HTML::from(
536 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
537 )
538 );
539 assert_eq!(
540 render_cell(
541 "foo",
542 &mut Row {
543 label: "nope".to_owned(),
544 entries: HashMap::from([("bar".to_owned(), vec![None])]),
545 }
546 ),
547 HTML::from(
548 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
549 )
550 );
551 assert_eq!(
552 render_cell(
553 "foo",
554 &mut Row {
555 label: "nope".to_owned(),
556 entries: HashMap::from([("foo".to_owned(), vec![None])]),
557 }
558 ),
559 HTML::from(
560 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
561 )
562 );
563 assert_eq!(
564 render_cell(
565 "foo",
566 &mut Row {
567 label: "nope".to_owned(),
568 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
569 }
570 ),
571 HTML::from(
572 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
573 )
574 );
575 assert_eq!(
576 render_cell(
577 "foo",
578 &mut Row {
579 label: "nope".to_owned(),
580 entries: HashMap::from([(
581 "foo".to_owned(),
582 vec![Some("5".to_owned()), Some("10".to_owned())]
583 )]),
584 }
585 ),
586 HTML::from(
587 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
588 )
589 );
590 assert_eq!(
591 render_cell(
592 "foo",
593 &mut Row {
594 label: "nope".to_owned(),
595 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
596 }
597 ),
598 HTML::from(
599 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
600 )
601 );
602 assert_eq!(
603 render_cell(
604 "heart",
605 &mut Row {
606 label: "nope".to_owned(),
607 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
608 }
609 ),
610 HTML::from(
611 r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')">&lt;3</td>"#
612 )
613 );
614 assert_eq!(
615 render_cell(
616 "foo",
617 &mut Row {
618 label: "bob's".to_owned(),
619 entries: HashMap::from([("foo".to_owned(), vec![None])]),
620 }
621 ),
622 HTML::from(
623 r#"<td class="yes" onmouseover="h2('bob&#39;s','foo')" onmouseout="ch2('bob&#39;s','foo')"></td>"#
624 )
625 );
626 let mut r = Row {
627 label: "nope".to_owned(),
628 entries: HashMap::from([
629 ("foo".to_owned(), vec![None]),
630 ("baz".to_owned(), vec![None]),
631 ]),
632 };
633 assert_eq!(r.entries.len(), 2);
634 render_cell("foo", &mut r);
635 assert_eq!(r.entries.len(), 1);
636 render_cell("bar", &mut r);
637 assert_eq!(r.entries.len(), 1);
638 render_cell("baz", &mut r);
639 assert_eq!(r.entries.len(), 0);
640 }
641
642 #[test]
643 fn test_render_leftovers() {
644 assert_eq!(
645 render_all_leftovers(&Row {
646 label: "nope".to_owned(),
647 entries: HashMap::from([("foo".to_owned(), vec![None])]),
648 }),
649 HTML::from("foo")
650 );
651 assert_eq!(
652 render_all_leftovers(&Row {
653 label: "nope".to_owned(),
654 entries: HashMap::from([
655 ("foo".to_owned(), vec![None]),
656 ("bar".to_owned(), vec![None])
657 ]),
658 }),
659 HTML::from("bar, foo")
660 );
661 assert_eq!(
662 render_all_leftovers(&Row {
663 label: "nope".to_owned(),
664 entries: HashMap::from([
665 ("foo".to_owned(), vec![None]),
666 ("bar".to_owned(), vec![None, None])
667 ]),
668 }),
669 HTML::from("bar: 2, foo")
670 );
671 }
672
673 #[test]
674 fn test_render_row() {
675 assert_eq!(
676 render_row(
677 &["foo".to_owned()],
678 &mut Rowlike::Row(Row {
679 label: "nope".to_owned(),
680 entries: HashMap::from([("bar".to_owned(), vec![None])]),
681 })
682 ),
683 HTML::from(
684 r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')">bar</td></tr>
685 "#
686 )
687 );
688 }
689 }