1use std::{fs::File, io::BufReader};
2use calamine::{Reader, Sheets};
3use heck::ToSnakeCase;
4use indexmap::IndexMap;
5use serde::Serialize;
6use serde_json::{json, Value};
7
8use crate::{OptionSet, PathData, ReadMode};
9
10
11#[derive(Debug, Clone)]
13pub struct WorkbookInfo {
14 pub filename: String,
15 pub extension: String,
16 pub selected: Option<Vec<String>>,
17 pub sheets: Vec<String>,
18}
19
20impl WorkbookInfo {
21 pub fn new(path_data: &PathData, selected: &[String], sheet_refs: &[String]) -> Self {
22 WorkbookInfo {
23 extension: path_data.extension(),
24 filename: path_data.filename(),
25 selected: Some(selected.to_vec()),
26 sheets: sheet_refs.to_vec(),
27 }
28 }
29
30 pub fn simple(path_data: &PathData) -> Self {
31 let sheet_name = "single";
32 WorkbookInfo {
33 extension: path_data.extension(),
34 filename: path_data.filename(),
35 selected: None,
36 sheets: vec![sheet_name.to_owned()],
37 }
38 }
39
40 pub fn ext(&self) -> String {
41 self.extension.to_owned()
42 }
43
44 pub fn name(&self) -> String {
45 self.filename.to_owned()
46 }
47
48 pub fn sheet(&self, index: usize) -> (String, usize) {
49 let sheet_name = self.sheets.get(index).unwrap_or(&"single".to_owned()).to_owned();
50 (sheet_name, index)
51 }
52
53 pub fn sheets(&self) -> Vec<String> {
54 self.sheets.clone()
55 }
56}
57
58
59#[derive(Debug, Clone)]
61pub struct ResultSet {
62 pub filename: String,
63 pub extension: String,
64 pub selected: Option<Vec<String>>,
65 pub sheets: Vec<String>,
66 pub keys: Vec<String>,
67 pub num_rows: usize,
68 pub data: SpreadData,
69 pub out_ref: Option<String>,
70 pub opts: OptionSet,
71}
72
73impl ResultSet {
74
75 pub fn new(info: &WorkbookInfo, keys: &[String], data_set: DataSet, opts: &OptionSet, out_ref: Option<&str>) -> Self {
77 let (num_rows, data) = match data_set {
78 DataSet::WithRows(size, rows) => (size, rows),
79 DataSet::Count(size) => (size, vec![])
80 };
81 ResultSet {
82 extension: info.ext(),
83 filename: info.name(),
84 selected: info.selected.clone(),
85 sheets: info.sheets(),
86 keys: keys.to_vec(),
87 num_rows,
88 data: SpreadData::from_single(data),
89 out_ref: out_ref.map(|s| s.to_string()),
90 opts: opts.to_owned()
91 }
92 }
93
94 pub fn from_multiple(sheets: &[SheetDataSet], info: &WorkbookInfo, opts: &OptionSet) -> Self {
95 let selected = None;
96 let mut sheet_names = vec![];
97 let filename = info.filename.clone();
98 let extension = info.extension.clone();
99 let mut keys: Vec<String> = vec![];
100 let mut num_rows = 0;
101 for (sheet_index, sheet) in sheets.iter().enumerate() {
102 num_rows += sheet.num_rows;
103 sheet_names.push(sheet.name());
104 if sheet_index == 0 {
105 keys = sheet.keys.clone();
106 }
107 }
108 ResultSet {
109 extension,
110 filename,
111 selected,
112 sheets: sheet_names,
113 keys,
114 num_rows,
115 data: SpreadData::Multiple(sheets.to_vec()),
116 out_ref: None,
117 opts: opts.to_owned()
118 }
119 }
120
121
122 pub fn multimode(&self) -> bool {
123 matches!(self.data, SpreadData::Multiple(_))
124 }
125
126 pub fn to_json(&self) -> Value {
128 let mut result = json!({
129 "name": self.filename,
130 "extension": self.extension,
131 "selected": self.selected.clone().unwrap_or(vec![]),
132 "sheets": self.sheets,
133 "num_rows": self.num_rows,
134 "fields": self.keys,
135 "multimode": self.multimode(),
136 "data": self.data.to_json(),
137 "opts": self.opts.to_json()
138 });
139 if let Some(out_ref_str) = self.out_ref.clone() {
140 result["outref"] = json!(out_ref_str);
141 }
142 result
143 }
144
145 pub fn to_output_lines(&self, json_lines: bool) -> Vec<String> {
147 let selected_names = self.selected.clone().unwrap_or(vec![]);
148 let num_selected = selected_names.len();
149 let plural = if num_selected > 1 {
150 "s"
151 } else {
152 ""
153 };
154 let mut lines = vec![
155 format!("name:{}", self.filename),
156 format!("extension: {}", self.extension),
157
158 format!("sheets: {}", self.sheets.join(", ")),
159 ];
160 if num_selected > 0 {
161 lines.push(format!("selected sheet{}: {}", plural, selected_names.join(", ")));
162 }
163 lines.push(format!("row count: {}", self.num_rows));
164 lines.push(format!("fields: {}", self.keys.join(",")));
165 lines.push(format!("multimode: {}", self.multimode()));
166 for opt_line in self.opts.to_lines() {
167 lines.push(opt_line);
168 }
169 if let Some(out_ref_str) = self.out_ref.clone() {
170 lines.push(format!("output reference: {}", out_ref_str));
171 } else {
172 let has_many_sheets = self.sheets.len() > 1;
173 if !has_many_sheets || !self.multimode() {
174 lines.push("data:".to_owned());
175 }
176 if json_lines {
177 for sheet in &self.data.sheets() {
178 if has_many_sheets {
179 lines.push(format!("Sheet `{}` ({}):", sheet.name(), sheet.num_rows));
180 }
181 for item in &sheet.rows {
182 lines.push(format!("{}", json!(item)));
183 }
184 }
185 } else {
186 if self.multimode() {
187 for sheet in self.data.sheets() {
188 lines.push(format!("Sheet `{}` ({}):", sheet.name(), sheet.num_rows));
189 lines.push(format!("{}", json!(sheet)));
190 }
191 } else {
192 lines.push(format!("{}", self.data.to_json()));
193 }
194 }
195 }
196 lines
197 }
198
199 pub fn to_vec(&self) -> Vec<IndexMap<String, Value>> {
202 self.data.first_sheet().clone()
203 }
204
205 pub fn json_data(&self) -> Value {
207 json!(self.data)
208 }
209
210 pub fn rows(&self) -> Vec<String> {
212 let sheet = self.data.first_sheet();
213 let mut lines = Vec::with_capacity(sheet.len());
214 for row in &sheet {
215 lines.push(json!(row).to_string());
216 }
217 lines
218 }
219
220}
221
222#[derive(Debug, Clone, Serialize)]
223pub struct SheetDataSet {
224 pub sheet: (String, String),
225 pub num_rows: usize,
226 pub keys: Vec<String>,
227 pub rows: Vec<IndexMap<String, Value>>
228}
229
230impl SheetDataSet {
231
232
233
234 pub fn new(name: &str, keys: &[String], rows: &[IndexMap<String, Value>], total: usize) -> Self {
235 Self {
236 sheet: (name.to_string(), name.to_snake_case()),
237 keys: keys.to_vec(),
238 rows: rows.to_vec(),
239 num_rows: total
240 }
241 }
242
243 pub fn key(&self) -> String {
244 self.sheet.1.clone()
245 }
246
247 pub fn name(&self) -> String {
248 self.sheet.0.clone()
249 }
250}
251
252#[derive(Debug, Clone, Serialize)]
253pub enum SpreadData {
254 Single(Vec<IndexMap<String, Value>>),
255 Multiple(Vec<SheetDataSet>)
256}
257
258impl SpreadData {
259 pub fn from_single(rows: Vec<IndexMap<String, Value>>) -> Self {
260 SpreadData::Single(rows)
261 }
262
263 pub fn from_multiple(sheet_data: &[SheetDataSet]) -> Self {
264 SpreadData::Multiple(sheet_data.to_owned())
265 }
266
267 pub fn first_sheet(&self) -> Vec<IndexMap<String, Value>> {
268 match self {
269 SpreadData::Single(rows) => rows.to_owned(),
270 SpreadData::Multiple(sheets) => {
271 if let Some(sheet) = sheets.first() {
272 sheet.rows.to_owned()
273 } else {
274 vec![]
275 }
276 }
277 }
278 }
279
280 pub fn sheets(&self) -> Vec<SheetDataSet> {
282 match self {
283 SpreadData::Single(_) => vec![],
284 SpreadData::Multiple(sheets) => sheets.to_owned()
285 }
286 }
287
288 pub fn to_json(&self) -> Value {
289 match self {
290 SpreadData::Single(sheet) => json!(sheet),
291 SpreadData::Multiple(sheet_map) => json!(sheet_map)
292 }
293 }
294}
295
296
297#[derive(Debug, Clone, Serialize)]
298pub enum DataSet {
299 WithRows(usize, Vec<IndexMap<String, Value>>),
300 Count(usize)
301}
302
303impl DataSet {
304 pub fn from_count_and_rows(count: usize, rows: Vec<IndexMap<String, Value>>, opts: &OptionSet) -> Self {
305 match opts.read_mode() {
306 ReadMode::Sync | ReadMode::PreviewMultiple => DataSet::WithRows(count, rows),
307 ReadMode::Async => DataSet::Count(count),
308 }
309 }
310}
311
312
313pub fn to_index_map(row: &[serde_json::Value], headers: &[String]) -> IndexMap<String, Value> {
314 let mut hm: IndexMap<String, serde_json::Value> = IndexMap::new();
315 for (sub_index, hk) in headers.iter().enumerate() {
316 if let Some(cell) = row.get(sub_index) {
317 hm.insert(hk.to_owned(), cell.to_owned());
318 }
319 }
320 hm
321}
322
323pub fn match_sheet_name_and_index(workbook: &mut Sheets<BufReader<File>>, opts: &OptionSet) -> (Vec<String>, Vec<String>, Vec<usize>) {
324 let mut sheet_indices = vec![];
325 let mut selected_names: Vec<String> = vec![];
326 let sheet_names = workbook.worksheets().into_iter().map(|ws| ws.0).collect::<Vec<String>>();
327 if let Some(sheet_keys) = opts.selected.clone() {
328 for sheet_key in sheet_keys {
329 if let Some(sheet_index) = sheet_names.iter().position(|s| s.to_snake_case() == sheet_key.to_snake_case()) {
330 sheet_indices.push(sheet_index);
331 selected_names.push(sheet_names[sheet_index].clone());
332 }
333 }
334 }
335 if sheet_indices.is_empty() && !opts.indices.is_empty() {
336 for s_index in opts.indices.clone() {
337 let sheet_index = s_index as usize;
338 if let Some(sheet_name) = sheet_names.get(sheet_index) {
339 sheet_indices.push(sheet_index);
340 selected_names.push(sheet_name.to_owned());
341 }
342 }
343 }
344 if sheet_indices.is_empty() {
345 sheet_indices = vec![0];
346 if !sheet_names.is_empty() {
347 selected_names.push(sheet_names[0].clone());
348 }
349 }
350 (selected_names, sheet_names, sheet_indices)
351}
352
353#[cfg(test)]
354mod tests {
355 use super::*;
356 use calamine::open_workbook_auto;
357
358 const SAMPLE_PATH: &str = "data/sample-data-2.ods";
360
361 fn opts_selecting(sheet_key: &str) -> OptionSet {
362 OptionSet::new(SAMPLE_PATH).sheet_name(sheet_key)
363 }
364
365 #[test]
366 fn test_sheet_name_matching_is_case_insensitive_and_ignores_punctuation() {
367 let mut workbook = open_workbook_auto(SAMPLE_PATH).unwrap();
368 for variant in ["results 1", "RESULTS 1", "Results_1", "results-1", " results 1 "] {
371 let opts = opts_selecting(variant);
372 let (selected_names, _sheet_names, sheet_indices) = match_sheet_name_and_index(&mut workbook, &opts);
373 assert_eq!(selected_names, vec!["results 1".to_string()], "variant '{}' should match 'results 1'", variant);
374 assert_eq!(sheet_indices, vec![1], "variant '{}' should resolve to index 1", variant);
375 }
376 }
377
378 #[test]
379 fn test_sheet_name_matching_handles_names_with_no_spaces() {
380 let mut workbook = open_workbook_auto(SAMPLE_PATH).unwrap();
383 for variant in ["Rsults-2", "rsults-2", "RSULTS-2", "rsults_2"] {
384 let opts = opts_selecting(variant);
385 let (selected_names, _sheet_names, sheet_indices) = match_sheet_name_and_index(&mut workbook, &opts);
386 assert_eq!(selected_names, vec!["Rsults-2".to_string()], "variant '{}' should match 'Rsults-2'", variant);
387 assert_eq!(sheet_indices, vec![0]);
388 }
389 }
390
391 #[test]
392 fn test_sheet_name_matching_falls_back_to_first_sheet_when_unmatched() {
393 let mut workbook = open_workbook_auto(SAMPLE_PATH).unwrap();
394 let opts = opts_selecting("nonexistent sheet name");
395 let (selected_names, sheet_names, sheet_indices) = match_sheet_name_and_index(&mut workbook, &opts);
396 assert_eq!(sheet_names, vec!["Rsults-2".to_string(), "results 1".to_string()]);
397 assert_eq!(selected_names, vec!["Rsults-2".to_string()]);
399 assert_eq!(sheet_indices, vec![0]);
400 }
401
402 #[test]
403 fn test_sheet_index_selection_still_works() {
404 let mut workbook = open_workbook_auto(SAMPLE_PATH).unwrap();
405 let opts = OptionSet::new(SAMPLE_PATH).sheet_index(1);
406 let (selected_names, _sheet_names, sheet_indices) = match_sheet_name_and_index(&mut workbook, &opts);
407 assert_eq!(selected_names, vec!["results 1".to_string()]);
408 assert_eq!(sheet_indices, vec![1]);
409 }
410}
411