1use crate::{
2 display::format_grid_cell_display_value,
3 models::{GridColumnDef, GridRow},
4 utils::{titleize, to_csv_value},
5};
6
7pub fn header_label(column: &GridColumnDef) -> String {
8 column
9 .display_name
10 .clone()
11 .unwrap_or_else(|| titleize(&column.name))
12}
13
14pub fn export_csv_rows(columns: &[GridColumnDef], rows: &[GridRow]) -> String {
15 let header = columns
16 .iter()
17 .map(|column| to_csv_value(&header_label(column)))
18 .collect::<Vec<_>>()
19 .join(",");
20
21 let body = rows
22 .iter()
23 .map(|row| {
24 columns
25 .iter()
26 .map(|column| to_csv_value(&format_grid_cell_display_value(row, column)))
27 .collect::<Vec<_>>()
28 .join(",")
29 })
30 .collect::<Vec<_>>();
31
32 std::iter::once(header)
33 .chain(body)
34 .collect::<Vec<_>>()
35 .join("\n")
36}