dice_nom_shim 0.2.0 → 0.4.1
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/Cargo.lock +36 -89
- data/ext/dice_nom_shim/Cargo.toml +2 -2
- data/ext/dice_nom_shim/src/lib.rs +74 -1
- data/lib/dice_nom_shim/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: ce27429c2bf107ec2c250cb90718d58519bdf927289c822e2e859b081d1036f2
|
4
|
+
data.tar.gz: 177c3c022305d2910b8ea5403f6e5d63641998f7b0d635bfa277165ebea6479f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aec79bbd2d0e09c74bd31763d0b93545038e5051a4219fb64a1160cac295ec4f2a4b906bc4aafa8794c4def5b817956ce0a650546e3cbcc85f6a5e0f3ebe27d
|
7
|
+
data.tar.gz: b05178e970118c26a84a6735db56d31815b67055101ce08587959f075a36d2aa71e77b50473595cee27ad818bc3d42799d705f42d8b027dcadfadc06f2b77a1a
|
data/Cargo.lock
CHANGED
@@ -61,19 +61,13 @@ dependencies = [
|
|
61
61
|
"windows-sys",
|
62
62
|
]
|
63
63
|
|
64
|
-
[[package]]
|
65
|
-
name = "arrayvec"
|
66
|
-
version = "0.5.2"
|
67
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
68
|
-
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
|
69
|
-
|
70
64
|
[[package]]
|
71
65
|
name = "bindgen"
|
72
66
|
version = "0.69.5"
|
73
67
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
74
68
|
checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
|
75
69
|
dependencies = [
|
76
|
-
"bitflags
|
70
|
+
"bitflags",
|
77
71
|
"cexpr",
|
78
72
|
"clang-sys",
|
79
73
|
"itertools",
|
@@ -87,30 +81,12 @@ dependencies = [
|
|
87
81
|
"syn",
|
88
82
|
]
|
89
83
|
|
90
|
-
[[package]]
|
91
|
-
name = "bitflags"
|
92
|
-
version = "1.3.2"
|
93
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
94
|
-
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
95
|
-
|
96
84
|
[[package]]
|
97
85
|
name = "bitflags"
|
98
86
|
version = "2.9.4"
|
99
87
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
100
88
|
checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394"
|
101
89
|
|
102
|
-
[[package]]
|
103
|
-
name = "bitvec"
|
104
|
-
version = "0.19.6"
|
105
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
106
|
-
checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33"
|
107
|
-
dependencies = [
|
108
|
-
"funty",
|
109
|
-
"radium",
|
110
|
-
"tap",
|
111
|
-
"wyz",
|
112
|
-
]
|
113
|
-
|
114
90
|
[[package]]
|
115
91
|
name = "cexpr"
|
116
92
|
version = "0.6.0"
|
@@ -185,12 +161,12 @@ checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
|
185
161
|
|
186
162
|
[[package]]
|
187
163
|
name = "dice-nom"
|
188
|
-
version = "0.
|
164
|
+
version = "0.4.0"
|
189
165
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
190
|
-
checksum = "
|
166
|
+
checksum = "495027bf2593bca1f1fb662059c69b43dcf0fd128031d67ac9e4ce51106744a4"
|
191
167
|
dependencies = [
|
192
168
|
"clap",
|
193
|
-
"nom
|
169
|
+
"nom 8.0.0",
|
194
170
|
"rand",
|
195
171
|
"serde",
|
196
172
|
"serde_json",
|
@@ -213,20 +189,15 @@ version = "1.15.0"
|
|
213
189
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
214
190
|
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
215
191
|
|
216
|
-
[[package]]
|
217
|
-
name = "funty"
|
218
|
-
version = "1.1.0"
|
219
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
220
|
-
checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
|
221
|
-
|
222
192
|
[[package]]
|
223
193
|
name = "getrandom"
|
224
|
-
version = "0.
|
194
|
+
version = "0.3.3"
|
225
195
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
226
|
-
checksum = "
|
196
|
+
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
227
197
|
dependencies = [
|
228
198
|
"cfg-if",
|
229
199
|
"libc",
|
200
|
+
"r-efi",
|
230
201
|
"wasi",
|
231
202
|
]
|
232
203
|
|
@@ -275,19 +246,6 @@ version = "1.3.0"
|
|
275
246
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
276
247
|
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
277
248
|
|
278
|
-
[[package]]
|
279
|
-
name = "lexical-core"
|
280
|
-
version = "0.7.6"
|
281
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
282
|
-
checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe"
|
283
|
-
dependencies = [
|
284
|
-
"arrayvec",
|
285
|
-
"bitflags 1.3.2",
|
286
|
-
"cfg-if",
|
287
|
-
"ryu",
|
288
|
-
"static_assertions",
|
289
|
-
]
|
290
|
-
|
291
249
|
[[package]]
|
292
250
|
name = "libc"
|
293
251
|
version = "0.2.175"
|
@@ -341,25 +299,21 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
|
341
299
|
|
342
300
|
[[package]]
|
343
301
|
name = "nom"
|
344
|
-
version = "
|
302
|
+
version = "7.1.3"
|
345
303
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
346
|
-
checksum = "
|
304
|
+
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
347
305
|
dependencies = [
|
348
|
-
"bitvec",
|
349
|
-
"funty",
|
350
|
-
"lexical-core",
|
351
306
|
"memchr",
|
352
|
-
"
|
307
|
+
"minimal-lexical",
|
353
308
|
]
|
354
309
|
|
355
310
|
[[package]]
|
356
311
|
name = "nom"
|
357
|
-
version = "
|
312
|
+
version = "8.0.0"
|
358
313
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
359
|
-
checksum = "
|
314
|
+
checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"
|
360
315
|
dependencies = [
|
361
316
|
"memchr",
|
362
|
-
"minimal-lexical",
|
363
317
|
]
|
364
318
|
|
365
319
|
[[package]]
|
@@ -396,27 +350,26 @@ dependencies = [
|
|
396
350
|
]
|
397
351
|
|
398
352
|
[[package]]
|
399
|
-
name = "
|
400
|
-
version = "
|
353
|
+
name = "r-efi"
|
354
|
+
version = "5.3.0"
|
401
355
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
402
|
-
checksum = "
|
356
|
+
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
403
357
|
|
404
358
|
[[package]]
|
405
359
|
name = "rand"
|
406
|
-
version = "0.
|
360
|
+
version = "0.9.2"
|
407
361
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
408
|
-
checksum = "
|
362
|
+
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
409
363
|
dependencies = [
|
410
|
-
"libc",
|
411
364
|
"rand_chacha",
|
412
365
|
"rand_core",
|
413
366
|
]
|
414
367
|
|
415
368
|
[[package]]
|
416
369
|
name = "rand_chacha"
|
417
|
-
version = "0.
|
370
|
+
version = "0.9.0"
|
418
371
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
419
|
-
checksum = "
|
372
|
+
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
420
373
|
dependencies = [
|
421
374
|
"ppv-lite86",
|
422
375
|
"rand_core",
|
@@ -424,9 +377,9 @@ dependencies = [
|
|
424
377
|
|
425
378
|
[[package]]
|
426
379
|
name = "rand_core"
|
427
|
-
version = "0.
|
380
|
+
version = "0.9.3"
|
428
381
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
429
|
-
checksum = "
|
382
|
+
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
430
383
|
dependencies = [
|
431
384
|
"getrandom",
|
432
385
|
]
|
@@ -552,12 +505,6 @@ version = "1.3.0"
|
|
552
505
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
553
506
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
554
507
|
|
555
|
-
[[package]]
|
556
|
-
name = "static_assertions"
|
557
|
-
version = "1.1.0"
|
558
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
559
|
-
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
560
|
-
|
561
508
|
[[package]]
|
562
509
|
name = "strsim"
|
563
510
|
version = "0.11.1"
|
@@ -575,12 +522,6 @@ dependencies = [
|
|
575
522
|
"unicode-ident",
|
576
523
|
]
|
577
524
|
|
578
|
-
[[package]]
|
579
|
-
name = "tap"
|
580
|
-
version = "1.0.1"
|
581
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
582
|
-
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
583
|
-
|
584
525
|
[[package]]
|
585
526
|
name = "unicode-ident"
|
586
527
|
version = "1.0.18"
|
@@ -594,16 +535,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
594
535
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
595
536
|
|
596
537
|
[[package]]
|
597
|
-
name = "
|
598
|
-
version = "0.
|
538
|
+
name = "wasi"
|
539
|
+
version = "0.14.5+wasi-0.2.4"
|
599
540
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
600
|
-
checksum = "
|
541
|
+
checksum = "a4494f6290a82f5fe584817a676a34b9d6763e8d9d18204009fb31dceca98fd4"
|
542
|
+
dependencies = [
|
543
|
+
"wasip2",
|
544
|
+
]
|
601
545
|
|
602
546
|
[[package]]
|
603
|
-
name = "
|
604
|
-
version = "0.
|
547
|
+
name = "wasip2"
|
548
|
+
version = "1.0.0+wasi-0.2.4"
|
605
549
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
606
|
-
checksum = "
|
550
|
+
checksum = "03fa2761397e5bd52002cd7e73110c71af2109aca4e521a9f40473fe685b0a24"
|
551
|
+
dependencies = [
|
552
|
+
"wit-bindgen",
|
553
|
+
]
|
607
554
|
|
608
555
|
[[package]]
|
609
556
|
name = "windows-link"
|
@@ -686,10 +633,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
686
633
|
checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
687
634
|
|
688
635
|
[[package]]
|
689
|
-
name = "
|
690
|
-
version = "0.
|
636
|
+
name = "wit-bindgen"
|
637
|
+
version = "0.45.1"
|
691
638
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
692
|
-
checksum = "
|
639
|
+
checksum = "5c573471f125075647d03df72e026074b7203790d41351cd6edc96f46bcccd36"
|
693
640
|
|
694
641
|
[[package]]
|
695
642
|
name = "zerocopy"
|
@@ -1,4 +1,6 @@
|
|
1
1
|
use magnus::{function, prelude::*, Error, Ruby};
|
2
|
+
use std::collections::BTreeMap;
|
3
|
+
use std::i32::MAX;
|
2
4
|
|
3
5
|
use dice_nom::parsers::generator_parser;
|
4
6
|
|
@@ -7,7 +9,7 @@ fn roll(roll: String) -> String {
|
|
7
9
|
Ok((_, g)) => g,
|
8
10
|
Err(e) => return format!("{{\"error\": \"{}\"}}", e),
|
9
11
|
};
|
10
|
-
let mut rng = rand::
|
12
|
+
let mut rng = rand::rng();
|
11
13
|
let mut results_array = Vec::new();
|
12
14
|
let n = 1;
|
13
15
|
for _ in 0..n {
|
@@ -20,9 +22,80 @@ fn roll(roll: String) -> String {
|
|
20
22
|
}
|
21
23
|
}
|
22
24
|
|
25
|
+
#[derive(serde::Serialize)]
|
26
|
+
struct Histo {
|
27
|
+
min: i32,
|
28
|
+
max: i32,
|
29
|
+
max_cnt: u32,
|
30
|
+
map: BTreeMap<i32, u32>,
|
31
|
+
}
|
32
|
+
|
33
|
+
impl Histo {
|
34
|
+
pub fn build(roll: String, count: u32) -> Histo {
|
35
|
+
let mut histo = Histo {
|
36
|
+
min: MAX,
|
37
|
+
max: 0,
|
38
|
+
max_cnt: 0,
|
39
|
+
map: BTreeMap::new(),
|
40
|
+
};
|
41
|
+
let g = match generator_parser(roll.as_ref()) {
|
42
|
+
Ok((_, g)) => g,
|
43
|
+
Err(_) => return histo,
|
44
|
+
};
|
45
|
+
let mut rng = rand::rng();
|
46
|
+
for _ in 0..count {
|
47
|
+
let v = g.generate(&mut rng).sum();
|
48
|
+
if v < histo.min {
|
49
|
+
histo.min = v;
|
50
|
+
}
|
51
|
+
if v > histo.max {
|
52
|
+
histo.max = v;
|
53
|
+
}
|
54
|
+
match histo.map.get(&v) {
|
55
|
+
Some(n) => {
|
56
|
+
let cnt = n + 1;
|
57
|
+
if cnt > histo.max_cnt {
|
58
|
+
histo.max_cnt = cnt;
|
59
|
+
}
|
60
|
+
histo.map.insert(v, cnt);
|
61
|
+
}
|
62
|
+
None => {
|
63
|
+
histo.map.insert(v, 1);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
histo
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
fn histo(roll: String) -> String {
|
72
|
+
let num = 2000.0;
|
73
|
+
let histo = Histo::build(roll, num as u32);
|
74
|
+
let mut chart_data = Vec::new();
|
75
|
+
let mut total = 100.0;
|
76
|
+
for k in histo.min..=histo.max {
|
77
|
+
let count = histo.map.get(&k).unwrap_or(&0);
|
78
|
+
let percentage = (*count as f64 / num) * 100.0;
|
79
|
+
chart_data.push(serde_json::json!({
|
80
|
+
"value": k,
|
81
|
+
"count": count,
|
82
|
+
"percentage": percentage,
|
83
|
+
"total": total,
|
84
|
+
}));
|
85
|
+
total = total - percentage;
|
86
|
+
}
|
87
|
+
|
88
|
+
let json = serde_json::to_string(&chart_data);
|
89
|
+
match json {
|
90
|
+
Ok(json) => format!("{}", json),
|
91
|
+
Err(err) => format!("{{\"error\": \"{}\"}}", err),
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
23
95
|
#[magnus::init]
|
24
96
|
fn init(ruby: &Ruby) -> Result<(), Error> {
|
25
97
|
let module = ruby.define_module("DiceNomShim")?;
|
26
98
|
module.define_singleton_method("roll", function!(roll, 1))?;
|
99
|
+
module.define_singleton_method("histo", function!(histo, 1))?;
|
27
100
|
Ok(())
|
28
101
|
}
|