whatlang 0.1.7 → 0.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fe408a4eb64c2503c40b8ab8aecdada6911921fe21f70b280666ac1a8e6caef
4
- data.tar.gz: 97543a0ee08c9c4dbaedf432f13fae42e717277e175330b5a750dedac0a3eddb
3
+ metadata.gz: 69c49c403761528be76daea679fbf38b5c7fbf2f508a819d9bef3e5b6bf14b48
4
+ data.tar.gz: 748275ab31ce880de74a32c92ebd680f3b05af4303cf29e3985cfb774076d0e9
5
5
  SHA512:
6
- metadata.gz: ed1d876556395802faad05ef54858d0ebfb2c8d4050a68fa2da806c9cfc870bde75d3716bab9e3ef30734913308c719b4dd5f87410cc84fbb2e6aca3a9f4f6d2
7
- data.tar.gz: 3944216a4447a75408e0bc1ebb2e9d865be9ddc8739091cdf14cd4ac9a8e44942f89bedc6d8a54b254c5432813ab0dc937d2c78dc755a7a3fb889cdd5af39a24
6
+ metadata.gz: 8c7acef11c2721c3ea347f4c64d199114c22ebd3258cef88acbb8cd81633d9ec1a0843f8fa3543c667b1c416ffa22631a71fe0e0ddbb5b7803a7bc0d581f729d
7
+ data.tar.gz: 381ad3386f4bae91ed497e1a49f5bf60df9346457f775f0fe7f6df335cb05d2b5ca5123a30f4e2fcae518c81d2a0e9da12aa1c69841b3a192b3349909d96e618
data/.gitignore CHANGED
@@ -7,8 +7,5 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  Gemfile.lock
10
-
11
- # Added by cargo
12
-
13
- /target
14
-
10
+ /lib/whatlang-rb/whatlang_rb.bundle
11
+ /whatlang-rb/
data/.gitlab-ci.yml CHANGED
@@ -1,16 +1,16 @@
1
1
  default:
2
- image: ruby:3.2.2
2
+ image: ruby:3.4.7
3
3
 
4
4
  before_script:
5
5
  - apt-get update && apt-get install -y clang
6
- - gem update --system '3.4.22'
7
- - gem install bundler -v 2.4.22
8
- - gem install tomlrb
9
- - bundle install
10
6
  - curl https://sh.rustup.rs -sSf | sh -s -- -y
7
+ - . "$HOME/.cargo/env"
8
+ - gem update --system
9
+ - gem install bundler
10
+ - bundle install
11
11
 
12
12
  test:
13
13
  variables:
14
14
  RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN: 'true'
15
15
  script:
16
- - source "$HOME/.cargo/env" && bundle exec rake test
16
+ - source "$HOME/.cargo/env" && rake test
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.1.8
2
+ =====
3
+
4
+ * Switch Rust glue library from [Rutie][] to [Magnus][]
5
+
1
6
  0.1.7
2
7
  =====
3
8
 
@@ -41,3 +46,5 @@
41
46
  * Initial release
42
47
 
43
48
  [@bendangelo]: https://gitlab.com/bendangelo
49
+ [Rutie]: https://github.com/danielpclark/rutie
50
+ [Magnus]: https://docs.rs/magnus/latest/magnus/
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in whatlang.gemspec
6
6
  gemspec
7
+
8
+ gem "fiddle", "~> 1.1"
data/README.md CHANGED
@@ -8,7 +8,7 @@ Features
8
8
 
9
9
  Features are derived from original [Whatlang][] library, which includes:
10
10
 
11
- * Supports [69 languages](https://github.com/greyblake/whatlang-rs/blob/master/SUPPORTED_LANGUAGES.md)
11
+ * Supports [70 languages](https://github.com/greyblake/whatlang-rs/blob/master/SUPPORTED_LANGUAGES.md)
12
12
  * Lightweight, fast and simple
13
13
  * Recognizes not only a language, but also a script (Latin, Cyrillic, etc)
14
14
  * Provides reliability information
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rake/clean"
4
+ require "rubygems/ext"
4
5
  require "rubygems/tasks"
5
6
  require "rake/testtask"
6
7
  require "yard"
@@ -10,16 +11,13 @@ task default: :test
10
11
  Gem::Tasks.new
11
12
  YARD::Rake::YardocTask.new
12
13
 
13
- RUST_TARGET = "target/release/libwhatlang_rb.so"
14
- RUST_SRC = FileList["src/**/*.rs"]
15
-
16
- RUST_SRC.each do |path|
17
- file path
18
- end
19
-
20
- file RUST_TARGET => RUST_SRC + ["Cargo.toml", "Cargo.lock"] do
21
- sh "cargo build --release"
14
+ EXTENSION = "lib/whatlang-rb/whatlang_rb.#{RbConfig::CONFIG["DLEXT"]}"
15
+ file EXTENSION do
16
+ results = Rake.verbose == true ? $stdout : []
17
+ Gem::Ext::CargoBuilder.new.build "ext/whatlang-rb/Cargo.toml", ".", results, [], "lib", File.expand_path("ext/whatlang-rb")
22
18
  end
23
- CLEAN.include RUST_TARGET
19
+ CLEAN.include "whatlang-rb"
20
+ CLOBBER.include EXTENSION
24
21
 
25
- Rake::TestTask.new test: RUST_TARGET
22
+ Rake::TestTask.new
23
+ task test: EXTENSION
@@ -0,0 +1,360 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "ahash"
7
+ version = "0.8.12"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
10
+ dependencies = [
11
+ "cfg-if",
12
+ "once_cell",
13
+ "version_check",
14
+ "zerocopy",
15
+ ]
16
+
17
+ [[package]]
18
+ name = "aho-corasick"
19
+ version = "1.1.3"
20
+ source = "registry+https://github.com/rust-lang/crates.io-index"
21
+ checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
22
+ dependencies = [
23
+ "memchr",
24
+ ]
25
+
26
+ [[package]]
27
+ name = "allocator-api2"
28
+ version = "0.2.21"
29
+ source = "registry+https://github.com/rust-lang/crates.io-index"
30
+ checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
31
+
32
+ [[package]]
33
+ name = "bindgen"
34
+ version = "0.69.5"
35
+ source = "registry+https://github.com/rust-lang/crates.io-index"
36
+ checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
37
+ dependencies = [
38
+ "bitflags",
39
+ "cexpr",
40
+ "clang-sys",
41
+ "itertools",
42
+ "lazy_static",
43
+ "lazycell",
44
+ "proc-macro2",
45
+ "quote",
46
+ "regex",
47
+ "rustc-hash",
48
+ "shlex",
49
+ "syn",
50
+ ]
51
+
52
+ [[package]]
53
+ name = "bitflags"
54
+ version = "2.10.0"
55
+ source = "registry+https://github.com/rust-lang/crates.io-index"
56
+ checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
57
+
58
+ [[package]]
59
+ name = "cexpr"
60
+ version = "0.6.0"
61
+ source = "registry+https://github.com/rust-lang/crates.io-index"
62
+ checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
63
+ dependencies = [
64
+ "nom",
65
+ ]
66
+
67
+ [[package]]
68
+ name = "cfg-if"
69
+ version = "1.0.4"
70
+ source = "registry+https://github.com/rust-lang/crates.io-index"
71
+ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
72
+
73
+ [[package]]
74
+ name = "clang-sys"
75
+ version = "1.8.1"
76
+ source = "registry+https://github.com/rust-lang/crates.io-index"
77
+ checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
78
+ dependencies = [
79
+ "glob",
80
+ "libc",
81
+ "libloading",
82
+ ]
83
+
84
+ [[package]]
85
+ name = "either"
86
+ version = "1.15.0"
87
+ source = "registry+https://github.com/rust-lang/crates.io-index"
88
+ checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
89
+
90
+ [[package]]
91
+ name = "glob"
92
+ version = "0.3.3"
93
+ source = "registry+https://github.com/rust-lang/crates.io-index"
94
+ checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
95
+
96
+ [[package]]
97
+ name = "hashbrown"
98
+ version = "0.14.5"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
101
+ dependencies = [
102
+ "ahash",
103
+ "allocator-api2",
104
+ ]
105
+
106
+ [[package]]
107
+ name = "itertools"
108
+ version = "0.12.1"
109
+ source = "registry+https://github.com/rust-lang/crates.io-index"
110
+ checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
111
+ dependencies = [
112
+ "either",
113
+ ]
114
+
115
+ [[package]]
116
+ name = "lazy_static"
117
+ version = "1.5.0"
118
+ source = "registry+https://github.com/rust-lang/crates.io-index"
119
+ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
120
+
121
+ [[package]]
122
+ name = "lazycell"
123
+ version = "1.3.0"
124
+ source = "registry+https://github.com/rust-lang/crates.io-index"
125
+ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
126
+
127
+ [[package]]
128
+ name = "libc"
129
+ version = "0.2.177"
130
+ source = "registry+https://github.com/rust-lang/crates.io-index"
131
+ checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
132
+
133
+ [[package]]
134
+ name = "libloading"
135
+ version = "0.8.9"
136
+ source = "registry+https://github.com/rust-lang/crates.io-index"
137
+ checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
138
+ dependencies = [
139
+ "cfg-if",
140
+ "windows-link",
141
+ ]
142
+
143
+ [[package]]
144
+ name = "magnus"
145
+ version = "0.8.2"
146
+ source = "registry+https://github.com/rust-lang/crates.io-index"
147
+ checksum = "3b36a5b126bbe97eb0d02d07acfeb327036c6319fd816139a49824a83b7f9012"
148
+ dependencies = [
149
+ "magnus-macros",
150
+ "rb-sys",
151
+ "rb-sys-env",
152
+ "seq-macro",
153
+ ]
154
+
155
+ [[package]]
156
+ name = "magnus-macros"
157
+ version = "0.8.0"
158
+ source = "registry+https://github.com/rust-lang/crates.io-index"
159
+ checksum = "47607461fd8e1513cb4f2076c197d8092d921a1ea75bd08af97398f593751892"
160
+ dependencies = [
161
+ "proc-macro2",
162
+ "quote",
163
+ "syn",
164
+ ]
165
+
166
+ [[package]]
167
+ name = "memchr"
168
+ version = "2.7.6"
169
+ source = "registry+https://github.com/rust-lang/crates.io-index"
170
+ checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
171
+
172
+ [[package]]
173
+ name = "minimal-lexical"
174
+ version = "0.2.1"
175
+ source = "registry+https://github.com/rust-lang/crates.io-index"
176
+ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
177
+
178
+ [[package]]
179
+ name = "nom"
180
+ version = "7.1.3"
181
+ source = "registry+https://github.com/rust-lang/crates.io-index"
182
+ checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
183
+ dependencies = [
184
+ "memchr",
185
+ "minimal-lexical",
186
+ ]
187
+
188
+ [[package]]
189
+ name = "once_cell"
190
+ version = "1.21.3"
191
+ source = "registry+https://github.com/rust-lang/crates.io-index"
192
+ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
193
+
194
+ [[package]]
195
+ name = "proc-macro2"
196
+ version = "1.0.101"
197
+ source = "registry+https://github.com/rust-lang/crates.io-index"
198
+ checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
199
+ dependencies = [
200
+ "unicode-ident",
201
+ ]
202
+
203
+ [[package]]
204
+ name = "quote"
205
+ version = "1.0.41"
206
+ source = "registry+https://github.com/rust-lang/crates.io-index"
207
+ checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
208
+ dependencies = [
209
+ "proc-macro2",
210
+ ]
211
+
212
+ [[package]]
213
+ name = "rb-sys"
214
+ version = "0.9.117"
215
+ source = "registry+https://github.com/rust-lang/crates.io-index"
216
+ checksum = "f900d1ce4629a2ebffaf5de74bd8f9c1188d4c5ed406df02f97e22f77a006f44"
217
+ dependencies = [
218
+ "rb-sys-build",
219
+ ]
220
+
221
+ [[package]]
222
+ name = "rb-sys-build"
223
+ version = "0.9.117"
224
+ source = "registry+https://github.com/rust-lang/crates.io-index"
225
+ checksum = "ef1e9c857028f631056bcd6d88cec390c751e343ce2223ddb26d23eb4a151d59"
226
+ dependencies = [
227
+ "bindgen",
228
+ "lazy_static",
229
+ "proc-macro2",
230
+ "quote",
231
+ "regex",
232
+ "shell-words",
233
+ "syn",
234
+ ]
235
+
236
+ [[package]]
237
+ name = "rb-sys-env"
238
+ version = "0.2.2"
239
+ source = "registry+https://github.com/rust-lang/crates.io-index"
240
+ checksum = "08f8d2924cf136a1315e2b4c7460a39f62ef11ee5d522df9b2750fab55b868b6"
241
+
242
+ [[package]]
243
+ name = "regex"
244
+ version = "1.12.2"
245
+ source = "registry+https://github.com/rust-lang/crates.io-index"
246
+ checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
247
+ dependencies = [
248
+ "aho-corasick",
249
+ "memchr",
250
+ "regex-automata",
251
+ "regex-syntax",
252
+ ]
253
+
254
+ [[package]]
255
+ name = "regex-automata"
256
+ version = "0.4.13"
257
+ source = "registry+https://github.com/rust-lang/crates.io-index"
258
+ checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
259
+ dependencies = [
260
+ "aho-corasick",
261
+ "memchr",
262
+ "regex-syntax",
263
+ ]
264
+
265
+ [[package]]
266
+ name = "regex-syntax"
267
+ version = "0.8.8"
268
+ source = "registry+https://github.com/rust-lang/crates.io-index"
269
+ checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
270
+
271
+ [[package]]
272
+ name = "rustc-hash"
273
+ version = "1.1.0"
274
+ source = "registry+https://github.com/rust-lang/crates.io-index"
275
+ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
276
+
277
+ [[package]]
278
+ name = "seq-macro"
279
+ version = "0.3.6"
280
+ source = "registry+https://github.com/rust-lang/crates.io-index"
281
+ checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc"
282
+
283
+ [[package]]
284
+ name = "shell-words"
285
+ version = "1.1.0"
286
+ source = "registry+https://github.com/rust-lang/crates.io-index"
287
+ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
288
+
289
+ [[package]]
290
+ name = "shlex"
291
+ version = "1.3.0"
292
+ source = "registry+https://github.com/rust-lang/crates.io-index"
293
+ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
294
+
295
+ [[package]]
296
+ name = "syn"
297
+ version = "2.0.107"
298
+ source = "registry+https://github.com/rust-lang/crates.io-index"
299
+ checksum = "2a26dbd934e5451d21ef060c018dae56fc073894c5a7896f882928a76e6d081b"
300
+ dependencies = [
301
+ "proc-macro2",
302
+ "quote",
303
+ "unicode-ident",
304
+ ]
305
+
306
+ [[package]]
307
+ name = "unicode-ident"
308
+ version = "1.0.20"
309
+ source = "registry+https://github.com/rust-lang/crates.io-index"
310
+ checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06"
311
+
312
+ [[package]]
313
+ name = "version_check"
314
+ version = "0.9.5"
315
+ source = "registry+https://github.com/rust-lang/crates.io-index"
316
+ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
317
+
318
+ [[package]]
319
+ name = "whatlang"
320
+ version = "0.16.4"
321
+ source = "registry+https://github.com/rust-lang/crates.io-index"
322
+ checksum = "471d1c1645d361eb782a1650b1786a8fb58dd625e681a04c09f5ff7c8764a7b0"
323
+ dependencies = [
324
+ "hashbrown",
325
+ "once_cell",
326
+ ]
327
+
328
+ [[package]]
329
+ name = "whatlang-rb"
330
+ version = "0.1.7"
331
+ dependencies = [
332
+ "magnus",
333
+ "whatlang",
334
+ ]
335
+
336
+ [[package]]
337
+ name = "windows-link"
338
+ version = "0.2.1"
339
+ source = "registry+https://github.com/rust-lang/crates.io-index"
340
+ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
341
+
342
+ [[package]]
343
+ name = "zerocopy"
344
+ version = "0.8.27"
345
+ source = "registry+https://github.com/rust-lang/crates.io-index"
346
+ checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c"
347
+ dependencies = [
348
+ "zerocopy-derive",
349
+ ]
350
+
351
+ [[package]]
352
+ name = "zerocopy-derive"
353
+ version = "0.8.27"
354
+ source = "registry+https://github.com/rust-lang/crates.io-index"
355
+ checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
356
+ dependencies = [
357
+ "proc-macro2",
358
+ "quote",
359
+ "syn",
360
+ ]
@@ -0,0 +1,12 @@
1
+ [package]
2
+ name = "whatlang-rb"
3
+ version = "0.1.8"
4
+ authors = ["Kitaiti Makoto <KitaitiMakoto@gmail.com>"]
5
+ edition = "2024"
6
+
7
+ [dependencies]
8
+ magnus = "0.8"
9
+ whatlang = "0.16.4"
10
+
11
+ [lib]
12
+ crate-type = ["cdylib"]
@@ -0,0 +1,122 @@
1
+ use magnus::{
2
+ Error, Ruby, Value, function, method,
3
+ prelude::*,
4
+ scan_args::{get_kwargs, scan_args},
5
+ };
6
+ use whatlang as wl;
7
+
8
+ #[magnus::wrap(class = "Whatlang::Lang")]
9
+ struct Lang(wl::Lang);
10
+
11
+ impl Lang {
12
+ fn code(&self) -> &str {
13
+ self.0.code()
14
+ }
15
+
16
+ fn name(&self) -> &str {
17
+ self.0.name()
18
+ }
19
+
20
+ fn eng_name(&self) -> &str {
21
+ self.0.eng_name()
22
+ }
23
+ }
24
+
25
+ #[magnus::wrap(class = "Whatlang::Info")]
26
+ struct Info(wl::Info);
27
+
28
+ impl Info {
29
+ fn lang(&self) -> Lang {
30
+ Lang(self.0.lang())
31
+ }
32
+
33
+ fn script(&self) -> String {
34
+ self.0.script().to_string()
35
+ }
36
+
37
+ fn confidence(&self) -> f64 {
38
+ self.0.confidence()
39
+ }
40
+
41
+ fn is_reliable(&self) -> bool {
42
+ self.0.is_reliable()
43
+ }
44
+ }
45
+
46
+ fn detect(ruby: &Ruby, args: &[Value]) -> Result<Option<Info>, Error> {
47
+ type LangList = Option<Vec<String>>;
48
+
49
+ let args = scan_args::<(Value,), (), (), (), _, ()>(args)?;
50
+ let kw_args = get_kwargs::<_, (), (LangList, LangList), ()>(
51
+ args.keywords,
52
+ &[],
53
+ &["allowlist", "denylist"],
54
+ )?;
55
+ let (allowlist, denylist) = kw_args.optional;
56
+ if allowlist.is_some() && denylist.is_some() {
57
+ return Err(Error::new(
58
+ ruby.exception_arg_error(),
59
+ "Couldn't specify `allowlist' and `denylist' at a time. Choose one.",
60
+ ));
61
+ }
62
+
63
+ let text = args.required.0.to_r_string()?.to_string()?;
64
+ Ok(if let Some(allowlist) = allowlist {
65
+ detect_with_allowlist(text, allowlist)
66
+ } else if let Some(denylist) = denylist {
67
+ detect_with_denylist(text, denylist)
68
+ } else {
69
+ detect_without_options(text)
70
+ })
71
+ }
72
+
73
+ fn detect_without_options(text: String) -> Option<Info> {
74
+ wl::detect(&text).map(Info)
75
+ }
76
+
77
+ fn detect_with_allowlist(text: String, allowlist: Vec<String>) -> Option<Info> {
78
+ wl::Detector::with_allowlist(allowlist.iter().filter_map(wl::Lang::from_code).collect())
79
+ .detect(&text)
80
+ .map(Info)
81
+ }
82
+
83
+ fn detect_with_denylist(text: String, denylist: Vec<String>) -> Option<Info> {
84
+ wl::Detector::with_denylist(denylist.iter().filter_map(wl::Lang::from_code).collect())
85
+ .detect(&text)
86
+ .map(Info)
87
+ }
88
+
89
+ fn detect_lang(text: String) -> Option<Lang> {
90
+ wl::detect_lang(&text).map(Lang)
91
+ }
92
+
93
+ fn detect_script(text: String) -> Option<String> {
94
+ wl::detect_script(&text).map(|script| script.to_string())
95
+ }
96
+
97
+ #[magnus::init]
98
+ fn init(ruby: &Ruby) -> Result<(), Error> {
99
+ let module = ruby.define_module("Whatlang")?;
100
+ module.define_singleton_method(
101
+ "detect_without_options",
102
+ function!(detect_without_options, 1),
103
+ )?;
104
+ module.define_singleton_method("detect", function!(detect, -1))?;
105
+ module.define_singleton_method("detect_with_allowlist", function!(detect_with_allowlist, 2))?;
106
+ module.define_singleton_method("detect_with_denylist", function!(detect_with_denylist, 2))?;
107
+ module.define_singleton_method("detect_lang", function!(detect_lang, 1))?;
108
+ module.define_singleton_method("detect_script", function!(detect_script, 1))?;
109
+
110
+ let lang = module.define_class("Lang", ruby.class_object())?;
111
+ lang.define_method("code", method!(Lang::code, 0))?;
112
+ lang.define_method("name", method!(Lang::name, 0))?;
113
+ lang.define_method("eng_name", method!(Lang::eng_name, 0))?;
114
+
115
+ let info = module.define_class("Info", ruby.class_object())?;
116
+ info.define_method("lang", method!(Info::lang, 0))?;
117
+ info.define_method("script", method!(Info::script, 0))?;
118
+ info.define_method("confidence", method!(Info::confidence, 0))?;
119
+ info.define_method("reliable?", method!(Info::is_reliable, 0))?;
120
+
121
+ Ok(())
122
+ }
data/lib/whatlang.rb CHANGED
@@ -1,57 +1 @@
1
- require "rutie"
2
-
3
- Rutie.new(:whatlang_rb).init "Init_whatlang", __dir__
4
-
5
- module Whatlang
6
- class << self
7
- def detect(text, allowlist: nil, denylist: nil)
8
- if denylist && allowlist
9
- raise ArgumentError, "Couldn't specify `allowlist' and `denylist' at a time. Choose one."
10
- end
11
-
12
- text = text.to_s
13
-
14
- case
15
- when allowlist
16
- detect_with_allowlist(text, allowlist)
17
- when denylist
18
- detect_with_denylist(text, denylist)
19
- else
20
- detect_without_options(text)
21
- end
22
- end
23
- end
24
-
25
- class Info
26
- attr_reader :lang, :script, :confidence
27
-
28
- def initialize(lang, script, is_reliable, confidence)
29
- @lang = lang
30
- @script = script
31
- @is_reliable = is_reliable
32
- @confidence = confidence
33
- end
34
-
35
- def reliable?
36
- @is_reliable
37
- end
38
- end
39
-
40
- class Lang
41
- attr_reader :code, :name, :eng_name
42
-
43
- def initialize(code, name, eng_name)
44
- @code = code
45
- @name = name
46
- @eng_name = eng_name
47
- end
48
- end
49
-
50
- private
51
-
52
- # Class returnned by detect when no lang detected.
53
- #
54
- # Needed because Rutie cause segmentation fault when it returns NilClass.
55
- # Should be removed after https://github.com/danielpclark/rutie/pull/180 is solved.
56
- NO_INFO = nil
57
- end
1
+ require "whatlang-rb/whatlang_rb"
@@ -0,0 +1,38 @@
1
+ require "test/unit"
2
+ require 'tempfile'
3
+ require 'tmpdir'
4
+ require 'shellwords'
5
+
6
+ class TestPackage < Test::Unit::TestCase
7
+ def test_build
8
+ Tempfile.create do |file|
9
+ assert system("gem", "build", "whatlang.gemspec", "--output", file.to_path.shellescape, exception: true)
10
+ assert file.size > 0
11
+ assert_path_exist file.to_path
12
+ end
13
+ end
14
+
15
+ class TestInstall < self
16
+ def setup
17
+ system "rake", "build", exception: true
18
+ end
19
+
20
+ def test_install
21
+ gemspec = Gem::Specification.load("whatlang.gemspec")
22
+ Dir.mktmpdir do |dir|
23
+ dir = File.realpath(dir)
24
+ system "gem", "install", "--install-dir", dir.shellescape, "--no-document", "pkg/#{gemspec.file_name.shellescape}", exception: true
25
+ assert_installed dir, gemspec.version
26
+
27
+ libdir = File.join(dir, "gems", "#{gemspec.name}-#{gemspec.version}", "lib")
28
+ assert_match(/ita/, `ruby -I #{libdir.shellescape} -r whatlang -e 'print Whatlang.detect("Jen la trinkejo fermitis, ni iras tra mallumo kaj pluvo.", allowlist: ["eng", "ita"]).lang.code'`)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def assert_installed(dir, version)
35
+ assert_path_exist File.join(dir, "gems/whatlang-#{version}/lib/whatlang-rb", "whatlang_rb.#{RbConfig::CONFIG["DLEXT"]}")
36
+ end
37
+ end
38
+ end
@@ -26,6 +26,7 @@ class TestWhatlang < Test::Unit::TestCase
26
26
  text = "Jen la trinkejo fermitis, ni iras tra mallumo kaj pluvo."
27
27
  list = ["eng", "ita"]
28
28
  assert_equal "ita", Whatlang.detect_with_allowlist(text, list).lang.code
29
+ assert_equal "ita", Whatlang.detect_with_allowlist(text, list + ["unknown"]).lang.code
29
30
  end
30
31
 
31
32
  def test_detect_with_denylist
data/whatlang.gemspec CHANGED
@@ -1,8 +1,8 @@
1
- require "tomlrb"
1
+ require "json"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "whatlang"
5
- spec.version = Tomlrb.load_file("Cargo.toml")["package"]["version"]
5
+ spec.version = JSON.parse(`cargo metadata --no-deps --format-version=1 --manifest-path=ext/whatlang-rb/Cargo.toml`)["packages"][0]["version"]
6
6
  spec.license = "Ruby"
7
7
  spec.authors = ["Kitaiti Makoto"]
8
8
  spec.email = ["KitaitiMakoto@gmail.com"]
@@ -18,14 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
19
  `git ls-files -z`.split("\x0")
20
20
  end
21
- spec.extensions = ["ext/Rakefile"]
21
+ spec.extensions = ["ext/whatlang-rb/Cargo.toml"]
22
22
 
23
- spec.add_runtime_dependency "rutie"
24
-
25
- spec.add_development_dependency "tomlrb"
26
23
  spec.add_development_dependency "test-unit"
27
24
  spec.add_development_dependency "rake"
28
25
  spec.add_development_dependency "yard"
29
26
  spec.add_development_dependency "rubygems-tasks"
30
- spec.add_runtime_dependency "racc"
31
27
  end
metadata CHANGED
@@ -1,43 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatlang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kitaiti Makoto
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-09-06 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rutie
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: tomlrb
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
12
  - !ruby/object:Gem::Dependency
42
13
  name: test-unit
43
14
  requirement: !ruby/object:Gem::Requirement
@@ -94,26 +65,12 @@ dependencies:
94
65
  - - ">="
95
66
  - !ruby/object:Gem::Version
96
67
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: racc
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
68
  description: Ruby bindings for Whatlang, a natural language detection for Rust.
112
69
  email:
113
70
  - KitaitiMakoto@gmail.com
114
71
  executables: []
115
72
  extensions:
116
- - ext/Rakefile
73
+ - ext/whatlang-rb/Cargo.toml
117
74
  extra_rdoc_files: []
118
75
  files:
119
76
  - ".gitignore"
@@ -122,16 +79,15 @@ files:
122
79
  - BSDL
123
80
  - CHANGELOG.md
124
81
  - COPYING
125
- - Cargo.lock
126
- - Cargo.toml
127
82
  - Gemfile
128
83
  - README.md
129
84
  - Rakefile
130
- - ext/Rakefile
85
+ - ext/whatlang-rb/Cargo.lock
86
+ - ext/whatlang-rb/Cargo.toml
87
+ - ext/whatlang-rb/src/lib.rs
131
88
  - lib/whatlang.rb
132
- - rust-toolchain.toml
133
- - src/lib.rs
134
89
  - test/helper.rb
90
+ - test/test_package.rb
135
91
  - test/test_whatlang.rb
136
92
  - whatlang.gemspec
137
93
  homepage: https://gitlab.com/KitaitiMakoto/whatlang-rb
@@ -141,7 +97,6 @@ metadata:
141
97
  homepage_uri: https://gitlab.com/KitaitiMakoto/whatlang-rb
142
98
  source_code_uri: https://gitlab.com/KitaitiMakoto/whatlang-rb
143
99
  changelog_uri: https://gitlab.com/KitaitiMakoto/whatlang-rb/-/blob/master/CHANGELOG.md
144
- post_install_message:
145
100
  rdoc_options: []
146
101
  require_paths:
147
102
  - lib
@@ -156,8 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
111
  - !ruby/object:Gem::Version
157
112
  version: '0'
158
113
  requirements: []
159
- rubygems_version: 3.5.18
160
- signing_key:
114
+ rubygems_version: 3.7.2
161
115
  specification_version: 4
162
116
  summary: Fast natural language detection library.
163
117
  test_files: []
data/Cargo.lock DELETED
@@ -1,144 +0,0 @@
1
- # This file is automatically @generated by Cargo.
2
- # It is not intended for manual editing.
3
- version = 3
4
-
5
- [[package]]
6
- name = "ahash"
7
- version = "0.8.11"
8
- source = "registry+https://github.com/rust-lang/crates.io-index"
9
- checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
10
- dependencies = [
11
- "cfg-if",
12
- "once_cell",
13
- "version_check",
14
- "zerocopy",
15
- ]
16
-
17
- [[package]]
18
- name = "allocator-api2"
19
- version = "0.2.18"
20
- source = "registry+https://github.com/rust-lang/crates.io-index"
21
- checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
22
-
23
- [[package]]
24
- name = "cfg-if"
25
- version = "1.0.0"
26
- source = "registry+https://github.com/rust-lang/crates.io-index"
27
- checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
28
-
29
- [[package]]
30
- name = "hashbrown"
31
- version = "0.14.5"
32
- source = "registry+https://github.com/rust-lang/crates.io-index"
33
- checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
34
- dependencies = [
35
- "ahash",
36
- "allocator-api2",
37
- ]
38
-
39
- [[package]]
40
- name = "lazy_static"
41
- version = "1.5.0"
42
- source = "registry+https://github.com/rust-lang/crates.io-index"
43
- checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
44
-
45
- [[package]]
46
- name = "libc"
47
- version = "0.2.158"
48
- source = "registry+https://github.com/rust-lang/crates.io-index"
49
- checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
50
-
51
- [[package]]
52
- name = "once_cell"
53
- version = "1.19.0"
54
- source = "registry+https://github.com/rust-lang/crates.io-index"
55
- checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
56
-
57
- [[package]]
58
- name = "proc-macro2"
59
- version = "1.0.86"
60
- source = "registry+https://github.com/rust-lang/crates.io-index"
61
- checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
62
- dependencies = [
63
- "unicode-ident",
64
- ]
65
-
66
- [[package]]
67
- name = "quote"
68
- version = "1.0.37"
69
- source = "registry+https://github.com/rust-lang/crates.io-index"
70
- checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
71
- dependencies = [
72
- "proc-macro2",
73
- ]
74
-
75
- [[package]]
76
- name = "rutie"
77
- version = "0.9.0"
78
- source = "registry+https://github.com/rust-lang/crates.io-index"
79
- checksum = "e5e8e4f6480c30609e3480adfab87b8d4792525225a1caf98b371fbc9a7b698a"
80
- dependencies = [
81
- "lazy_static",
82
- "libc",
83
- ]
84
-
85
- [[package]]
86
- name = "syn"
87
- version = "2.0.76"
88
- source = "registry+https://github.com/rust-lang/crates.io-index"
89
- checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
90
- dependencies = [
91
- "proc-macro2",
92
- "quote",
93
- "unicode-ident",
94
- ]
95
-
96
- [[package]]
97
- name = "unicode-ident"
98
- version = "1.0.12"
99
- source = "registry+https://github.com/rust-lang/crates.io-index"
100
- checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
101
-
102
- [[package]]
103
- name = "version_check"
104
- version = "0.9.5"
105
- source = "registry+https://github.com/rust-lang/crates.io-index"
106
- checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
107
-
108
- [[package]]
109
- name = "whatlang"
110
- version = "0.16.4"
111
- source = "registry+https://github.com/rust-lang/crates.io-index"
112
- checksum = "471d1c1645d361eb782a1650b1786a8fb58dd625e681a04c09f5ff7c8764a7b0"
113
- dependencies = [
114
- "hashbrown",
115
- "once_cell",
116
- ]
117
-
118
- [[package]]
119
- name = "whatlang-rb"
120
- version = "0.1.6"
121
- dependencies = [
122
- "rutie",
123
- "whatlang",
124
- ]
125
-
126
- [[package]]
127
- name = "zerocopy"
128
- version = "0.7.35"
129
- source = "registry+https://github.com/rust-lang/crates.io-index"
130
- checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
131
- dependencies = [
132
- "zerocopy-derive",
133
- ]
134
-
135
- [[package]]
136
- name = "zerocopy-derive"
137
- version = "0.7.35"
138
- source = "registry+https://github.com/rust-lang/crates.io-index"
139
- checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
140
- dependencies = [
141
- "proc-macro2",
142
- "quote",
143
- "syn",
144
- ]
data/Cargo.toml DELETED
@@ -1,14 +0,0 @@
1
- [package]
2
- name = "whatlang-rb"
3
- version = "0.1.7"
4
- authors = ["Kitaiti Makoto <KitaitiMakoto@gmail.com>"]
5
- edition = "2018"
6
-
7
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
-
9
- [dependencies]
10
- rutie = { version = "0.9.0", features = ["no-link"] }
11
- whatlang = "0.16.4"
12
-
13
- [lib]
14
- crate-type = ["cdylib"]
data/ext/Rakefile DELETED
@@ -1,23 +0,0 @@
1
- task :default do
2
- cargo_exists = `cargo` rescue nil
3
- if cargo_exists
4
- sh "cargo build --release"
5
- else
6
- fail <<EOS
7
- Rust environment is required to install this gem.
8
-
9
- For Unix like system, run
10
-
11
- % curl https://sh.rustup.rs -sSf | sh
12
-
13
- For Windows, download installer from
14
-
15
- https://static.rust-lang.org/rustup/dist/i686-pc-windows-gnu/rustup-init.exe
16
-
17
- See the Rust official page below for details
18
-
19
- https://www.rust-lang.org/tools/install
20
-
21
- EOS
22
- end
23
- end
data/rust-toolchain.toml DELETED
@@ -1,3 +0,0 @@
1
- [toolchain]
2
- channel = "1.77.2"
3
- profile = "minimal"
data/src/lib.rs DELETED
@@ -1,95 +0,0 @@
1
- use rutie::{
2
- methods, module, AnyException, AnyObject, Array, Boolean, Float, Module, Object,
3
- RString, VM,
4
- };
5
- use whatlang::{detect, detect_lang, detect_script, Detector, Info, Lang};
6
-
7
- module!(Whatlang);
8
-
9
- methods!(
10
- Whatlang,
11
- _rtself,
12
-
13
- fn wl_detect_without_options(text: RString) -> AnyObject {
14
- detect(rstring(text).to_str()).map_or(no_info(), rinfo)
15
- }
16
-
17
- fn wl_detect_with_allowlist(text: RString, list: Array) -> AnyObject {
18
- let detector = Detector::with_allowlist(rarray_to_lang_list(list));
19
- detector.detect(rstring(text).to_str()).map_or(no_info(), rinfo)
20
- }
21
-
22
- fn wl_detect_with_denylist(text: RString, list: Array) -> AnyObject {
23
- let detector = Detector::with_denylist(rarray_to_lang_list(list));
24
- detector.detect(rstring(text).to_str()).map_or(no_info(), rinfo)
25
- }
26
-
27
- fn wl_detect_lang(text: RString) -> AnyObject {
28
- detect_lang(rstring(text).to_str()).map_or(no_info(), rlang)
29
- }
30
-
31
- fn wl_detect_script(text: RString) -> AnyObject {
32
- detect_script(rstring(text).to_str()).map_or(no_info(), |script| {
33
- RString::new_utf8(script.name()).into()
34
- })
35
- }
36
- );
37
-
38
- fn rstring(rstring: Result<RString, AnyException>) -> RString {
39
- rstring.map_err(VM::raise_ex).unwrap()
40
- }
41
-
42
- fn rarray_to_lang_list(rarray: Result<Array, AnyException>) -> Vec<Lang> {
43
- rarray
44
- .map_err(VM::raise_ex)
45
- .unwrap()
46
- .into_iter()
47
- .filter_map(|rcode| {
48
- let code = rcode
49
- .try_convert_to::<RString>()
50
- .map_err(VM::raise_ex)
51
- .unwrap();
52
- Lang::from_code(code.to_str())
53
- })
54
- .collect()
55
- }
56
-
57
- fn rinfo(info: Info) -> AnyObject {
58
- Module::from_existing("Whatlang")
59
- .get_nested_class("Info")
60
- .new_instance(&[
61
- rlang(info.lang()),
62
- RString::new_utf8(info.script().name()).into(),
63
- Boolean::new(info.is_reliable()).into(),
64
- Float::new(info.confidence()).into(),
65
- ])
66
- }
67
-
68
- fn rlang(lang: Lang) -> AnyObject {
69
- Module::from_existing("Whatlang")
70
- .get_nested_class("Lang")
71
- .new_instance(&[
72
- RString::new_utf8(lang.code()).into(),
73
- RString::new_utf8(lang.name()).into(),
74
- RString::new_utf8(lang.eng_name()).into(),
75
- ])
76
- }
77
-
78
- // Needed because Rutie cause segmentation fault when it returns NilClass.
79
- // Should be removed after https://github.com/danielpclark/rutie/pull/180 is solved.
80
- fn no_info() -> AnyObject {
81
- Module::from_existing("Whatlang")
82
- .const_get("NO_INFO")
83
- }
84
-
85
- #[allow(non_snake_case)]
86
- #[no_mangle]
87
- pub extern "C" fn Init_whatlang() {
88
- Module::new("Whatlang").define(|itself| {
89
- itself.def_self("detect_without_options", wl_detect_without_options);
90
- itself.def_self("detect_with_allowlist", wl_detect_with_allowlist);
91
- itself.def_self("detect_with_denylist", wl_detect_with_denylist);
92
- itself.def_self("detect_lang", wl_detect_lang);
93
- itself.def_self("detect_script", wl_detect_script);
94
- });
95
- }