@@ -8,64 +8,44 @@ pub fn condition(
8
8
target_value : & ValueType ,
9
9
storage : & HashMap < & String , ValueType > ,
10
10
) -> 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) ;
12
13
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) ,
57
19
} ,
58
20
}
59
21
}
60
22
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
+
62
28
if * true_or_false {
63
29
return first == second;
64
30
} else {
65
31
return first != second;
66
32
}
67
33
}
68
34
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
+
69
49
#[ cfg( test) ]
70
50
mod tests {
71
51
use super :: * ;
@@ -108,6 +88,21 @@ mod tests {
108
88
assert_eq ! ( result, true ) ;
109
89
}
110
90
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
+
111
106
#[ test]
112
107
fn test_condition_line_one ( ) {
113
108
let mut map: HashMap < & String , ValueType > = HashMap :: new ( ) ;
0 commit comments