Skip to content

Commit d621c20

Browse files
authored
😝 Yes, this is another refactoring, I'm stupid (#7)
1 parent 6da65fb commit d621c20

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "minicode"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
edition = "2021"
55

66
[dependencies]

src/interpreter/condition.rs

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,44 @@ pub fn condition(
88
target_value: &ValueType,
99
storage: &HashMap<&String, ValueType>,
1010
) -> bool {
11-
let value = storage.get(key);
11+
let key_string = Line(key.to_string());
12+
let first_value = storage.get(key).unwrap_or(&key_string);
1213

13-
match value {
14-
Some(s) => match s {
15-
Int(first_integer) => match target_value {
16-
Int(second_integer) => {
17-
condition_result(first_integer, second_integer, true_or_false)
18-
}
19-
Line(second_str) => match storage.get(second_str) {
20-
Some(second_str_value) => match second_str_value {
21-
Int(second_str_value_int) => {
22-
condition_result(first_integer, second_str_value_int, true_or_false)
23-
}
24-
Line(_second_str_value_str) => {
25-
panic!("You can't compare a string with a number")
26-
}
27-
},
28-
None => panic!("You can't compare a string with a number"),
29-
},
30-
},
31-
Line(first_str) => match target_value {
32-
Int(_it) => panic!("You can't compare a string with a number"),
33-
Line(second_str) => match storage.get(second_str) {
34-
Some(second_str_value) => match second_str_value {
35-
Int(_second_str_value_int) => {
36-
panic!("You can't compare a string with a number")
37-
}
38-
Line(second_str_value_str) => {
39-
condition_result(first_str, second_str_value_str, true_or_false)
40-
}
41-
},
42-
None => condition_result(first_str, second_str, true_or_false),
43-
},
44-
},
45-
},
46-
None => match target_value {
47-
Int(_second_int) => panic!("You can't compare a string with a number"),
48-
Line(second_line) => match storage.get(second_line) {
49-
Some(s) => match s {
50-
Int(_second_value_int) => panic!("You can't compare a string with a number"),
51-
Line(second_value_str) => {
52-
condition_result(key, second_value_str, true_or_false)
53-
}
54-
},
55-
None => condition_result(key, second_line, true_or_false),
56-
},
14+
match target_value {
15+
Int(int) => condition_result(first_value, &Int(*int), true_or_false),
16+
Line(str) => match storage.get(str) {
17+
Some(some) => condition_result(first_value, some, true_or_false),
18+
None => condition_result(first_value, &Line(str.to_string()), true_or_false),
5719
},
5820
}
5921
}
6022

61-
fn condition_result<T: std::cmp::PartialEq>(first: T, second: T, true_or_false: &bool) -> bool {
23+
fn condition_result(first: &ValueType, second: &ValueType, true_or_false: &bool) -> bool {
24+
if type_are_different(first, second) {
25+
panic!("You cannot compare values of different types!");
26+
}
27+
6228
if *true_or_false {
6329
return first == second;
6430
} else {
6531
return first != second;
6632
}
6733
}
6834

35+
fn type_are_different(v1: &ValueType, v2: &ValueType) -> bool {
36+
match (v1, v2) {
37+
(ValueType::Int(_), ValueType::Line(_)) => true,
38+
(ValueType::Line(_), ValueType::Int(_)) => true,
39+
_ => false,
40+
}
41+
}
42+
43+
#[test]
44+
fn test_condition_result() {
45+
let c = condition_result(&Int(32), &Int(1), &true);
46+
assert_eq!(c, false);
47+
}
48+
6949
#[cfg(test)]
7050
mod tests {
7151
use super::*;
@@ -108,6 +88,21 @@ mod tests {
10888
assert_eq!(result, true);
10989
}
11090

91+
#[test]
92+
#[should_panic]
93+
fn test_condition_int_and_line() {
94+
let mut map: HashMap<&String, ValueType> = HashMap::new();
95+
let binding = String::from("test_key");
96+
map.insert(&binding, Int(10));
97+
let result = condition(
98+
&String::from("test_key"),
99+
&true,
100+
&Line("5".to_string()),
101+
&map,
102+
);
103+
assert_eq!(result, false);
104+
}
105+
111106
#[test]
112107
fn test_condition_line_one() {
113108
let mut map: HashMap<&String, ValueType> = HashMap::new();

0 commit comments

Comments
 (0)