]>
Commit | Line | Data |
---|---|---|
397ef957 | 1 | use std::collections::{HashMap, HashSet}; |
9dfa98b7 | 2 | use std::io::BufRead; |
75bb888a SW |
3 | use std::iter::Iterator; |
4 | ||
cc2378d5 SW |
5 | const HEADER: &str = "<!DOCTYPE html> |
6 | <html> | |
7 | <head> | |
8 | <meta charset=\"utf-8\"> | |
9 | <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> | |
10 | <style> | |
11 | /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */ | |
12 | th, td { white-space: nowrap; } | |
13 | th { text-align: left; font-weight: normal; } | |
14 | table { border-collapse: collapse } | |
15 | tr.key > th { height: 8em; vertical-align: bottom; line-height: 1 } | |
16 | tr.key > th > div { width: 1em; } | |
17 | tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) } | |
18 | td { border: thin solid gray; } | |
19 | td.numeric { text-align: right; } | |
20 | td.yes { border: thin solid gray; background-color: gray; } | |
21 | td.spacer { border: none; } | |
22 | /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */ | |
23 | .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; } | |
24 | img { height: 1.2em; } | |
25 | </style> | |
26 | <script> | |
27 | function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( \"highlight\"); } } | |
28 | function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } } | |
29 | function h2(a, b) { highlight(a); highlight(b); } | |
30 | function ch2(a, b) { clear_highlight(a); clear_highlight(b); } | |
31 | </script> | |
32 | </head> | |
33 | <body> | |
34 | <table> | |
35 | <tbody>"; | |
36 | const FOOTER: &str = " </tbody> | |
37 | </table> | |
38 | </body> | |
39 | </html>"; | |
40 | ||
14e9852b | 41 | #[derive(Debug, PartialEq, Eq, Hash)] |
e8657dff SW |
42 | struct Entry { |
43 | col: String, | |
b8907770 | 44 | instance: Option<String>, |
e8657dff SW |
45 | } |
46 | impl From<&str> for Entry { | |
47 | fn from(value: &str) -> Entry { | |
b8907770 SW |
48 | match value.split_once(':') { |
49 | None => Entry { | |
50 | col: String::from(value), | |
51 | instance: None, | |
52 | }, | |
53 | Some((col, instance)) => Entry { | |
0d999bc3 SW |
54 | col: String::from(col.trim()), |
55 | instance: Some(String::from(instance.trim())), | |
b8907770 | 56 | }, |
e8657dff SW |
57 | } |
58 | } | |
59 | } | |
14e9852b | 60 | |
75bb888a SW |
61 | #[derive(Debug, PartialEq, Eq)] |
62 | struct RowInput { | |
63 | label: String, | |
14e9852b | 64 | entries: Vec<Entry>, |
75bb888a SW |
65 | } |
66 | ||
201b9ef3 | 67 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { |
8110b492 | 68 | input: std::iter::Enumerate<Input>, |
201b9ef3 SW |
69 | row: Option<RowInput>, |
70 | } | |
71 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> { | |
201b9ef3 | 72 | fn new(input: Input) -> Self { |
8110b492 SW |
73 | Self { |
74 | input: input.enumerate(), | |
75 | row: None, | |
76 | } | |
201b9ef3 SW |
77 | } |
78 | } | |
79 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> { | |
80 | type Item = Result<RowInput, std::io::Error>; | |
81 | fn next(&mut self) -> Option<Self::Item> { | |
82 | loop { | |
1f6bd845 SW |
83 | match self |
84 | .input | |
85 | .next() | |
8110b492 | 86 | .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end())))) |
1f6bd845 | 87 | { |
201b9ef3 | 88 | None => return Ok(std::mem::take(&mut self.row)).transpose(), |
8110b492 SW |
89 | Some((_, Err(e))) => return Some(Err(e)), |
90 | Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => { | |
201b9ef3 SW |
91 | return Ok(std::mem::take(&mut self.row)).transpose() |
92 | } | |
8110b492 SW |
93 | Some((_, Ok(line))) if line.is_empty() => {} |
94 | Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row { | |
95 | None => { | |
96 | return Some(Err(std::io::Error::other(format!( | |
97 | "{}: Entry with no header", | |
98 | n + 1 | |
99 | )))) | |
100 | } | |
e8657dff | 101 | Some(ref mut row) => row.entries.push(Entry::from(line.trim())), |
201b9ef3 | 102 | }, |
8110b492 | 103 | Some((_, Ok(line))) => { |
201b9ef3 SW |
104 | let prev = std::mem::take(&mut self.row); |
105 | self.row = Some(RowInput { | |
106 | label: line, | |
107 | entries: vec![], | |
108 | }); | |
109 | if prev.is_some() { | |
110 | return Ok(prev).transpose(); | |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
201b9ef3 SW |
118 | fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> { |
119 | Reader::new(std::io::BufReader::new(input).lines()) | |
75bb888a SW |
120 | } |
121 | ||
58b5f36d SW |
122 | fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> { |
123 | let mut counts: Vec<_> = rows | |
124 | .iter() | |
b8907770 SW |
125 | .flat_map(|r| { |
126 | r.entries | |
127 | .iter() | |
128 | .map(|e| &e.col) | |
129 | .collect::<HashSet<_>>() | |
130 | .into_iter() | |
131 | }) | |
132 | .fold(HashMap::new(), |mut cs, col| { | |
133 | cs.entry(String::from(col)) | |
58b5f36d | 134 | .and_modify(|n| *n += 1) |
f272e502 | 135 | .or_insert(1); |
58b5f36d | 136 | cs |
f272e502 | 137 | }) |
58b5f36d SW |
138 | .into_iter() |
139 | .map(|(col, n)| (n, col)) | |
140 | .collect(); | |
141 | counts.sort(); | |
142 | counts | |
f272e502 | 143 | } |
d22b2e05 SW |
144 | fn column_order(rows: &[RowInput]) -> Vec<String> { |
145 | column_counts(rows) | |
146 | .into_iter() | |
147 | .map(|(_, col)| col) | |
148 | .collect() | |
149 | } | |
f272e502 | 150 | |
4b99fb70 SW |
151 | /// # Errors |
152 | /// | |
153 | /// Will return `Err` if | |
154 | /// * there's an i/o error while reading `input` | |
155 | /// * the log has invalid syntax: | |
156 | /// * an indented line with no preceding non-indented line | |
157 | pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> { | |
158 | let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?; | |
d22b2e05 | 159 | let _columns = column_order(&rows); |
cc2378d5 | 160 | Ok(String::from(HEADER) + "Hello, world!" + FOOTER) |
ece97615 | 161 | } |
75bb888a SW |
162 | |
163 | #[cfg(test)] | |
164 | mod tests { | |
165 | use super::*; | |
166 | ||
b8907770 SW |
167 | #[test] |
168 | fn test_parse_entry() { | |
169 | assert_eq!( | |
170 | Entry::from("foo"), | |
171 | Entry { | |
172 | col: String::from("foo"), | |
173 | instance: None | |
174 | } | |
175 | ); | |
176 | assert_eq!( | |
177 | Entry::from("foo:bar"), | |
178 | Entry { | |
179 | col: String::from("foo"), | |
180 | instance: Some(String::from("bar")) | |
181 | } | |
182 | ); | |
0d999bc3 SW |
183 | assert_eq!( |
184 | Entry::from("foo: bar"), | |
185 | Entry { | |
186 | col: String::from("foo"), | |
187 | instance: Some(String::from("bar")) | |
188 | } | |
189 | ); | |
b8907770 SW |
190 | } |
191 | ||
75bb888a SW |
192 | #[test] |
193 | fn test_read_rows() { | |
194 | assert_eq!( | |
201b9ef3 | 195 | read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(), |
75bb888a SW |
196 | vec![RowInput { |
197 | label: String::from("foo"), | |
198 | entries: vec![] | |
199 | }] | |
200 | ); | |
9dfa98b7 | 201 | assert_eq!( |
201b9ef3 | 202 | read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(), |
9dfa98b7 SW |
203 | vec![RowInput { |
204 | label: String::from("bar"), | |
205 | entries: vec![] | |
206 | }] | |
207 | ); | |
2aa9ef94 | 208 | assert_eq!( |
201b9ef3 | 209 | read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(), |
2aa9ef94 SW |
210 | vec![ |
211 | RowInput { | |
212 | label: String::from("foo"), | |
213 | entries: vec![] | |
214 | }, | |
215 | RowInput { | |
216 | label: String::from("bar"), | |
217 | entries: vec![] | |
218 | } | |
219 | ] | |
220 | ); | |
201b9ef3 SW |
221 | assert_eq!( |
222 | read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(), | |
223 | vec![RowInput { | |
224 | label: String::from("foo"), | |
e8657dff | 225 | entries: vec![Entry::from("bar")] |
201b9ef3 SW |
226 | }] |
227 | ); | |
228 | assert_eq!( | |
229 | read_rows(&b"foo\n bar\n baz\n"[..]) | |
230 | .flatten() | |
231 | .collect::<Vec<_>>(), | |
232 | vec![RowInput { | |
233 | label: String::from("foo"), | |
e8657dff | 234 | entries: vec![Entry::from("bar"), Entry::from("baz")] |
201b9ef3 SW |
235 | }] |
236 | ); | |
237 | assert_eq!( | |
238 | read_rows(&b"foo\n\nbar\n"[..]) | |
239 | .flatten() | |
240 | .collect::<Vec<_>>(), | |
241 | vec![ | |
242 | RowInput { | |
243 | label: String::from("foo"), | |
244 | entries: vec![] | |
245 | }, | |
246 | RowInput { | |
247 | label: String::from("bar"), | |
248 | entries: vec![] | |
249 | } | |
250 | ] | |
251 | ); | |
1f6bd845 SW |
252 | assert_eq!( |
253 | read_rows(&b"foo\n \nbar\n"[..]) | |
254 | .flatten() | |
255 | .collect::<Vec<_>>(), | |
256 | vec![ | |
257 | RowInput { | |
258 | label: String::from("foo"), | |
259 | entries: vec![] | |
260 | }, | |
261 | RowInput { | |
262 | label: String::from("bar"), | |
263 | entries: vec![] | |
264 | } | |
265 | ] | |
266 | ); | |
267 | assert_eq!( | |
268 | read_rows(&b"foo \n bar \n"[..]) | |
269 | .flatten() | |
270 | .collect::<Vec<_>>(), | |
271 | vec![RowInput { | |
272 | label: String::from("foo"), | |
e8657dff | 273 | entries: vec![Entry::from("bar")] |
1f6bd845 SW |
274 | }] |
275 | ); | |
201b9ef3 SW |
276 | |
277 | let bad = read_rows(&b" foo"[..]).next().unwrap(); | |
278 | assert!(bad.is_err()); | |
8110b492 | 279 | assert!(format!("{bad:?}").contains("1: Entry with no header")); |
201b9ef3 SW |
280 | |
281 | let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap(); | |
282 | assert!(bad2.is_err()); | |
8110b492 | 283 | assert!(format!("{bad2:?}").contains("3: Entry with no header")); |
75bb888a | 284 | } |
f272e502 SW |
285 | |
286 | #[test] | |
287 | fn test_column_counts() { | |
288 | assert_eq!( | |
289 | column_counts( | |
290 | &read_rows(&b"foo\n bar\n baz\n"[..]) | |
291 | .collect::<Result<Vec<_>, _>>() | |
292 | .unwrap() | |
293 | ), | |
58b5f36d | 294 | vec![(1, String::from("bar")), (1, String::from("baz"))] |
f272e502 SW |
295 | ); |
296 | assert_eq!( | |
297 | column_counts( | |
298 | &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]) | |
299 | .collect::<Result<Vec<_>, _>>() | |
300 | .unwrap() | |
301 | ), | |
58b5f36d | 302 | vec![(1, String::from("bar")), (2, String::from("baz"))] |
f272e502 | 303 | ); |
397ef957 SW |
304 | assert_eq!( |
305 | column_counts( | |
306 | &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]) | |
307 | .collect::<Result<Vec<_>, _>>() | |
308 | .unwrap() | |
309 | ), | |
58b5f36d | 310 | vec![(1, String::from("bar")), (2, String::from("baz"))] |
397ef957 | 311 | ); |
b8907770 SW |
312 | assert_eq!( |
313 | column_counts( | |
314 | &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]) | |
315 | .collect::<Result<Vec<_>, _>>() | |
316 | .unwrap() | |
317 | ), | |
318 | vec![(1, String::from("bar")), (2, String::from("baz"))] | |
319 | ); | |
f272e502 | 320 | } |
75bb888a | 321 | } |