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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use calamine::{DataType, Reader, Xlsx, open_workbook};
use serde_json::Value;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
pub struct RecordImporter<'a> {
path: &'a Path,
}
impl<'a> RecordImporter<'a> {
pub fn new(path: &'a Path) -> Self {
Self { path }
}
/// Import a CSV into a JSON array.
///
/// # Examples
///
/// ```no_run
/// use nahpu_db::io::import::RecordImporter;
/// use std::path::Path;
///
/// let path = Path::new("test/data/test.csv");
/// let importer = RecordImporter::new(path);
/// let data = importer.import_csv().unwrap();
/// ```
pub fn import_csv(&self) -> Result<Vec<Value>, String> {
self.import_delimited(b',')
}
/// Get all sheet names from an Excel file.
///
/// # Examples
///
/// ```no_run
/// use nahpu_db::io::import::RecordImporter;
/// use std::path::Path;
///
/// let path = Path::new("test/data/test.xlsx");
/// let importer = RecordImporter::new(path);
/// let sheet_names = importer.get_excel_sheet_names().unwrap();
/// ```
pub fn get_excel_sheet_names(&self) -> Result<Vec<String>, String> {
let workbook: Xlsx<BufReader<File>> =
open_workbook(self.path).map_err(|e: calamine::XlsxError| e.to_string())?;
Ok(workbook.sheet_names().to_vec())
}
/// Import an Excel sheet into a raw 2D string grid.
///
/// # Examples
///
/// ```no_run
/// use nahpu_db::io::import::RecordImporter;
/// use std::path::Path;
///
/// let path = Path::new("test/data/test.xlsx");
/// let importer = RecordImporter::new(path);
/// let raw_data = importer.import_excel_raw("Sheet1").unwrap();
/// ```
pub fn import_excel_raw(&self, sheet_name: &str) -> Result<Vec<Vec<String>>, String> {
let mut workbook: Xlsx<BufReader<File>> =
open_workbook(self.path).map_err(|e: calamine::XlsxError| e.to_string())?;
let range = workbook
.worksheet_range(sheet_name)
.map_err(|e| e.to_string())?;
let mut rows = Vec::new();
for row in range.rows() {
let string_row: Vec<String> = row
.iter()
.map(|c| match c {
calamine::Data::Empty => String::new(),
calamine::Data::String(s) => s.clone(),
calamine::Data::Float(f) => f.to_string(),
calamine::Data::Int(i) => i.to_string(),
calamine::Data::Bool(b) => b.to_string(),
calamine::Data::Error(e) => e.to_string(),
calamine::Data::DateTime(_) => {
if let Some(dt) = c.as_datetime() {
dt.to_string()
} else if let Some(s) = c.as_string() {
s.clone()
} else {
String::new()
}
}
calamine::Data::DateTimeIso(s) | calamine::Data::DurationIso(s) => s.clone(),
})
.collect();
if string_row.iter().any(|s| !s.trim().is_empty()) {
rows.push(string_row);
}
}
if rows.is_empty() {
return Err("Empty sheet".to_string());
}
Ok(rows)
}
/// Import a TSV into a JSON array.
///
/// # Examples
///
/// ```no_run
/// use nahpu_db::io::import::RecordImporter;
/// use std::path::Path;
///
/// let path = Path::new("test/data/test.tsv");
/// let importer = RecordImporter::new(path);
/// let data = importer.import_tsv().unwrap();
/// ```
pub fn import_tsv(&self) -> Result<Vec<Value>, String> {
self.import_delimited(b'\t')
}
/// Import an Excel sheet into a JSON array.
///
/// # Examples
///
/// ```no_run
/// use nahpu_db::io::import::RecordImporter;
/// use std::path::Path;
///
/// let path = Path::new("test/data/test.xlsx");
/// let importer = RecordImporter::new(path);
/// let data = importer.import_excel("Sheet1").unwrap();
/// ```
pub fn import_excel(&self, sheet_name: &str) -> Result<Vec<Value>, String> {
let mut workbook: Xlsx<BufReader<File>> =
open_workbook(self.path).map_err(|e: calamine::XlsxError| e.to_string())?;
let range = workbook
.worksheet_range(sheet_name)
.map_err(|e| e.to_string())?;
let mut rows = range.rows();
let header_row = rows.next().ok_or("Empty sheet")?;
let headers: Vec<String> = header_row.iter().map(|c| c.to_string()).collect();
let mut json_array = Vec::new();
for row in rows {
let mut map = serde_json::Map::new();
for (i, cell) in row.iter().enumerate() {
if i >= headers.len() {
break;
}
let key = headers[i].clone();
let val = self.parse_excel_cell(cell);
map.insert(key, val);
}
json_array.push(Value::Object(map));
}
Ok(json_array)
}
fn import_delimited(&self, delimiter: u8) -> Result<Vec<Value>, String> {
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.delimiter(delimiter)
.from_path(self.path)
.map_err(|e| e.to_string())?;
let mut json_array = Vec::new();
let headers = rdr.headers().map_err(|e| e.to_string())?.clone();
for result in rdr.records() {
let record = result.map_err(|e| e.to_string())?;
let mut map = serde_json::Map::new();
for (i, val) in record.iter().enumerate() {
if i >= headers.len() {
break;
}
let key = headers[i].to_string();
let value = self.parse_delimited_cell(val);
map.insert(key, value);
}
json_array.push(Value::Object(map));
}
Ok(json_array)
}
fn parse_delimited_cell(&self, val: &str) -> Value {
if val.is_empty() {
Value::Null
} else if let Ok(n) = val.parse::<i64>() {
Value::Number(n.into())
} else if let Ok(f) = val.parse::<f64>() {
if let Some(num) = serde_json::Number::from_f64(f) {
Value::Number(num)
} else {
Value::String(val.to_string())
}
} else if let Ok(b) = val.parse::<bool>() {
Value::Bool(b)
} else {
Value::String(val.to_string())
}
}
fn parse_excel_cell(&self, cell: &calamine::Data) -> Value {
match cell {
calamine::Data::Empty => Value::Null,
calamine::Data::String(s) => Value::String(s.clone()),
calamine::Data::Float(f) => {
if f.fract() == 0.0 {
Value::Number(serde_json::Number::from(*f as i64))
} else {
serde_json::Number::from_f64(*f)
.map(Value::Number)
.unwrap_or(Value::Null)
}
}
calamine::Data::Int(i) => Value::Number(serde_json::Number::from(*i)),
calamine::Data::Bool(b) => Value::Bool(*b),
calamine::Data::Error(e) => Value::String(e.to_string()),
calamine::Data::DateTime(_) => {
if let Some(dt) = cell.as_datetime() {
Value::String(dt.to_string())
} else if let Some(s) = cell.as_string() {
Value::String(s)
} else {
Value::Null
}
}
calamine::Data::DateTimeIso(s) | calamine::Data::DurationIso(s) => {
Value::String(s.clone())
}
}
}
}