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