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