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;
9pub const DEFAULT_MAX_ROWS: usize = 10_000;
11pub const DEFAULT_MAX_ROWS_PREVIEW: usize = 1000;
13
14#[derive(Debug, Clone, Default)]
16pub struct RowOptionSet {
17 pub columns: Vec<Column>,
18 pub decimal_comma: bool, pub date_only: bool,
20}
21
22impl RowOptionSet {
23
24 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 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#[derive(Debug, Clone, Default)]
65pub struct OptionSet {
66 pub selected: Option<Vec<String>>, pub indices: Vec<u32>, pub path: Option<String>, 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 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 pub fn sheet_name(mut self, name: &str) -> Self {
97 self.selected = Some(vec![name.to_string()]);
98 self
99 }
100
101 pub fn sheet_names(mut self, names: &[String]) -> Self {
103 self.selected = Some(names.to_vec());
104 self
105 }
106
107 pub fn sheet_index(mut self, index: u32) -> Self {
109 self.indices = vec![index];
110 self
111 }
112
113 pub fn sheet_indices(mut self, indices: &[u32]) -> Self {
115 self.indices = indices.to_vec();
116 self
117}
118
119 pub fn json_lines(mut self) -> Self {
121 self.jsonl = true;
122 self
123 }
124
125 pub fn set_json_lines(mut self, mode: bool) -> Self {
127 self.jsonl = mode;
128 self
129 }
130
131 pub fn omit_header(mut self) -> Self {
133 self.omit_header = true;
134 self
135 }
136
137 pub fn header_row(mut self, row: u8) -> Self {
139 self.header_row = row;
140 self
141 }
142
143 pub fn max_row_count(mut self, max: u32) -> Self {
145 self.max = Some(max);
146 self
147 }
148
149 pub fn read_mode_async(mut self) -> Self {
152 self.read_mode = ReadMode::Async;
153 self
154 }
155
156 pub fn read_mode_preview(mut self) -> Self {
159 self.read_mode = ReadMode::PreviewMultiple;
160 self
161}
162
163 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 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 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 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 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 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 pub fn header_row_index(&self) -> usize {
309 self.header_row as usize
310 }
311
312 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 #[allow(dead_code)]
326 pub fn columns(&self) -> Vec<Column> {
327 self.rows.columns.clone()
328 }
329
330 pub fn read_mode(&self) -> ReadMode {
332 self.read_mode.clone()
333 }
334
335 pub fn is_async(&self) -> bool {
337 self.read_mode.is_async()
338 }
339
340 pub fn capture_rows(&self) -> bool {
342 match self.read_mode {
343 ReadMode::Async => false,
344 _ => true
345 }
346 }
347
348}
349
350
351#[derive(Debug, Clone)]
353pub enum Format {
354 Auto, Text, Integer, Decimal(u8), Float, Boolean, Date, DateTime, DateTimeCustom(Arc<str>),
363 Truthy, #[allow(dead_code)]
365 TruthyCustom(TruthyRuleSet) }
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() }
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 pub format: Format,
458 pub default: Option<Value>,
459 pub date_only: bool, pub decimal_comma: bool, }
462
463impl Column {
464
465 pub fn new(key_opt: Option<&str>) -> Self {
467 Self::from_key_ref_with_format(key_opt, Format::Auto, None, false, false)
468 }
469
470 pub fn new_format(fmt: Format, default: Option<Value>) -> Self {
472 Self::from_key_ref_with_format(None, fmt, default, false, false)
473 }
474
475 pub fn from_json(json: &Value) -> Self {
477 let key_opt = json.get("key").map(|v| v.as_str().unwrap_or(""));
478 let fmt = match json.get("format").and_then(|v| v.as_str()) {
479 Some(fmt_str) => {
480 match Format::from_str(fmt_str) {
481 Ok(fmt) => fmt,
482 Err(_) => Format::Auto
483 }
484 },
485 None => Format::Auto
486 };
487 let default = match json.get("default") {
488 Some(def_val) => {
489 match def_val {
490 Value::String(s) => Some(Value::String(s.clone())),
491 Value::Number(n) => Some(Value::Number(n.clone())),
492 Value::Bool(b) => Some(Value::Bool(b.clone())),
493 _ => None
494 }
495 },
496 None => None
497 };
498 let date_only = match json.get("date_only") {
499 Some(date_val) => date_val.as_bool().unwrap_or(false),
500 None => false
501 };
502 let dec_commas_keys = ["decimal_comma", "dec_comma"];
503 let mut decimal_comma = false;
504
505 for key in &dec_commas_keys {
506 if let Some(euro_val) = json.get(*key) {
507 decimal_comma = euro_val.as_bool().unwrap_or(false);
508 break;
509 }
510 }
511 Column::from_key_ref_with_format(key_opt, fmt, default, date_only, decimal_comma)
512}
513
514
515 #[allow(dead_code)]
517 pub fn set_format(mut self, fmt: Format) -> Self {
518 self.format = fmt;
519 self
520 }
521
522 #[allow(dead_code)]
523 pub fn set_default(mut self, val: Value) -> Self {
524 self.default = Some(val);
525 self
526 }
527
528 #[allow(dead_code)]
529 pub fn set_date_only(mut self, val: bool) -> Self {
530 self.date_only = val;
531 self
532 }
533
534 #[allow(dead_code)]
535 pub fn set_decimal_comma(mut self, val: bool) -> Self {
536 self.decimal_comma = val;
537 self
538 }
539
540 pub fn from_key_ref_with_format(key_opt: Option<&str>, format: Format, default: Option<Value>, date_only: bool, decimal_comma: bool) -> Self {
541 let mut key = None;
542 if let Some(k_str) = key_opt {
543 key = Some(Arc::from(k_str));
544 }
545 Column {
546 key,
547 format,
548 default,
549 date_only,
550 decimal_comma
551 }
552 }
553
554 pub fn key_name(&self) -> String {
555 self.key.clone().unwrap_or(Arc::from("")).to_string()
556 }
557
558 pub fn to_json(&self) -> Value {
559 json!({
560 "key": self.key_name(),
561 "format": self.format.to_string(),
562 "default": self.default,
563 "date_only": self.date_only,
564 "decimal_comma": self.decimal_comma
565 })
566 }
567
568 pub fn to_line(&self) -> String {
569 let date_only_str = if self.date_only {
570 ", date only"
571 } else {
572 ""
573 }.to_owned();
574 let def_string = if let Some(def_val) = self.default.clone() {
575 format!("default: {}", def_val.to_string())
576 } else {
577 "".to_string()
578 };
579 let comma_str = if self.decimal_comma {
580 ", decimal comma"
581 } else {
582 ""
583 };
584 format!(
585 "\tkey {}, format {}{}{}{}",
586 self.key_name(),
587 self.format.to_string(),
588 def_string,
589 date_only_str,
590 comma_str)
591 }
592
593}
594
595
596#[derive(Debug, Clone, Copy)]
599pub enum Extension {
600 Unmatched,
601 Ods,
602 Xlsx,
603 Xlsb,
604 Xls,
605 Csv,
606 Tsv,
607}
608
609impl Extension {
610 pub fn from_path(path:&Path) -> Extension {
611 if let Some(ext) = path.extension() {
612 if let Some(ext_str) = ext.to_str() {
613 let ext_lc = ext_str.to_lowercase();
614 return match ext_lc.as_str() {
615 "ods" => Extension::Ods,
616 "xlsx" => Extension::Xlsx,
617 "xlsb" => Extension::Xlsb,
618 "xls" => Extension::Xls,
619 "csv" => Extension::Csv,
620 "tsv" => Extension::Tsv,
621 _ => Extension::Unmatched
622 }
623 }
624 }
625 Extension::Unmatched
626 }
627
628 pub fn use_calamine(&self) -> bool {
630 match self {
631 Self::Ods | Self::Xlsx | Self::Xlsb | Self::Xls => true,
632 _ => false
633 }
634 }
635
636 #[allow(dead_code)]
639 pub fn use_csv(&self) -> bool {
640 match self {
641 Self::Csv | Self::Tsv => true,
642 _ => false
643 }
644 }
645
646}
647
648impl ToString for Extension {
649 fn to_string(&self) -> String {
650 match self {
651 Self::Ods => "ods",
652 Self::Xlsx => "xlsx",
653 Self::Xlsb => "xlsb",
654 Self::Xls => "xls",
655 Self::Csv => "csv",
656 Self::Tsv => "tsv",
657 _ => ""
658 }.to_string()
659 }
660}
661
662pub struct PathData<'a> {
663 path: &'a Path,
664 ext: Extension
665}
666
667impl<'a> PathData<'a> {
668 pub fn new(path: &'a Path) -> Self {
669 PathData {
670 path,
671 ext: Extension::from_path(path)
672 }
673 }
674
675 pub fn mode(&self) -> Extension {
676 self.ext
677 }
678
679 pub fn extension(&self) -> String {
680 self.ext.to_string()
681 }
682
683 pub fn ext(&self) -> Extension {
684 self.ext
685 }
686
687 pub fn path(&self) -> &Path {
688 self.path
689 }
690
691 pub fn is_valid(&self) -> bool {
692 match self.ext {
693 Extension::Unmatched => false,
694 _ => true
695 }
696 }
697
698 pub fn use_calamine(&self) -> bool {
699 self.ext.use_calamine()
700 }
701
702 pub fn filename(&self) -> String {
703 if let Some(file_ref) = self.path.file_name() {
704 file_ref.to_string_lossy().to_string()
705 } else {
706 "".to_owned()
707 }
708 }
709}
710
711
712
713#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
714pub enum ReadMode {
715 #[default]
716 Sync,
717 PreviewMultiple,
718 Async
719}
720
721impl ReadMode {
723
724 pub fn from_key(key: &str) -> Self {
725 let sample = key.to_lowercase().strip_non_alphanum();
726 match sample.as_str() {
727 "async" | "defer" | "deferred" | "a" => ReadMode::Async,
728 "preview" | "p" | "pre" | "multimode" | "multiple" | "previewmultiple" | "previewmulti" | "m" => ReadMode::PreviewMultiple,
729 _ => ReadMode::Sync
730 }
731 }
732
733 pub fn is_async(&self) -> bool {
734 match self {
735 Self::Async => true,
736 _ => false
737 }
738 }
739
740 pub fn is_multimode(&self) -> bool {
742 match self {
743 Self::PreviewMultiple => true,
744 _ => false
745 }
746 }
747}
748
749impl ToString for ReadMode {
750
751 fn to_string(&self) -> String {
752 match self {
753 Self::Async => "deferred",
754 Self::PreviewMultiple => "preview",
755 _ => "direct"
756 }.to_string()
757 }
758}
759
760#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
762pub enum FieldNameMode {
763 #[default]
764 AutoA1, AutoNumPadded, A1, NumPadded, }
769
770impl FieldNameMode {
772
773
774 pub fn from_key(system: &str, override_header: bool) -> Self {
775 if system.starts_with_ci("a1") {
776 if override_header {
777 FieldNameMode::A1
778 } else {
779 FieldNameMode::AutoA1
780 }
781 } else if system.starts_with_ci("c") || system.starts_with_ci("n") {
782 if override_header {
783 FieldNameMode::NumPadded
784 } else {
785 FieldNameMode::AutoNumPadded
786 }
787 } else {
788 FieldNameMode::AutoA1
789 }
790 }
791
792
793 pub fn use_a1(&self) -> bool {
795 match self {
796 Self::AutoA1 | Self::A1 => true,
797 _ => false
798 }
799 }
800
801 pub fn use_c01(&self) -> bool {
803 match self {
804 Self::AutoNumPadded | Self::NumPadded => true,
805 _ => false
806 }
807 }
808
809 pub fn override_headers(&self) -> bool {
811 match self {
812 Self::NumPadded | Self::A1 => true,
813 _ => false
814 }
815 }
816
817 pub fn keep_headers(&self) -> bool {
819 self.override_headers() == false
820 }
821}
822
823impl ToString for FieldNameMode {
824 fn to_string(&self) -> String {
825 match self {
826 Self::AutoNumPadded => "C01 auto",
827 Self::NumPadded => "C01 override",
828 Self::A1 => "A1 override",
829 _ => "A1 auto",
830 }.to_string()
831 }
832}
833
834#[cfg(test)]
835mod tests {
836 use super::*;
837
838 #[test]
839 fn test_format_mode() {
840 let custom_boolean = Format::truthy_custom("si", "no");
841 assert_eq!(custom_boolean.to_string(), "truthy(si,no)");
842 }
843
844 #[test]
845 fn test_match_truthy_custom() {
846 let (true_keys, false_keys) = match_custom_truthy("tr:si,no").unwrap();
847 assert_eq!("si", true_keys);
848 assert_eq!("no", false_keys);
849 }
850
851}