X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/d9bfcf4d1b74bebfcc9881aad4f0aacd01238b01..58c0a717743d262025e664eb6ce79ad9cd4c7968:/src/lib.rs?ds=inline
diff --git a/src/lib.rs b/src/lib.rs
index c5761d8..f1da5f7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,11 +6,11 @@ use std::iter::Iterator;
pub struct Config {}
-const HEADER: &str = "
+const HEADER: &str = r#"
-
-
+
+
@@ -35,7 +35,7 @@ const HEADER: &str = "
-";
+"#;
const FOOTER: &str = "
@@ -187,7 +187,7 @@ fn column_order(rows: &[Row]) -> Vec {
.collect()
}
-fn render_instance(instance: &Option) -> HTML {
+fn render_one_instance(instance: &Option) -> HTML {
match instance {
None => HTML::from("â"),
Some(instance) => HTML::escape(instance.as_ref()),
@@ -212,35 +212,37 @@ fn render_cell(col: &str, row: &mut Row) -> HTML {
instances
.unwrap()
.iter()
- .map(render_instance)
+ .map(render_one_instance)
.map(|html| html.0) // Waiting for slice_concat_trait to stabilize
.collect::>()
.join(" "),
)
};
row.entries.remove(col);
- HTML(format!("{contents} | "))
+ HTML(format!(
+ r#"{contents} | "#
+ ))
}
fn render_row(columns: &[String], row: &mut Row) -> HTML {
let row_label = HTML::escape(row.label.as_ref());
+ let cells = columns
+ .iter()
+ .map(|col| render_cell(col, row))
+ .collect::();
HTML(format!(
- "{row_label} | {}
\n",
- &columns
- .iter()
- .map(|col| render_cell(col, row))
- .collect::()
+ "{row_label} | {cells}
\n"
))
}
fn render_column_headers(columns: &[String]) -> HTML {
HTML(
- String::from(" | ")
+ String::from(r#"
---|
| "#)
+ &columns.iter().fold(String::new(), |mut acc, col| {
let col_header = HTML::escape(col.as_ref());
write!(
&mut acc,
- ""
+ r#""#
)
.unwrap();
acc
@@ -443,7 +445,9 @@ mod tests {
entries: HashMap::new(),
}
),
- HTML::from(" | ")
+ HTML::from(
+ r#" | "#
+ )
);
assert_eq!(
render_cell(
@@ -453,7 +457,9 @@ mod tests {
entries: HashMap::from([("bar".to_owned(), vec![None])]),
}
),
- HTML::from(" | ")
+ HTML::from(
+ r#" | "#
+ )
);
assert_eq!(
render_cell(
@@ -463,7 +469,9 @@ mod tests {
entries: HashMap::from([("foo".to_owned(), vec![None])]),
}
),
- HTML::from(" | ")
+ HTML::from(
+ r#" | "#
+ )
);
assert_eq!(
render_cell(
@@ -473,17 +481,24 @@ mod tests {
entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
}
),
- HTML::from("2 | ")
+ HTML::from(
+ r#"2 | "#
+ )
);
assert_eq!(
render_cell(
"foo",
&mut Row {
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("5 10 | ")
+ HTML::from(
+ r#"5 10 | "#
+ )
);
assert_eq!(
render_cell(
@@ -493,7 +508,9 @@ mod tests {
entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
}
),
- HTML::from("5 â | ")
+ HTML::from(
+ r#"5 â | "#
+ )
);
assert_eq!(
render_cell(
@@ -503,7 +520,9 @@ mod tests {
entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
}
),
- HTML::from("<3 | ")
+ HTML::from(
+ r#"<3 | "#
+ )
);
assert_eq!(
render_cell(
@@ -513,7 +532,9 @@ mod tests {
entries: HashMap::from([("foo".to_owned(), vec![None])]),
}
),
- HTML::from(" | ")
+ HTML::from(
+ r#" | "#
+ )
);
let mut r = Row {
label: "nope".to_owned(),
@@ -530,4 +551,21 @@ mod tests {
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#"
---|
nope | |
+"#
+ )
+ );
+ }
}