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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#[cfg(feature = "debugger")]
use std::fmt;
use log::debug;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InputInitializer {
Constant(ConstantInputInitializer),
OneTime(OneTimeInputInitializer),
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct OneTimeInputInitializer {
pub once: Value,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ConstantInputInitializer {
pub constant: Value,
}
#[derive(Deserialize, Serialize, Clone)]
pub struct Input {
#[serde(default = "default_depth", skip_serializing_if = "is_default_depth")]
depth: usize,
#[serde(default = "default_initial_value", skip_serializing_if = "Option::is_none")]
pub initializer: Option<InputInitializer>,
#[serde(default = "default_is_array", skip_serializing_if = "is_not_array")]
pub is_array: bool,
#[serde(skip)]
received: Vec<Value>,
}
#[cfg(feature = "debugger")]
impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for input_value in &self.received {
write!(f, "{}, ", input_value)?;
}
write!(f, "")
}
}
fn is_not_array(is_array: &bool) -> bool {
!*is_array
}
fn default_is_array() -> bool { false }
fn is_default_depth(depth: &usize) -> bool {
*depth == default_depth()
}
fn default_depth() -> usize {
1
}
fn default_initial_value() -> Option<InputInitializer> {
None
}
impl Input {
pub fn new(depth: usize, initial_value: &Option<InputInitializer>, is_array: bool) -> Self {
Input {
depth,
initializer: initial_value.clone(),
received: Vec::with_capacity(depth),
is_array
}
}
#[cfg(feature = "debugger")]
pub fn reset(&mut self) {
self.received.clear();
}
pub fn take(&mut self) -> Vec<Value> {
self.received.drain(0..self.depth).collect()
}
pub fn init(&mut self, first_time: bool) -> bool {
let input_value = match (first_time, &self.initializer) {
(true, Some(InputInitializer::OneTime(one_time))) => Some(one_time.once.clone()),
(_, Some(InputInitializer::Constant(constant))) => Some(constant.constant.clone()),
(_, None) | (false, Some(InputInitializer::OneTime(_))) => None
};
match input_value {
Some(value) => {
debug!("\t\tInput initialized with '{:?}'", value);
self.push(value);
true
}
_ => false
}
}
pub fn push(&mut self, value: Value) {
self.received.push(value);
}
pub fn is_empty(&self) -> bool { self.received.is_empty() }
pub fn full(&self) -> bool {
self.received.len() >= self.depth
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use serde_json::Value;
use super::Input;
#[test]
fn default_depth_is_1() {
assert_eq!(super::default_depth(), 1);
}
#[test]
fn one_is_default_depth() {
assert!(super::is_default_depth(&1));
}
#[test]
fn default_initial_value_is_none() {
assert!(super::default_initial_value().is_none());
}
#[test]
fn default_is_not_array() {
assert_eq!(super::default_is_array(), false);
}
#[test]
fn is_not_array() {
assert!(super::is_not_array(&false));
}
#[test]
fn no_inputs_initially() {
let input = Input::new(1, &None, false);
assert!(input.is_empty());
}
#[test]
fn accepts_value() {
let mut input = Input::new(1, &None, false);
input.push(Value::Null);
assert!(!input.is_empty());
}
#[test]
fn gets_full() {
let mut input = Input::new(1, &None, false);
input.push(Value::Null);
assert!(input.full());
}
#[test]
fn take_empties() {
let mut input = Input::new(1, &None, false);
input.push(json!(10));
assert!(!input.is_empty());
input.take();
assert!(input.is_empty());
}
#[cfg(feature = "debugger")]
#[test]
fn reset_empties() {
let mut input = Input::new(1, &None, false);
input.push(json!(10));
assert!(!input.is_empty());
input.reset();
assert!(input.is_empty());
}
#[test]
fn depth_works() {
let mut input = Input::new(2, &None, false);
input.push(json!(5));
assert!(!input.full());
input.push(json!(10));
assert!(input.full());
assert_eq!(input.take().len(), 2);
}
#[test]
fn can_hold_more_than_depth() {
let mut input = Input::new(2, &None, false);
input.push(json!(5));
input.push(json!(10));
input.push(json!(15));
input.push(json!(20));
input.push(json!(25));
assert!(input.full());
}
#[test]
fn can_take_from_more_than_depth() {
let mut input = Input::new(2, &None, false);
input.push(json!(5));
input.push(json!(10));
input.push(json!(15));
input.push(json!(20));
input.push(json!(25));
assert!(input.full());
let mut next_set = input.take();
assert_eq!(vec!(json!(5), json!(10)), next_set);
assert!(input.full());
next_set = input.take();
assert_eq!(vec!(json!(15), json!(20)), next_set);
assert!(!input.full());
}
}