]> git.scottworley.com Git - tablify/blob - src/lib.rs
Don't be O(n^2) unnecessarily
[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 const 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>
13 td { text-align: center; }
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 }
18 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
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; }
22 td.yes { border: thin solid gray; background-color: #ddd; }
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; }
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 ";
37 const FOOTER: &str = " </tbody>
38 </table>
39 </body>
40 </html>";
41
42 #[derive(PartialEq, Eq, Debug)]
43 pub struct HTML(String);
44 impl 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 }
60 impl From<&str> for HTML {
61 fn from(value: &str) -> HTML {
62 HTML(String::from(value))
63 }
64 }
65 impl 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 }
73 impl 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
79 #[derive(Debug, PartialEq, Eq)]
80 enum InputLine<'a> {
81 Blank,
82 RowHeader(&'a str),
83 Entry(&'a str, Option<&'a str>),
84 }
85 impl<'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 }
97 }
98 }
99 }
100
101 #[derive(Debug, PartialEq, Eq)]
102 struct Row {
103 label: String,
104 entries: HashMap<String, Vec<Option<String>>>,
105 }
106
107 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
108 input: std::iter::Enumerate<Input>,
109 row: Option<Row>,
110 }
111 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
112 fn new(input: Input) -> Self {
113 Self {
114 input: input.enumerate(),
115 row: None,
116 }
117 }
118 }
119 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
120 type Item = Result<Row, std::io::Error>;
121 fn next(&mut self) -> Option<Self::Item> {
122 loop {
123 match self.input.next() {
124 None => return Ok(std::mem::take(&mut self.row)).transpose(),
125 Some((_, Err(e))) => return Some(Err(e)),
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()
129 }
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 }
154 }
155 },
156 }
157 }
158 }
159 }
160
161 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<Row, std::io::Error>> {
162 Reader::new(std::io::BufReader::new(input).lines())
163 }
164
165 fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
166 let mut counts: Vec<_> = rows
167 .iter()
168 .flat_map(|r| r.entries.keys())
169 .fold(HashMap::new(), |mut cs, col| {
170 cs.entry(col.to_owned())
171 .and_modify(|n| *n += 1)
172 .or_insert(1);
173 cs
174 })
175 .into_iter()
176 .map(|(col, n)| (n, col))
177 .collect();
178 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
179 counts
180 }
181 fn column_order(rows: &[Row]) -> Vec<String> {
182 column_counts(rows)
183 .into_iter()
184 .map(|(_, col)| col)
185 .collect()
186 }
187
188 fn render_instance(instance: &Option<String>) -> HTML {
189 match instance {
190 None => HTML::from("✓"),
191 Some(instance) => HTML::escape(instance.as_ref()),
192 }
193 }
194
195 fn render_cell(col: &str, row: &Row) -> HTML {
196 let row_label = HTML::escape(row.label.as_ref());
197 let col_label = HTML::escape(col);
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) {
205 HTML::from("")
206 } else if all_empty {
207 HTML(format!("{}", instances.unwrap().len()))
208 } else {
209 HTML(
210 instances
211 .unwrap()
212 .iter()
213 .map(render_instance)
214 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
215 .collect::<Vec<_>>()
216 .join(" "),
217 )
218 };
219 HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
220 }
221
222 fn render_row(columns: &[String], row: &Row) -> HTML {
223 let row_label = HTML::escape(row.label.as_ref());
224 HTML(format!(
225 "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
226 &columns
227 .iter()
228 .map(|col| render_cell(col, row))
229 .collect::<HTML>()
230 ))
231 }
232
233 fn 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 )
247 }
248
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
255 pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
256 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
257 let columns = column_order(&rows);
258 Ok(HTML(format!(
259 "{HEADER}{}{}{FOOTER}",
260 render_column_headers(&columns),
261 rows.into_iter()
262 .map(|r| render_row(&columns, &r))
263 .collect::<HTML>()
264 )))
265 }
266
267 #[cfg(test)]
268 mod tests {
269 use super::*;
270
271 #[test]
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));
278 assert_eq!(
279 InputLine::from(" foo:bar"),
280 InputLine::Entry("foo", Some("bar"))
281 );
282 assert_eq!(
283 InputLine::from(" foo: bar"),
284 InputLine::Entry("foo", Some("bar"))
285 );
286 assert_eq!(
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"))
297 );
298 }
299
300 #[test]
301 fn test_read_rows() {
302 assert_eq!(
303 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
304 vec![Row {
305 label: "foo".to_owned(),
306 entries: HashMap::new(),
307 }]
308 );
309 assert_eq!(
310 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
311 vec![Row {
312 label: "bar".to_owned(),
313 entries: HashMap::new(),
314 }]
315 );
316 assert_eq!(
317 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
318 vec![
319 Row {
320 label: "foo".to_owned(),
321 entries: HashMap::new(),
322 },
323 Row {
324 label: "bar".to_owned(),
325 entries: HashMap::new(),
326 }
327 ]
328 );
329 assert_eq!(
330 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
331 vec![Row {
332 label: "foo".to_owned(),
333 entries: HashMap::from([("bar".to_owned(), vec![None])]),
334 }]
335 );
336 assert_eq!(
337 read_rows(&b"foo\n bar\n baz\n"[..])
338 .flatten()
339 .collect::<Vec<_>>(),
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 ]),
346 }]
347 );
348 assert_eq!(
349 read_rows(&b"foo\n\nbar\n"[..])
350 .flatten()
351 .collect::<Vec<_>>(),
352 vec![
353 Row {
354 label: "foo".to_owned(),
355 entries: HashMap::new(),
356 },
357 Row {
358 label: "bar".to_owned(),
359 entries: HashMap::new(),
360 }
361 ]
362 );
363 assert_eq!(
364 read_rows(&b"foo\n \nbar\n"[..])
365 .flatten()
366 .collect::<Vec<_>>(),
367 vec![
368 Row {
369 label: "foo".to_owned(),
370 entries: HashMap::new(),
371 },
372 Row {
373 label: "bar".to_owned(),
374 entries: HashMap::new(),
375 }
376 ]
377 );
378 assert_eq!(
379 read_rows(&b"foo \n bar \n"[..])
380 .flatten()
381 .collect::<Vec<_>>(),
382 vec![Row {
383 label: "foo".to_owned(),
384 entries: HashMap::from([("bar".to_owned(), vec![None])]),
385 }]
386 );
387
388 let bad = read_rows(&b" foo"[..]).next().unwrap();
389 assert!(bad.is_err());
390 assert!(format!("{bad:?}").contains("1: Entry with no header"));
391
392 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
393 assert!(bad2.is_err());
394 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
395 }
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 ),
405 vec![(1, String::from("bar")), (1, String::from("baz"))]
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 ),
413 vec![(2, String::from("baz")), (1, String::from("bar"))]
414 );
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 ),
421 vec![(2, String::from("baz")), (1, String::from("bar"))]
422 );
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 ),
429 vec![(2, String::from("baz")), (1, String::from("bar"))]
430 );
431 }
432
433 #[test]
434 fn test_render_cell() {
435 assert_eq!(
436 render_cell(
437 "foo",
438 &Row {
439 label: "nope".to_owned(),
440 entries: HashMap::new(),
441 }
442 ),
443 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
444 );
445 assert_eq!(
446 render_cell(
447 "foo",
448 &Row {
449 label: "nope".to_owned(),
450 entries: HashMap::from([("bar".to_owned(), vec![None])]),
451 }
452 ),
453 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
454 );
455 assert_eq!(
456 render_cell(
457 "foo",
458 &Row {
459 label: "nope".to_owned(),
460 entries: HashMap::from([("foo".to_owned(), vec![None])]),
461 }
462 ),
463 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
464 );
465 assert_eq!(
466 render_cell(
467 "foo",
468 &Row {
469 label: "nope".to_owned(),
470 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
471 }
472 ),
473 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>")
474 );
475 assert_eq!(
476 render_cell(
477 "foo",
478 &Row {
479 label: "nope".to_owned(),
480 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
481 }
482 ),
483 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>")
484 );
485 assert_eq!(
486 render_cell(
487 "foo",
488 &Row {
489 label: "nope".to_owned(),
490 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
491 }
492 ),
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",
498 &Row {
499 label: "nope".to_owned(),
500 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
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",
508 &Row {
509 label: "bob's".to_owned(),
510 entries: HashMap::from([("foo".to_owned(), vec![None])]),
511 }
512 ),
513 HTML::from("<td class=\"yes\" onmouseover=\"h2('bob&#39;s','foo')\" onmouseout=\"ch2('bob&#39;s','foo')\"></td>")
514 );
515 }
516 }