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)]
/// An `Input` can be initialized in one of two ways with an `InputInitializer`
pub enum InputInitializer {
    /// A `ConstantInputInitializer` initializes an input "constantly".
    /// i.e. after each time the associated function is run
    Constant(ConstantInputInitializer),
    /// A `OneTimeInputInitializer` initializes an `Input` once - at start-up before any
    /// functions are run. Then it is not initialized again, unless a reset if done for debugging
    OneTime(OneTimeInputInitializer),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
/// A `ConstantInputInitializer` initializes an input "constantly".
pub struct OneTimeInputInitializer {
    /// `once` is the `Valuez that the `Input` will be initialized with once on start-up
    pub once: Value,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
/// A `OneTimeInputInitializer` initializes an `Input` once - at start-up
pub struct ConstantInputInitializer {
    /// `constant` will be the `Value` that the `Input` will be initized with before each
    /// time the `Function` is run.
    pub constant: Value,
}

#[derive(Deserialize, Serialize, Clone)]
/// An `Input` to a `Function`.
pub struct Input {
    #[serde(default = "default_depth", skip_serializing_if = "is_default_depth")]
    /// An `Input` can accept `depth` number of inputs before it is considered "full" and
    /// ready to be used by the associated `Function`
    depth: usize,
    #[serde(default = "default_initial_value", skip_serializing_if = "Option::is_none")]
    /// An optional `InputInitializer` associated with this input
    pub initializer: Option<InputInitializer>,
    #[serde(default = "default_is_array", skip_serializing_if = "is_not_array")]
    /// `is_array` is used for implicit Object to Array of Objects conversion
    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 {
    /// Create a new `Input` with an optional `InputInitializer`
    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")]
    /// reset the value of an `Input` - usually only used while debugging
    pub fn reset(&mut self) {
        self.received.clear();
    }

    /// Take 'depth' number of elements from the Input and leave the rest for the next time
    pub fn take(&mut self) -> Vec<Value> {
        self.received.drain(0..self.depth).collect()
    }

    /// Initialize an input with the InputInitializer if it has one.
    /// When called at start-up    it will initialize      if it's a OneTime or Constant initializer
    /// When called after start-up it will initialize only if it's a            Constant initializer
    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
        }
    }

    /// Add a value to this `Input`
    pub fn push(&mut self, value: Value) {
        self.received.push(value);
    }

    /// Return true if the `Input` is empty or false otherwise
    pub fn is_empty(&self) -> bool { self.received.is_empty() }

    /// Return true of the `Input` is "full" and it's values can be taken for executing the `Function`
    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());
    }
}