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