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