From: Scott Worley Date: Thu, 12 Sep 2024 06:02:00 +0000 (-0700) Subject: Remove entries as they're rendered X-Git-Tag: v0.3.0~15 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/d9bfcf4d1b74bebfcc9881aad4f0aacd01238b01?ds=sidebyside Remove entries as they're rendered --- diff --git a/src/lib.rs b/src/lib.rs index 3e0efd0..c5761d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,11 @@ fn render_cell(col: &str, row: &Row) -> HTML { .join(" "), ) }; + row.entries.remove(col); HTML(format!("{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", @@ -261,7 +262,7 @@ pub fn tablify(config: &Config, input: impl std::io::Read) -> Result() ))) } @@ -437,7 +438,7 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::new(), } @@ -447,7 +448,7 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("bar".to_owned(), vec![None])]), } @@ -457,7 +458,7 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } @@ -467,7 +468,7 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None, None])]), } @@ -477,7 +478,7 @@ mod tests { 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())])]), } @@ -487,7 +488,7 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]), } @@ -497,7 +498,7 @@ mod tests { assert_eq!( render_cell( "heart", - &Row { + &mut Row { label: "nope".to_owned(), entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]), } @@ -507,12 +508,26 @@ mod tests { assert_eq!( render_cell( "foo", - &Row { + &mut Row { label: "bob's".to_owned(), entries: HashMap::from([("foo".to_owned(), vec![None])]), } ), HTML::from("") ); + 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); } }