]>
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; } | |
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 |
43 | const FOOTER: &str = " </tbody> |
44 | </table> | |
45 | </body> | |
46 | </html>"; | |
47 | ||
70436f23 SW |
48 | #[derive(PartialEq, Eq, Debug)] |
49 | pub struct HTML(String); | |
50 | impl 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(">"), | |
56 | '<' => escaped.push_str("<"), | |
57 | '\'' => escaped.push_str("'"), | |
58 | '"' => escaped.push_str("""), | |
59 | '&' => escaped.push_str("&"), | |
60 | ok_c => escaped.push(ok_c), | |
61 | } | |
62 | } | |
63 | HTML(escaped) | |
64 | } | |
65 | } | |
66 | impl From<&str> for HTML { | |
67 | fn from(value: &str) -> HTML { | |
68 | HTML(String::from(value)) | |
69 | } | |
70 | } | |
71 | impl 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 | } | |
79 | impl 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)] |
86 | enum InputLine<'a> { | |
87 | Blank, | |
88 | RowHeader(&'a str), | |
89 | Entry(&'a str, Option<&'a str>), | |
e8657dff | 90 | } |
88a08162 SW |
91 | impl<'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 |
108 | struct Row { |
109 | label: String, | |
110 | entries: HashMap<String, Vec<Option<String>>>, | |
75bb888a SW |
111 | } |
112 | ||
06a6a5ca SW |
113 | #[derive(Debug, PartialEq, Eq)] |
114 | enum Rowlike { | |
115 | Row(Row), | |
116 | Spacer, | |
117 | } | |
118 | ||
88a08162 | 119 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { |
8110b492 | 120 | input: std::iter::Enumerate<Input>, |
88a08162 | 121 | row: Option<Row>, |
201b9ef3 | 122 | } |
88a08162 | 123 | impl<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 | 131 | impl<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 | ||
586b332a SW |
173 | fn read_input(input: impl std::io::Read) -> Result<(Vec<Rowlike>, Config), std::io::Error> { |
174 | let default_config = Config { | |
175 | column_threshold: 2, | |
176 | }; | |
177 | Reader::new(std::io::BufReader::new(input).lines()) | |
178 | .collect::<Result<Vec<_>, _>>() | |
179 | .map(|rows| (rows, default_config)) | |
75bb888a SW |
180 | } |
181 | ||
06a6a5ca SW |
182 | fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> { |
183 | let empty = HashMap::new(); | |
58b5f36d SW |
184 | let mut counts: Vec<_> = rows |
185 | .iter() | |
06a6a5ca SW |
186 | .flat_map(|rl| match rl { |
187 | Rowlike::Row(r) => r.entries.keys(), | |
188 | Rowlike::Spacer => empty.keys(), | |
189 | }) | |
b8907770 | 190 | .fold(HashMap::new(), |mut cs, col| { |
88a08162 | 191 | cs.entry(col.to_owned()) |
58b5f36d | 192 | .and_modify(|n| *n += 1) |
f272e502 | 193 | .or_insert(1); |
58b5f36d | 194 | cs |
f272e502 | 195 | }) |
58b5f36d SW |
196 | .into_iter() |
197 | .map(|(col, n)| (n, col)) | |
198 | .collect(); | |
38d1167a | 199 | counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol))); |
58b5f36d | 200 | counts |
f272e502 | 201 | } |
06a6a5ca | 202 | fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> { |
d22b2e05 SW |
203 | column_counts(rows) |
204 | .into_iter() | |
31af9aac | 205 | .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col)) |
d22b2e05 SW |
206 | .collect() |
207 | } | |
f272e502 | 208 | |
58c0a717 | 209 | fn render_one_instance(instance: &Option<String>) -> HTML { |
88a08162 | 210 | match instance { |
70436f23 SW |
211 | None => HTML::from("✓"), |
212 | Some(instance) => HTML::escape(instance.as_ref()), | |
de408c29 SW |
213 | } |
214 | } | |
215 | ||
f915bc90 SW |
216 | fn render_instances(instances: &[Option<String>]) -> HTML { |
217 | let all_empty = instances.iter().all(Option::is_none); | |
218 | if all_empty && instances.len() == 1 { | |
70436f23 | 219 | HTML::from("") |
de408c29 | 220 | } else if all_empty { |
f915bc90 | 221 | HTML(format!("{}", instances.len())) |
de408c29 | 222 | } else { |
70436f23 | 223 | HTML( |
88a08162 | 224 | instances |
70436f23 | 225 | .iter() |
58c0a717 | 226 | .map(render_one_instance) |
70436f23 SW |
227 | .map(|html| html.0) // Waiting for slice_concat_trait to stabilize |
228 | .collect::<Vec<_>>() | |
229 | .join(" "), | |
230 | ) | |
f915bc90 SW |
231 | } |
232 | } | |
233 | ||
234 | fn render_cell(col: &str, row: &mut Row) -> HTML { | |
235 | let row_label = HTML::escape(row.label.as_ref()); | |
236 | let col_label = HTML::escape(col); | |
237 | let instances: Option<&Vec<Option<String>>> = row.entries.get(col); | |
238 | let class = HTML::from(if instances.is_none() { "" } else { "yes" }); | |
239 | let contents = match instances { | |
240 | None => HTML::from(""), | |
241 | Some(is) => render_instances(is), | |
de408c29 | 242 | }; |
d9bfcf4d | 243 | row.entries.remove(col); |
5ffe8e3a SW |
244 | HTML(format!( |
245 | r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"# | |
246 | )) | |
de408c29 SW |
247 | } |
248 | ||
9a626020 SW |
249 | fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML { |
250 | let label = HTML::escape(notcol); | |
251 | let rest = render_instances(instances); | |
252 | if rest == HTML::from("") { | |
253 | HTML(format!("{label}")) | |
254 | } else { | |
255 | HTML(format!("{label}: {rest}")) | |
256 | } | |
257 | } | |
258 | ||
259 | fn render_all_leftovers(row: &Row) -> HTML { | |
260 | let mut order: Vec<_> = row.entries.keys().collect(); | |
261 | order.sort_unstable(); | |
262 | HTML( | |
263 | order | |
264 | .into_iter() | |
265 | .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!"))) | |
266 | .map(|html| html.0) // Waiting for slice_concat_trait to stabilize | |
267 | .collect::<Vec<_>>() | |
268 | .join(", "), | |
269 | ) | |
270 | } | |
271 | ||
06a6a5ca SW |
272 | fn render_row(columns: &[String], rowlike: &mut Rowlike) -> HTML { |
273 | match rowlike { | |
529cbaa2 | 274 | Rowlike::Spacer => HTML::from("<tr><th class=\"spacer_row\"></th></tr>\n"), |
06a6a5ca SW |
275 | Rowlike::Row(row) => { |
276 | let row_label = HTML::escape(row.label.as_ref()); | |
277 | let cells = columns | |
278 | .iter() | |
279 | .map(|col| render_cell(col, row)) | |
280 | .collect::<HTML>(); | |
281 | let leftovers = render_all_leftovers(row); | |
282 | HTML(format!( | |
283 | "<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" | |
284 | )) | |
285 | } | |
286 | } | |
de408c29 SW |
287 | } |
288 | ||
70436f23 SW |
289 | fn render_column_headers(columns: &[String]) -> HTML { |
290 | HTML( | |
5ffe8e3a | 291 | String::from(r#"<tr class="key"><th></th>"#) |
70436f23 SW |
292 | + &columns.iter().fold(String::new(), |mut acc, col| { |
293 | let col_header = HTML::escape(col.as_ref()); | |
294 | write!( | |
295 | &mut acc, | |
5ffe8e3a | 296 | r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"# |
70436f23 SW |
297 | ) |
298 | .unwrap(); | |
299 | acc | |
300 | }) | |
301 | + "</tr>\n", | |
302 | ) | |
76638ea1 SW |
303 | } |
304 | ||
4b99fb70 SW |
305 | /// # Errors |
306 | /// | |
307 | /// Will return `Err` if | |
308 | /// * there's an i/o error while reading `input` | |
309 | /// * the log has invalid syntax: | |
310 | /// * an indented line with no preceding non-indented line | |
586b332a SW |
311 | pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> { |
312 | let (rows, config) = read_input(input)?; | |
313 | let columns = column_order(&config, &rows); | |
70436f23 SW |
314 | Ok(HTML(format!( |
315 | "{HEADER}{}{}{FOOTER}", | |
316 | render_column_headers(&columns), | |
317 | rows.into_iter() | |
d9bfcf4d | 318 | .map(|mut r| render_row(&columns, &mut r)) |
70436f23 SW |
319 | .collect::<HTML>() |
320 | ))) | |
ece97615 | 321 | } |
75bb888a SW |
322 | |
323 | #[cfg(test)] | |
324 | mod tests { | |
325 | use super::*; | |
326 | ||
b8907770 | 327 | #[test] |
88a08162 SW |
328 | fn test_parse_line() { |
329 | assert_eq!(InputLine::from(""), InputLine::Blank); | |
330 | assert_eq!(InputLine::from(" "), InputLine::Blank); | |
331 | assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo")); | |
332 | assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo")); | |
333 | assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None)); | |
b8907770 | 334 | assert_eq!( |
88a08162 SW |
335 | InputLine::from(" foo:bar"), |
336 | InputLine::Entry("foo", Some("bar")) | |
b8907770 SW |
337 | ); |
338 | assert_eq!( | |
88a08162 SW |
339 | InputLine::from(" foo: bar"), |
340 | InputLine::Entry("foo", Some("bar")) | |
b8907770 | 341 | ); |
0d999bc3 | 342 | assert_eq!( |
88a08162 SW |
343 | InputLine::from(" foo: bar "), |
344 | InputLine::Entry("foo", Some("bar")) | |
345 | ); | |
346 | assert_eq!( | |
347 | InputLine::from(" foo: bar "), | |
348 | InputLine::Entry("foo", Some("bar")) | |
349 | ); | |
350 | assert_eq!( | |
351 | InputLine::from(" foo : bar "), | |
352 | InputLine::Entry("foo", Some("bar")) | |
0d999bc3 | 353 | ); |
b8907770 SW |
354 | } |
355 | ||
586b332a SW |
356 | fn read_rows(input: impl std::io::Read) -> Result<Vec<Rowlike>, std::io::Error> { |
357 | read_input(input).map(|(rows, _)| rows) | |
358 | } | |
75bb888a SW |
359 | #[test] |
360 | fn test_read_rows() { | |
361 | assert_eq!( | |
12e91300 | 362 | read_rows(&b"foo"[..]).unwrap(), |
06a6a5ca | 363 | vec![Rowlike::Row(Row { |
88a08162 SW |
364 | label: "foo".to_owned(), |
365 | entries: HashMap::new(), | |
06a6a5ca | 366 | })] |
75bb888a | 367 | ); |
9dfa98b7 | 368 | assert_eq!( |
12e91300 | 369 | read_rows(&b"bar"[..]).unwrap(), |
06a6a5ca | 370 | vec![Rowlike::Row(Row { |
88a08162 SW |
371 | label: "bar".to_owned(), |
372 | entries: HashMap::new(), | |
06a6a5ca | 373 | })] |
9dfa98b7 | 374 | ); |
2aa9ef94 | 375 | assert_eq!( |
12e91300 | 376 | read_rows(&b"foo\nbar\n"[..]).unwrap(), |
2aa9ef94 | 377 | vec![ |
06a6a5ca | 378 | Rowlike::Row(Row { |
88a08162 SW |
379 | label: "foo".to_owned(), |
380 | entries: HashMap::new(), | |
06a6a5ca SW |
381 | }), |
382 | Rowlike::Row(Row { | |
88a08162 SW |
383 | label: "bar".to_owned(), |
384 | entries: HashMap::new(), | |
06a6a5ca | 385 | }) |
2aa9ef94 SW |
386 | ] |
387 | ); | |
201b9ef3 | 388 | assert_eq!( |
12e91300 | 389 | read_rows(&b"foo\n bar\n"[..]).unwrap(), |
06a6a5ca | 390 | vec![Rowlike::Row(Row { |
88a08162 SW |
391 | label: "foo".to_owned(), |
392 | entries: HashMap::from([("bar".to_owned(), vec![None])]), | |
06a6a5ca | 393 | })] |
201b9ef3 SW |
394 | ); |
395 | assert_eq!( | |
12e91300 | 396 | read_rows(&b"foo\n bar\n baz\n"[..]).unwrap(), |
06a6a5ca | 397 | vec![Rowlike::Row(Row { |
88a08162 SW |
398 | label: "foo".to_owned(), |
399 | entries: HashMap::from([ | |
400 | ("bar".to_owned(), vec![None]), | |
401 | ("baz".to_owned(), vec![None]) | |
402 | ]), | |
06a6a5ca | 403 | })] |
201b9ef3 SW |
404 | ); |
405 | assert_eq!( | |
12e91300 | 406 | read_rows(&b"foo\n\nbar\n"[..]).unwrap(), |
722ea297 SW |
407 | vec![ |
408 | Rowlike::Row(Row { | |
409 | label: "foo".to_owned(), | |
410 | entries: HashMap::new(), | |
411 | }), | |
412 | Rowlike::Row(Row { | |
413 | label: "bar".to_owned(), | |
414 | entries: HashMap::new(), | |
415 | }) | |
416 | ] | |
417 | ); | |
418 | assert_eq!( | |
12e91300 | 419 | read_rows(&b"foo\n\n\nbar\n"[..]).unwrap(), |
201b9ef3 | 420 | vec![ |
06a6a5ca | 421 | Rowlike::Row(Row { |
88a08162 SW |
422 | label: "foo".to_owned(), |
423 | entries: HashMap::new(), | |
06a6a5ca | 424 | }), |
14a039db | 425 | Rowlike::Spacer, |
06a6a5ca | 426 | Rowlike::Row(Row { |
88a08162 SW |
427 | label: "bar".to_owned(), |
428 | entries: HashMap::new(), | |
06a6a5ca | 429 | }) |
201b9ef3 SW |
430 | ] |
431 | ); | |
1f6bd845 | 432 | assert_eq!( |
12e91300 | 433 | read_rows(&b"foo\n \nbar\n"[..]).unwrap(), |
1f6bd845 | 434 | vec![ |
06a6a5ca | 435 | Rowlike::Row(Row { |
88a08162 SW |
436 | label: "foo".to_owned(), |
437 | entries: HashMap::new(), | |
06a6a5ca SW |
438 | }), |
439 | Rowlike::Row(Row { | |
88a08162 SW |
440 | label: "bar".to_owned(), |
441 | entries: HashMap::new(), | |
06a6a5ca | 442 | }) |
1f6bd845 SW |
443 | ] |
444 | ); | |
445 | assert_eq!( | |
12e91300 | 446 | read_rows(&b"foo \n bar \n"[..]).unwrap(), |
06a6a5ca | 447 | vec![Rowlike::Row(Row { |
88a08162 SW |
448 | label: "foo".to_owned(), |
449 | entries: HashMap::from([("bar".to_owned(), vec![None])]), | |
06a6a5ca | 450 | })] |
1f6bd845 | 451 | ); |
201b9ef3 | 452 | |
12e91300 | 453 | let bad = read_rows(&b" foo"[..]); |
201b9ef3 | 454 | assert!(bad.is_err()); |
8110b492 | 455 | assert!(format!("{bad:?}").contains("1: Entry with no header")); |
201b9ef3 | 456 | |
12e91300 | 457 | let bad2 = read_rows(&b"foo\n\n bar"[..]); |
201b9ef3 | 458 | assert!(bad2.is_err()); |
8110b492 | 459 | assert!(format!("{bad2:?}").contains("3: Entry with no header")); |
75bb888a | 460 | } |
f272e502 SW |
461 | |
462 | #[test] | |
463 | fn test_column_counts() { | |
464 | assert_eq!( | |
12e91300 | 465 | column_counts(&read_rows(&b"foo\n bar\n baz\n"[..]).unwrap()), |
58b5f36d | 466 | vec![(1, String::from("bar")), (1, String::from("baz"))] |
f272e502 SW |
467 | ); |
468 | assert_eq!( | |
12e91300 | 469 | column_counts(&read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]).unwrap()), |
38d1167a | 470 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
f272e502 | 471 | ); |
397ef957 | 472 | assert_eq!( |
12e91300 | 473 | column_counts(&read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]).unwrap()), |
38d1167a | 474 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
397ef957 | 475 | ); |
b8907770 SW |
476 | assert_eq!( |
477 | column_counts( | |
12e91300 | 478 | &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]).unwrap() |
b8907770 | 479 | ), |
38d1167a | 480 | vec![(2, String::from("baz")), (1, String::from("bar"))] |
b8907770 | 481 | ); |
f272e502 | 482 | } |
de408c29 SW |
483 | |
484 | #[test] | |
485 | fn test_render_cell() { | |
486 | assert_eq!( | |
487 | render_cell( | |
488 | "foo", | |
d9bfcf4d | 489 | &mut Row { |
88a08162 SW |
490 | label: "nope".to_owned(), |
491 | entries: HashMap::new(), | |
de408c29 SW |
492 | } |
493 | ), | |
5ffe8e3a SW |
494 | HTML::from( |
495 | r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"# | |
496 | ) | |
de408c29 SW |
497 | ); |
498 | assert_eq!( | |
499 | render_cell( | |
500 | "foo", | |
d9bfcf4d | 501 | &mut Row { |
88a08162 SW |
502 | label: "nope".to_owned(), |
503 | entries: HashMap::from([("bar".to_owned(), vec![None])]), | |
de408c29 SW |
504 | } |
505 | ), | |
5ffe8e3a SW |
506 | HTML::from( |
507 | r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"# | |
508 | ) | |
de408c29 SW |
509 | ); |
510 | assert_eq!( | |
511 | render_cell( | |
512 | "foo", | |
d9bfcf4d | 513 | &mut Row { |
88a08162 SW |
514 | label: "nope".to_owned(), |
515 | entries: HashMap::from([("foo".to_owned(), vec![None])]), | |
de408c29 SW |
516 | } |
517 | ), | |
5ffe8e3a SW |
518 | HTML::from( |
519 | r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"# | |
520 | ) | |
de408c29 SW |
521 | ); |
522 | assert_eq!( | |
523 | render_cell( | |
524 | "foo", | |
d9bfcf4d | 525 | &mut Row { |
88a08162 SW |
526 | label: "nope".to_owned(), |
527 | entries: HashMap::from([("foo".to_owned(), vec![None, None])]), | |
de408c29 SW |
528 | } |
529 | ), | |
5ffe8e3a SW |
530 | HTML::from( |
531 | r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"# | |
532 | ) | |
de408c29 SW |
533 | ); |
534 | assert_eq!( | |
535 | render_cell( | |
536 | "foo", | |
d9bfcf4d | 537 | &mut Row { |
88a08162 | 538 | label: "nope".to_owned(), |
5ffe8e3a SW |
539 | entries: HashMap::from([( |
540 | "foo".to_owned(), | |
541 | vec![Some("5".to_owned()), Some("10".to_owned())] | |
542 | )]), | |
de408c29 SW |
543 | } |
544 | ), | |
5ffe8e3a SW |
545 | HTML::from( |
546 | r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"# | |
547 | ) | |
de408c29 SW |
548 | ); |
549 | assert_eq!( | |
550 | render_cell( | |
551 | "foo", | |
d9bfcf4d | 552 | &mut Row { |
88a08162 SW |
553 | label: "nope".to_owned(), |
554 | entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]), | |
de408c29 SW |
555 | } |
556 | ), | |
5ffe8e3a SW |
557 | HTML::from( |
558 | r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"# | |
559 | ) | |
70436f23 SW |
560 | ); |
561 | assert_eq!( | |
562 | render_cell( | |
563 | "heart", | |
d9bfcf4d | 564 | &mut Row { |
88a08162 SW |
565 | label: "nope".to_owned(), |
566 | entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]), | |
70436f23 SW |
567 | } |
568 | ), | |
5ffe8e3a SW |
569 | HTML::from( |
570 | r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')"><3</td>"# | |
571 | ) | |
70436f23 SW |
572 | ); |
573 | assert_eq!( | |
574 | render_cell( | |
575 | "foo", | |
d9bfcf4d | 576 | &mut Row { |
88a08162 SW |
577 | label: "bob's".to_owned(), |
578 | entries: HashMap::from([("foo".to_owned(), vec![None])]), | |
70436f23 SW |
579 | } |
580 | ), | |
5ffe8e3a SW |
581 | HTML::from( |
582 | r#"<td class="yes" onmouseover="h2('bob's','foo')" onmouseout="ch2('bob's','foo')"></td>"# | |
583 | ) | |
de408c29 | 584 | ); |
d9bfcf4d SW |
585 | let mut r = Row { |
586 | label: "nope".to_owned(), | |
587 | entries: HashMap::from([ | |
588 | ("foo".to_owned(), vec![None]), | |
589 | ("baz".to_owned(), vec![None]), | |
590 | ]), | |
591 | }; | |
592 | assert_eq!(r.entries.len(), 2); | |
593 | render_cell("foo", &mut r); | |
594 | assert_eq!(r.entries.len(), 1); | |
595 | render_cell("bar", &mut r); | |
596 | assert_eq!(r.entries.len(), 1); | |
597 | render_cell("baz", &mut r); | |
598 | assert_eq!(r.entries.len(), 0); | |
de408c29 | 599 | } |
25fd008e | 600 | |
9a626020 SW |
601 | #[test] |
602 | fn test_render_leftovers() { | |
603 | assert_eq!( | |
604 | render_all_leftovers(&Row { | |
605 | label: "nope".to_owned(), | |
606 | entries: HashMap::from([("foo".to_owned(), vec![None])]), | |
607 | }), | |
608 | HTML::from("foo") | |
609 | ); | |
610 | assert_eq!( | |
611 | render_all_leftovers(&Row { | |
612 | label: "nope".to_owned(), | |
613 | entries: HashMap::from([ | |
614 | ("foo".to_owned(), vec![None]), | |
615 | ("bar".to_owned(), vec![None]) | |
616 | ]), | |
617 | }), | |
618 | HTML::from("bar, foo") | |
619 | ); | |
620 | assert_eq!( | |
621 | render_all_leftovers(&Row { | |
622 | label: "nope".to_owned(), | |
623 | entries: HashMap::from([ | |
624 | ("foo".to_owned(), vec![None]), | |
625 | ("bar".to_owned(), vec![None, None]) | |
626 | ]), | |
627 | }), | |
628 | HTML::from("bar: 2, foo") | |
629 | ); | |
630 | } | |
631 | ||
25fd008e SW |
632 | #[test] |
633 | fn test_render_row() { | |
634 | assert_eq!( | |
635 | render_row( | |
636 | &["foo".to_owned()], | |
06a6a5ca | 637 | &mut Rowlike::Row(Row { |
25fd008e SW |
638 | label: "nope".to_owned(), |
639 | entries: HashMap::from([("bar".to_owned(), vec![None])]), | |
06a6a5ca | 640 | }) |
25fd008e SW |
641 | ), |
642 | HTML::from( | |
36bc3a39 | 643 | 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 |
644 | "# |
645 | ) | |
646 | ); | |
647 | } | |
75bb888a | 648 | } |