Skip to content

Commit 3843cc7

Browse files
committed
fixes and fmt
1 parent cd342bf commit 3843cc7

File tree

6 files changed

+66
-37
lines changed

6 files changed

+66
-37
lines changed

.run/Clippy.run.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Clippy" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
3+
<option name="channel" value="DEFAULT" />
4+
<option name="command" value="clippy" />
5+
<option name="allFeatures" value="false" />
6+
<option name="emulateTerminal" value="false" />
7+
<option name="backtrace" value="SHORT" />
8+
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
9+
<envs />
10+
<method v="2">
11+
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
12+
</method>
13+
</configuration>
14+
</component>

roll-lib/src/interpreter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Value {
122122
(Value::Float(i), Value::Float(j)) => Value::Float((i as f64).powf(j as f64)),
123123
(Value::Int(i), Value::Float(j)) => Value::Float((i as f64).powf(j as f64)),
124124
(Value::Float(i), Value::Int(j)) => Value::Float((i as f64).powf(j as f64)),
125-
(Value::Int(i), Value::Int(j)) if j < 0 => Value::Float((i as f64).powf(j as f64)),
125+
(Value::Int(i), Value::Int(j)) if j < 0 => Value::Float((i as f64).powf(j as f64)),
126126
(Value::Int(i), Value::Int(j)) => Value::Int((i as i64).pow(j as u32)),
127127
}
128128
}

roll-lib/src/lib.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod options;
44
mod parser;
55
mod roll;
66

7+
use crate::interpreter::Ast;
78
pub use crate::parser::*;
89
pub use crate::roll::*;
910
pub use rand_core;
10-
use crate::interpreter::Ast;
1111
use std::collections::HashMap;
1212

1313
pub fn inplace_interp(s: &str, advanced: bool) -> String {
@@ -20,7 +20,6 @@ pub fn inplace_interp(s: &str, advanced: bool) -> String {
2020
Ok(i) => i,
2121
Err(e) => {
2222
panic!("{}", e);
23-
2423
}
2524
};
2625

@@ -34,35 +33,54 @@ pub fn inplace_interp(s: &str, advanced: bool) -> String {
3433
map.insert(pos, roll);
3534
}
3635

37-
let res = replace_rolls(copy, &map, |roll| {
38-
format!("{:?}", roll.vals)
39-
});
36+
let res = replace_rolls(copy, &map, |roll| format!("{:?}", roll.vals));
4037
format!("{} = {} = {}", s, res, total)
4138
}
4239

43-
fn replace_rolls(ast: Ast, lookup: &HashMap<u64, Roll>, func: fn(&Roll) -> String ) -> Ast {
40+
fn replace_rolls(ast: Ast, lookup: &HashMap<u64, Roll>, func: fn(&Roll) -> String) -> Ast {
4441
return match ast {
45-
Ast::Add(l, r) => Ast::Add(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
46-
Ast::Sub(l, r) => Ast::Sub(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
47-
Ast::Mul(l, r) => Ast::Mul(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
48-
Ast::Div(l, r) => Ast::Div(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
49-
Ast::Mod(l, r) => Ast::Mod(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
50-
Ast::IDiv(l, r) => Ast::IDiv(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
51-
Ast::Power(l, r) => Ast::Power(Box::from(replace_rolls(*l, lookup, func)), Box::from(replace_rolls(*r, lookup, func))),
42+
Ast::Add(l, r) => Ast::Add(
43+
Box::from(replace_rolls(*l, lookup, func)),
44+
Box::from(replace_rolls(*r, lookup, func)),
45+
),
46+
Ast::Sub(l, r) => Ast::Sub(
47+
Box::from(replace_rolls(*l, lookup, func)),
48+
Box::from(replace_rolls(*r, lookup, func)),
49+
),
50+
Ast::Mul(l, r) => Ast::Mul(
51+
Box::from(replace_rolls(*l, lookup, func)),
52+
Box::from(replace_rolls(*r, lookup, func)),
53+
),
54+
Ast::Div(l, r) => Ast::Div(
55+
Box::from(replace_rolls(*l, lookup, func)),
56+
Box::from(replace_rolls(*r, lookup, func)),
57+
),
58+
Ast::Mod(l, r) => Ast::Mod(
59+
Box::from(replace_rolls(*l, lookup, func)),
60+
Box::from(replace_rolls(*r, lookup, func)),
61+
),
62+
Ast::IDiv(l, r) => Ast::IDiv(
63+
Box::from(replace_rolls(*l, lookup, func)),
64+
Box::from(replace_rolls(*r, lookup, func)),
65+
),
66+
Ast::Power(l, r) => Ast::Power(
67+
Box::from(replace_rolls(*l, lookup, func)),
68+
Box::from(replace_rolls(*r, lookup, func)),
69+
),
5270
Ast::Minus(l) => Ast::Minus(Box::from(replace_rolls(*l, lookup, func))),
5371
Ast::Dice(_, _, _, pos) => {
5472
let roll = lookup.get(&pos).unwrap();
5573
Ast::Const(func(roll))
56-
},
57-
x@Ast::Const(_) => x
58-
}
74+
}
75+
x @ Ast::Const(_) => x,
76+
};
5977
}
6078

6179
#[cfg(test)]
6280
mod test {
81+
use super::*;
6382
use crate::parser::Parser;
6483
use bnf::Grammar;
65-
use super::*;
6684

6785
const GRAMMAR: &str = include_str!("../../grammar.bnf");
6886

@@ -92,6 +110,6 @@ mod test {
92110

93111
#[test]
94112
fn test_inplace() {
95-
println!("{}", inplace_interp("4d8 + 2d8"));
113+
println!("{}", inplace_interp("4d8 + 2d8", false));
96114
}
97115
}

roll-lib/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl<'a> Parser<'a> {
147147
} else if self.accept_string("mod", options.clone()).is_ok() {
148148
'%'
149149
} else {
150+
//FIXME options assigned but never read
150151
options = options.add_str("mod");
151152
options = options.add_str("//");
152153
break;

roll/src/main.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use roll_lib::rand_core::OsRng;
2-
use roll_lib::{roll_direction, Parser, Roll, inplace_interp};
2+
use roll_lib::{inplace_interp, roll_direction, Parser, Roll};
33
use std::{env, process};
44

55
fn main() {
@@ -16,18 +16,16 @@ fn main() {
1616
let mut advanced = false;
1717
let mut short = false;
1818

19-
argv.retain(|x| {
20-
match x.as_str() {
21-
"-a" => {
22-
advanced = true;
23-
false
24-
}
25-
"-s" => {
26-
short = true;
27-
false
28-
}
29-
_ => true
19+
argv.retain(|x| match x.as_str() {
20+
"-a" => {
21+
advanced = true;
22+
false
3023
}
24+
"-s" => {
25+
short = true;
26+
false
27+
}
28+
_ => true,
3129
});
3230

3331
if short {
@@ -67,8 +65,6 @@ fn dice_roller(s: &str, advanced: bool) {
6765
}
6866
};
6967

70-
println!("ast : {}\n", ast);
71-
7268
let mut rolls = Vec::new();
7369
let total = match ast.interp(&mut rolls) {
7470
Ok(i) => i,

wasm/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use roll_lib::Parser;
2-
use serde::Serialize;
32
use serde::Deserialize;
3+
use serde::Serialize;
44
use wasm_bindgen::prelude::*;
55

66
// to build: wasm-pack build --target web
@@ -18,13 +18,13 @@ pub fn init_console() {
1818
console_error_panic_hook::set_once();
1919
}
2020

21-
#[derive(Debug,PartialEq,Serialize,Deserialize)]
21+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
2222
pub enum ObjType {
2323
JsRoll,
2424
JsRolls,
2525
}
2626

27-
#[derive(Serialize,Deserialize)]
27+
#[derive(Serialize, Deserialize)]
2828
pub struct JsRoll {
2929
#[serde(rename = "type")]
3030
pub obj_type: ObjType,
@@ -34,7 +34,7 @@ pub struct JsRoll {
3434
pub dpos: u64,
3535
}
3636

37-
#[derive(Serialize,Deserialize)]
37+
#[derive(Serialize, Deserialize)]
3838
pub struct JsRolls {
3939
#[serde(rename = "type")]
4040
pub obj_type: ObjType,

0 commit comments

Comments
 (0)