]> git.scottworley.com Git - tablify/blame_incremental - src/lib.rs
Split entries at ':' to separate instance data
[tablify] / src / lib.rs
... / ...
CommitLineData
1use std::collections::{HashMap, HashSet};
2use std::io::BufRead;
3use std::iter::Iterator;
4
5const 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>";
36const FOOTER: &str = " </tbody>
37 </table>
38</body>
39</html>";
40
41#[derive(Debug, PartialEq, Eq, Hash)]
42struct Entry {
43 col: String,
44 instance: Option<String>,
45}
46impl From<&str> for Entry {
47 fn from(value: &str) -> Entry {
48 match value.split_once(':') {
49 None => Entry {
50 col: String::from(value),
51 instance: None,
52 },
53 Some((col, instance)) => Entry {
54 col: String::from(col),
55 instance: Some(String::from(instance)),
56 },
57 }
58 }
59}
60
61#[derive(Debug, PartialEq, Eq)]
62struct RowInput {
63 label: String,
64 entries: Vec<Entry>,
65}
66
67struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
68 input: std::iter::Enumerate<Input>,
69 row: Option<RowInput>,
70}
71impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
72 fn new(input: Input) -> Self {
73 Self {
74 input: input.enumerate(),
75 row: None,
76 }
77 }
78}
79impl<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 {
83 match self
84 .input
85 .next()
86 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
87 {
88 None => return Ok(std::mem::take(&mut self.row)).transpose(),
89 Some((_, Err(e))) => return Some(Err(e)),
90 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
91 return Ok(std::mem::take(&mut self.row)).transpose()
92 }
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 }
101 Some(ref mut row) => row.entries.push(Entry::from(line.trim())),
102 },
103 Some((_, Ok(line))) => {
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
118fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
119 Reader::new(std::io::BufReader::new(input).lines())
120}
121
122fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
123 let mut counts: Vec<_> = rows
124 .iter()
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))
134 .and_modify(|n| *n += 1)
135 .or_insert(1);
136 cs
137 })
138 .into_iter()
139 .map(|(col, n)| (n, col))
140 .collect();
141 counts.sort();
142 counts
143}
144
145/// # Errors
146///
147/// Will return `Err` if
148/// * there's an i/o error while reading `input`
149/// * the log has invalid syntax:
150/// * an indented line with no preceding non-indented line
151pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> {
152 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
153 let _columns = column_counts(&rows);
154 Ok(String::from(HEADER) + "Hello, world!" + FOOTER)
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_parse_entry() {
163 assert_eq!(
164 Entry::from("foo"),
165 Entry {
166 col: String::from("foo"),
167 instance: None
168 }
169 );
170 assert_eq!(
171 Entry::from("foo:bar"),
172 Entry {
173 col: String::from("foo"),
174 instance: Some(String::from("bar"))
175 }
176 );
177 }
178
179 #[test]
180 fn test_read_rows() {
181 assert_eq!(
182 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
183 vec![RowInput {
184 label: String::from("foo"),
185 entries: vec![]
186 }]
187 );
188 assert_eq!(
189 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
190 vec![RowInput {
191 label: String::from("bar"),
192 entries: vec![]
193 }]
194 );
195 assert_eq!(
196 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
197 vec![
198 RowInput {
199 label: String::from("foo"),
200 entries: vec![]
201 },
202 RowInput {
203 label: String::from("bar"),
204 entries: vec![]
205 }
206 ]
207 );
208 assert_eq!(
209 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
210 vec![RowInput {
211 label: String::from("foo"),
212 entries: vec![Entry::from("bar")]
213 }]
214 );
215 assert_eq!(
216 read_rows(&b"foo\n bar\n baz\n"[..])
217 .flatten()
218 .collect::<Vec<_>>(),
219 vec![RowInput {
220 label: String::from("foo"),
221 entries: vec![Entry::from("bar"), Entry::from("baz")]
222 }]
223 );
224 assert_eq!(
225 read_rows(&b"foo\n\nbar\n"[..])
226 .flatten()
227 .collect::<Vec<_>>(),
228 vec![
229 RowInput {
230 label: String::from("foo"),
231 entries: vec![]
232 },
233 RowInput {
234 label: String::from("bar"),
235 entries: vec![]
236 }
237 ]
238 );
239 assert_eq!(
240 read_rows(&b"foo\n \nbar\n"[..])
241 .flatten()
242 .collect::<Vec<_>>(),
243 vec![
244 RowInput {
245 label: String::from("foo"),
246 entries: vec![]
247 },
248 RowInput {
249 label: String::from("bar"),
250 entries: vec![]
251 }
252 ]
253 );
254 assert_eq!(
255 read_rows(&b"foo \n bar \n"[..])
256 .flatten()
257 .collect::<Vec<_>>(),
258 vec![RowInput {
259 label: String::from("foo"),
260 entries: vec![Entry::from("bar")]
261 }]
262 );
263
264 let bad = read_rows(&b" foo"[..]).next().unwrap();
265 assert!(bad.is_err());
266 assert!(format!("{bad:?}").contains("1: Entry with no header"));
267
268 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
269 assert!(bad2.is_err());
270 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
271 }
272
273 #[test]
274 fn test_column_counts() {
275 assert_eq!(
276 column_counts(
277 &read_rows(&b"foo\n bar\n baz\n"[..])
278 .collect::<Result<Vec<_>, _>>()
279 .unwrap()
280 ),
281 vec![(1, String::from("bar")), (1, String::from("baz"))]
282 );
283 assert_eq!(
284 column_counts(
285 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
286 .collect::<Result<Vec<_>, _>>()
287 .unwrap()
288 ),
289 vec![(1, String::from("bar")), (2, String::from("baz"))]
290 );
291 assert_eq!(
292 column_counts(
293 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
294 .collect::<Result<Vec<_>, _>>()
295 .unwrap()
296 ),
297 vec![(1, String::from("bar")), (2, String::from("baz"))]
298 );
299 assert_eq!(
300 column_counts(
301 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
302 .collect::<Result<Vec<_>, _>>()
303 .unwrap()
304 ),
305 vec![(1, String::from("bar")), (2, String::from("baz"))]
306 );
307 }
308}