spreadsheet_to_json/
data_set.rs

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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::{fs::File, io::BufReader};
use calamine::{Reader, Sheets};
use heck::ToSnakeCase;
use indexmap::IndexMap;
use serde::Serialize;
use serde_json::{json, Value};

use crate::{OptionSet, PathData, ReadMode};


/// Core info about a spreadsheet with extension, matched worksheet name and index an all worksheet keys
#[derive(Debug, Clone)]
pub struct WorkbookInfo {
    pub filename: String,
    pub extension: String,
    pub selected: Option<Vec<String>>,
    pub sheets: Vec<String>,
}

impl WorkbookInfo {
    pub fn new(path_data: &PathData, selected: &[String], sheet_refs: &[String]) -> Self {
        WorkbookInfo {
            extension: path_data.extension(),
            filename: path_data.filename(), 
            selected: Some(selected.to_vec()),
            sheets: sheet_refs.to_vec(),
        }
    }

    pub fn simple(path_data: &PathData) -> Self {
        let sheet_name = "single";
        WorkbookInfo {
            extension: path_data.extension(),
            filename: path_data.filename(), 
            selected: None,
            sheets: vec![sheet_name.to_owned()],
        }
    }

    pub fn ext(&self) -> String {
        self.extension.to_owned()
    }

    pub fn name(&self) -> String {
        self.filename.to_owned()
    }

    pub fn sheet(&self, index: usize) -> (String, usize) {
      let sheet_name = self.sheets.get(index).unwrap_or(&"single".to_owned()).to_owned();
      (sheet_name, index)
    }

    pub fn sheets(&self) -> Vec<String> {
        self.sheets.clone()
    }
}


// Result set
#[derive(Debug, Clone)]
pub struct ResultSet {
    pub filename: String,
    pub extension: String,
    pub selected: Option<Vec<String>>,
    pub sheets: Vec<String>,
    pub keys: Vec<String>,
    pub num_rows: usize,
    pub data: SpreadData,
    pub out_ref: Option<String>,
    pub opts: OptionSet,
}

impl ResultSet {

  /// Instantiate with Core workbook info, header keys, data set and optional output reference
  pub fn new(info: &WorkbookInfo, keys: &[String], data_set: DataSet, opts: &OptionSet, out_ref: Option<&str>) -> Self {
    let (num_rows, data) = match data_set {
      DataSet::WithRows(size, rows) => (size, rows),
      DataSet::Count(size) => (size, vec![])
    };
    ResultSet {
      extension: info.ext(),
      filename: info.name(), 
      selected: info.selected.clone(),
      sheets: info.sheets(),
      keys: keys.to_vec(),
      num_rows,
      data: SpreadData::from_single(data),
      out_ref: out_ref.map(|s| s.to_string()),
      opts: opts.to_owned()
    }
  }

  pub fn from_multiple(sheets: &[SheetDataSet], info: &WorkbookInfo, opts: &OptionSet) -> Self {
    let selected = None;
    let mut sheet_names = vec![];
    let filename = info.filename.clone();
    let extension = info.extension.clone();
    let mut keys: Vec<String> = vec![];
    let mut num_rows = 0;
    let mut sheet_index: usize = 0;
    for sheet in sheets {
      num_rows += sheet.num_rows;
      sheet_names.push(sheet.name());
      if sheet_index == 0 {
        keys = sheet.keys.clone();
      }
      sheet_index += 1;
    }
    ResultSet {
      extension,
      filename, 
      selected,
      sheets: sheet_names,
      keys,
      num_rows,
      data: SpreadData::Multiple(sheets.to_vec()),
      out_ref: None,
      opts: opts.to_owned()
    }
  }


  pub fn multimode(&self) -> bool {
    match self.data {
      SpreadData::Multiple(_) => true,
      _ => false
    }
  }

  /// Full result set as JSON with criteria, options and data in synchronous mode
  pub fn to_json(&self) -> Value {
    let mut result = json!({
      "name": self.filename,
      "extension": self.extension,
      "selected": self.selected.clone().unwrap_or(vec![]),
      "sheets": self.sheets,
      "num_rows": self.num_rows,
      "fields": self.keys,
      "multimode": self.multimode(),
      "data": self.data.to_json(),
      "opts": self.opts.to_json()
    });
    if let Some(out_ref_str) = self.out_ref.clone() {
      result["outref"] = json!(out_ref_str);
    }
    result
  }

   /// Full result set as CLI-friendly lines
   pub fn to_output_lines(&self, json_lines: bool) -> Vec<String> {
    let selected_names = self.selected.clone().unwrap_or(vec![]);
    let num_selected = selected_names.len();
    let plural = if num_selected > 1 {
      "s"
    } else {
      ""
    };
    let mut lines = vec![
      format!("name:{}", self.filename),
      format!("extension: {}", self.extension),
      
      format!("sheets: {}", self.sheets.join(", ")),
    ];
    if num_selected > 0 {
      lines.push(format!("selected sheet{}: {}", plural, selected_names.join(", ")));
    }
    lines.push(format!("row count: {}", self.num_rows));
    lines.push(format!("fields: {}", self.keys.join(",")));
    lines.push(format!("multimode: {}", self.multimode()));
    for opt_line in self.opts.to_lines() {
      lines.push(opt_line);
    }
    if let Some(out_ref_str) = self.out_ref.clone() {
      lines.push(format!("output reference: {}", out_ref_str));
    } else {
      let has_many_sheets = self.sheets.len() > 1;
      if !has_many_sheets || !self.multimode() {
        lines.push("data:".to_owned());
      }
      if json_lines {
        for sheet in &self.data.sheets() {
          if has_many_sheets {
            lines.push(format!("Sheet `{}` ({}):", sheet.name(), sheet.num_rows));
          }
          for item in &sheet.rows {
            lines.push(format!("{}", json!(item)));
          }
        }
      } else {
        if self.multimode() {
          for sheet in self.data.sheets() {
            lines.push(format!("Sheet `{}` ({}):", sheet.name(), sheet.num_rows));
            lines.push(format!("{}", json!(sheet)));
          }
        } else {
          lines.push(format!("{}", self.data.to_json()));
        }
      }
    }
    lines
  }

  /// Extract the vector of rows as Index Maps of JSON values
  /// Good for post-processing results
  pub fn to_vec(&self) -> Vec<IndexMap<String, Value>> {
    self.data.first_sheet().clone()
  }
  
  /// JSON object of row arrays only
  pub fn json_data(&self) -> Value {
    json!(self.data)
  }

  /// final output as vector of JSON-serializable array
  pub fn rows(&self) -> Vec<String> {
    let sheet = self.data.first_sheet();
    let mut lines = Vec::with_capacity(sheet.len());
    for row in &self.data.first_sheet() {
      lines.push(json!(row).to_string());
    }
    lines
  }

}

#[derive(Debug, Clone, Serialize)]
pub struct SheetDataSet {
  pub sheet: (String, String),
  pub num_rows: usize,
  pub keys: Vec<String>,
  pub rows: Vec<IndexMap<String, Value>>
}

impl SheetDataSet {

  

  pub fn new(name: &str, keys: &[String], rows: &[IndexMap<String, Value>], total: usize) -> Self {
    Self {
      sheet: (name.to_string(), name.to_snake_case()),
      keys: keys.to_vec(),
      rows: rows.to_vec(),
      num_rows: total
    }
  }

  pub fn key(&self) -> String {
    self.sheet.1.clone()
  }

  pub fn name(&self) -> String {
    self.sheet.0.clone()
  }
}

#[derive(Debug, Clone, Serialize)]
pub enum SpreadData {
   Single(Vec<IndexMap<String, Value>>),
   Multiple(Vec<SheetDataSet>)
}

impl SpreadData {
  pub fn from_single(rows: Vec<IndexMap<String, Value>>) -> Self {
    SpreadData::Single(rows)
  }

  pub fn from_multiple(sheet_data: &[SheetDataSet]) -> Self {
    SpreadData::Multiple(sheet_data.to_owned())
  }

  pub fn first_sheet(&self) -> Vec<IndexMap<String, Value>> {
    match self {
      SpreadData::Single(rows) => rows.to_owned(),
      SpreadData::Multiple(sheets) => {
        if let Some(sheet) = sheets.get(0) {
          sheet.rows.to_owned()
        } else {
          vec![]
        }
      }
    }
  }

  // Only for preview multiple mode
  pub fn sheets(&self) -> Vec<SheetDataSet> {
    match self {
      SpreadData::Single(_) => vec![],
      SpreadData::Multiple(sheets) => sheets.to_owned()
    }
  }

  pub fn to_json(&self) -> Value {
    match self {
      SpreadData::Single(sheet) => json!(sheet),
      SpreadData::Multiple(sheet_map) => json!(sheet_map)
    }
  }
}


#[derive(Debug, Clone, Serialize)]
pub enum DataSet {
   WithRows(usize, Vec<IndexMap<String, Value>>),
   Count(usize) 
}

impl DataSet {
  pub fn from_count_and_rows(count: usize, rows: Vec<IndexMap<String, Value>>, opts: &OptionSet) -> Self {
    match opts.read_mode() {
      ReadMode::Sync | ReadMode::PreviewMultiple => DataSet::WithRows(count, rows),
      ReadMode::Async => DataSet::Count(count),
    }
  }
}


pub fn to_index_map(row: &[serde_json::Value], headers: &[String]) -> IndexMap<String, Value> {
    let mut hm: IndexMap<String, serde_json::Value> = IndexMap::new();
    let mut sub_index = 0;
    for hk in headers {
        if let Some(cell) = row.get(sub_index) {
            hm.insert(hk.to_owned(), cell.to_owned());
        } 
        sub_index += 1;
    }
    hm
}

pub fn match_sheet_name_and_index(workbook: &mut Sheets<BufReader<File>>, opts: &OptionSet) -> (Vec<String>, Vec<String>, Vec<usize>) {
  let mut sheet_indices = vec![];
  let mut selected_names: Vec<String> = vec![];
  let sheet_names = workbook.worksheets().into_iter().map(|ws| ws.0).collect::<Vec<String>>();
  if let Some(sheet_keys) = opts.selected.clone() {
      for sheet_key in sheet_keys {
          if let Some(sheet_index) = sheet_names.iter().position(|s| s.to_snake_case() == sheet_key.to_snake_case()) {
              sheet_indices.push(sheet_index);
              selected_names.push(sheet_names[sheet_index].clone());
          }
      }
  }
  if sheet_indices.len() < 1 && opts.indices.len() > 0 {
    for s_index in opts.indices.clone() {
      let sheet_index = s_index as usize;
      if let Some(sheet_name) = sheet_names.get(sheet_index) {
          sheet_indices.push(sheet_index);
          selected_names.push(sheet_name.to_owned());
      }
    }
  }
  if sheet_indices.len() < 1 {
    sheet_indices = vec![0];
    if sheet_names.len() > 0 {
      selected_names.push(sheet_names[0].clone());
    }
  }
  (selected_names, sheet_names, sheet_indices)
}