1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::{convert::TryFrom, io::Read};
use csv::{Error, Reader, StringRecord};
use crate::{Cell, RowStruct, Style, Table, TableStruct};
impl<R: Read> TryFrom<&mut Reader<R>> for TableStruct {
type Error = Error;
fn try_from(reader: &mut Reader<R>) -> Result<Self, Self::Error> {
let records = reader.records();
let rows = records
.map(|record| Ok(row(&record?)))
.collect::<Result<Vec<RowStruct>, Error>>()?;
let table = if reader.has_headers() {
let headers = reader.headers()?;
let title: RowStruct = title(headers);
rows.table().title(title)
} else {
rows.table()
};
Ok(table)
}
}
fn row(record: &StringRecord) -> RowStruct {
RowStruct {
cells: record.iter().map(Cell::cell).collect(),
}
}
fn title(record: &StringRecord) -> RowStruct {
RowStruct {
cells: record.iter().map(|cell| cell.cell().bold(true)).collect(),
}
}