Skip to main content

spreadsheet_to_json/
options.rs

1use heck::ToSnakeCase;
2use indexmap::IndexMap;
3use serde_json::{json, Error, Value};
4use simple_string_patterns::{SimpleMatch, StripCharacters};
5use to_segments::ToSegments;
6use std::{path::Path, str::FromStr, sync::Arc};
7
8use is_truthy::TruthyRuleSet;
9/// default max number of rows in direct single sheet mode without an override via ->max_row_count(max_row_count)
10pub const DEFAULT_MAX_ROWS: usize = 10_000;
11/// default max number of rows multiple sheet preview mode without an override via ->max_row_count(max_row_count)
12pub const DEFAULT_MAX_ROWS_PREVIEW: usize = 1000;
13
14/// Row parsing options with nested column options
15#[derive(Debug, Clone, Default)]
16pub struct RowOptionSet {
17  pub columns: Vec<Column>,
18  pub decimal_comma: bool, // always parse as euro number format
19  pub date_only: bool,
20}
21
22impl RowOptionSet {
23
24  // simple constructor with column keys only
25  pub fn simple(cols: &[Column]) -> Self {
26    RowOptionSet {
27      decimal_comma: false,
28      date_only: false,
29      columns: cols.to_vec()
30    }
31  }
32
33  // lets you set all options
34  pub fn new(cols: &[Column], decimal_comma: bool, date_only: bool) -> Self {
35    RowOptionSet {
36      decimal_comma: decimal_comma,
37      date_only,
38      columns: cols.to_vec()
39    }
40  }
41
42  pub fn column(&self, index: usize) -> Option<&Column> {
43    self.columns.get(index)
44  }
45
46  pub fn date_mode(&self) -> String {
47    if self.date_only {
48      "date only"
49    } else {
50      "date/time"
51    }.to_string()
52  }
53
54  pub fn decimal_separator(&self) -> String {
55    if self.decimal_comma {
56      ","
57    } else {
58      "."
59    }.to_string()
60  }
61}
62
63/// Core options with nested row options
64#[derive(Debug, Clone, Default)]
65pub struct OptionSet {
66  pub selected: Option<Vec<String>>, // Optional sheet name reference. Will default to index value if not matched
67  pub indices: Vec<u32>, // worksheet index
68  pub path: Option<String>, // path argument. If None, do not attempt to parse
69  pub rows: RowOptionSet,
70  pub jsonl: bool,
71  pub max: Option<u32>,
72  pub omit_header: bool,
73  pub header_row: u8,
74  pub read_mode: ReadMode,
75  pub field_mode: FieldNameMode
76}
77
78impl OptionSet {
79  /// Instantiates a new option set with a path string for file operations.
80  pub fn new(path_str: &str) -> Self {
81    OptionSet {
82        selected: None,
83        indices: vec![0],
84        path: Some(path_str.to_string()),
85        rows: RowOptionSet::default(),
86        jsonl: false,
87        max: None,
88        omit_header: false,
89        header_row: 0,
90        read_mode: ReadMode::Sync,
91        field_mode: FieldNameMode::AutoA1,
92    }
93  }
94
95  /// Sets the sheet name for the operation.
96  pub fn sheet_name(mut self, name: &str) -> Self {
97    self.selected = Some(vec![name.to_string()]);
98    self
99  }
100
101  /// Sets the sheet name for the operation.
102  pub fn sheet_names(mut self, names: &[String]) -> Self {
103    self.selected = Some(names.to_vec());
104    self
105  }
106
107  /// Sets the sheet index.
108  pub fn sheet_index(mut self, index: u32) -> Self {
109      self.indices = vec![index];
110      self
111  }
112
113  /// Sets the sheet index.
114  pub fn sheet_indices(mut self, indices: &[u32]) -> Self {
115    self.indices = indices.to_vec();
116    self
117}
118
119  /// Sets JSON Lines mode to true.
120  pub fn json_lines(mut self) -> Self {
121      self.jsonl = true;
122      self
123  }
124
125  /// Sets JSON Lines mode
126  pub fn set_json_lines(mut self, mode: bool) -> Self {
127    self.jsonl = mode;
128    self
129  }
130
131  /// Omits the header when reading.
132  pub fn omit_header(mut self) -> Self {
133      self.omit_header = true;
134      self
135  }
136
137  /// Sets the header row index.
138  pub fn header_row(mut self, row: u8) -> Self {
139      self.header_row = row;
140      self
141  }
142
143  /// Sets the maximum number of rows to read.
144  pub fn max_row_count(mut self, max: u32) -> Self {
145      self.max = Some(max);
146      self
147  }
148
149  /// Sets the read mode to asynchronous, single sheet mode
150  /// This is for reading long files with 10K+ rows in the target sheet
151  pub fn read_mode_async(mut self) -> Self {
152      self.read_mode = ReadMode::Async;
153      self
154  }
155
156   /// Sets the read mode to direct with multiple sheet output
157   /// This serves to fetch quick a overview of a spreadsheet
158   pub fn read_mode_preview(mut self) -> Self {
159    self.read_mode = ReadMode::PreviewMultiple;
160    self
161}
162
163  /// Sets read mode from a range of common key names
164  /// async, preview or sync (default) with synonyms such as `a`, `p` and `s`
165  /// If the key is unmatched, it will always default to Sync
166  pub fn set_read_mode(mut self, key: &str) -> Self {
167    self.read_mode = ReadMode::from_key(key);
168    self
169  }
170
171  pub fn multimode(&self) -> bool {
172    self.read_mode.is_multimode()
173  }
174
175  pub fn file_name(&self) -> Option<String> {
176    if let Some(path_str) = self.path.clone() {
177      Path::new(&path_str).file_name().map(|f| f.to_string_lossy().to_string())
178    } else {
179      None
180    }
181  }
182
183  /// Override matched and unmatched headers with custom headers.
184  pub fn override_headers(mut self, keys: &[&str]) -> Self {
185    let mut columns: Vec<Column> = Vec::with_capacity(keys.len());
186    for ck in keys {
187        columns.push(Column::new(Some(&ck.to_snake_case())));
188    }
189    self.rows = RowOptionSet::simple(&columns);
190    self
191  }
192
193  /// Override matched and unmatched columns with custom keys and/or formatting options
194  pub fn override_columns(mut self, cols: &[Value]) -> Self {
195    let mut columns: Vec<Column> = Vec::with_capacity(cols.len());
196    for json_value in cols {
197        columns.push(Column::from_json(json_value));
198    }
199    self.rows = RowOptionSet::simple(&columns);
200    self
201  }
202
203  /// Sets the column key naming convention.
204  pub fn field_name_mode(mut self, system: &str, override_header: bool) -> Self {
205      self.field_mode = FieldNameMode::from_key(system, override_header);
206      self
207  }
208
209  pub fn row_mode(&self) -> String {
210    if self.jsonl {
211      "JSON lines"
212    } else {
213      "JSON"
214    }.to_string()
215  }
216
217  pub fn header_mode(&self) -> String {
218    if self.omit_header {
219      "ignore"
220    } else {
221      "capture"
222    }.to_string()
223  }
224
225  /// render option output contextually as JSON
226  pub fn to_json(&self) -> Value {
227    
228    let mut output: IndexMap<String, Value> = IndexMap::new();
229    if let Some(selected) =  self.selected.clone() {
230      let selected = if self.multimode() {
231        json!({
232          "sheets": selected,
233          "indices": self.indices.clone()
234        })
235      } else {
236        json!({
237          "sheet": selected.first().unwrap_or(&"".to_string()),
238          "index": self.indices.get(0).unwrap_or(&0)
239        })
240      };
241      output.insert("selected".to_string(), selected.into());
242    }
243    if let Some(fname) = self.file_name() {
244      output.insert("file name".to_string(), fname.into());
245    }
246    if let Some(max_val) = self.max {
247      output.insert("max".to_string(), max_val.into());
248    }
249    output.insert("omit_header".to_string(), self.omit_header.into());
250    output.insert("header_row".to_string(), self.header_row.into());
251    output.insert("read_mode".to_string(), self.read_mode.to_string().into());
252    output.insert("jsonl".to_string(), self.jsonl.into());
253    output.insert("decimal_separator".to_string(), self.rows.decimal_separator().into());
254    output.insert("date_only".to_string(), self.rows.date_only.into());
255    if self.columns().len() > 0 {
256      let columns: Vec<Value> = self.rows.columns.clone().into_iter().map(|c| c.to_json()).collect();
257      output.insert("columns".to_string(), columns.into());
258    }
259    json!(output)
260  }
261
262  pub fn index_list(&self) -> String {
263    self.indices.clone().into_iter().map(|s| s.to_string()).collect::<Vec<String>>().join(", ")
264  }
265
266  /// render option output contextually as a list of strings
267  /// for use in a terminal or text output
268  pub fn to_lines(&self) -> Vec<String> {
269    let mut lines = vec![];
270    if let Some(s_names) = self.selected.clone() {
271      let plural = if s_names.len() > 1 {
272        "s"
273      } else {
274        ""
275      };
276      lines.push(format!("sheet name{}: {}", plural, s_names.join(",")));
277    } else if self.indices.len() > 0 {
278      lines.push(format!("sheet indices: {}", self.index_list()));
279    }
280    if let Some(fname) = self.file_name() {
281      lines.push(format!("file name: {}", fname));
282    }
283    if self.max.is_some() {
284      let max_val = self.max.unwrap_or(0);
285      if max_val > 0 {
286        lines.push(format!("max rows: {}", max_val));
287      }
288    }
289    lines.extend(vec![
290      format!("mode: {}", self.row_mode()),
291      format!("headers: {}", self.header_mode()),
292      format!("header row: {}", self.header_row),
293      format!("decimal separator: {}", self.rows.decimal_separator()),
294      format!("date mode: {}", self.rows.date_mode()),
295      format!("column style: {}", self.field_mode.to_string())
296    ]);
297
298    if self.columns().len() > 0 {
299      lines.push("columns:".to_string());
300      for col in self.rows.columns.clone() {
301        lines.push(col.to_line());
302      }
303    }
304    lines
305  }
306
307  /// header row index as usize
308  pub fn header_row_index(&self) -> usize {
309    self.header_row as usize
310  }
311
312  /// get the maximum of rows to be output synchronously
313  pub fn max_rows(&self) -> usize {
314    if let Some(mr) = self.max {
315      mr as usize
316    } else {
317      match self.read_mode {
318        ReadMode::PreviewMultiple => DEFAULT_MAX_ROWS_PREVIEW,
319        _ => DEFAULT_MAX_ROWS
320      }
321    }
322  }
323
324  /// future development with advanced column options
325  #[allow(dead_code)]
326  pub fn columns(&self) -> Vec<Column> {
327    self.rows.columns.clone()
328  }
329
330  /// cloned read mode
331  pub fn read_mode(&self) -> ReadMode {
332    self.read_mode.clone()
333  }
334
335  /// Needs full data set to processed later
336  pub fn is_async(&self) -> bool {
337    self.read_mode.is_async()
338  }
339
340  // Should rows be captured synchronously
341  pub fn capture_rows(&self) -> bool {
342    match self.read_mode {
343      ReadMode::Async => false,
344      _ => true
345    }
346  }
347
348}
349
350
351/// Cell format overrides
352#[derive(Debug, Clone)]
353pub enum Format {
354  Auto, // automatic interpretation
355  Text, // text
356  Integer, // integer only
357  Decimal(u8), // decimal to stated precision
358  Float, // f64 
359  Boolean, // Boolean or  cast to boolean from integers
360  Date, // Interpret as date only
361  DateTime, // Interpret as full datetime
362  DateTimeCustom(Arc<str>),
363  Truthy, // interpret common yes/no, y/n, true/false text strings as true/false
364  #[allow(dead_code)]
365  TruthyCustom(TruthyRuleSet) // define custom yes/no values
366}
367
368impl ToString for Format {
369  fn to_string(&self) -> String {
370    let result = match self {
371      Self::Auto => "auto",
372      Self::Text => "text",
373      Self::Integer => "integer",
374      Self::Decimal(n) => &format!("decimal({})", n),
375      Self::Float => "float",
376      Self::Boolean => "boolean",
377      Self::Date => "date",
378      Self::DateTime => "datetime",
379      Self::DateTimeCustom(fmt) => &format!("datetime({})", fmt),
380      Self::Truthy => "truthy",
381      Self::TruthyCustom(rules) => {
382        let true_str: Vec<String> = rules.true_options().iter().map(|o| o.pattern().to_string()).collect();
383        let false_str: Vec<String> = rules.false_options().iter().map(|o| o.pattern().to_string()).collect();
384        &format!("truthy({},{})", true_str.join("|"), false_str.join("|"))
385      },
386    };
387    result.to_string() // Convert the string slice to a String
388  }
389}
390
391impl FromStr for Format {
392  type Err = Error;
393  fn from_str(key: &str) -> Result<Self, Self::Err> {
394      let fmt = match key {
395        "s" | "str" | "string" | "t" | "txt" | "text" => Self::Text,
396        "i" | "int" | "integer" => Self::Integer,
397        "d1" | "decimal_1" => Self::Decimal(1),
398        "d2" | "decimal_2" => Self::Decimal(2),
399        "d3" | "decimal_3" => Self::Decimal(3),
400        "d4" | "decimal_4" => Self::Decimal(4),
401        "d5" | "decimal_5" => Self::Decimal(5),
402        "d6" | "decimal_6" => Self::Decimal(6),
403        "d7" | "decimal_7" => Self::Decimal(7),
404        "d8" | "decimal_8" => Self::Decimal(8),
405        "fl" | "f" | "float" => Self::Float,
406        "b" | "bool" | "boolean" => Self::Boolean,
407        "da" | "date" => Self::Date,
408        "dt" | "datetime" => Self::DateTime,
409        "tr" | "truthy" => Self::Truthy,
410        _ => {
411          if let Some(str) = match_custom_dt(key) {
412            Self::DateTimeCustom(Arc::from(str))
413          } else if let Some((yes, no)) = match_custom_truthy(key) {
414            Self::TruthyCustom(TruthyRuleSet::new().add_true(&yes).add_false(&no))
415          } else {
416            Self::Auto
417          }
418        },
419      };
420      Ok(fmt)
421  }
422}
423
424fn match_custom_dt(key: &str) -> Option<String> {
425  let test_str = key.trim();
426  if test_str.starts_with_ci("dt:") {
427    Some(test_str[3..].to_string())
428  } else {
429    None
430  }
431}
432
433fn match_custom_truthy(key: &str) -> Option<(String,String)> {
434  let test_str = key.trim();
435  if let (Some(head), Some(tail)) = test_str.to_head_tail(":") {
436    if tail.len() > 1 && head.len() > 1 && head.starts_with_ci("tr") {
437      if let (Some(yes), Some(no)) = tail.to_head_tail(",") {
438        if !yes.is_empty() && !no.is_empty() {
439          return Some((yes.to_string(), no.to_string()));
440        }
441      }
442    }
443  }
444  None
445}
446
447impl Format {
448  #[allow(dead_code)]
449  pub fn truthy_custom(yes: &str, no: &str) -> Self {
450    Format::TruthyCustom(TruthyRuleSet::new().add_true(yes).add_false(no))
451  }
452}
453
454#[derive(Debug, Clone)]
455pub struct Column {
456  pub key:  Option<Arc<str>>,
457  /// Natural (auto-detected, snake_cased) key to match this override against, regardless
458  /// of the column's actual position. When None, the column applies positionally instead
459  /// (matched by its index within the configured column list), as before.
460  pub source_key: Option<Arc<str>>,
461  pub format: Format,
462  pub default: Option<Value>,
463  pub date_only: bool, // date only in Format::Auto mode with datetime objects
464  pub decimal_comma: bool, // parse as euro number format
465}
466
467impl Column {
468
469  /// build new column with an optional key name only
470  pub fn new(key_opt: Option<&str>) -> Self {
471    Self::from_key_ref_with_format(key_opt, Format::Auto, None, false, false)
472  }
473
474  /// build new column data type override and optional default
475  pub fn new_format(fmt: Format, default: Option<Value>) -> Self {
476    Self::from_key_ref_with_format(None, fmt, default, false, false)
477  }
478
479  /// build a column override matched by its natural (auto-detected) key rather than
480  /// by position, e.g. to rename and/or reformat a single field out of many without
481  /// needing to enumerate every column ahead of it.
482  pub fn from_source_key_with_format(source_key: &str, key_opt: Option<&str>, format: Format, default: Option<Value>, date_only: bool, decimal_comma: bool) -> Self {
483    let mut col = Self::from_key_ref_with_format(key_opt, format, default, date_only, decimal_comma);
484    col.source_key = Some(Arc::from(source_key));
485    col
486  }
487
488  /// build new column data type override and optional default
489  pub fn from_json(json: &Value) -> Self {
490    let key_opt = json.get("key").map(|v| v.as_str().unwrap_or(""));
491    let source_key = json.get("source_key").and_then(|v| v.as_str()).filter(|s| !s.is_empty());
492    let fmt = match json.get("format").and_then(|v| v.as_str()) {
493      Some(fmt_str) => {
494        match Format::from_str(fmt_str) {
495          Ok(fmt) => fmt,
496          Err(_) => Format::Auto
497        }
498      },
499      None => Format::Auto
500    };
501    let default = match json.get("default") {
502      Some(def_val) => {
503        match def_val {
504          Value::String(s) => Some(Value::String(s.clone())),
505          Value::Number(n) => Some(Value::Number(n.clone())),
506          Value::Bool(b) => Some(Value::Bool(b.clone())),
507          _ => None
508        }
509      },
510      None => None
511    };
512    let date_only = match json.get("date_only") {
513      Some(date_val) => date_val.as_bool().unwrap_or(false),
514      None => false
515    };
516    let dec_commas_keys = ["decimal_comma", "dec_comma"];
517    let mut decimal_comma = false;
518
519    for key in &dec_commas_keys {
520      if let Some(euro_val) = json.get(*key) {
521        decimal_comma = euro_val.as_bool().unwrap_or(false);
522        break;
523      }
524    }
525    if let Some(src) = source_key {
526      Column::from_source_key_with_format(src, key_opt, fmt, default, date_only, decimal_comma)
527    } else {
528      Column::from_key_ref_with_format(key_opt, fmt, default, date_only, decimal_comma)
529    }
530}
531
532
533  // future development with column options
534  #[allow(dead_code)]
535  pub fn set_format(mut self, fmt: Format) -> Self {
536    self.format = fmt;
537    self
538  }
539
540  #[allow(dead_code)]
541  pub fn set_default(mut self, val: Value) -> Self {
542    self.default = Some(val);
543    self
544  }
545
546  #[allow(dead_code)]
547  pub fn set_date_only(mut self, val: bool) -> Self {
548    self.date_only = val;
549    self
550  }
551
552  #[allow(dead_code)]
553  pub fn set_decimal_comma(mut self, val: bool) -> Self {
554    self.decimal_comma = val;
555    self
556  }
557
558  pub fn from_key_ref_with_format(key_opt: Option<&str>, format: Format, default: Option<Value>, date_only: bool, decimal_comma: bool) -> Self {
559    let mut key = None;
560    if let Some(k_str) = key_opt {
561      key = Some(Arc::from(k_str));
562    }
563    Column {
564      key,
565      source_key: None,
566      format,
567      default,
568      date_only,
569      decimal_comma
570    }
571  }
572
573  pub fn key_name(&self) -> String {
574    self.key.clone().unwrap_or(Arc::from("")).to_string()
575  }
576
577  pub fn source_key_name(&self) -> String {
578    self.source_key.clone().unwrap_or(Arc::from("")).to_string()
579  }
580
581  pub fn to_json(&self) -> Value {
582    json!({
583      "key": self.key_name(),
584      "source_key": self.source_key_name(),
585      "format": self.format.to_string(),
586      "default": self.default,
587      "date_only": self.date_only,
588      "decimal_comma": self.decimal_comma
589    })
590  }
591
592  pub fn to_line(&self) -> String {
593    let date_only_str = if self.date_only {
594      ", date only"
595    } else {
596      ""
597    }.to_owned();
598    let def_string = if let Some(def_val) = self.default.clone() {
599      format!("default: {}", def_val.to_string())
600    } else {
601      "".to_string()
602    };
603    let comma_str = if self.decimal_comma {
604      ", decimal comma"
605    } else {
606      ""
607    };
608    let source_str = if self.source_key.is_some() {
609      format!(", matched from {}", self.source_key_name())
610    } else {
611      "".to_string()
612    };
613    format!(
614      "\tkey {}, format {}{}{}{}{}",
615      self.key_name(),
616      self.format.to_string(),
617      def_string,
618      date_only_str,
619      comma_str,
620      source_str)
621  }
622
623}
624
625
626/// Match on permitted file types identified by file extensions
627/// Unmatched means do not process
628#[derive(Debug, Clone, Copy)]
629pub enum Extension {
630  Unmatched,
631  Ods,
632  Xlsx,
633  Xlsb,
634  Xls,
635  Csv,
636  Tsv,
637}
638
639impl Extension {
640  pub fn from_path(path:&Path) -> Extension {
641    if let Some(ext) = path.extension() {
642      if let Some(ext_str) = ext.to_str() {
643        let ext_lc = ext_str.to_lowercase();
644        return match  ext_lc.as_str() {
645          "ods" => Extension::Ods,
646          "xlsx" => Extension::Xlsx,
647          "xlsb" => Extension::Xlsb,
648          "xls" => Extension::Xls,
649          "csv" => Extension::Csv,
650          "tsv" => Extension::Tsv,
651          _ => Extension::Unmatched
652        }
653      }
654    }
655    Extension::Unmatched
656  }
657
658  /// use the Calamine library
659  pub fn use_calamine(&self) -> bool {
660    match self {
661      Self::Ods | Self::Xlsx | Self::Xlsb | Self::Xls => true,
662      _ => false
663    }
664  }
665  
666  /// added for future development
667  /// Process a simple CSV or TSV
668  #[allow(dead_code)]
669  pub fn use_csv(&self) -> bool {
670    match self {
671      Self::Csv | Self::Tsv => true,
672      _ => false
673    }
674  }
675
676}
677
678impl ToString for Extension {
679  fn to_string(&self) -> String {
680    match self {
681      Self::Ods => "ods",
682      Self::Xlsx => "xlsx",
683      Self::Xlsb => "xlsb",
684      Self::Xls => "xls",
685      Self::Csv => "csv",
686      Self::Tsv => "tsv",
687      _ => ""
688    }.to_string()
689  }
690}
691
692pub struct PathData<'a> {
693  path: &'a Path,
694  ext: Extension
695}
696
697impl<'a> PathData<'a> {
698  pub fn new(path: &'a Path) -> Self {
699    PathData {
700      path,
701      ext: Extension::from_path(path)
702    }
703  }
704
705  pub fn mode(&self) -> Extension {
706    self.ext
707  }
708
709  pub fn extension(&self) -> String {
710    self.ext.to_string()
711  }
712
713  pub fn ext(&self) -> Extension {
714    self.ext
715  }
716
717  pub fn path(&self) -> &Path {
718    self.path
719  }
720
721  pub fn is_valid(&self) -> bool {
722    match self.ext {
723      Extension::Unmatched => false,
724      _ => true
725    }
726  }
727
728  pub fn use_calamine(&self) -> bool {
729    self.ext.use_calamine()
730  }
731
732  pub fn filename(&self) -> String {
733    if let Some(file_ref) = self.path.file_name() {
734        file_ref.to_string_lossy().to_string()
735    } else {
736        "".to_owned()
737    }
738  }
739}
740
741
742
743#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
744pub enum ReadMode {
745  #[default]
746  Sync,
747  PreviewMultiple,
748  Async
749}
750
751/// either Preview or Async mode
752impl ReadMode {
753
754  pub fn from_key(key: &str) -> Self {
755    let sample = key.to_lowercase().strip_non_alphanum();
756    match sample.as_str() {
757      "async" | "defer" | "deferred" | "a" => ReadMode::Async,
758      "preview" | "p" | "pre" | "multimode" | "multiple" | "previewmultiple" | "previewmulti" | "m" => ReadMode::PreviewMultiple,
759      _ => ReadMode::Sync
760    }
761  }
762
763  pub fn is_async(&self) -> bool {
764    match self {
765      Self::Async => true,
766      _ => false
767    }
768  }
769
770  /// not preview or sync mode
771  pub fn is_multimode(&self) -> bool {
772    match self {
773      Self::PreviewMultiple => true,
774      _ => false
775    }
776  }
777}
778
779impl ToString for ReadMode {
780
781  fn to_string(&self) -> String {
782    match self {
783      Self::Async => "deferred",
784      Self::PreviewMultiple => "preview",
785      _ => "direct"
786    }.to_string()
787  }
788}
789
790/// defines the column key naming convention
791#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
792pub enum FieldNameMode {
793  #[default]
794  AutoA1, // will use A1 column keys if headers are unavailable
795  AutoNumPadded, // will use C01 format if column headers are unavailable
796  A1, // Defaults to A1 columns unless custom keys are added
797  NumPadded, // Defaults to C01 format unless custom keys are added
798}
799
800/// either Preview or Async mode
801impl FieldNameMode {
802
803
804  pub fn from_key(system: &str, override_header: bool) -> Self {
805    if system.starts_with_ci("a1") {
806      if override_header {
807        FieldNameMode::A1
808      } else {
809        FieldNameMode::AutoA1
810      }
811    } else if system.starts_with_ci("c") || system.starts_with_ci("n") {
812      if override_header {
813        FieldNameMode::NumPadded
814      } else {
815        FieldNameMode::AutoNumPadded
816      }
817    } else {
818      FieldNameMode::AutoA1
819    }
820  }
821
822
823  /// use AQ column field style
824  pub fn use_a1(&self) -> bool {
825    match self {
826      Self::AutoA1 | Self::A1 => true,
827      _ => false
828    }
829  }
830
831  /// use c01 column field style
832  pub fn use_c01(&self) -> bool {
833    match self {
834      Self::AutoNumPadded | Self::NumPadded => true,
835      _ => false
836    }
837  }
838
839   /// use seqquential a1 or C01 column style unless custom overrides are added
840   pub fn override_headers(&self) -> bool {
841    match self {
842      Self::NumPadded | Self::A1 => true,
843      _ => false
844    }
845  }
846
847  /// use default headers if available unless override by custom headers
848  pub fn keep_headers(&self) -> bool {
849    self.override_headers() == false
850  }
851}
852
853impl ToString for FieldNameMode {
854  fn to_string(&self) -> String {
855    match self {
856      Self::AutoNumPadded => "C01 auto",
857      Self::NumPadded => "C01 override",
858      Self::A1 => "A1 override",
859      _ => "A1 auto",
860    }.to_string()    
861  }
862}
863
864#[cfg(test)]
865mod tests {
866  use super::*;
867
868  #[test]
869  fn test_format_mode() {
870    let custom_boolean = Format::truthy_custom("si", "no");
871    assert_eq!(custom_boolean.to_string(), "truthy(si,no)");
872  }
873
874  #[test]
875  fn test_match_truthy_custom() {
876    let (true_keys, false_keys) = match_custom_truthy("tr:si,no").unwrap();
877    assert_eq!("si", true_keys);
878    assert_eq!("no", false_keys);
879  }
880
881}