+struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
+ input: std::iter::Enumerate<Input>,
+ row: Option<RowInput>,
+}
+impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
+ fn new(input: Input) -> Self {
+ Self {
+ input: input.enumerate(),
+ row: None,
+ }
+ }
+}
+impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
+ type Item = Result<RowInput, std::io::Error>;
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self
+ .input
+ .next()
+ .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
+ {
+ None => return Ok(std::mem::take(&mut self.row)).transpose(),
+ Some((_, Err(e))) => return Some(Err(e)),
+ Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
+ return Ok(std::mem::take(&mut self.row)).transpose()
+ }
+ Some((_, Ok(line))) if line.is_empty() => {}
+ Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
+ None => {
+ return Some(Err(std::io::Error::other(format!(
+ "{}: Entry with no header",
+ n + 1
+ ))))
+ }
+ Some(ref mut row) => row.entries.push(Entry::from(line.trim())),
+ },
+ Some((_, Ok(line))) => {
+ let prev = std::mem::take(&mut self.row);
+ self.row = Some(RowInput {
+ label: line,
+ entries: vec![],
+ });
+ if prev.is_some() {
+ return Ok(prev).transpose();
+ }
+ }
+ }
+ }
+ }
+}
+
+fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
+ Reader::new(std::io::BufReader::new(input).lines())
+}
+
+fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
+ let mut counts: Vec<_> = rows
+ .iter()
+ .flat_map(|r| {
+ r.entries
+ .iter()
+ .map(|e| &e.col)
+ .collect::<HashSet<_>>()
+ .into_iter()
+ })
+ .fold(HashMap::new(), |mut cs, col| {
+ cs.entry(String::from(col))
+ .and_modify(|n| *n += 1)
+ .or_insert(1);
+ cs
+ })
+ .into_iter()
+ .map(|(col, n)| (n, col))
+ .collect();
+ counts.sort();
+ counts
+}
+fn column_order(rows: &[RowInput]) -> Vec<String> {
+ column_counts(rows)
+ .into_iter()
+ .map(|(_, col)| col)
+ .collect()
+}
+
+fn render_instance(entry: &Entry) -> String {
+ match &entry.instance {
+ None => String::from("✓ "),
+ Some(instance) => String::from(instance) + " ",
+ }
+}
+
+fn render_cell(col: &str, row: &RowInput) -> String {
+ // TODO: Escape HTML special characters
+ let row_label = &row.label;
+ let entries: Vec<&Entry> = row.entries.iter().filter(|e| e.col == col).collect();
+ let class = if entries.is_empty() { "" } else { "yes" };
+ let all_empty = entries.iter().all(|e| e.instance.is_none());
+ let contents = if entries.is_empty() || (all_empty && entries.len() == 1) {
+ String::new()
+ } else if all_empty {
+ format!("{}", entries.len())
+ } else {
+ entries
+ .iter()
+ .map(|i| render_instance(i))
+ .collect::<String>()
+ };
+ format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col}')\" onmouseout=\"ch2('{row_label}','{col}')\">{}</td>", contents.trim())
+}
+
+fn render_row(columns: &[String], row: &RowInput) -> String {
+ // This is O(n^2) & doesn't need to be
+ // TODO: Escape HTML special characters
+ let row_label = &row.label;
+ format!(
+ "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
+ &columns
+ .iter()
+ .map(|col| render_cell(col, row))
+ .collect::<String>()
+ )
+}
+
+fn render_column_headers(columns: &[String]) -> String {
+ // TODO: Escape HTML special characters
+ String::from("<tr class=\"key\"><th></th>")
+ + &columns.iter().fold(String::new(), |mut acc, c| {
+ write!(&mut acc, "<th id=\"{c}\"><div><div>{c}</div></div></th>").unwrap();
+ acc
+ })
+ + "</tr>\n"