]>
Commit | Line | Data |
---|---|---|
1 | use std::collections::{HashMap, HashSet}; | |
2 | use std::io::BufRead; | |
3 | use std::iter::Iterator; | |
4 | ||
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 | ||
41 | #[derive(Debug, PartialEq, Eq, Hash)] | |
42 | struct Entry { | |
43 | col: String, | |
44 | instance: String, | |
45 | } | |
46 | impl From<&str> for Entry { | |
47 | fn from(value: &str) -> Entry { | |
48 | Entry { | |
49 | col: String::from(value), | |
50 | instance: String::from(""), | |
51 | } | |
52 | } | |
53 | } | |
54 | ||
55 | #[derive(Debug, PartialEq, Eq)] | |
56 | struct RowInput { | |
57 | label: String, | |
58 | entries: Vec<Entry>, | |
59 | } | |
60 | ||
61 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { | |
62 | input: std::iter::Enumerate<Input>, | |
63 | row: Option<RowInput>, | |
64 | } | |
65 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> { | |
66 | fn new(input: Input) -> Self { | |
67 | Self { | |
68 | input: input.enumerate(), | |
69 | row: None, | |
70 | } | |
71 | } | |
72 | } | |
73 | impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> { | |
74 | type Item = Result<RowInput, std::io::Error>; | |
75 | fn next(&mut self) -> Option<Self::Item> { | |
76 | loop { | |
77 | match self | |
78 | .input | |
79 | .next() | |
80 | .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end())))) | |
81 | { | |
82 | None => return Ok(std::mem::take(&mut self.row)).transpose(), | |
83 | Some((_, Err(e))) => return Some(Err(e)), | |
84 | Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => { | |
85 | return Ok(std::mem::take(&mut self.row)).transpose() | |
86 | } | |
87 | Some((_, Ok(line))) if line.is_empty() => {} | |
88 | Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row { | |
89 | None => { | |
90 | return Some(Err(std::io::Error::other(format!( | |
91 | "{}: Entry with no header", | |
92 | n + 1 | |
93 | )))) | |
94 | } | |
95 | Some(ref mut row) => row.entries.push(Entry::from(line.trim())), | |
96 | }, | |
97 | Some((_, Ok(line))) => { | |
98 | let prev = std::mem::take(&mut self.row); | |
99 | self.row = Some(RowInput { | |
100 | label: line, | |
101 | entries: vec![], | |
102 | }); | |
103 | if prev.is_some() { | |
104 | return Ok(prev).transpose(); | |
105 | } | |
106 | } | |
107 | } | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> { | |
113 | Reader::new(std::io::BufReader::new(input).lines()) | |
114 | } | |
115 | ||
116 | fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> { | |
117 | let mut counts: Vec<_> = rows | |
118 | .iter() | |
119 | .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter()) | |
120 | .fold(HashMap::new(), |mut cs, e| { | |
121 | cs.entry(String::from(&e.col)) | |
122 | .and_modify(|n| *n += 1) | |
123 | .or_insert(1); | |
124 | cs | |
125 | }) | |
126 | .into_iter() | |
127 | .map(|(col, n)| (n, col)) | |
128 | .collect(); | |
129 | counts.sort(); | |
130 | counts | |
131 | } | |
132 | ||
133 | /// # Errors | |
134 | /// | |
135 | /// Will return `Err` if | |
136 | /// * there's an i/o error while reading `input` | |
137 | /// * the log has invalid syntax: | |
138 | /// * an indented line with no preceding non-indented line | |
139 | pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> { | |
140 | let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?; | |
141 | let _columns = column_counts(&rows); | |
142 | Ok(String::from(HEADER) + "Hello, world!" + FOOTER) | |
143 | } | |
144 | ||
145 | #[cfg(test)] | |
146 | mod tests { | |
147 | use super::*; | |
148 | ||
149 | #[test] | |
150 | fn test_read_rows() { | |
151 | assert_eq!( | |
152 | read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(), | |
153 | vec![RowInput { | |
154 | label: String::from("foo"), | |
155 | entries: vec![] | |
156 | }] | |
157 | ); | |
158 | assert_eq!( | |
159 | read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(), | |
160 | vec![RowInput { | |
161 | label: String::from("bar"), | |
162 | entries: vec![] | |
163 | }] | |
164 | ); | |
165 | assert_eq!( | |
166 | read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(), | |
167 | vec![ | |
168 | RowInput { | |
169 | label: String::from("foo"), | |
170 | entries: vec![] | |
171 | }, | |
172 | RowInput { | |
173 | label: String::from("bar"), | |
174 | entries: vec![] | |
175 | } | |
176 | ] | |
177 | ); | |
178 | assert_eq!( | |
179 | read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(), | |
180 | vec![RowInput { | |
181 | label: String::from("foo"), | |
182 | entries: vec![Entry::from("bar")] | |
183 | }] | |
184 | ); | |
185 | assert_eq!( | |
186 | read_rows(&b"foo\n bar\n baz\n"[..]) | |
187 | .flatten() | |
188 | .collect::<Vec<_>>(), | |
189 | vec![RowInput { | |
190 | label: String::from("foo"), | |
191 | entries: vec![Entry::from("bar"), Entry::from("baz")] | |
192 | }] | |
193 | ); | |
194 | assert_eq!( | |
195 | read_rows(&b"foo\n\nbar\n"[..]) | |
196 | .flatten() | |
197 | .collect::<Vec<_>>(), | |
198 | vec![ | |
199 | RowInput { | |
200 | label: String::from("foo"), | |
201 | entries: vec![] | |
202 | }, | |
203 | RowInput { | |
204 | label: String::from("bar"), | |
205 | entries: vec![] | |
206 | } | |
207 | ] | |
208 | ); | |
209 | assert_eq!( | |
210 | read_rows(&b"foo\n \nbar\n"[..]) | |
211 | .flatten() | |
212 | .collect::<Vec<_>>(), | |
213 | vec![ | |
214 | RowInput { | |
215 | label: String::from("foo"), | |
216 | entries: vec![] | |
217 | }, | |
218 | RowInput { | |
219 | label: String::from("bar"), | |
220 | entries: vec![] | |
221 | } | |
222 | ] | |
223 | ); | |
224 | assert_eq!( | |
225 | read_rows(&b"foo \n bar \n"[..]) | |
226 | .flatten() | |
227 | .collect::<Vec<_>>(), | |
228 | vec![RowInput { | |
229 | label: String::from("foo"), | |
230 | entries: vec![Entry::from("bar")] | |
231 | }] | |
232 | ); | |
233 | ||
234 | let bad = read_rows(&b" foo"[..]).next().unwrap(); | |
235 | assert!(bad.is_err()); | |
236 | assert!(format!("{bad:?}").contains("1: Entry with no header")); | |
237 | ||
238 | let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap(); | |
239 | assert!(bad2.is_err()); | |
240 | assert!(format!("{bad2:?}").contains("3: Entry with no header")); | |
241 | } | |
242 | ||
243 | #[test] | |
244 | fn test_column_counts() { | |
245 | assert_eq!( | |
246 | column_counts( | |
247 | &read_rows(&b"foo\n bar\n baz\n"[..]) | |
248 | .collect::<Result<Vec<_>, _>>() | |
249 | .unwrap() | |
250 | ), | |
251 | vec![(1, String::from("bar")), (1, String::from("baz"))] | |
252 | ); | |
253 | assert_eq!( | |
254 | column_counts( | |
255 | &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]) | |
256 | .collect::<Result<Vec<_>, _>>() | |
257 | .unwrap() | |
258 | ), | |
259 | vec![(1, String::from("bar")), (2, String::from("baz"))] | |
260 | ); | |
261 | assert_eq!( | |
262 | column_counts( | |
263 | &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]) | |
264 | .collect::<Result<Vec<_>, _>>() | |
265 | .unwrap() | |
266 | ), | |
267 | vec![(1, String::from("bar")), (2, String::from("baz"))] | |
268 | ); | |
269 | } | |
270 | } |