xcsv 0.2.0 → 0.3.0
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/.gitignore +3 -0
- data/Gemfile.lock +2 -2
- data/README.md +8 -9
- data/bin/console +14 -0
- data/lib/xcsv.rb +13 -2
- data/lib/xcsv/version.rb +1 -1
- data/src/lib.rs +45 -48
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42eb140ee193e61a54d51f784eb2e793bcb4811892344b1edb3e0a0146eb92b1
|
4
|
+
data.tar.gz: cdd5a40fa9e4faf7586bae3c8d42689cadfc6f969cbfe6e384c65c687a1228a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21f528621774e3da62632ab9b5669764270cce4ef6aed37d81b0d6630b5b56a1569a919da2755f19e246718e92840747b3e1b4032cc918d6d029fa08ab6b9575
|
7
|
+
data.tar.gz: 70a4c13f561575d12d48e0a40e2691cd6485f5b0d5d8152c2e6e6ab6b33eed9343f80f4736b0529dbc8c72f1b27dd422783c82072b069241bcb0dd84528ab003
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
xcsv (0.
|
4
|
+
xcsv (0.2.0)
|
5
5
|
helix_runtime (= 0.7.5)
|
6
6
|
|
7
7
|
GEM
|
@@ -34,9 +34,9 @@ PLATFORMS
|
|
34
34
|
|
35
35
|
DEPENDENCIES
|
36
36
|
bundler (~> 1.17)
|
37
|
-
xcsv!
|
38
37
|
rake (~> 12.3)
|
39
38
|
rspec (~> 3.8)
|
39
|
+
xcsv!
|
40
40
|
|
41
41
|
BUNDLED WITH
|
42
42
|
1.17.1
|
data/README.md
CHANGED
@@ -8,15 +8,14 @@ Fast CSV reader based on [Rust CSV crate](https://docs.rs/csv/1.0.2/csv/)
|
|
8
8
|
|
9
9
|
Don't miss this message:
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
```
|
12
|
+
Rust is installed now. Great!
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
|
15
|
+
environment variable. Next time you log in this will be done automatically.
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
```
|
17
|
+
To configure your current shell run source $HOME/.cargo/env
|
18
|
+
```
|
20
19
|
|
21
20
|
2. `gem install xcsv`
|
22
21
|
|
@@ -34,7 +33,7 @@ end
|
|
34
33
|
csv_reader = XSV.new("foo.csv")
|
35
34
|
csv_reader.take(10).to_a #=> [[col1, ...], [col1, ...], ...]
|
36
35
|
|
37
|
-
#
|
36
|
+
# while loop
|
38
37
|
csv_reader = XSV.new("bar.csv")
|
39
38
|
while (rec = csv_reader.next) do
|
40
39
|
rec #=> [col1, col2, col3, ...]
|
@@ -58,7 +57,7 @@ end
|
|
58
57
|
|
59
58
|
# XCSV
|
60
59
|
csv_reader = XCSV.new('sample.csv')
|
61
|
-
while (
|
60
|
+
while (rec = csv_reader.next) do
|
62
61
|
end
|
63
62
|
|
64
63
|
# CSV
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "xcsv"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/lib/xcsv.rb
CHANGED
@@ -1,12 +1,23 @@
|
|
1
1
|
require 'helix_runtime'
|
2
2
|
require 'xcsv/native'
|
3
|
+
require 'xcsv/version'
|
3
4
|
|
4
5
|
class XCSV
|
5
6
|
include Enumerable
|
6
7
|
|
8
|
+
def self.open(path)
|
9
|
+
xcsv = new(path)
|
10
|
+
xcsv.open
|
11
|
+
begin
|
12
|
+
yield xcsv
|
13
|
+
ensure
|
14
|
+
xcsv.close
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
7
18
|
def each
|
8
|
-
while (
|
9
|
-
yield
|
19
|
+
while (rec = self.next) do
|
20
|
+
yield rec
|
10
21
|
end
|
11
22
|
end
|
12
23
|
end
|
data/lib/xcsv/version.rb
CHANGED
data/src/lib.rs
CHANGED
@@ -12,14 +12,10 @@ use std::ops::{Deref, DerefMut};
|
|
12
12
|
use flate2::read::GzDecoder;
|
13
13
|
use regex::Regex;
|
14
14
|
|
15
|
-
use helix::{FromRuby, CheckResult};
|
16
|
-
use helix::sys::{VALUE};
|
17
|
-
|
18
15
|
type CSVIterType = Iterator<Item=Result<csv::StringRecord, csv::Error>>;
|
19
16
|
|
20
17
|
struct CSVIter {
|
21
18
|
iter: Box<CSVIterType>,
|
22
|
-
path: String,
|
23
19
|
}
|
24
20
|
|
25
21
|
impl Deref for CSVIter {
|
@@ -38,7 +34,7 @@ impl DerefMut for CSVIter {
|
|
38
34
|
|
39
35
|
impl std::fmt::Debug for CSVIter {
|
40
36
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
41
|
-
write!(f, "CSVIter
|
37
|
+
write!(f, "CSVIter")
|
42
38
|
}
|
43
39
|
}
|
44
40
|
|
@@ -48,61 +44,62 @@ impl Clone for CSVIter {
|
|
48
44
|
}
|
49
45
|
}
|
50
46
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
let path = String::from_checked(checked_path);
|
58
|
-
|
59
|
-
let gz_regex = Regex::new("\\.gz\\z").unwrap();
|
47
|
+
ruby! {
|
48
|
+
class XCSV {
|
49
|
+
struct {
|
50
|
+
path: String,
|
51
|
+
iter: Option<CSVIter>,
|
52
|
+
}
|
60
53
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
Err(e) => raise!(format!("Error while opening file: {}", e)),
|
65
|
-
};
|
54
|
+
def initialize(helix, path: String) {
|
55
|
+
XCSV { helix, path, iter: None }
|
56
|
+
}
|
66
57
|
|
67
|
-
|
68
|
-
|
69
|
-
Box::new(GzDecoder::new(buf_reader))
|
70
|
-
} else {
|
71
|
-
Box::new(buf_reader)
|
72
|
-
};
|
58
|
+
def open(&mut self) -> Result<(), helix::Error> {
|
59
|
+
self.iter = None;
|
73
60
|
|
74
|
-
|
75
|
-
csv::ReaderBuilder::new()
|
76
|
-
.has_headers(false)
|
77
|
-
.from_reader(gz_reader);
|
61
|
+
let gz_regex = Regex::new("\\.gz\\z").unwrap();
|
78
62
|
|
79
|
-
|
80
|
-
|
63
|
+
let buf_reader =
|
64
|
+
match File::open(&self.path) {
|
65
|
+
Ok(f) => BufReader::new(f),
|
66
|
+
Err(e) => raise!(e.to_string()),
|
67
|
+
};
|
81
68
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
}
|
69
|
+
let gz_reader: Box<Read> =
|
70
|
+
if gz_regex.is_match(&self.path) {
|
71
|
+
Box::new(GzDecoder::new(buf_reader))
|
72
|
+
} else {
|
73
|
+
Box::new(buf_reader)
|
74
|
+
};
|
86
75
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
}
|
76
|
+
let csv_reader =
|
77
|
+
csv::ReaderBuilder::new()
|
78
|
+
.has_headers(false)
|
79
|
+
.from_reader(gz_reader);
|
92
80
|
|
93
|
-
|
94
|
-
|
81
|
+
self.iter = Some(CSVIter{iter: Box::new(csv_reader.into_records())});
|
82
|
+
Ok(())
|
95
83
|
}
|
96
84
|
|
97
85
|
def next(&mut self) -> Result<Option<Vec<String>>, helix::Error> {
|
98
|
-
match self.iter
|
99
|
-
Some(
|
100
|
-
|
101
|
-
|
102
|
-
|
86
|
+
match self.iter {
|
87
|
+
Some(ref mut iter) =>
|
88
|
+
match iter.next() {
|
89
|
+
Some(Ok(record)) =>
|
90
|
+
Ok(Some(record.iter().map(|s| s.to_string()).collect())),
|
91
|
+
Some(Err(e)) =>
|
92
|
+
raise!(e.to_string()),
|
93
|
+
None =>
|
94
|
+
Ok(None)
|
95
|
+
}
|
103
96
|
None =>
|
104
|
-
|
97
|
+
raise!("closed file")
|
105
98
|
}
|
106
99
|
}
|
100
|
+
|
101
|
+
def close(&mut self) -> () {
|
102
|
+
self.iter = None
|
103
|
+
}
|
107
104
|
}
|
108
105
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xcsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Moroz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: helix_runtime
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- Gemfile.lock
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
+
- bin/console
|
83
84
|
- extconfig.rb
|
84
85
|
- lib/xcsv.rb
|
85
86
|
- lib/xcsv/version.rb
|