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