1 use std::collections::{HashMap, HashSet};
3 use std::iter::Iterator;
5 const HEADER: &str = "<!DOCTYPE html>
8 <meta charset=\"utf-8\">
9 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
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; }
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); }
36 const FOOTER: &str = " </tbody>
41 #[derive(Debug, PartialEq, Eq, Hash)]
44 instance: Option<String>,
46 impl From<&str> for Entry {
47 fn from(value: &str) -> Entry {
48 match value.split_once(':') {
50 col: String::from(value),
53 Some((col, instance)) => Entry {
54 col: String::from(col.trim()),
55 instance: Some(String::from(instance.trim())),
61 #[derive(Debug, PartialEq, Eq)]
67 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
68 input: std::iter::Enumerate<Input>,
69 row: Option<RowInput>,
71 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
72 fn new(input: Input) -> Self {
74 input: input.enumerate(),
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> {
86 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
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()
93 Some((_, Ok(line))) if line.is_empty() => {}
94 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
96 return Some(Err(std::io::Error::other(format!(
97 "{}: Entry with no header",
101 Some(ref mut row) => row.entries.push(Entry::from(line.trim())),
103 Some((_, Ok(line))) => {
104 let prev = std::mem::take(&mut self.row);
105 self.row = Some(RowInput {
110 return Ok(prev).transpose();
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())
122 fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
123 let mut counts: Vec<_> = rows
129 .collect::<HashSet<_>>()
132 .fold(HashMap::new(), |mut cs, col| {
133 cs.entry(String::from(col))
134 .and_modify(|n| *n += 1)
139 .map(|(col, n)| (n, col))
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
151 pub 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)
162 fn test_parse_entry() {
166 col: String::from("foo"),
171 Entry::from("foo:bar"),
173 col: String::from("foo"),
174 instance: Some(String::from("bar"))
178 Entry::from("foo: bar"),
180 col: String::from("foo"),
181 instance: Some(String::from("bar"))
187 fn test_read_rows() {
189 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
191 label: String::from("foo"),
196 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
198 label: String::from("bar"),
203 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
206 label: String::from("foo"),
210 label: String::from("bar"),
216 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
218 label: String::from("foo"),
219 entries: vec![Entry::from("bar")]
223 read_rows(&b"foo\n bar\n baz\n"[..])
225 .collect::<Vec<_>>(),
227 label: String::from("foo"),
228 entries: vec![Entry::from("bar"), Entry::from("baz")]
232 read_rows(&b"foo\n\nbar\n"[..])
234 .collect::<Vec<_>>(),
237 label: String::from("foo"),
241 label: String::from("bar"),
247 read_rows(&b"foo\n \nbar\n"[..])
249 .collect::<Vec<_>>(),
252 label: String::from("foo"),
256 label: String::from("bar"),
262 read_rows(&b"foo \n bar \n"[..])
264 .collect::<Vec<_>>(),
266 label: String::from("foo"),
267 entries: vec![Entry::from("bar")]
271 let bad = read_rows(&b" foo"[..]).next().unwrap();
272 assert!(bad.is_err());
273 assert!(format!("{bad:?}").contains("1: Entry with no header"));
275 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
276 assert!(bad2.is_err());
277 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
281 fn test_column_counts() {
284 &read_rows(&b"foo\n bar\n baz\n"[..])
285 .collect::<Result<Vec<_>, _>>()
288 vec![(1, String::from("bar")), (1, String::from("baz"))]
292 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
293 .collect::<Result<Vec<_>, _>>()
296 vec![(1, String::from("bar")), (2, String::from("baz"))]
300 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
301 .collect::<Result<Vec<_>, _>>()
304 vec![(1, String::from("bar")), (2, String::from("baz"))]
308 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
309 .collect::<Result<Vec<_>, _>>()
312 vec![(1, String::from("bar")), (2, String::from("baz"))]