]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Remove entries as they're rendered
[tablify] / src / lib.rs
index b406f957f50b457cdc53bbc70746b26176bc1d5e..c5761d86fc8c9bdd337897d5e9dc678ef00dbdef 100644 (file)
@@ -4,6 +4,8 @@ use std::fmt::Write;
 use std::io::BufRead;
 use std::iter::Iterator;
 
 use std::io::BufRead;
 use std::iter::Iterator;
 
+pub struct Config {}
+
 const HEADER: &str = "<!DOCTYPE html>
 <html>
 <head>
 const HEADER: &str = "<!DOCTYPE html>
 <html>
 <head>
@@ -192,7 +194,7 @@ fn render_instance(instance: &Option<String>) -> 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<Option<String>>> = row.entries.get(col);
     let row_label = HTML::escape(row.label.as_ref());
     let col_label = HTML::escape(col);
     let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
@@ -216,10 +218,11 @@ fn render_cell(col: &str, row: &Row) -> HTML {
                 .join(" "),
         )
     };
                 .join(" "),
         )
     };
+    row.entries.remove(col);
     HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
 }
 
     HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
 }
 
-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!(
         "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
     let row_label = HTML::escape(row.label.as_ref());
     HTML(format!(
         "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
@@ -252,14 +255,14 @@ 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
 ///   * 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(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
+pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::io::Error> {
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
     let columns = column_order(&rows);
     Ok(HTML(format!(
         "{HEADER}{}{}{FOOTER}",
         render_column_headers(&columns),
         rows.into_iter()
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
     let columns = column_order(&rows);
     Ok(HTML(format!(
         "{HEADER}{}{}{FOOTER}",
         render_column_headers(&columns),
         rows.into_iter()
-            .map(|r| render_row(&columns, &r))
+            .map(|mut r| render_row(&columns, &mut r))
             .collect::<HTML>()
     )))
 }
             .collect::<HTML>()
     )))
 }
@@ -435,7 +438,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::new(),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::new(),
                 }
@@ -445,7 +448,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::from([("bar".to_owned(), vec![None])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("bar".to_owned(), vec![None])]),
                 }
@@ -455,7 +458,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None])]),
                 }
@@ -465,7 +468,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
                 }
@@ -475,7 +478,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         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())])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
                 }
@@ -485,7 +488,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
                 }
@@ -495,7 +498,7 @@ mod tests {
         assert_eq!(
             render_cell(
                 "heart",
         assert_eq!(
             render_cell(
                 "heart",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
                 }
                     label: "nope".to_owned(),
                     entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
                 }
@@ -505,12 +508,26 @@ mod tests {
         assert_eq!(
             render_cell(
                 "foo",
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "bob's".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None])]),
                 }
             ),
             HTML::from("<td class=\"yes\" onmouseover=\"h2('bob&#39;s','foo')\" onmouseout=\"ch2('bob&#39;s','foo')\"></td>")
         );
                     label: "bob's".to_owned(),
                     entries: HashMap::from([("foo".to_owned(), vec![None])]),
                 }
             ),
             HTML::from("<td class=\"yes\" onmouseover=\"h2('bob&#39;s','foo')\" onmouseout=\"ch2('bob&#39;s','foo')\"></td>")
         );
+        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);
     }
 }
     }
 }