X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/f915bc906e6b8001bc748a1a5f3b13fafadcb86b..9a6260204a9a4bfa4254719c30710ea827212877:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 1396c9b..bad71d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,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" )) } @@ -555,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!( @@ -566,7 +621,7 @@ mod tests { } ), HTML::from( - r#"nope + r#"nopebar "# ) );