dice_nom_shim 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d593a0a7e8b3ca9c6b063845f34e6345198bded7e222125a1fea4d89724f430b
4
- data.tar.gz: a384c2665745f6e848a1ba9cdc7de0b5fce54c014755364576675f9ada397d71
3
+ metadata.gz: d6219410b5f2a939413f488c9ec51d7da0008781ad6cda38159a22074e5922f3
4
+ data.tar.gz: 8a32bbd3ffb35fac6fa21bac67b243521cbe4bef883e5353574322cfe2ba2aad
5
5
  SHA512:
6
- metadata.gz: 24b1dddbdb2ab8bcde211f5c6d35171017a0492c164c4ce0d68b5a90f3bf2436ad64c12222e7d82ce933b269b8e0c1bb306fc82200755b05efd1b44a9a95476e
7
- data.tar.gz: 0dd3dfa6c79cd1684d76dded4d4ba809562af5bbbe7e12fb6efe5d98c10e2f78be46742ffbff1497dc6c6a450ff9cafbf15b1e7e3f9441683950f5320b152a3e
6
+ metadata.gz: fe956b9f166b878c2a17995dbd4fe580b99cb302932e2886c8b8531a80047b13f1f7b31743251da2b6f710265d36fabbcb02b2a46a656216385c881028900888
7
+ data.tar.gz: ee2abeb34d39eb2cb55bc814c0f8c6f01f1bfc140b3d715cc539a88f1a7fa66c76ae7e998e159ebe1e52613a7efa9bf59e499380ede3fc9baca6ba81300c2704
data/Cargo.lock CHANGED
@@ -161,9 +161,9 @@ checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
161
161
 
162
162
  [[package]]
163
163
  name = "dice-nom"
164
- version = "0.4.0"
164
+ version = "0.5.0"
165
165
  source = "registry+https://github.com/rust-lang/crates.io-index"
166
- checksum = "495027bf2593bca1f1fb662059c69b43dcf0fd128031d67ac9e4ce51106744a4"
166
+ checksum = "2361b70380afbbc1fb4c2bfb8f85251f3147b61dfa3a160b025bd2459d096eca"
167
167
  dependencies = [
168
168
  "clap",
169
169
  "nom 8.0.0",
@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
11
11
 
12
12
  [dependencies]
13
13
  magnus = { version = "0.6.2" }
14
- dice-nom = "0.4.0"
14
+ dice-nom = "0.5"
15
15
  rand = "0.9"
16
16
  serde = { version = "1.0.219", features = ["derive"] }
17
17
  serde_json = "1.0.140"
@@ -31,7 +31,7 @@ struct Histo {
31
31
  }
32
32
 
33
33
  impl Histo {
34
- pub fn build(roll: String, count: u32) -> String {
34
+ pub fn build(roll: String, count: u32) -> Histo {
35
35
  let mut histo = Histo {
36
36
  min: MAX,
37
37
  max: 0,
@@ -40,12 +40,9 @@ impl Histo {
40
40
  };
41
41
  let g = match generator_parser(roll.as_ref()) {
42
42
  Ok((_, g)) => g,
43
- Err(e) => return format!("{{\"error\": \"{}\"}}", e),
43
+ Err(_) => return histo,
44
44
  };
45
45
  let mut rng = rand::rng();
46
- let results = g.generate(&mut rng);
47
- let result_json = serde_json::to_string(&results);
48
-
49
46
  for _ in 0..count {
50
47
  let v = g.generate(&mut rng).sum();
51
48
  if v < histo.min {
@@ -67,23 +64,32 @@ impl Histo {
67
64
  }
68
65
  }
69
66
  }
70
- let histo_json = serde_json::to_string(&histo);
71
-
72
- let result_error = format!("{{\"error\": \"could not parse result '{}'\"}}", roll);
73
- let histo_error = format!(
74
- "{{\"error\": \"could not parse result '{}' count={}\"}}",
75
- roll, count
76
- );
77
- format!(
78
- "[{}, {}]",
79
- result_json.unwrap_or(result_error),
80
- histo_json.unwrap_or(histo_error)
81
- )
67
+ histo
82
68
  }
83
69
  }
84
70
 
85
71
  fn histo(roll: String) -> String {
86
- Histo::build(roll, 2000)
72
+ let num = 5000.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
+ }
87
93
  }
88
94
 
89
95
  #[magnus::init]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiceNomShim
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dice_nom_shim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer