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