+struct Reader<'a, Input: Iterator<Item = Result<String, std::io::Error>>> {
+ input: std::iter::Enumerate<Input>,
+ row: Option<RowInput<'a>>,
+}
+impl<'a, Input: Iterator<Item = Result<String, std::io::Error>>> Reader<'a, Input> {
+ fn new(input: Input) -> Self {
+ Self {
+ input: input.enumerate(),
+ row: None,
+ }
+ }
+}
+impl<'a, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<'a, Input> {
+ type Item = Result<RowInput<'a>, std::io::Error>;
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self.input.next() {
+ None => return Ok(std::mem::take(&mut self.row)).transpose(),
+ Some((_, Err(e))) => return Some(Err(e)),
+ Some((_, Ok(line))) if line.trim_end().is_empty() && self.row.is_some() => {
+ return Ok(std::mem::take(&mut self.row)).transpose()
+ }
+ Some((_, Ok(line))) if line.trim_end().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
+ ))))
+ }
+ // TODO: Don't leak
+ Some(ref mut row) => row.entries.push(Entry::from(line.leak().trim())),
+ },
+ Some((_, Ok(line))) => {
+ let prev = std::mem::take(&mut self.row);
+ self.row = Some(RowInput {
+ // TODO: Don't leak
+ label: line.leak().trim_end(),
+ entries: vec![],
+ });
+ if prev.is_some() {
+ return Ok(prev).transpose();
+ }
+ }
+ }
+ }
+ }
+}
+
+fn read_rows(
+ input: impl std::io::Read,
+) -> impl Iterator<Item = Result<RowInput<'static>, 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_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
+ counts
+}
+fn column_order(rows: &[RowInput]) -> Vec<String> {
+ column_counts(rows)
+ .into_iter()
+ .map(|(_, col)| col)
+ .collect()
+}
+
+fn render_instance(entry: &Entry) -> HTML {
+ match &entry.instance {
+ None => HTML::from("✓"),
+ Some(instance) => HTML::escape(instance.as_ref()),
+ }
+}
+
+fn render_cell(col: &str, row: &RowInput) -> HTML {
+ let row_label = HTML::escape(row.label.as_ref());
+ let col_label = HTML::escape(col);
+ let entries: Vec<&Entry> = row.entries.iter().filter(|e| e.col == col).collect();
+ let class = HTML::from(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) {
+ HTML::from("")
+ } else if all_empty {
+ HTML(format!("{}", entries.len()))
+ } else {
+ HTML(
+ entries
+ .iter()
+ .map(|i| render_instance(i))
+ .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
+ .collect::<Vec<_>>()
+ .join(" "),
+ )
+ };
+ HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
+}
+
+fn render_row(columns: &[String], row: &RowInput) -> HTML {
+ // This is O(n^2) & doesn't need to be
+ let row_label = HTML::escape(row.label.as_ref());
+ HTML(format!(
+ "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
+ &columns
+ .iter()
+ .map(|col| render_cell(col, row))
+ .collect::<HTML>()
+ ))
+}
+
+fn render_column_headers(columns: &[String]) -> HTML {
+ HTML(
+ String::from("<tr class=\"key\"><th></th>")
+ + &columns.iter().fold(String::new(), |mut acc, col| {
+ let col_header = HTML::escape(col.as_ref());
+ write!(
+ &mut acc,
+ "<th id=\"{col_header}\"><div><div>{col_header}</div></div></th>"
+ )
+ .unwrap();
+ acc
+ })
+ + "</tr>\n",
+ )