From: Scott Worley Date: Thu, 26 Sep 2024 01:16:23 +0000 (-0700) Subject: extract render_instances() X-Git-Tag: v0.3.0~10 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/f915bc906e6b8001bc748a1a5f3b13fafadcb86b extract render_instances() --- diff --git a/src/lib.rs b/src/lib.rs index f1da5f7..1396c9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,29 +194,32 @@ fn render_one_instance(instance: &Option) -> 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>> = 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_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!(