From: Scott Worley Date: Wed, 2 Oct 2024 09:32:34 +0000 (-0700) Subject: Get config from read_input() X-Git-Tag: v0.3.0~1 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/586b332ada9b821cbc7838ffa9151c9886387918?ds=inline;hp=12e913005080a9f60750d807c220a58704429361 Get config from read_input() --- diff --git a/src/lib.rs b/src/lib.rs index dbcfc7b..5c994c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,8 +170,13 @@ impl>> Iterator for Reader } } -fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> { - Reader::new(std::io::BufReader::new(input).lines()).collect::, _>>() +fn read_input(input: impl std::io::Read) -> Result<(Vec, Config), std::io::Error> { + let default_config = Config { + column_threshold: 2, + }; + Reader::new(std::io::BufReader::new(input).lines()) + .collect::, _>>() + .map(|rows| (rows, default_config)) } fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> { @@ -303,9 +308,9 @@ fn render_column_headers(columns: &[String]) -> HTML { /// * there's an i/o error while reading `input` /// * the log has invalid syntax: /// * an indented line with no preceding non-indented line -pub fn tablify(config: &Config, input: impl std::io::Read) -> Result { - let rows = read_rows(input)?; - let columns = column_order(config, &rows); +pub fn tablify(input: impl std::io::Read) -> Result { + let (rows, config) = read_input(input)?; + let columns = column_order(&config, &rows); Ok(HTML(format!( "{HEADER}{}{}{FOOTER}", render_column_headers(&columns), @@ -348,6 +353,9 @@ mod tests { ); } + fn read_rows(input: impl std::io::Read) -> Result, std::io::Error> { + read_input(input).map(|(rows, _)| rows) + } #[test] fn test_read_rows() { assert_eq!( diff --git a/src/main.rs b/src/main.rs index 6102247..8cf4859 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,3 @@ fn main() { - print!( - "{}", - tablify::tablify( - &tablify::Config { - column_threshold: 2 - }, - std::io::stdin() - ) - .unwrap() - ); + print!("{}", tablify::tablify(std::io::stdin()).unwrap()); }