X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/03b61def1a7e9d1c4dd09d1ffaf5558658afd661..1532a3826b3ce55682d5f312be3244d1c6fefe74:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 4773b88..1807333 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,15 +4,15 @@ use std::fmt::Write; use std::io::BufRead; use std::iter::Iterator; -fn tally_marks(n: usize, mark: Option<&str>) -> String { - match mark { +fn tally_marks(n: usize, mark: Option<&str>) -> HTML { + HTML(match mark { None => { let fives = { 0..n / 5 }.map(|_| '𝍸'); let ones = { 0..n % 5 }.map(|_| '𝍷'); fives.chain(ones).collect() } Some(m) => { 0..n }.map(|_| m).collect(), - } + }) } #[derive(PartialEq, Eq, Debug)] @@ -224,7 +224,7 @@ impl>> Iterator for Reader 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 { + InputLine::Entry(col, instance) => match self.row { None => { return Some(Err(std::io::Error::other(format!( "line {}: Entry with no header", @@ -266,8 +266,8 @@ 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(), + .flat_map(|rl| match *rl { + Rowlike::Row(ref r) => r.entries.keys(), Rowlike::Spacer => empty.keys(), }) .fold(HashMap::new(), |mut cs, col| { @@ -279,7 +279,7 @@ fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> { .into_iter() .map(|(col, n)| (n, col)) .collect(); - counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol))); + counts.sort_unstable_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1))); counts } fn column_order(config: &Config, rows: &[Rowlike]) -> Vec { @@ -304,11 +304,11 @@ fn render_instances(instances: &[Option], mark: Option<&str>) -> HTML { let mut tally = 0; let mut out = vec![]; for ins in instances { - match ins { + match *ins { None => tally += 1, - Some(content) => { + Some(ref content) => { if tally > 0 { - out.push(HTML(tally_marks(tally, mark))); + out.push(tally_marks(tally, mark)); tally = 0; } out.push(HTML::escape(content)); @@ -316,11 +316,11 @@ fn render_instances(instances: &[Option], mark: Option<&str>) -> HTML { } } if tally > 0 { - out.push(HTML(tally_marks(tally, mark))); + out.push(tally_marks(tally, mark)); } HTML( out.into_iter() - .map(|html| html.0) // Waiting for slice_concat_trait to stabilize + .map(|html| html.0) .collect::>() .join(" "), ) @@ -337,9 +337,9 @@ fn render_cell(config: &Config, col: &str, row: &mut Row) -> HTML { let instances: Option<&Vec>> = row.entries.get(col); let is_empty = match instances { None => true, - Some(is) => is.iter().all(|ins| match ins { + Some(is) => is.iter().all(|ins| match *ins { None => false, - Some(content) => content == "×", + Some(ref content) => content == "×", }), }; let class = HTML::from(if is_empty { "" } else { r#" class="yes""# }); @@ -380,23 +380,23 @@ fn render_all_leftovers(config: &Config, row: &Row) -> HTML { row.entries.get(notcol).expect("Key vanished?!"), ) }) - .map(|html| html.0) // Waiting for slice_concat_trait to stabilize + .map(|html| html.0) .collect::>() .join(", "), ) } fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTML { - match rowlike { + match *rowlike { Rowlike::Spacer => HTML::from("\n"), - Rowlike::Row(row) => { + Rowlike::Row(ref mut row) => { let row_label = HTML::escape(row.label.as_ref()); let static_cells = config .static_columns .iter() - .map(|ocol| match ocol { - Some(col) if config.hidden_columns.contains(col) => HTML::from(""), - Some(col) => render_cell(config, col, row), + .map(|ocol| match *ocol { + Some(ref col) if config.hidden_columns.contains(col) => HTML::from(""), + Some(ref col) => render_cell(config, col, row), None => HTML::from(r#""#), }) .collect::(); @@ -421,7 +421,7 @@ fn column_header_labels<'a>( let dynamic_columns = columns.iter().map(Some); static_columns .chain(dynamic_columns) - .filter(|ocol| ocol.map_or(true, |col| !config.hidden_columns.contains(col))) + .filter(|ocol| ocol.is_none_or(|col| !config.hidden_columns.contains(col))) .map(|ocol| { ocol.map(|col| match config.substitute_labels.get(col) { None => col, @@ -457,7 +457,7 @@ fn render_column_headers(config: &Config, columns: &[String]) -> HTML { /// * 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 { +pub fn tablify(input: R) -> Result { let (rows, config) = read_input(input)?; let columns = column_order(&config, &rows); Ok(HTML(format!( @@ -504,22 +504,22 @@ mod tests { #[test] fn test_tally_marks() { - assert_eq!(tally_marks(1, None), "𝍷"); - assert_eq!(tally_marks(2, None), "𝍷𝍷"); - assert_eq!(tally_marks(3, None), "𝍷𝍷𝍷"); - assert_eq!(tally_marks(4, None), "𝍷𝍷𝍷𝍷"); - assert_eq!(tally_marks(5, None), "𝍸"); - assert_eq!(tally_marks(6, None), "𝍸𝍷"); - assert_eq!(tally_marks(7, None), "𝍸𝍷𝍷"); - assert_eq!(tally_marks(8, None), "𝍸𝍷𝍷𝍷"); - assert_eq!(tally_marks(9, None), "𝍸𝍷𝍷𝍷𝍷"); - assert_eq!(tally_marks(10, None), "𝍸𝍸"); - assert_eq!(tally_marks(11, None), "𝍸𝍸𝍷"); - assert_eq!(tally_marks(1, Some("x")), "x"); - assert_eq!(tally_marks(4, Some("x")), "xxxx"); - assert_eq!(tally_marks(5, Some("x")), "xxxxx"); - assert_eq!(tally_marks(6, Some("x")), "xxxxxx"); - assert_eq!(tally_marks(2, Some("🍔")), "🍔🍔"); + assert_eq!(tally_marks(1, None), "𝍷".into()); + assert_eq!(tally_marks(2, None), "𝍷𝍷".into()); + assert_eq!(tally_marks(3, None), "𝍷𝍷𝍷".into()); + assert_eq!(tally_marks(4, None), "𝍷𝍷𝍷𝍷".into()); + assert_eq!(tally_marks(5, None), "𝍸".into()); + assert_eq!(tally_marks(6, None), "𝍸𝍷".into()); + assert_eq!(tally_marks(7, None), "𝍸𝍷𝍷".into()); + assert_eq!(tally_marks(8, None), "𝍸𝍷𝍷𝍷".into()); + assert_eq!(tally_marks(9, None), "𝍸𝍷𝍷𝍷𝍷".into()); + assert_eq!(tally_marks(10, None), "𝍸𝍸".into()); + assert_eq!(tally_marks(11, None), "𝍸𝍸𝍷".into()); + assert_eq!(tally_marks(1, Some("x")), "x".into()); + assert_eq!(tally_marks(4, Some("x")), "xxxx".into()); + assert_eq!(tally_marks(5, Some("x")), "xxxxx".into()); + assert_eq!(tally_marks(6, Some("x")), "xxxxxx".into()); + assert_eq!(tally_marks(2, Some("🍔")), "🍔🍔".into()); } fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> {