X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/71e34cc04258862ab4ef7177c2719ca3ec8bedcf..b4bc28bae91f901f88fdad4621b14da9eb18be06:/src/lib.rs?ds=sidebyside diff --git a/src/lib.rs b/src/lib.rs index 74a54a2..99c454d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,11 +31,14 @@ impl Config { Ok(()) } } - -const DEFAULT_CONFIG: Config = Config { - column_threshold: 2, - static_columns: vec![], -}; +impl Default for Config { + fn default() -> Self { + Self { + column_threshold: 2, + static_columns: vec![], + } + } +} const HEADER: &str = r#" @@ -213,7 +216,7 @@ impl<'cfg, Input: Iterator>> Iterator } fn read_input(input: impl std::io::Read) -> Result<(Vec, Config), std::io::Error> { - let mut config = DEFAULT_CONFIG; + let mut config = Config::default(); let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines()); reader .collect::, _>>() @@ -305,7 +308,7 @@ fn render_leftover(notcol: &str, instances: &[Option]) -> HTML { } } -fn render_all_leftovers(row: &Row) -> HTML { +fn render_all_leftovers(config: &Config, row: &Row) -> HTML { let mut order: Vec<_> = row.entries.keys().collect(); order.sort_unstable(); HTML( @@ -335,7 +338,7 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM .iter() .map(|col| render_cell(col, row)) .collect::(); - let leftovers = render_all_leftovers(row); + let leftovers = render_all_leftovers(config, row); HTML(format!( "{row_label}{static_cells}{dynamic_cells}{leftovers}\n" )) @@ -692,30 +695,39 @@ mod tests { #[test] fn test_render_leftovers() { assert_eq!( - render_all_leftovers(&Row { - label: "nope".to_owned(), - entries: HashMap::from([("foo".to_owned(), vec![None])]), - }), + render_all_leftovers( + &Config::default(), + &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]) - ]), - }), + render_all_leftovers( + &Config::default(), + &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]) - ]), - }), + render_all_leftovers( + &Config::default(), + &Row { + label: "nope".to_owned(), + entries: HashMap::from([ + ("foo".to_owned(), vec![None]), + ("bar".to_owned(), vec![None, None]) + ]), + } + ), HTML::from("bar: 2, foo") ); } @@ -724,7 +736,7 @@ mod tests { fn test_render_row() { assert_eq!( render_row( - &DEFAULT_CONFIG, + &Config::default(), &["foo".to_owned()], &mut Rowlike::Row(Row { label: "nope".to_owned(),