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