use std::borrow::ToOwned; use std::collections::{HashMap, HashSet}; use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; #[derive(PartialEq, Eq, Debug)] struct Config { column_threshold: usize, static_columns: Vec, } impl Config { fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> { if let Some(threshold) = cmd.strip_prefix("col_threshold ") { self.column_threshold = threshold.parse().map_err(|e| { std::io::Error::new( std::io::ErrorKind::InvalidInput, format!("line {line_num}: col_threshold must be numeric: {e}"), ) })?; } else if let Some(col) = cmd.strip_prefix("col ") { self.static_columns.push(col.to_owned()); } else { return Err(std::io::Error::new( std::io::ErrorKind::InvalidInput, format!("line {line_num}: Unknown command: {cmd}"), )); } Ok(()) } } const HEADER: &str = r#" "#; const FOOTER: &str = "
"; #[derive(PartialEq, Eq, Debug)] pub struct HTML(String); impl HTML { fn escape(value: &str) -> HTML { let mut escaped: String = String::new(); for c in value.chars() { match c { '>' => escaped.push_str(">"), '<' => escaped.push_str("<"), '\'' => escaped.push_str("'"), '"' => escaped.push_str("""), '&' => escaped.push_str("&"), ok_c => escaped.push(ok_c), } } HTML(escaped) } } impl From<&str> for HTML { fn from(value: &str) -> HTML { HTML(String::from(value)) } } impl FromIterator for HTML { fn from_iter(iter: T) -> HTML where T: IntoIterator, { HTML(iter.into_iter().map(|html| html.0).collect::()) } } impl std::fmt::Display for HTML { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } #[derive(Debug, PartialEq, Eq)] enum InputLine<'a> { Blank, RowHeader(&'a str), Entry(&'a str, Option<&'a str>), Command(&'a str), } impl<'a> From<&'a str> for InputLine<'a> { fn from(value: &'a str) -> InputLine<'a> { let trimmed = value.trim_end(); if trimmed.is_empty() { InputLine::Blank } else if let Some(cmd) = trimmed.strip_prefix('!') { InputLine::Command(cmd) } else if !trimmed.starts_with(' ') { InputLine::RowHeader(value.trim()) } else { match value.split_once(':') { None => InputLine::Entry(value.trim(), None), Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())), } } } } #[derive(Debug, PartialEq, Eq)] struct Row { label: String, entries: HashMap>>, } #[derive(Debug, PartialEq, Eq)] enum Rowlike { Row(Row), Spacer, } struct Reader<'cfg, Input: Iterator>> { input: std::iter::Enumerate, row: Option, config: &'cfg mut Config, } impl<'cfg, Input: Iterator>> Reader<'cfg, Input> { fn new(config: &'cfg mut Config, input: Input) -> Self { Self { input: input.enumerate(), row: None, config, } } } impl<'cfg, Input: Iterator>> Iterator for Reader<'cfg, Input> { type Item = Result; fn next(&mut self) -> Option { loop { match self.input.next() { None => return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose(), Some((_, Err(e))) => return Some(Err(e)), Some((n, Ok(line))) => match InputLine::from(line.as_ref()) { InputLine::Command(cmd) => { if let Err(e) = self.config.apply_command(n + 1, cmd) { return Some(Err(e)); } } InputLine::Blank if self.row.is_some() => { return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose() } InputLine::Blank => return Some(Ok(Rowlike::Spacer)), InputLine::Entry(col, instance) => match &mut self.row { None => { return Some(Err(std::io::Error::other(format!( "line {}: Entry with no header", n + 1 )))) } Some(ref mut row) => { row.entries .entry(col.to_owned()) .and_modify(|is| is.push(instance.map(ToOwned::to_owned))) .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]); } }, InputLine::RowHeader(row) => { let prev = std::mem::take(&mut self.row); self.row = Some(Row { label: row.to_owned(), entries: HashMap::new(), }); if prev.is_some() { return Ok(prev.map(Rowlike::Row)).transpose(); } } }, } } } } fn read_input(input: impl std::io::Read) -> Result<(Vec, Config), std::io::Error> { let mut config = Config { column_threshold: 2, static_columns: vec![], }; let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines()); reader .collect::, _>>() .map(|rows| (rows, config)) } fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> { let empty = HashMap::new(); let mut counts: Vec<_> = rows .iter() .flat_map(|rl| match rl { Rowlike::Row(r) => r.entries.keys(), Rowlike::Spacer => empty.keys(), }) .fold(HashMap::new(), |mut cs, col| { cs.entry(col.to_owned()) .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(config: &Config, rows: &[Rowlike]) -> Vec { let static_columns: HashSet<&str> = config .static_columns .iter() .map(std::string::String::as_str) .collect(); column_counts(rows) .into_iter() .filter_map(|(n, col)| { (n >= config.column_threshold && !static_columns.contains(col.as_str())).then_some(col) }) .collect() } fn render_one_instance(instance: &Option) -> HTML { match instance { None => HTML::from("✓"), Some(instance) => HTML::escape(instance.as_ref()), } } fn render_instances(instances: &[Option]) -> HTML { let all_empty = instances.iter().all(Option::is_none); if all_empty && instances.len() == 1 { HTML::from("") } else if all_empty { HTML(format!("{}", instances.len())) } else { HTML( instances .iter() .map(render_one_instance) .map(|html| html.0) // Waiting for slice_concat_trait to stabilize .collect::>() .join(" "), ) } } fn render_cell(col: &str, row: &mut Row) -> HTML { let row_label = HTML::escape(row.label.as_ref()); let col_label = HTML::escape(col); let instances: Option<&Vec>> = row.entries.get(col); let class = HTML::from(if instances.is_none() { "" } else { "yes" }); let contents = match instances { None => HTML::from(""), Some(is) => render_instances(is), }; row.entries.remove(col); HTML(format!( r#"{contents}"# )) } fn render_leftover(notcol: &str, instances: &[Option]) -> HTML { let label = HTML::escape(notcol); let rest = render_instances(instances); if rest == HTML::from("") { HTML(format!("{label}")) } else { HTML(format!("{label}: {rest}")) } } fn render_all_leftovers(row: &Row) -> HTML { let mut order: Vec<_> = row.entries.keys().collect(); order.sort_unstable(); HTML( order .into_iter() .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!"))) .map(|html| html.0) // Waiting for slice_concat_trait to stabilize .collect::>() .join(", "), ) } fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTML { match rowlike { Rowlike::Spacer => HTML::from("\n"), Rowlike::Row(row) => { let row_label = HTML::escape(row.label.as_ref()); let static_cells = config .static_columns .iter() .map(|col| render_cell(col, row)) .collect::(); let dynamic_cells = columns .iter() .map(|col| render_cell(col, row)) .collect::(); let leftovers = render_all_leftovers(row); HTML(format!( "{row_label}{static_cells}{dynamic_cells}{leftovers}\n" )) } } } fn render_column_headers(config: &Config, columns: &[String]) -> HTML { HTML( String::from(r#""#) + &config.static_columns.iter().chain(columns.iter()).fold( String::new(), |mut acc, col| { let col_header = HTML::escape(col.as_ref()); write!( &mut acc, r#"
{col_header}
"# ) .unwrap(); acc }, ) + "\n", ) } /// # Errors /// /// Will return `Err` if /// * there's an i/o error while reading `input` /// * the log has invalid syntax: /// * an indented line with no preceding non-indented line pub fn tablify(input: impl std::io::Read) -> Result { let (rows, config) = read_input(input)?; let columns = column_order(&config, &rows); Ok(HTML(format!( "{HEADER}{}{}{FOOTER}", render_column_headers(&config, &columns), rows.into_iter() .map(|mut r| render_row(&config, &columns, &mut r)) .collect::() ))) } #[cfg(test)] mod tests { use super::*; #[test] fn test_parse_line() { assert_eq!(InputLine::from(""), InputLine::Blank); assert_eq!(InputLine::from(" "), InputLine::Blank); assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo")); assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo")); assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None)); assert_eq!( InputLine::from(" foo:bar"), InputLine::Entry("foo", Some("bar")) ); assert_eq!( InputLine::from(" foo: bar"), InputLine::Entry("foo", Some("bar")) ); assert_eq!( InputLine::from(" foo: bar "), InputLine::Entry("foo", Some("bar")) ); assert_eq!( InputLine::from(" foo: bar "), InputLine::Entry("foo", Some("bar")) ); assert_eq!( InputLine::from(" foo : bar "), InputLine::Entry("foo", Some("bar")) ); } fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> { read_input(input).map(|(rows, _)| rows) } fn read_config(input: impl std::io::Read) -> Result { read_input(input).map(|(_, config)| config) } #[test] fn test_read_rows() { assert_eq!( read_rows(&b"foo"[..]).unwrap(), vec![Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::new(), })] ); assert_eq!( read_rows(&b"bar"[..]).unwrap(), vec![Rowlike::Row(Row { label: "bar".to_owned(), entries: HashMap::new(), })] ); assert_eq!( read_rows(&b"foo\nbar\n"[..]).unwrap(), vec![ Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::new(), }), Rowlike::Row(Row { label: "bar".to_owned(), entries: HashMap::new(), }) ] ); assert_eq!( read_rows(&b"foo\n bar\n"[..]).unwrap(), vec![Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), })] ); assert_eq!( read_rows(&b"foo\n bar\n baz\n"[..]).unwrap(), vec![Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::from([ ("bar".to_owned(), vec![None]), ("baz".to_owned(), vec![None]) ]), })] ); assert_eq!( read_rows(&b"foo\n\nbar\n"[..]).unwrap(), vec![ Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::new(), }), Rowlike::Row(Row { label: "bar".to_owned(), entries: HashMap::new(), }) ] ); assert_eq!( read_rows(&b"foo\n\n\nbar\n"[..]).unwrap(), vec![ Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::new(), }), Rowlike::Spacer, Rowlike::Row(Row { label: "bar".to_owned(), entries: HashMap::new(), }) ] ); assert_eq!( read_rows(&b"foo\n \nbar\n"[..]).unwrap(), vec![ Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::new(), }), Rowlike::Row(Row { label: "bar".to_owned(), entries: HashMap::new(), }) ] ); assert_eq!( read_rows(&b"foo \n bar \n"[..]).unwrap(), vec![Rowlike::Row(Row { label: "foo".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), })] ); let bad = read_rows(&b" foo"[..]); assert!(bad.is_err()); assert!(format!("{bad:?}").contains("line 1: Entry with no header")); let bad2 = read_rows(&b"foo\n\n bar"[..]); assert!(bad2.is_err()); assert!(format!("{bad2:?}").contains("line 3: Entry with no header")); } #[test] fn test_read_config() { assert_eq!( read_config(&b"!col_threshold 10"[..]) .unwrap() .column_threshold, 10 ); assert_eq!( read_config(&b"!col foo"[..]).unwrap().static_columns, vec!["foo".to_owned()] ); let bad_command = read_config(&b"!no such command"[..]); assert!(bad_command.is_err()); assert!(format!("{bad_command:?}").contains("line 1: Unknown command")); let bad_num = read_config(&b"!col_threshold foo"[..]); assert!(bad_num.is_err()); assert!(format!("{bad_num:?}").contains("line 1: col_threshold must be numeric")); } #[test] fn test_column_counts() { assert_eq!( column_counts(&read_rows(&b"foo\n bar\n baz\n"[..]).unwrap()), vec![(1, String::from("bar")), (1, String::from("baz"))] ); assert_eq!( column_counts(&read_rows(&b"foo\n bar\n baz\nquux\n baz"[..]).unwrap()), vec![(2, String::from("baz")), (1, String::from("bar"))] ); assert_eq!( column_counts(&read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..]).unwrap()), vec![(2, String::from("baz")), (1, String::from("bar"))] ); assert_eq!( column_counts( &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..]).unwrap() ), vec![(2, String::from("baz")), (1, String::from("bar"))] ); } #[test] fn test_render_cell() { assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::new(), } ), HTML::from( r#""# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), } ), HTML::from( r#""# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), HTML::from( r#""# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None, None])]), } ), HTML::from( r#"2"# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::from([( "foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())] )]), } ), HTML::from( r#"5 10"# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]), } ), HTML::from( r#"5 ✓"# ) ); assert_eq!( render_cell( "heart", &mut Row { label: "nope".to_owned(), entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]), } ), HTML::from( r#"<3"# ) ); assert_eq!( render_cell( "foo", &mut Row { label: "bob's".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), HTML::from( r#""# ) ); let mut r = Row { label: "nope".to_owned(), entries: HashMap::from([ ("foo".to_owned(), vec![None]), ("baz".to_owned(), vec![None]), ]), }; assert_eq!(r.entries.len(), 2); render_cell("foo", &mut r); assert_eq!(r.entries.len(), 1); render_cell("bar", &mut r); assert_eq!(r.entries.len(), 1); render_cell("baz", &mut r); assert_eq!(r.entries.len(), 0); } #[test] fn test_render_leftovers() { assert_eq!( render_all_leftovers(&Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), }), HTML::from("foo") ); assert_eq!( render_all_leftovers(&Row { label: "nope".to_owned(), entries: HashMap::from([ ("foo".to_owned(), vec![None]), ("bar".to_owned(), vec![None]) ]), }), HTML::from("bar, foo") ); assert_eq!( render_all_leftovers(&Row { label: "nope".to_owned(), entries: HashMap::from([ ("foo".to_owned(), vec![None]), ("bar".to_owned(), vec![None, None]) ]), }), HTML::from("bar: 2, foo") ); } #[test] fn test_render_row() { assert_eq!( render_row( &Config { column_threshold: 0, static_columns: vec![], }, &["foo".to_owned()], &mut Rowlike::Row(Row { label: "nope".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), }) ), HTML::from( r#"nopebar "# ) ); assert_eq!( render_row( &Config { column_threshold: 0, static_columns: vec!["foo".to_owned(), "bar".to_owned()], }, &["baz".to_owned()], &mut Rowlike::Row(Row { label: "nope".to_owned(), entries: HashMap::from([ ("bar".to_owned(), vec![Some("r".to_owned())]), ("baz".to_owned(), vec![Some("z".to_owned())]), ("foo".to_owned(), vec![Some("f".to_owned())]), ]), }) ), HTML::from( r#"nopefrz "# ) ); } }