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