osv 0.1.0 → 0.2.0

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: 1671e82e2f6ef7e401e69fd4c30ae527a1606b6dd495bf631a3c44b336f4ef2e
4
- data.tar.gz: 9d790124a6ba8fd9414525f1691880f96e0d3e2f6a4fc5ccb85cebcedb333d8c
3
+ metadata.gz: a1b0c347b0bab5c9d31069c56f47999bfa51e85dfc1e127d1c4474a84ac19c53
4
+ data.tar.gz: 847b199da27b7c1329c1fa64fc8636592f004e93a12fc2ddd8db6127298ac23d
5
5
  SHA512:
6
- metadata.gz: 92b99239fb1ee85f1c99b7573c52d3f334a0d92b337245440246427d44854aa43162e52d5b6f15998ddeb7a15985d0c4a48230149babafbcb4715d71dac6c2e6
7
- data.tar.gz: 51e048de674bb2718a9bfee89966191ad2942640d3f15113b57272e026fd8e7966e00c5185d569a0b6b161717ef0b2c66a5e9d0e0cb9a48684e1bb7ddc9b3423
6
+ metadata.gz: 62fa77c1ca98031f483569a4dba7cf9e4eca52a4b5fae293d274d5f89c48003e301eab01d95116cfc9cc6a2642e742d16046231a21d25e4a5143bd6ec3b40dac
7
+ data.tar.gz: 3832cbb6ebadfc718a8a5d1963de960ed3abf09d4559d2f0ffe031c642a6c2581dc6ad6edf5d65f22248812585ca464916375503753831024fc355fe4cd04455
data/Cargo.lock CHANGED
@@ -11,6 +11,12 @@ dependencies = [
11
11
  "memchr",
12
12
  ]
13
13
 
14
+ [[package]]
15
+ name = "autocfg"
16
+ version = "1.4.0"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
19
+
14
20
  [[package]]
15
21
  name = "bindgen"
16
22
  version = "0.69.5"
@@ -90,6 +96,12 @@ version = "1.13.0"
90
96
  source = "registry+https://github.com/rust-lang/crates.io-index"
91
97
  checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
92
98
 
99
+ [[package]]
100
+ name = "futures-core"
101
+ version = "0.3.31"
102
+ source = "registry+https://github.com/rust-lang/crates.io-index"
103
+ checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
104
+
93
105
  [[package]]
94
106
  name = "glob"
95
107
  version = "0.3.1"
@@ -111,6 +123,16 @@ version = "1.0.14"
111
123
  source = "registry+https://github.com/rust-lang/crates.io-index"
112
124
  checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
113
125
 
126
+ [[package]]
127
+ name = "kanal"
128
+ version = "0.1.0-pre8"
129
+ source = "registry+https://github.com/rust-lang/crates.io-index"
130
+ checksum = "b05d55519627edaf7fd0f29981f6dc03fb52df3f5b257130eb8d0bf2801ea1d7"
131
+ dependencies = [
132
+ "futures-core",
133
+ "lock_api",
134
+ ]
135
+
114
136
  [[package]]
115
137
  name = "lazy_static"
116
138
  version = "1.5.0"
@@ -139,6 +161,16 @@ dependencies = [
139
161
  "windows-targets",
140
162
  ]
141
163
 
164
+ [[package]]
165
+ name = "lock_api"
166
+ version = "0.4.12"
167
+ source = "registry+https://github.com/rust-lang/crates.io-index"
168
+ checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
169
+ dependencies = [
170
+ "autocfg",
171
+ "scopeguard",
172
+ ]
173
+
142
174
  [[package]]
143
175
  name = "magnus"
144
176
  version = "0.6.4"
@@ -201,6 +233,7 @@ name = "osv"
201
233
  version = "0.1.0"
202
234
  dependencies = [
203
235
  "csv",
236
+ "kanal",
204
237
  "magnus 0.7.1",
205
238
  "rb-sys",
206
239
  "serde",
@@ -296,6 +329,12 @@ version = "1.0.18"
296
329
  source = "registry+https://github.com/rust-lang/crates.io-index"
297
330
  checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
298
331
 
332
+ [[package]]
333
+ name = "scopeguard"
334
+ version = "1.2.0"
335
+ source = "registry+https://github.com/rust-lang/crates.io-index"
336
+ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
337
+
299
338
  [[package]]
300
339
  name = "seq-macro"
301
340
  version = "0.3.5"
@@ -0,0 +1,402 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 3
4
+
5
+ [[package]]
6
+ name = "aho-corasick"
7
+ version = "1.1.3"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
10
+ dependencies = [
11
+ "memchr",
12
+ ]
13
+
14
+ [[package]]
15
+ name = "bindgen"
16
+ version = "0.69.5"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
19
+ dependencies = [
20
+ "bitflags",
21
+ "cexpr",
22
+ "clang-sys",
23
+ "itertools",
24
+ "lazy_static",
25
+ "lazycell",
26
+ "proc-macro2",
27
+ "quote",
28
+ "regex",
29
+ "rustc-hash",
30
+ "shlex",
31
+ "syn",
32
+ ]
33
+
34
+ [[package]]
35
+ name = "bitflags"
36
+ version = "2.6.0"
37
+ source = "registry+https://github.com/rust-lang/crates.io-index"
38
+ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
39
+
40
+ [[package]]
41
+ name = "cexpr"
42
+ version = "0.6.0"
43
+ source = "registry+https://github.com/rust-lang/crates.io-index"
44
+ checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
45
+ dependencies = [
46
+ "nom",
47
+ ]
48
+
49
+ [[package]]
50
+ name = "cfg-if"
51
+ version = "1.0.0"
52
+ source = "registry+https://github.com/rust-lang/crates.io-index"
53
+ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
54
+
55
+ [[package]]
56
+ name = "clang-sys"
57
+ version = "1.8.1"
58
+ source = "registry+https://github.com/rust-lang/crates.io-index"
59
+ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
60
+ dependencies = [
61
+ "glob",
62
+ "libc",
63
+ "libloading",
64
+ ]
65
+
66
+ [[package]]
67
+ name = "csv"
68
+ version = "1.3.1"
69
+ source = "registry+https://github.com/rust-lang/crates.io-index"
70
+ checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
71
+ dependencies = [
72
+ "csv-core",
73
+ "itoa",
74
+ "ryu",
75
+ "serde",
76
+ ]
77
+
78
+ [[package]]
79
+ name = "csv-core"
80
+ version = "0.1.11"
81
+ source = "registry+https://github.com/rust-lang/crates.io-index"
82
+ checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70"
83
+ dependencies = [
84
+ "memchr",
85
+ ]
86
+
87
+ [[package]]
88
+ name = "either"
89
+ version = "1.13.0"
90
+ source = "registry+https://github.com/rust-lang/crates.io-index"
91
+ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
92
+
93
+ [[package]]
94
+ name = "glob"
95
+ version = "0.3.1"
96
+ source = "registry+https://github.com/rust-lang/crates.io-index"
97
+ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
98
+
99
+ [[package]]
100
+ name = "itertools"
101
+ version = "0.12.1"
102
+ source = "registry+https://github.com/rust-lang/crates.io-index"
103
+ checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
104
+ dependencies = [
105
+ "either",
106
+ ]
107
+
108
+ [[package]]
109
+ name = "itoa"
110
+ version = "1.0.14"
111
+ source = "registry+https://github.com/rust-lang/crates.io-index"
112
+ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
113
+
114
+ [[package]]
115
+ name = "lazy_static"
116
+ version = "1.5.0"
117
+ source = "registry+https://github.com/rust-lang/crates.io-index"
118
+ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
119
+
120
+ [[package]]
121
+ name = "lazycell"
122
+ version = "1.3.0"
123
+ source = "registry+https://github.com/rust-lang/crates.io-index"
124
+ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
125
+
126
+ [[package]]
127
+ name = "libc"
128
+ version = "0.2.169"
129
+ source = "registry+https://github.com/rust-lang/crates.io-index"
130
+ checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
131
+
132
+ [[package]]
133
+ name = "libloading"
134
+ version = "0.8.6"
135
+ source = "registry+https://github.com/rust-lang/crates.io-index"
136
+ checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
137
+ dependencies = [
138
+ "cfg-if",
139
+ "windows-targets",
140
+ ]
141
+
142
+ [[package]]
143
+ name = "magnus"
144
+ version = "0.7.1"
145
+ source = "registry+https://github.com/rust-lang/crates.io-index"
146
+ checksum = "3d87ae53030f3a22e83879e666cb94e58a7bdf31706878a0ba48752994146dab"
147
+ dependencies = [
148
+ "magnus-macros",
149
+ "rb-sys",
150
+ "rb-sys-env",
151
+ "seq-macro",
152
+ ]
153
+
154
+ [[package]]
155
+ name = "magnus-macros"
156
+ version = "0.6.0"
157
+ source = "registry+https://github.com/rust-lang/crates.io-index"
158
+ checksum = "5968c820e2960565f647819f5928a42d6e874551cab9d88d75e3e0660d7f71e3"
159
+ dependencies = [
160
+ "proc-macro2",
161
+ "quote",
162
+ "syn",
163
+ ]
164
+
165
+ [[package]]
166
+ name = "memchr"
167
+ version = "2.7.4"
168
+ source = "registry+https://github.com/rust-lang/crates.io-index"
169
+ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
170
+
171
+ [[package]]
172
+ name = "minimal-lexical"
173
+ version = "0.2.1"
174
+ source = "registry+https://github.com/rust-lang/crates.io-index"
175
+ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
176
+
177
+ [[package]]
178
+ name = "nom"
179
+ version = "7.1.3"
180
+ source = "registry+https://github.com/rust-lang/crates.io-index"
181
+ checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
182
+ dependencies = [
183
+ "memchr",
184
+ "minimal-lexical",
185
+ ]
186
+
187
+ [[package]]
188
+ name = "osv"
189
+ version = "0.1.0"
190
+ dependencies = [
191
+ "csv",
192
+ "magnus",
193
+ "rb-sys",
194
+ ]
195
+
196
+ [[package]]
197
+ name = "proc-macro2"
198
+ version = "1.0.92"
199
+ source = "registry+https://github.com/rust-lang/crates.io-index"
200
+ checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
201
+ dependencies = [
202
+ "unicode-ident",
203
+ ]
204
+
205
+ [[package]]
206
+ name = "quote"
207
+ version = "1.0.37"
208
+ source = "registry+https://github.com/rust-lang/crates.io-index"
209
+ checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
210
+ dependencies = [
211
+ "proc-macro2",
212
+ ]
213
+
214
+ [[package]]
215
+ name = "rb-sys"
216
+ version = "0.9.103"
217
+ source = "registry+https://github.com/rust-lang/crates.io-index"
218
+ checksum = "91dbe37ab6ac2fba187480fb6544b92445e41e5c6f553bf0c33743f3c450a1df"
219
+ dependencies = [
220
+ "rb-sys-build",
221
+ ]
222
+
223
+ [[package]]
224
+ name = "rb-sys-build"
225
+ version = "0.9.103"
226
+ source = "registry+https://github.com/rust-lang/crates.io-index"
227
+ checksum = "c4d56a49dcb646b70b758789c0d16c055a386a4f2a3346333abb69850fa860ce"
228
+ dependencies = [
229
+ "bindgen",
230
+ "lazy_static",
231
+ "proc-macro2",
232
+ "quote",
233
+ "regex",
234
+ "shell-words",
235
+ "syn",
236
+ ]
237
+
238
+ [[package]]
239
+ name = "rb-sys-env"
240
+ version = "0.1.2"
241
+ source = "registry+https://github.com/rust-lang/crates.io-index"
242
+ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
243
+
244
+ [[package]]
245
+ name = "regex"
246
+ version = "1.11.1"
247
+ source = "registry+https://github.com/rust-lang/crates.io-index"
248
+ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
249
+ dependencies = [
250
+ "aho-corasick",
251
+ "memchr",
252
+ "regex-automata",
253
+ "regex-syntax",
254
+ ]
255
+
256
+ [[package]]
257
+ name = "regex-automata"
258
+ version = "0.4.9"
259
+ source = "registry+https://github.com/rust-lang/crates.io-index"
260
+ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
261
+ dependencies = [
262
+ "aho-corasick",
263
+ "memchr",
264
+ "regex-syntax",
265
+ ]
266
+
267
+ [[package]]
268
+ name = "regex-syntax"
269
+ version = "0.8.5"
270
+ source = "registry+https://github.com/rust-lang/crates.io-index"
271
+ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
272
+
273
+ [[package]]
274
+ name = "rustc-hash"
275
+ version = "1.1.0"
276
+ source = "registry+https://github.com/rust-lang/crates.io-index"
277
+ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
278
+
279
+ [[package]]
280
+ name = "ryu"
281
+ version = "1.0.18"
282
+ source = "registry+https://github.com/rust-lang/crates.io-index"
283
+ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
284
+
285
+ [[package]]
286
+ name = "seq-macro"
287
+ version = "0.3.5"
288
+ source = "registry+https://github.com/rust-lang/crates.io-index"
289
+ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4"
290
+
291
+ [[package]]
292
+ name = "serde"
293
+ version = "1.0.216"
294
+ source = "registry+https://github.com/rust-lang/crates.io-index"
295
+ checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
296
+ dependencies = [
297
+ "serde_derive",
298
+ ]
299
+
300
+ [[package]]
301
+ name = "serde_derive"
302
+ version = "1.0.216"
303
+ source = "registry+https://github.com/rust-lang/crates.io-index"
304
+ checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
305
+ dependencies = [
306
+ "proc-macro2",
307
+ "quote",
308
+ "syn",
309
+ ]
310
+
311
+ [[package]]
312
+ name = "shell-words"
313
+ version = "1.1.0"
314
+ source = "registry+https://github.com/rust-lang/crates.io-index"
315
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
316
+
317
+ [[package]]
318
+ name = "shlex"
319
+ version = "1.3.0"
320
+ source = "registry+https://github.com/rust-lang/crates.io-index"
321
+ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
322
+
323
+ [[package]]
324
+ name = "syn"
325
+ version = "2.0.90"
326
+ source = "registry+https://github.com/rust-lang/crates.io-index"
327
+ checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
328
+ dependencies = [
329
+ "proc-macro2",
330
+ "quote",
331
+ "unicode-ident",
332
+ ]
333
+
334
+ [[package]]
335
+ name = "unicode-ident"
336
+ version = "1.0.14"
337
+ source = "registry+https://github.com/rust-lang/crates.io-index"
338
+ checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
339
+
340
+ [[package]]
341
+ name = "windows-targets"
342
+ version = "0.52.6"
343
+ source = "registry+https://github.com/rust-lang/crates.io-index"
344
+ checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
345
+ dependencies = [
346
+ "windows_aarch64_gnullvm",
347
+ "windows_aarch64_msvc",
348
+ "windows_i686_gnu",
349
+ "windows_i686_gnullvm",
350
+ "windows_i686_msvc",
351
+ "windows_x86_64_gnu",
352
+ "windows_x86_64_gnullvm",
353
+ "windows_x86_64_msvc",
354
+ ]
355
+
356
+ [[package]]
357
+ name = "windows_aarch64_gnullvm"
358
+ version = "0.52.6"
359
+ source = "registry+https://github.com/rust-lang/crates.io-index"
360
+ checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
361
+
362
+ [[package]]
363
+ name = "windows_aarch64_msvc"
364
+ version = "0.52.6"
365
+ source = "registry+https://github.com/rust-lang/crates.io-index"
366
+ checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
367
+
368
+ [[package]]
369
+ name = "windows_i686_gnu"
370
+ version = "0.52.6"
371
+ source = "registry+https://github.com/rust-lang/crates.io-index"
372
+ checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
373
+
374
+ [[package]]
375
+ name = "windows_i686_gnullvm"
376
+ version = "0.52.6"
377
+ source = "registry+https://github.com/rust-lang/crates.io-index"
378
+ checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
379
+
380
+ [[package]]
381
+ name = "windows_i686_msvc"
382
+ version = "0.52.6"
383
+ source = "registry+https://github.com/rust-lang/crates.io-index"
384
+ checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
385
+
386
+ [[package]]
387
+ name = "windows_x86_64_gnu"
388
+ version = "0.52.6"
389
+ source = "registry+https://github.com/rust-lang/crates.io-index"
390
+ checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
391
+
392
+ [[package]]
393
+ name = "windows_x86_64_gnullvm"
394
+ version = "0.52.6"
395
+ source = "registry+https://github.com/rust-lang/crates.io-index"
396
+ checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
397
+
398
+ [[package]]
399
+ name = "windows_x86_64_msvc"
400
+ version = "0.52.6"
401
+ source = "registry+https://github.com/rust-lang/crates.io-index"
402
+ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
@@ -0,0 +1,15 @@
1
+ [package]
2
+ name = "osv"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [dependencies]
10
+ csv = "1.3.1"
11
+ magnus = { version = "0.7", features = ["rb-sys"] }
12
+ rb-sys = "0.9"
13
+ serde = { version = "1.0", features = ["derive"] }
14
+ serde_magnus = "0.8.1"
15
+ kanal = "0.1.0-pre8"
@@ -0,0 +1,15 @@
1
+ mod reader;
2
+ mod utils;
3
+
4
+ use crate::reader::*;
5
+
6
+ use magnus::{Error, Ruby};
7
+
8
+ /// Initializes the Ruby extension and defines methods.
9
+ #[magnus::init]
10
+ fn init(ruby: &Ruby) -> Result<(), Error> {
11
+ let module = ruby.define_module("OSV")?;
12
+ module.define_module_function("for_each", magnus::method!(parse_csv, -1))?;
13
+ module.define_module_function("for_each_compat", magnus::method!(parse_compat, -1))?;
14
+ Ok(())
15
+ }
@@ -0,0 +1,230 @@
1
+ use crate::utils::*;
2
+ use magnus::{
3
+ block::Yield, rb_sys::AsRawValue, value::ReprValue, Error, RClass, RString, Ruby, Value,
4
+ };
5
+ use std::{fs::File, io::Read, os::fd::FromRawFd, thread};
6
+
7
+ /// Parses CSV data from a file and yields each row as a hash to the block.
8
+ pub fn parse_csv(
9
+ ruby: &Ruby,
10
+ rb_self: Value,
11
+ args: &[Value],
12
+ ) -> Result<Yield<impl Iterator<Item = std::collections::HashMap<String, String>>>, Error> {
13
+ if !ruby.block_given() {
14
+ return Ok(Yield::Enumerator(rb_self.enumeratorize("for_each", args)));
15
+ }
16
+ let (to_read, has_headers, delimiter) = parse_csv_args(args)?;
17
+
18
+ let iter = RecordReader::<std::collections::HashMap<String, String>>::new(
19
+ ruby,
20
+ to_read,
21
+ has_headers,
22
+ delimiter.unwrap_or_else(|| ",".to_string()).as_bytes()[0],
23
+ 1000,
24
+ )?;
25
+
26
+ Ok(Yield::Iter(iter))
27
+ }
28
+
29
+ pub fn parse_compat(
30
+ ruby: &Ruby,
31
+ rb_self: Value,
32
+ args: &[Value],
33
+ ) -> Result<Yield<impl Iterator<Item = Vec<String>>>, Error> {
34
+ if !ruby.block_given() {
35
+ return Ok(Yield::Enumerator(
36
+ rb_self.enumeratorize("for_each_compat", args),
37
+ ));
38
+ }
39
+ let (to_read, has_headers, delimiter) = parse_csv_args(args)?;
40
+
41
+ let iter = RecordReader::<Vec<String>>::new(
42
+ ruby,
43
+ to_read,
44
+ has_headers,
45
+ delimiter.unwrap_or_else(|| ",".to_string()).as_bytes()[0],
46
+ 1000,
47
+ )?;
48
+
49
+ Ok(Yield::Iter(iter))
50
+ }
51
+
52
+ pub trait RecordParser {
53
+ type Output;
54
+
55
+ fn parse(headers: &[String], record: &csv::StringRecord) -> Self::Output;
56
+ }
57
+
58
+ impl RecordParser for std::collections::HashMap<String, String> {
59
+ type Output = Self;
60
+
61
+ fn parse(headers: &[String], record: &csv::StringRecord) -> Self::Output {
62
+ record
63
+ .iter()
64
+ .enumerate()
65
+ .map(|(i, field)| (headers[i].clone(), field.to_string()))
66
+ .collect()
67
+ }
68
+ }
69
+
70
+ impl RecordParser for Vec<String> {
71
+ type Output = Self;
72
+
73
+ fn parse(_headers: &[String], record: &csv::StringRecord) -> Self::Output {
74
+ record.iter().map(|field| field.to_string()).collect()
75
+ }
76
+ }
77
+
78
+ struct RecordReader<T: RecordParser> {
79
+ reader: ReadImpl<T>,
80
+ }
81
+
82
+ #[allow(dead_code)]
83
+ enum ReadImpl<T: RecordParser> {
84
+ SingleThreaded {
85
+ reader: csv::Reader<Box<dyn Read + Send + 'static>>,
86
+ headers: Vec<String>,
87
+ },
88
+ MultiThreaded {
89
+ receiver: kanal::Receiver<T::Output>,
90
+ handle: Option<thread::JoinHandle<()>>,
91
+ },
92
+ }
93
+
94
+ impl<T: RecordParser + Send + 'static> RecordReader<T> {
95
+ fn new(
96
+ ruby: &Ruby,
97
+ to_read: Value,
98
+ has_headers: bool,
99
+ delimiter: u8,
100
+ buffer: usize,
101
+ ) -> Result<Self, Error> {
102
+ let string_io = RClass::from(ruby.eval("StringIO").map_err(|e| {
103
+ Error::new(
104
+ ruby.exception_runtime_error(),
105
+ format!("Failed to get StringIO class: {}", e),
106
+ )
107
+ })?);
108
+
109
+ let readable: Box<dyn Read + Send + 'static> = if to_read.is_kind_of(string_io) {
110
+ let string: RString = to_read.funcall("string", ()).map_err(|e| {
111
+ Error::new(
112
+ ruby.exception_runtime_error(),
113
+ format!("Failed to get string from StringIO: {}", e),
114
+ )
115
+ })?;
116
+ let content = string.to_string().map_err(|e| {
117
+ Error::new(
118
+ ruby.exception_runtime_error(),
119
+ format!("Failed to convert string to Rust String: {}", e),
120
+ )
121
+ })?;
122
+ Box::new(std::io::Cursor::new(content))
123
+ } else if to_read.is_kind_of(ruby.class_io()) {
124
+ let fd = unsafe { rb_sys::rb_io_descriptor(to_read.as_raw()) };
125
+ let file = unsafe { File::from_raw_fd(fd) };
126
+ Box::new(file)
127
+ } else {
128
+ let path = to_read
129
+ .to_r_string()
130
+ .map_err(|e| {
131
+ Error::new(
132
+ ruby.exception_runtime_error(),
133
+ format!("Failed to convert path to string: {}", e),
134
+ )
135
+ })?
136
+ .to_string()
137
+ .map_err(|e| {
138
+ Error::new(
139
+ ruby.exception_runtime_error(),
140
+ format!("Failed to convert RString to Rust String: {}", e),
141
+ )
142
+ })?;
143
+ let file = std::fs::File::open(&path).map_err(|e| {
144
+ Error::new(
145
+ ruby.exception_runtime_error(),
146
+ format!("Failed to open file: {}", e),
147
+ )
148
+ })?;
149
+ Box::new(file)
150
+ };
151
+
152
+ let mut reader = csv::ReaderBuilder::new()
153
+ .has_headers(has_headers)
154
+ .delimiter(delimiter)
155
+ .from_reader(readable);
156
+
157
+ let headers = Self::get_headers(&mut reader, has_headers)?;
158
+ let headers_clone = headers.clone();
159
+
160
+ let (sender, receiver) = kanal::bounded(buffer);
161
+ let handle = thread::spawn(move || {
162
+ let mut record = csv::StringRecord::new();
163
+ while let Ok(read) = reader.read_record(&mut record) {
164
+ if !read {
165
+ let file_to_forget = reader.into_inner();
166
+ std::mem::forget(file_to_forget);
167
+ break;
168
+ }
169
+ let row = T::parse(&headers_clone, &record);
170
+ if sender.send(row).is_err() {
171
+ break;
172
+ }
173
+ }
174
+ });
175
+
176
+ let read_impl = ReadImpl::MultiThreaded {
177
+ receiver,
178
+ handle: Some(handle),
179
+ };
180
+
181
+ Ok(Self { reader: read_impl })
182
+ }
183
+
184
+ fn get_headers(
185
+ reader: &mut csv::Reader<impl Read>,
186
+ has_headers: bool,
187
+ ) -> Result<Vec<String>, Error> {
188
+ let first_row = reader
189
+ .headers()
190
+ .map_err(|e| {
191
+ Error::new(
192
+ magnus::exception::runtime_error(),
193
+ format!("Failed to read headers: {}", e),
194
+ )
195
+ })?
196
+ .clone();
197
+ let num_fields = first_row.len();
198
+
199
+ Ok(if has_headers {
200
+ first_row.iter().map(|h| h.to_string()).collect()
201
+ } else {
202
+ (0..num_fields).map(|i| format!("c{}", i)).collect()
203
+ })
204
+ }
205
+ }
206
+
207
+ impl<T: RecordParser> Iterator for RecordReader<T> {
208
+ type Item = T::Output;
209
+
210
+ fn next(&mut self) -> Option<Self::Item> {
211
+ match &mut self.reader {
212
+ ReadImpl::MultiThreaded { receiver, handle } => match receiver.recv() {
213
+ Ok(record) => Some(record),
214
+ Err(_) => {
215
+ if let Some(handle) = handle.take() {
216
+ let _ = handle.join();
217
+ }
218
+ None
219
+ }
220
+ },
221
+ ReadImpl::SingleThreaded { reader, headers } => {
222
+ let mut record = csv::StringRecord::new();
223
+ match reader.read_record(&mut record) {
224
+ Ok(true) => Some(T::parse(headers, &record)),
225
+ _ => None,
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
@@ -0,0 +1,20 @@
1
+ use magnus::{
2
+ scan_args::{get_kwargs, scan_args},
3
+ Error, Value,
4
+ };
5
+
6
+ /// Parse common arguments for CSV parsing
7
+ pub fn parse_csv_args(args: &[Value]) -> Result<(Value, bool, Option<String>), Error> {
8
+ let parsed_args = scan_args::<(Value,), (), (), (), _, ()>(args)?;
9
+ let (to_read,) = parsed_args.required;
10
+
11
+ let kwargs = get_kwargs::<_, (), (Option<bool>, Option<String>), ()>(
12
+ parsed_args.keywords,
13
+ &[],
14
+ &["has_headers", "delimiter"],
15
+ )?;
16
+
17
+ let has_headers = kwargs.optional.0.unwrap_or(true);
18
+
19
+ Ok((to_read, has_headers, kwargs.optional.1))
20
+ }
data/lib/osv/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OSV
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/osv.rbi ADDED
@@ -0,0 +1,29 @@
1
+ # typed: strict
2
+
3
+ module OSV
4
+ sig do
5
+ type_parameters(:T)
6
+ .params(
7
+ input: T.any(String, StringIO, IO),
8
+ has_headers: T.nilable(T::Boolean),
9
+ delimiter: T.nilable(String),
10
+ blk: T.proc.params(row: T::Hash[String, String]).void
11
+ )
12
+ .returns(T.untyped)
13
+ end
14
+ def self.for_each(input, has_headers: true, delimiter: nil, &blk)
15
+ end
16
+
17
+ sig do
18
+ type_parameters(:T)
19
+ .params(
20
+ input: T.any(String, StringIO, IO),
21
+ has_headers: T.nilable(T::Boolean),
22
+ delimiter: T.nilable(String),
23
+ blk: T.proc.params(row: T::Array[String]).void
24
+ )
25
+ .returns(T.untyped)
26
+ end
27
+ def self.for_each_compat(input, has_headers: true, delimiter: nil, &blk)
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Jaremko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-21 00:00:00.000000000 Z
11
+ date: 2024-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -50,8 +50,14 @@ files:
50
50
  - Cargo.toml
51
51
  - Gemfile
52
52
  - Rakefile
53
+ - ext/osv/Cargo.lock
54
+ - ext/osv/Cargo.toml
53
55
  - ext/osv/extconf.rb
56
+ - ext/osv/src/lib.rs
57
+ - ext/osv/src/reader.rs
58
+ - ext/osv/src/utils.rs
54
59
  - lib/osv.rb
60
+ - lib/osv.rbi
55
61
  - lib/osv/version.rb
56
62
  homepage: https://github.com/njaremko/osv
57
63
  licenses: