patchwork_csv_utils 0.1.8-arm64-darwin → 0.1.10-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cee57c41f3e282435e1d7de609e9674dfd8f12b3b58a800ec5d5208c4563956f
4
- data.tar.gz: 1c14864e23f63a738cb88f077d272a15d6efdba60ee0ed086cd7f3e99cd6ccc1
3
+ metadata.gz: b60384aea2adb2338f9f4c18f1e1d1655bc0ed8f1d6d0d9063041f84d5f8bc14
4
+ data.tar.gz: c78780ff1baa4962ad8c510fa296c36a85fc1cc9dd87f44ad68b0fba6f370d6c
5
5
  SHA512:
6
- metadata.gz: d54494b5517a1624c2904ca9ff74a1d66ae64cefb3cd3eabd2452c0daa034e69af6292b307629ad7075974e5cb59aff44a2843d5d7f2c18e0c08e604481d1d32
7
- data.tar.gz: 25837083730e79c5f161ef4a5cef157659662f639d5c3c6cd0601cbd53fdabf8a8c303458927071163a2ff7e8b15ecdf07d04d01aefebf797f83e96d43a0d2b3
6
+ metadata.gz: b35ac44ac9b8f0c608beb3b38af2aba2d0e237ea5bc93e9481e2b7f2e6f67fd2a3109e9262826ef4a1b34d16cee4097ffcc59ac75d222a725355ddb036ea7cdb
7
+ data.tar.gz: 798596165d66f0883265f6c64b513b18b5d7c17a1ef9bfb46b55f4121cc9f82928bcce617217aa9c8857c883b64003556bc554395b6b45185c416a25d4fb0e76
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- patchwork_csv_utils (0.1.8)
4
+ patchwork_csv_utils (0.1.10)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -3,9 +3,9 @@ use std::fs::File;
3
3
 
4
4
  use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc};
5
5
  use csv::{StringRecord, Writer};
6
- use magnus::{RArray, Ruby};
6
+ use magnus::{Error, RArray, Ruby};
7
7
 
8
- use crate::utils::{FileExtension, magnus_err, missing_header};
8
+ use crate::utils::{FileExtension, magnus_err, missing_header, to_datetime_error};
9
9
 
10
10
  pub fn transform_csv(ruby: &Ruby, csv_path: String, target_path: String, exclusions: RArray) -> magnus::error::Result<()> {
11
11
  if !csv_path.has_extension(&["csv"]) {
@@ -47,11 +47,8 @@ pub fn transform_csv(ruby: &Ruby, csv_path: String, target_path: String, exclusi
47
47
  Ok(current.to_string())
48
48
  } else if i == *start || i == *end || i == *actual_start || i == *actual_end {
49
49
  if c.is_empty() { return Ok(c.to_string()); }
50
- let unknown = "Unknown".to_string();
51
- let column_name = inverse_header_map.get(&i).unwrap_or(&unknown);
52
- let current_time = string_to_time(c).ok_or(to_datetime_error(ruby, c, ri, column_name))?;
53
- let datetime = transform_time_to_datetime(date_value, current_time);
54
- Ok(datetime.to_string())
50
+ let column_name = get_column_name(&inverse_header_map, &i);
51
+ process_datetime(ruby, ri, date_value, c, &column_name)
55
52
  } else {
56
53
  Ok(c.to_string())
57
54
  }
@@ -66,15 +63,39 @@ pub fn transform_csv(ruby: &Ruby, csv_path: String, target_path: String, exclusi
66
63
  Ok(())
67
64
  }
68
65
 
66
+ fn process_datetime(ruby: &Ruby, ri: usize, date_value: NaiveDateTime, c: &str, column_name: &String) -> magnus::error::Result<String> {
67
+ let maybe_correct = correct_datetime(c);
68
+ if let Some(correct) = maybe_correct {
69
+ return Ok(correct.to_string());
70
+ }
71
+
72
+ let current_time = string_to_time(c).ok_or(to_datetime_error(ruby, c, ri, column_name))?;
73
+ let datetime = transform_time_to_datetime(date_value, current_time);
74
+ Ok(datetime.to_string())
75
+ }
76
+
77
+ fn get_column_name(inverse_header_map: &HashMap<usize, String>, i: &usize) -> String {
78
+ let unknown = "Unknown".to_string();
79
+ let column_name = inverse_header_map.get(&i).unwrap_or(&unknown);
80
+ column_name.to_string()
81
+ }
82
+
69
83
  fn skip_excluded_rows(request_id: &usize, r: &StringRecord, exclusions: &Vec<String>) -> bool {
70
84
  let value = r.get(*request_id).unwrap_or_default();
71
85
  exclusions.contains(&value.to_string())
72
86
  }
73
87
 
74
88
  fn string_to_datetime(s: &str) -> Option<NaiveDateTime> {
89
+ let maybe_correct = correct_datetime(s);
90
+ if maybe_correct.is_some() { return maybe_correct; }
91
+
75
92
  NaiveDate::parse_from_str(s, "%d-%b-%y").ok().map(|d| d.and_hms_opt(0, 0, 0)).flatten()
76
93
  }
77
94
 
95
+ fn correct_datetime(s: &str) -> Option<NaiveDateTime> {
96
+ NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").ok()
97
+ }
98
+
78
99
  fn string_to_time(s: &str) -> Option<NaiveTime> {
79
100
  NaiveTime::parse_from_str(s, "%H:%M").ok()
80
101
  }
@@ -83,10 +104,6 @@ fn transform_time_to_datetime(t1: NaiveDateTime, t2: NaiveTime) -> NaiveDateTime
83
104
  NaiveDateTime::new(t1.date(), t2)
84
105
  }
85
106
 
86
- fn to_datetime_error(ruby: &Ruby, value: &str, row: usize, col: &str) -> magnus::Error {
87
- magnus::Error::new(ruby.exception_standard_error(), format!("Could not parse datetime '{}', row: {}, col: {}", value, row, col))
88
- }
89
-
90
107
  fn has_empty_first_col_skip_row(record: &StringRecord) -> bool {
91
108
  record[0].is_empty()
92
109
  }
@@ -15,6 +15,10 @@ fn magnus_err<E: Error>(ruby: &Ruby, e: E, msg: &str) -> magnus::Error {
15
15
  magnus::Error::new(ruby.exception_standard_error(), format!("{}: {}", msg, e.to_string()))
16
16
  }
17
17
 
18
+ fn to_datetime_error(ruby: &Ruby, value: &str, row: usize, col: &str) -> magnus::Error {
19
+ magnus::Error::new(ruby.exception_standard_error(), format!("Could not parse datetime '{}', row: {}, col: {}", value, row, col))
20
+ }
21
+
18
22
  pub trait FileExtension {
19
23
  fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool;
20
24
  }
@@ -6,7 +6,7 @@ use calamine::{Data, open_workbook, Range, Reader, Xls};
6
6
  use chrono::{NaiveDateTime, Utc};
7
7
  use magnus::{RArray, Ruby};
8
8
 
9
- use crate::utils::{FileExtension, magnus_err, missing_header};
9
+ use crate::utils::{FileExtension, magnus_err, missing_header, to_datetime_error};
10
10
 
11
11
  pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String, exclusions: RArray) -> magnus::error::Result<()> {
12
12
  if !xls_path.has_extension(&["xls"]) {
@@ -98,10 +98,6 @@ fn transform_time_to_datetime(t1: NaiveDateTime, t2: NaiveDateTime) -> NaiveDate
98
98
  NaiveDateTime::new(t1.date(), t2.time())
99
99
  }
100
100
 
101
- fn to_datetime_error(ruby: &Ruby, value: &str, row: usize, col: &str) -> magnus::Error {
102
- magnus::Error::new(ruby.exception_standard_error(), format!("Could not parse datetime '{}', row: {}, col: {}", value, row, col))
103
- }
104
-
105
101
  fn handle_commas<W: Write>(dest: &mut W, s: &str) -> std::io::Result<()> {
106
102
  if s.contains(",") {
107
103
  write!(dest, "{:?}", clean_strings(s).trim_end())
Binary file
Binary file
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CsvUtils
4
- VERSION = '0.1.8'
4
+ VERSION = '0.1.10'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchwork_csv_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  platform: arm64-darwin
6
6
  authors:
7
7
  - kingsley.hendrickse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-07 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Deduplication of CSV files and XLS to CSV conversion.
14
14
  email: