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