1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use rhai::plugin::*;
#[export_module]
pub mod assert_functions {
use rhai::{Array, Dynamic, EvalAltResult, Position, FLOAT};
use crate::if_list_convert_to_vec_float_and_do;
/// Assert that a statement is true and throw an error if it is not.
/// ```typescript
/// assert(2==2);
/// ```
#[rhai_fn(name = "assert", return_raw)]
pub fn assert(comparison: bool) -> Result<bool, Box<EvalAltResult>> {
if comparison {
Ok(comparison)
} else {
Err(EvalAltResult::ErrorArithmetic(
"The comparison is not true".to_string(),
Position::NONE,
)
.into())
}
}
/// Assert that two arguments are equal and throw an error if they are not.
/// ```typescript
/// assert_eq(2, 2);
/// ```
#[rhai_fn(name = "assert_eq", return_raw)]
pub fn assert_eq(lhs: Dynamic, rhs: Dynamic) -> Result<bool, Box<EvalAltResult>> {
let lhs_type = lhs.type_name();
let rhs_type = rhs.type_name();
if lhs_type != rhs_type {
return Err(EvalAltResult::ErrorArithmetic(
format!(
"The left-hand side ({}) and right-hand side ({}) do not have the same type",
lhs_type, rhs_type
),
Position::NONE,
)
.into());
}
let comparison = format!("{:?}", lhs) == format!("{:?}", rhs);
if comparison {
Ok(comparison)
} else {
println!("LHS: {:?}", lhs);
println!("RHS: {:?}", rhs);
Err(EvalAltResult::ErrorArithmetic(
"The left-hand side and right-hand side are not equal".to_string(),
Position::NONE,
)
.into())
}
}
/// Assert that two arguments are unequal and throw an error if they are not.
/// ```typescript
/// assert_ne(2, 1);
/// ```
#[rhai_fn(name = "assert_ne", return_raw)]
pub fn assert_ne(lhs: Dynamic, rhs: Dynamic) -> Result<bool, Box<EvalAltResult>> {
let lhs_type = lhs.type_name();
let rhs_type = rhs.type_name();
if lhs_type != rhs_type {
return Err(EvalAltResult::ErrorArithmetic(
format!(
"The left-hand side ({}) and right-hand side ({}) do not have the same type",
lhs_type, rhs_type
),
Position::NONE,
)
.into());
}
let comparison = format!("{:?}", lhs) != format!("{:?}", rhs);
if comparison {
Ok(comparison)
} else {
println!("LHS: {:?}", lhs);
println!("RHS: {:?}", rhs);
Err(EvalAltResult::ErrorArithmetic(
"The left-hand side and right-hand side are equal".to_string(),
Position::NONE,
)
.into())
}
}
/// Assert that two floats are approximately equal (within `eps`) and return an error if they
/// are not.
/// ```typescript
/// assert_approx_eq(2.0, 2.000000000000000001, 1e-10);
/// ```
#[rhai_fn(name = "assert_approx_eq", return_raw)]
pub fn assert_approx_eq(
lhs: FLOAT,
rhs: FLOAT,
eps: FLOAT,
) -> Result<bool, Box<EvalAltResult>> {
if (lhs - rhs).abs() < eps {
Ok(true)
} else {
println!("LHS: {:?}", lhs);
println!("RHS: {:?}", rhs);
Err(EvalAltResult::ErrorArithmetic(
"The left-hand side and right-hand side are not equal".to_string(),
Position::NONE,
)
.into())
}
}
/// Assert that two floats are approximately equal and return an error if they
/// are not. Use the default tolerance of 1e-10 for the comparison.
/// ```typescript
/// assert_approx_eq(2.0, 2.000000000000000001);
/// ```
#[rhai_fn(name = "assert_approx_eq", return_raw)]
pub fn assert_approx_eq_with_default(
lhs: FLOAT,
rhs: FLOAT,
) -> Result<bool, Box<EvalAltResult>> {
assert_approx_eq(lhs, rhs, 1e-10)
}
/// Assert that two arrays are approximately equal (within `eps`) and return an error if they
/// are not.
/// ```typescript
/// assert_approx_eq([2.0, 2.0], [2.0, 2.000000000000000001], 1e-10);
/// ```
#[rhai_fn(name = "assert_approx_eq", return_raw)]
pub fn assert_approx_eq_list(
lhs: Array,
rhs: Array,
eps: FLOAT,
) -> Result<bool, Box<EvalAltResult>> {
if_list_convert_to_vec_float_and_do(&mut rhs.clone(), |rhs_as_vec_float| {
if_list_convert_to_vec_float_and_do(&mut lhs.clone(), |lhs_as_vec_float| {
let mut result = Ok(true);
for i in 0..rhs_as_vec_float.len() {
result = result.and(assert_approx_eq(
lhs_as_vec_float[i],
rhs_as_vec_float[i],
eps,
))
}
result
})
})
}
/// Assert that two arrays are approximately equal and return an error if they
/// are not. Use the default tolerance of 1e-10 for the comparison.
/// ```typescript
/// assert_approx_eq([2.0, 2.0], [2.0, 2.000000000000000001]);
/// ```
#[rhai_fn(name = "assert_approx_eq", return_raw)]
pub fn assert_approx_eq_list_with_default(
lhs: Array,
rhs: Array,
) -> Result<bool, Box<EvalAltResult>> {
assert_approx_eq_list(lhs, rhs, 1e-10)
}
}