X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/28cf4fa29f4e47d8b78894fbe86b175e0ab292be..5ffe8e3a8394fc46b50659b0e197b0b0a22e18c8:/src/lib.rs?ds=sidebyside diff --git a/src/lib.rs b/src/lib.rs index 3e0efd0..cfaeb4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,11 +6,11 @@ use std::iter::Iterator; pub struct Config {} -const HEADER: &str = " +const HEADER: &str = r#" - - + + @@ -35,7 +35,7 @@ const HEADER: &str = " -"; +"#; const FOOTER: &str = "
@@ -194,7 +194,7 @@ fn render_instance(instance: &Option) -> HTML { } } -fn render_cell(col: &str, row: &Row) -> 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); @@ -218,10 +218,13 @@ fn render_cell(col: &str, row: &Row) -> HTML { .join(" "), ) }; - HTML(format!("{contents}")) + row.entries.remove(col); + HTML(format!( + r#"{contents}"# + )) } -fn render_row(columns: &[String], row: &Row) -> HTML { +fn render_row(columns: &[String], row: &mut Row) -> HTML { let row_label = HTML::escape(row.label.as_ref()); HTML(format!( "{row_label}{}\n", @@ -234,12 +237,12 @@ fn render_row(columns: &[String], row: &Row) -> HTML { fn render_column_headers(columns: &[String]) -> HTML { HTML( - String::from("") + String::from(r#""#) + &columns.iter().fold(String::new(), |mut acc, col| { let col_header = HTML::escape(col.as_ref()); write!( &mut acc, - "
{col_header}
" + r#"
{col_header}
"# ) .unwrap(); acc @@ -261,7 +264,7 @@ pub fn tablify(config: &Config, input: impl std::io::Read) -> Result() ))) } @@ -437,82 +440,115 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::new(), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), - HTML::from("") + HTML::from( + r#""# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None, None])]), } ), - HTML::from("2") + HTML::from( + r#"2"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), - entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]), + entries: HashMap::from([( + "foo".to_owned(), + vec![Some("5".to_owned()), Some("10".to_owned())] + )]), } ), - HTML::from("5 10") + HTML::from( + r#"5 10"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]), } ), - HTML::from("5 ✓") + HTML::from( + r#"5 ✓"# + ) ); assert_eq!( render_cell( "heart", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]), } ), - HTML::from("<3") + HTML::from( + r#"<3"# + ) ); assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "bob's".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), - HTML::from("") + 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); } }