]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
in render_row: name 'cells'
[tablify] / src / lib.rs
index 3e0efd0d68751ce974401dbfd3c66fd4c0e5f23f..357a7eec0afc72411c76398b124c9cf47d16ed76 100644 (file)
@@ -6,11 +6,11 @@ use std::iter::Iterator;
 
 pub struct Config {}
 
 
 pub struct Config {}
 
-const HEADER: &str = "<!DOCTYPE html>
+const HEADER: &str = r#"<!DOCTYPE html>
 <html>
 <head>
 <html>
 <head>
-  <meta charset=\"utf-8\">
-  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
+  <meta charset="utf-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>
     td { text-align: center; }
     /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
   <style>
     td { text-align: center; }
     /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
@@ -26,8 +26,8 @@ const HEADER: &str = "<!DOCTYPE html>
     .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
   </style>
   <script>
     .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
   </style>
   <script>
-    function highlight(id)       { const e = document.getElementById(id); if (e) { e.classList.add(   \"highlight\"); } }
-    function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } }
+    function highlight(id)       { const e = document.getElementById(id); if (e) { e.classList.add(   "highlight"); } }
+    function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
     function h2(a, b)  {       highlight(a);       highlight(b); }
     function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
   </script>
     function h2(a, b)  {       highlight(a);       highlight(b); }
     function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
   </script>
@@ -35,7 +35,7 @@ const HEADER: &str = "<!DOCTYPE html>
 <body>
   <table>
     <tbody>
 <body>
   <table>
     <tbody>
-";
+"#;
 const FOOTER: &str = "    </tbody>
   </table>
 </body>
 const FOOTER: &str = "    </tbody>
   </table>
 </body>
@@ -194,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);
@@ -218,28 +218,31 @@ fn render_cell(col: &str, row: &Row) -> HTML {
                 .join(" "),
         )
     };
                 .join(" "),
         )
     };
-    HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
+    row.entries.remove(col);
+    HTML(format!(
+        r#"<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());
     let row_label = HTML::escape(row.label.as_ref());
+    let cells = columns
+        .iter()
+        .map(|col| render_cell(col, row))
+        .collect::<HTML>();
     HTML(format!(
     HTML(format!(
-        "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
-        &columns
-            .iter()
-            .map(|col| render_cell(col, row))
-            .collect::<HTML>()
+        "<tr><th id=\"{row_label}\">{row_label}</th>{cells}</tr>\n"
     ))
 }
 
 fn render_column_headers(columns: &[String]) -> HTML {
     HTML(
     ))
 }
 
 fn render_column_headers(columns: &[String]) -> HTML {
     HTML(
-        String::from("<tr class=\"key\"><th></th>")
+        String::from(r#"<tr class="key"><th></th>"#)
             + &columns.iter().fold(String::new(), |mut acc, col| {
                 let col_header = HTML::escape(col.as_ref());
                 write!(
                     &mut acc,
             + &columns.iter().fold(String::new(), |mut acc, col| {
                 let col_header = HTML::escape(col.as_ref());
                 write!(
                     &mut acc,
-                    "<th id=\"{col_header}\"><div><div>{col_header}</div></div></th>"
+                    r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
                 )
                 .unwrap();
                 acc
                 )
                 .unwrap();
                 acc
@@ -261,7 +264,7 @@ pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::
         "{HEADER}{}{}{FOOTER}",
         render_column_headers(&columns),
         rows.into_iter()
         "{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>()
     )))
 }
@@ -437,82 +440,132 @@ 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(),
                 }
             ),
-            HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+            HTML::from(
+                r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+            )
         );
         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])]),
                 }
             ),
-            HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+            HTML::from(
+                r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+            )
         );
         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])]),
                 }
             ),
-            HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+            HTML::from(
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+            )
         );
         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])]),
                 }
             ),
-            HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>")
+            HTML::from(
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
+            )
         );
         assert_eq!(
             render_cell(
                 "foo",
         );
         assert_eq!(
             render_cell(
                 "foo",
-                &Row {
+                &mut Row {
                     label: "nope".to_owned(),
                     label: "nope".to_owned(),
-                    entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
+                    entries: HashMap::from([(
+                        "foo".to_owned(),
+                        vec![Some("5".to_owned()), Some("10".to_owned())]
+                    )]),
                 }
             ),
                 }
             ),
-            HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>")
+            HTML::from(
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
+            )
         );
         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])]),
                 }
             ),
-            HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 ✓</td>")
+            HTML::from(
+                r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
+            )
         );
         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())])]),
                 }
             ),
-            HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','heart')\" onmouseout=\"ch2('nope','heart')\">&lt;3</td>")
+            HTML::from(
+                r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')">&lt;3</td>"#
+            )
         );
         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])]),
                 }
             ),
                     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>")
+            HTML::from(
+                r#"<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);
+    }
+
+    #[test]
+    fn test_render_row() {
+        assert_eq!(
+            render_row(
+                &["foo".to_owned()],
+                &mut Row {
+                    label: "nope".to_owned(),
+                    entries: HashMap::from([("bar".to_owned(), vec![None])]),
+                }
+            ),
+            HTML::from(
+                r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td></tr>
+"#
+            )
         );
     }
 }
         );
     }
 }