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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use nadi_plugin::nadi_internal_plugin;
#[nadi_internal_plugin]
mod logic {
use crate::prelude::*;
use nadi_plugin::env_func;
/// Simple if else condition
///
/// This is similar to using the `if-else` expression, the
/// difference being the condition is relaxed. For example, for
/// `if-else` the condition should be true or false, but for this
/// function, the attribute can be anything that can be cast as
/// true or false. (e.g. 1 => true, 0 => false)
///
/// ```task
/// env assert_eq(ifelse(true, 1, 2), 1)
/// env assert_eq(ifelse(false, 1, 2), 2)
/// env assert_eq(ifelse(100.0, 1, 2), 1)
/// env assert_eq(ifelse(true, 1, 2), if (true) {1} else {2})
/// ```
///
/// There is a special syntax on the task system to do if-else
/// conditions, which should be preferred over this function for
/// easier readability.
///
/// ```task
/// if (true) {
/// env.somevar = 12;
/// } else {
/// env.someothervar = 12;
/// }
/// env assert_eq(somevar, 12)
/// env assert_eq(someothervar?, false)
/// ```
#[env_func]
fn ifelse(
/// Attribute that can be cast to bool value
#[relaxed]
cond: bool,
/// Output if `cond` is true
iftrue: Attribute,
/// Output if `cond` is false
iffalse: Attribute,
) -> Result<Attribute, String> {
let v = if cond { iftrue } else { iffalse };
Ok(v)
}
/// Greater than check
///
/// ```task
/// env assert_eq(gt(1, 2), 1 > 2)
/// env assert_eq(gt(1.0, 20), 1.0 > 20)
/// ```
#[env_func]
fn gt(
/// first attribute
a: &Attribute,
/// second attribute
b: &Attribute,
) -> bool {
a > b
}
/// Less than check
///
/// ```task
/// env assert_eq(lt(1, 2), 1 < 2)
/// env assert_eq(lt(1.0, 20), 1.0 < 20)
/// ```
#[env_func]
fn lt(
/// first attribute
a: &Attribute,
/// second attribute
b: &Attribute,
) -> bool {
a < b
}
/// Equality than check
///
/// ```task
/// env assert_eq(eq(1, 2), 1 == 2)
/// env assert_eq(eq(2.0, 2.0), 2.0 == 2.0)
/// env assert_eq(eq(2.0, 2), 2.0 == 2)
/// ```
#[env_func]
fn eq(
/// first attribute
a: &Attribute,
/// second attribute
b: &Attribute,
) -> bool {
a == b
}
/// Boolean and
///
/// Similar to the operator `&` but the values are cast to boolean
///
/// ```task
/// env assert_eq(and(true, true), true)
/// env assert_eq(and(true, false), false)
/// env assert_eq(and(true, false), false & true)
/// ```
#[env_func]
fn and(
/// List of bools
#[args]
conds: Vec<bool>,
) -> bool {
conds.into_iter().all(|t| t)
}
/// boolean or
///
/// Similar to the operator `|` but the values are cast to boolean
///
/// ```task
/// env assert_eq(or(true, false), true)
/// env assert_eq(or(false, false), false)
/// env assert_eq(or(true, false), false | true)
/// ```
#[env_func]
fn or(
/// List of bools
#[args]
conds: Vec<bool>,
) -> bool {
conds.into_iter().any(|t| t)
}
/// boolean not
///
/// Similar to the operator `!` but the values are cast to boolean
/// ```task
/// env assert_eq(not(true), false)
/// env assert_eq(not(false), true)
/// env assert_eq(not(true), !true)
/// env assert_eq(not(false), !false)
/// ```
#[env_func]
fn not(
/// attribute that can be cast to bool
#[relaxed]
cond: bool,
) -> bool {
!cond
}
/// check if all of the bool are true
///
/// ```task
/// env assert_eq(all(true), true)
/// env assert_eq(all(false, true), false)
/// env assert_eq(all(true, true), true)
/// env assert_eq(all(false), false)
/// ```
#[env_func]
fn all(#[args] vars: Vec<bool>) -> bool {
for v in vars {
if !v {
return v;
}
}
true
}
/// check if any of the bool are true
///
/// ```task
/// env assert_eq(any(true), true)
/// env assert_eq(any(false, true), true)
/// env assert_eq(any(false, false), false)
/// env assert_eq(any(false), false)
/// ```
#[env_func]
fn any(#[args] vars: Vec<bool>) -> bool {
for v in vars {
if v {
return v;
}
}
false
}
}