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