]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Use Default trait for default Config
[tablify] / src / lib.rs
index e20d23d342fe9fffaadc9ef162fde04a70d377f4..6d147dd9f250506eb0061a85071d6f329c438cdf 100644 (file)
@@ -7,25 +7,38 @@ use std::iter::Iterator;
 #[derive(PartialEq, Eq, Debug)]
 struct Config {
     column_threshold: usize,
-    static_columns: Vec<String>,
+    static_columns: Vec<Option<String>>,
 }
 impl Config {
-    fn apply_command(&mut self, cmd: &str) -> Result<(), std::io::Error> {
+    fn apply_command(&mut self, line_num: usize, cmd: &str) -> Result<(), std::io::Error> {
         if let Some(threshold) = cmd.strip_prefix("col_threshold ") {
-            self.column_threshold = threshold
-                .parse()
-                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
+            self.column_threshold = threshold.parse().map_err(|e| {
+                std::io::Error::new(
+                    std::io::ErrorKind::InvalidInput,
+                    format!("line {line_num}: col_threshold must be numeric: {e}"),
+                )
+            })?;
         } else if let Some(col) = cmd.strip_prefix("col ") {
-            self.static_columns.push(col.to_owned());
+            self.static_columns.push(Some(col.to_owned()));
+        } else if cmd == "colsep" {
+            self.static_columns.push(None);
         } else {
             return Err(std::io::Error::new(
                 std::io::ErrorKind::InvalidInput,
-                format!("Unknown command: {cmd}"),
+                format!("line {line_num}: Unknown command: {cmd}"),
             ));
         }
         Ok(())
     }
 }
+impl Default for Config {
+    fn default() -> Self {
+        Self {
+            column_threshold: 2,
+            static_columns: vec![],
+        }
+    }
+}
 
 const HEADER: &str = r#"<!DOCTYPE html>
 <html>
@@ -38,6 +51,7 @@ const HEADER: &str = r#"<!DOCTYPE html>
     th, td { white-space: nowrap; }
     th { text-align: left; font-weight: normal; }
     th.spacer_row { height: .3em; }
+    .spacer_col { border: none; width: .2em; }
     table { border-collapse: collapse }
     tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
     tr.key > th > div { width: 1em; }
@@ -163,7 +177,7 @@ impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
                 Some((_, Err(e))) => return Some(Err(e)),
                 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
                     InputLine::Command(cmd) => {
-                        if let Err(e) = self.config.apply_command(cmd) {
+                        if let Err(e) = self.config.apply_command(n + 1, cmd) {
                             return Some(Err(e));
                         }
                     }
@@ -202,10 +216,7 @@ impl<'cfg, Input: Iterator<Item = Result<String, std::io::Error>>> Iterator
 }
 
 fn read_input(input: impl std::io::Read) -> Result<(Vec<Rowlike>, Config), std::io::Error> {
-    let mut config = Config {
-        column_threshold: 2,
-        static_columns: vec![],
-    };
+    let mut config = Config::default();
     let reader = Reader::new(&mut config, std::io::BufReader::new(input).lines());
     reader
         .collect::<Result<Vec<_>, _>>()
@@ -236,6 +247,7 @@ fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
     let static_columns: HashSet<&str> = config
         .static_columns
         .iter()
+        .flatten()
         .map(std::string::String::as_str)
         .collect();
     column_counts(rows)
@@ -317,7 +329,10 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
             let static_cells = config
                 .static_columns
                 .iter()
-                .map(|col| render_cell(col, row))
+                .map(|ocol| match ocol {
+                    Some(col) => render_cell(col, row),
+                    None => HTML::from(r#"<td class="spacer_col"></td>"#),
+                })
                 .collect::<HTML>();
             let dynamic_cells = columns
                 .iter()
@@ -332,20 +347,26 @@ fn render_row(config: &Config, columns: &[String], rowlike: &mut Rowlike) -> HTM
 }
 
 fn render_column_headers(config: &Config, columns: &[String]) -> HTML {
+    let static_columns = config.static_columns.iter().map(|oc| oc.as_ref());
+    let dynamic_columns = columns.iter().map(Some);
     HTML(
         String::from(r#"<tr class="key"><th></th>"#)
-            + &config.static_columns.iter().chain(columns.iter()).fold(
-                String::new(),
-                |mut acc, col| {
-                    let col_header = HTML::escape(col.as_ref());
-                    write!(
-                        &mut acc,
-                        r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
-                    )
+            + &static_columns
+                .chain(dynamic_columns)
+                .fold(String::new(), |mut acc, ocol| {
+                    match ocol {
+                        Some(col) => {
+                            let col_header = HTML::escape(col);
+                            write!(
+                                &mut acc,
+                                r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
+                            )
+                        }
+                        None => write!(&mut acc, r#"<th class="col_spacer"></th>"#),
+                    }
                     .unwrap();
                     acc
-                },
-            )
+                })
             + "</tr>\n",
     )
 }
@@ -520,16 +541,16 @@ mod tests {
         );
         assert_eq!(
             read_config(&b"!col foo"[..]).unwrap().static_columns,
-            vec!["foo".to_owned()]
+            vec![Some("foo".to_owned())]
         );
 
         let bad_command = read_config(&b"!no such command"[..]);
         assert!(bad_command.is_err());
-        assert!(format!("{bad_command:?}").contains("Unknown command"));
+        assert!(format!("{bad_command:?}").contains("line 1: Unknown command"));
 
         let bad_num = read_config(&b"!col_threshold foo"[..]);
         assert!(bad_num.is_err());
-        assert!(format!("{bad_num:?}").contains("Parse"));
+        assert!(format!("{bad_num:?}").contains("line 1: col_threshold must be numeric"));
     }
 
     #[test]
@@ -706,10 +727,7 @@ mod tests {
     fn test_render_row() {
         assert_eq!(
             render_row(
-                &Config {
-                    column_threshold: 0,
-                    static_columns: vec![],
-                },
+                &Config::default(),
                 &["foo".to_owned()],
                 &mut Rowlike::Row(Row {
                     label: "nope".to_owned(),
@@ -725,7 +743,7 @@ mod tests {
             render_row(
                 &Config {
                     column_threshold: 0,
-                    static_columns: vec!["foo".to_owned(), "bar".to_owned()],
+                    static_columns: vec![Some("foo".to_owned()), Some("bar".to_owned())],
                 },
                 &["baz".to_owned()],
                 &mut Rowlike::Row(Row {
@@ -739,6 +757,26 @@ mod tests {
             ),
             HTML::from(
                 r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">f</td><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')">r</td><td class="yes" onmouseover="h2('nope','baz')" onmouseout="ch2('nope','baz')">z</td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
+"#
+            )
+        );
+        assert_eq!(
+            render_row(
+                &Config {
+                    column_threshold: 0,
+                    static_columns: vec![Some("foo".to_owned()), None, Some("bar".to_owned())],
+                },
+                &[],
+                &mut Rowlike::Row(Row {
+                    label: "nope".to_owned(),
+                    entries: HashMap::from([
+                        ("bar".to_owned(), vec![Some("r".to_owned())]),
+                        ("foo".to_owned(), vec![Some("f".to_owned())]),
+                    ]),
+                })
+            ),
+            HTML::from(
+                r#"<tr><th id="nope">nope</th><td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">f</td><td class="spacer_col"></td><td class="yes" onmouseover="h2('nope','bar')" onmouseout="ch2('nope','bar')">r</td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')"></td></tr>
 "#
             )
         );