X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/58c0a717743d262025e664eb6ce79ad9cd4c7968..9a6260204a9a4bfa4254719c30710ea827212877:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index f1da5f7..bad71d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,29 +194,32 @@ fn render_one_instance(instance: &Option) -> HTML { } } -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 all_empty = instances - .iter() - .flat_map(|is| is.iter()) - .all(Option::is_none); - let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) { +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.unwrap().len())) + HTML(format!("{}", instances.len())) } else { HTML( instances - .unwrap() .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!( @@ -224,14 +227,38 @@ fn render_cell(col: &str, row: &mut Row) -> HTML { )) } +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(columns: &[String], row: &mut Row) -> HTML { let row_label = HTML::escape(row.label.as_ref()); let cells = columns .iter() .map(|col| render_cell(col, row)) .collect::(); + let leftovers = render_all_leftovers(row); HTML(format!( - "{row_label}{cells}\n" + "{row_label}{cells}{leftovers}\n" )) } @@ -552,6 +579,37 @@ mod tests { 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!( @@ -563,7 +621,7 @@ mod tests { } ), HTML::from( - r#"nope + r#"nopebar "# ) );