X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/25fd008e11cc44e03ddf0ac12da513c27bfe7a95..f915bc906e6b8001bc748a1a5f3b13fafadcb86b:/src/lib.rs?ds=sidebyside diff --git a/src/lib.rs b/src/lib.rs index 47e1d3a..1396c9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -187,36 +187,39 @@ 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()), } } -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>> = row.entries.get(col); - let class = HTML::from(if instances.is_none() { "" } else { "yes" }); - let all_empty = instances - .iter() - .flat_map(|is| is.iter()) - .all(Option::is_none); - let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) { +fn render_instances(instances: &[Option]) -> HTML { + let all_empty = instances.iter().all(Option::is_none); + if all_empty && instances.len() == 1 { HTML::from("") } else if all_empty { - HTML(format!("{}", instances.unwrap().len())) + HTML(format!("{}", instances.len())) } else { HTML( instances - .unwrap() .iter() - .map(render_instance) + .map(render_one_instance) .map(|html| html.0) // Waiting for slice_concat_trait to stabilize .collect::>() .join(" "), ) + } +} + +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>> = row.entries.get(col); + let class = HTML::from(if instances.is_none() { "" } else { "yes" }); + let contents = match instances { + None => HTML::from(""), + Some(is) => render_instances(is), }; row.entries.remove(col); HTML(format!( @@ -226,12 +229,12 @@ fn render_cell(col: &str, row: &mut Row) -> HTML { 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" )) }