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