patchwork_csv_utils 0.1.3-x86_64-darwin → 0.1.5-x86_64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/Gemfile.lock +1 -1
 - data/ext/csv_utils/src/lib.rs +1 -1
 - data/ext/csv_utils/src/utils/dedup.rs +22 -0
 - data/ext/csv_utils/src/utils/xls.rs +28 -11
 - data/lib/csv_utils/2.7/csv_utils.bundle +0 -0
 - data/lib/csv_utils/3.0/csv_utils.bundle +0 -0
 - data/lib/csv_utils/3.1/csv_utils.bundle +0 -0
 - data/lib/csv_utils/3.2/csv_utils.bundle +0 -0
 - data/lib/csv_utils/version.rb +1 -1
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 53bd775b8a062a5418959e170dce922799739ebbf4a4845eb48f65e889ae4fff
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 26e9f478dd57a8a9307b4536e242af967145c688ae9e4f4002655da86f8405cc
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 256012cce7a6b5802c9ab05fe1e9063e7d99f63fa83f3f011f8375698a5abb1897312776c333fd9757e17c20b7674aff9b9997c3519effb39f9833be2be3f8e9
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 6ce45dcb2f95347350f6272fc8b0c4b84e0b8d24bd773d86b40e9b370de334839773c2363be1ef55e6809d6f510f19bfd25ebecdc669dd65dbb61a8aaa91b172
         
     | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/ext/csv_utils/src/lib.rs
    CHANGED
    
    | 
         @@ -8,6 +8,6 @@ pub mod utils; 
     | 
|
| 
       8 
8 
     | 
    
         
             
            fn init() -> Result<(), magnus::Error> {
         
     | 
| 
       9 
9 
     | 
    
         
             
                let module = define_module("CsvUtils")?;
         
     | 
| 
       10 
10 
     | 
    
         
             
                module.define_singleton_method("dedup", function!(dedup, 3))?;
         
     | 
| 
       11 
     | 
    
         
            -
                module.define_singleton_method("to_csv", function!(to_csv,  
     | 
| 
      
 11 
     | 
    
         
            +
                module.define_singleton_method("to_csv", function!(to_csv, 2))?;
         
     | 
| 
       12 
12 
     | 
    
         
             
                Ok(())
         
     | 
| 
       13 
13 
     | 
    
         
             
            }
         
     | 
| 
         @@ -33,12 +33,20 @@ pub fn dedup(ruby: &Ruby, previous_csv_path: String, new_csv_path: String, targe 
     | 
|
| 
       33 
33 
     | 
    
         
             
                let mut previous_records = vec![];
         
     | 
| 
       34 
34 
     | 
    
         
             
                for previous_record in previous_csv.records() {
         
     | 
| 
       35 
35 
     | 
    
         
             
                    let previous_record = previous_record.map_err(|e| magnus_err(ruby, e, "previous_record"))?;
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                    if has_empty_row_skip(&previous_record) { continue; }
         
     | 
| 
      
 38 
     | 
    
         
            +
                    if has_empty_first_col_skip_row(&previous_record) { continue; }
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
       36 
40 
     | 
    
         
             
                    let previous_record = previous_record.into_iter().map(|r| r.trim_end()).collect::<StringRecord>();
         
     | 
| 
       37 
41 
     | 
    
         
             
                    previous_records.push(previous_record)
         
     | 
| 
       38 
42 
     | 
    
         
             
                }
         
     | 
| 
       39 
43 
     | 
    
         | 
| 
       40 
44 
     | 
    
         
             
                for new_record in new_csv.records() {
         
     | 
| 
       41 
45 
     | 
    
         
             
                    let new_record = new_record.map_err(|e| magnus_err(ruby, e, "new_record"))?;
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    if has_empty_row_skip(&new_record) { continue; }
         
     | 
| 
      
 48 
     | 
    
         
            +
                    if has_empty_first_col_skip_row(&new_record) { continue; }
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       42 
50 
     | 
    
         
             
                    let new_record = new_record.into_iter().map(|r| r.trim_end()).collect::<StringRecord>();
         
     | 
| 
       43 
51 
     | 
    
         
             
                    if !previous_records.contains(&new_record) {
         
     | 
| 
       44 
52 
     | 
    
         
             
                        wtr.write_byte_record(new_record.as_byte_record()).unwrap();
         
     | 
| 
         @@ -50,4 +58,18 @@ pub fn dedup(ruby: &Ruby, previous_csv_path: String, new_csv_path: String, targe 
     | 
|
| 
       50 
58 
     | 
    
         
             
                Ok(())
         
     | 
| 
       51 
59 
     | 
    
         
             
            }
         
     | 
| 
       52 
60 
     | 
    
         | 
| 
      
 61 
     | 
    
         
            +
            fn has_empty_first_col_skip_row(previous_record: &StringRecord) -> bool {
         
     | 
| 
      
 62 
     | 
    
         
            +
                if previous_record[0].is_empty() {
         
     | 
| 
      
 63 
     | 
    
         
            +
                    return true;
         
     | 
| 
      
 64 
     | 
    
         
            +
                }
         
     | 
| 
      
 65 
     | 
    
         
            +
                false
         
     | 
| 
      
 66 
     | 
    
         
            +
            }
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            fn has_empty_row_skip(record: &StringRecord) -> bool {
         
     | 
| 
      
 69 
     | 
    
         
            +
                if record.iter().all(|r| r.is_empty()) {
         
     | 
| 
      
 70 
     | 
    
         
            +
                    return true;
         
     | 
| 
      
 71 
     | 
    
         
            +
                }
         
     | 
| 
      
 72 
     | 
    
         
            +
                false
         
     | 
| 
      
 73 
     | 
    
         
            +
            }
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
       53 
75 
     | 
    
         | 
| 
         @@ -8,13 +8,15 @@ use magnus::Ruby; 
     | 
|
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            use crate::utils::{FileExtension, magnus_err};
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
     | 
    
         
            -
            pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String 
     | 
| 
      
 11 
     | 
    
         
            +
            pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String) -> magnus::error::Result<()> {
         
     | 
| 
       12 
12 
     | 
    
         
             
                if !xls_path.has_extension(&["xls"]) {
         
     | 
| 
       13 
13 
     | 
    
         
             
                    return Err(magnus::Error::new(ruby.exception_standard_error(), "xls_path must be an xls file".to_string()));
         
     | 
| 
       14 
14 
     | 
    
         
             
                }
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
                let mut workbook: Xls<_> = open_workbook(xls_path.clone()).map_err(|e| magnus_err(ruby, e, format!("could not open xls: {}", xls_path).as_str()))?;
         
     | 
| 
       17 
     | 
    
         
            -
                let range = workbook. 
     | 
| 
      
 17 
     | 
    
         
            +
                let range = workbook.worksheet_range_at(0)
         
     | 
| 
      
 18 
     | 
    
         
            +
                    .ok_or(magnus::Error::new(ruby.exception_standard_error(), "no worksheet found in xls".to_string()))
         
     | 
| 
      
 19 
     | 
    
         
            +
                    .and_then(|r| r.map_err(|e| magnus_err(ruby, e, "could not read worksheet range")))?;
         
     | 
| 
       18 
20 
     | 
    
         | 
| 
       19 
21 
     | 
    
         
             
                let headers = range.headers().ok_or(magnus::Error::new(ruby.exception_standard_error(), "no headers found in xls".to_string()))?;
         
     | 
| 
       20 
22 
     | 
    
         
             
                let header_map: HashMap<String, usize> = headers.iter().enumerate().map(|(i, h)| (h.to_string(), i)).collect();
         
     | 
| 
         @@ -29,17 +31,16 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma 
     | 
|
| 
       29 
31 
     | 
    
         
             
                for (ri, r) in range.rows().enumerate() {
         
     | 
| 
       30 
32 
     | 
    
         
             
                    let mut date_value = Utc::now().naive_utc();
         
     | 
| 
       31 
33 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                    if r 
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
                    }
         
     | 
| 
      
 34 
     | 
    
         
            +
                    if skip_empty_rows(r) { continue; }
         
     | 
| 
      
 35 
     | 
    
         
            +
                    if skip_rows_with_no_request_id(&header_map, r) { continue; }
         
     | 
| 
       35 
36 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
                     
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
                        let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
         
     | 
| 
      
 37 
     | 
    
         
            +
                    let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
         
     | 
| 
      
 38 
     | 
    
         
            +
                    let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
         
     | 
| 
      
 39 
     | 
    
         
            +
                    let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
         
     | 
| 
      
 40 
     | 
    
         
            +
                    let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
         
     | 
| 
      
 41 
     | 
    
         
            +
                    let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
         
     | 
| 
       42 
42 
     | 
    
         | 
| 
      
 43 
     | 
    
         
            +
                    for (i, c) in r.iter().enumerate() {
         
     | 
| 
       43 
44 
     | 
    
         
             
                        match *c {
         
     | 
| 
       44 
45 
     | 
    
         
             
                            Data::Empty => Ok(()),
         
     | 
| 
       45 
46 
     | 
    
         
             
                            Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => {
         
     | 
| 
         @@ -68,6 +69,22 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma 
     | 
|
| 
       68 
69 
     | 
    
         
             
                Ok(())
         
     | 
| 
       69 
70 
     | 
    
         
             
            }
         
     | 
| 
       70 
71 
     | 
    
         | 
| 
      
 72 
     | 
    
         
            +
            fn skip_empty_rows(r: &[Data]) -> bool {
         
     | 
| 
      
 73 
     | 
    
         
            +
                if r.iter().all(|c| c == &Data::Empty) {
         
     | 
| 
      
 74 
     | 
    
         
            +
                    return true;
         
     | 
| 
      
 75 
     | 
    
         
            +
                }
         
     | 
| 
      
 76 
     | 
    
         
            +
                false
         
     | 
| 
      
 77 
     | 
    
         
            +
            }
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
            fn skip_rows_with_no_request_id(header_map: &HashMap<String, usize>, r: &[Data]) -> bool {
         
     | 
| 
      
 80 
     | 
    
         
            +
                if let Some(request_id) = header_map.get("Request Id") {
         
     | 
| 
      
 81 
     | 
    
         
            +
                    if r[*request_id] == Data::Empty {
         
     | 
| 
      
 82 
     | 
    
         
            +
                        return true;
         
     | 
| 
      
 83 
     | 
    
         
            +
                    }
         
     | 
| 
      
 84 
     | 
    
         
            +
                }
         
     | 
| 
      
 85 
     | 
    
         
            +
                false
         
     | 
| 
      
 86 
     | 
    
         
            +
            }
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
       71 
88 
     | 
    
         
             
            fn missing_header(ruby: &Ruby, header: &str) -> magnus::Error {
         
     | 
| 
       72 
89 
     | 
    
         
             
                magnus::Error::new(ruby.exception_standard_error(), format!("Missing '{}' header in xls", header))
         
     | 
| 
       73 
90 
     | 
    
         
             
            }
         
     | 
| 
         Binary file 
     | 
| 
         Binary file 
     | 
| 
         Binary file 
     | 
| 
         Binary file 
     | 
    
        data/lib/csv_utils/version.rb
    CHANGED