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