patchwork_csv_utils 0.1.15-x86_64-linux → 0.1.17-x86_64-linux

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: e996387ff4380d6fd613b82146c030db354bba2a6b922b1726c1bbbb41863554
4
- data.tar.gz: 88feb5ae5c6847311576c69cc6f9f6dcdd4fedda10acc0adc2303239a59dab9e
3
+ metadata.gz: f651263457993e883e97d83546cac1a67e20bc27803fb50ae696c7a8cbba01b5
4
+ data.tar.gz: f2c54d9821a1681f0c4f1558bae989430a10e05fd3e0aa9fb15b111f782faab6
5
5
  SHA512:
6
- metadata.gz: 558188ef57034619feae637bf5e561660635d656a2724441cc82b53075afef6b5de18035999359d9496ccb354f756048e8d7028936586ed85c0e966efe55ab88
7
- data.tar.gz: 60ce7b849799296e6c9afc57c5fcbafa8753b33c2949c5fccd77a1f32c8d39f56eff817ac2fe438299531eef859e33ecaf9c051731778373e11e0c57de0d9a7d
6
+ metadata.gz: 33405ba1d371558c4d86e2be3ad2d5607764cd7a20c0e5ae00c505392b44587a814d0f1907dcfa9f18cc1be335fa3dde27ac44f407a0b66fa800609432e5377c
7
+ data.tar.gz: 48d09709995deffa38a6381b43640075b390e6863023aeaf4f4a40b4d385bf02ec626c31b4d4c28fe8cd0b3432928fc8fda3657f70a509be13d75c535373de26
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- patchwork_csv_utils (0.1.15)
4
+ patchwork_csv_utils (0.1.17)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,7 +9,7 @@ pub mod utils;
9
9
  fn init() -> Result<(), magnus::Error> {
10
10
  let module = define_module("CsvUtils")?;
11
11
  module.define_singleton_method("dedup", function!(dedup, 4))?;
12
- module.define_singleton_method("to_csv", function!(to_csv, 5))?;
13
- module.define_singleton_method("transform_csv", function!(transform_csv, 5))?;
12
+ module.define_singleton_method("to_csv", function!(to_csv, 6))?;
13
+ module.define_singleton_method("transform_csv", function!(transform_csv, 6))?;
14
14
  Ok(())
15
15
  }
@@ -1,6 +1,5 @@
1
1
  use std::collections::HashMap;
2
2
  use std::fs::File;
3
- use calamine::Data;
4
3
  use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc};
5
4
  use csv::{Reader, StringRecord, Writer};
6
5
  use magnus::{Error, RArray, Ruby};
@@ -10,7 +9,8 @@ use crate::utils::{FileExtension, magnus_err, missing_header, to_datetime_error,
10
9
  pub fn transform_csv(ruby: &Ruby, csv_path: String,
11
10
  target_path: String, exclusions: RArray,
12
11
  mandatory_headers: RArray,
13
- status_exclusions: RArray) -> magnus::error::Result<()> {
12
+ status_exclusions: RArray,
13
+ expected_trust_name: String,) -> magnus::error::Result<()> {
14
14
  if !csv_path.has_extension(&["csv"]) {
15
15
  return Err(Error::new(ruby.exception_standard_error(), "csv_path must be a csv file".to_string()));
16
16
  }
@@ -41,6 +41,7 @@ pub fn transform_csv(ruby: &Ruby, csv_path: String,
41
41
  let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
42
42
  let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
43
43
  let status = header_map.get("Status");
44
+ let trust_name = header_map.get("Trust").ok_or(missing_header(ruby, "Trust"))?;
44
45
 
45
46
  let mandatory_records = get_mandatory_records(&ruby, &mut csv, &headers_list, &mandatory_headers)?;
46
47
 
@@ -59,6 +60,8 @@ pub fn transform_csv(ruby: &Ruby, csv_path: String,
59
60
  let column_value = record.get(*column_index).ok_or(missing_value(ruby, column))?;
60
61
  let column_value = column_value.trim_end();
61
62
 
63
+ validate_trust_name(ruby, &expected_trust_name, trust_name, ri, i, &column_value.to_string())?;
64
+
62
65
  if i == *date {
63
66
  let current = string_to_datetime(column_value).ok_or(to_datetime_error(ruby, column_value, ri, "Date"))?;
64
67
  date_value = current;
@@ -84,6 +87,16 @@ pub fn transform_csv(ruby: &Ruby, csv_path: String,
84
87
  Ok(())
85
88
  }
86
89
 
90
+
91
+ fn validate_trust_name(ruby: &Ruby, expected_trust_name: &String, trust_name: &usize, ri: usize, i: usize, s: &String) -> magnus::error::Result<()> {
92
+ if i == *trust_name {
93
+ if s != &expected_trust_name.clone() {
94
+ return Err(magnus::Error::new(ruby.exception_standard_error(), format!("Trust actual name: '{}' is not as expected: '{}'", s, expected_trust_name)));
95
+ }
96
+ }
97
+ Ok(())
98
+ }
99
+
87
100
  fn get_mandatory_records(ruby: &Ruby, csv: &mut Reader<File>, csv_header_list: &Vec<String>, mandatory_headers_list: &Vec<String>) -> magnus::error::Result<Vec<StringRecord>> {
88
101
  let inverse_header_map: HashMap<usize, String> = csv_header_list.iter().enumerate().map(|(i, h)| (i, h.to_string())).collect();
89
102
 
@@ -13,6 +13,7 @@ pub fn to_csv(ruby: &Ruby, xls_path: String,
13
13
  exclusions: RArray,
14
14
  mandatory_headers: RArray,
15
15
  status_exclusions: RArray,
16
+ expected_trust_name: String,
16
17
  ) -> magnus::error::Result<()> {
17
18
  if !xls_path.has_extension(&["xls"]) {
18
19
  return Err(magnus::Error::new(ruby.exception_standard_error(), "xls_path must be an xls file".to_string()));
@@ -37,14 +38,15 @@ pub fn to_csv(ruby: &Ruby, xls_path: String,
37
38
  let csv_out_file = File::create(target_path.clone()).map_err(|e| magnus_err(ruby, e, format!("could not create csv file: {}", target_path).as_str()))?;
38
39
  let mut dest = BufWriter::new(csv_out_file);
39
40
 
40
- write_csv(ruby, &mut dest, &range, header_map, exclusions, mandatory_headers, headers_list, status_exclusions)
41
+ write_csv(ruby, &mut dest, &range, header_map, exclusions, mandatory_headers, headers_list, status_exclusions, expected_trust_name)
41
42
  }
42
43
 
43
44
  fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>,
44
45
  header_map: HashMap<String, usize>, exclusions: Vec<String>,
45
46
  mandatory_headers: Vec<String>,
46
47
  headers_list: Vec<String>,
47
- status_exclusions: Vec<String>) -> magnus::error::Result<()> {
48
+ status_exclusions: Vec<String>,
49
+ expected_trust_name: String) -> magnus::error::Result<()> {
48
50
  let n = mandatory_headers.len() - 1;
49
51
  let request_id = header_map.get("Request Id").ok_or(missing_header(ruby, "Request Id"))?;
50
52
  let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
@@ -53,6 +55,7 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>,
53
55
  let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
54
56
  let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
55
57
  let status = header_map.get("Status");
58
+ let trust_name = header_map.get("Trust").ok_or(missing_header(ruby, "Trust"))?;
56
59
 
57
60
  let mandatory_rows = get_mandatory_records(ruby, range, &headers_list, &mandatory_headers)?;
58
61
 
@@ -66,6 +69,7 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>,
66
69
  if date_value_is_not_present(&date, &r) {
67
70
  return Err(magnus::Error::new(ruby.exception_standard_error(), format!("Date value is not present in row: {}", ri)));
68
71
  }
72
+ validate_trust_name(ruby, &expected_trust_name, trust_name, ri, &r)?;
69
73
 
70
74
  for (i, c) in mandatory_headers.iter().enumerate() {
71
75
  let column_index = header_map.get(c).ok_or(missing_header(ruby, c))?;
@@ -99,6 +103,16 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>,
99
103
  Ok(())
100
104
  }
101
105
 
106
+ fn validate_trust_name(ruby: &Ruby, expected_trust_name: &String, trust_name: &usize, ri: usize, r: &Vec<&Data>) -> magnus::error::Result<()> {
107
+ if ri > 0 {
108
+ let s = r[*trust_name].to_string();
109
+ if s != expected_trust_name.clone() {
110
+ return Err(magnus::Error::new(ruby.exception_standard_error(), format!("Trust actual name: '{}' is not as expected: '{}'", s, expected_trust_name)));
111
+ }
112
+ }
113
+ Ok(())
114
+ }
115
+
102
116
  fn get_mandatory_records<'a>(ruby: &Ruby, range: &'a Range<Data>, csv_header_list: &Vec<String>, mandatory_headers_list: &Vec<String>) -> magnus::error::Result<Vec<Vec<&'a Data>>> {
103
117
  let inverse_header_map: HashMap<usize, String> = csv_header_list.iter().enumerate().map(|(i, h)| (i, h.to_string())).collect();
104
118
 
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.15'
4
+ VERSION = '0.1.17'
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.15
4
+ version: 0.1.17
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - kingsley.hendrickse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-31 00:00:00.000000000 Z
11
+ date: 2024-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Deduplication of CSV files and XLS to CSV conversion.
14
14
  email: