flowcore 1.0.0

Structures shared between runtime and clients
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#[cfg(feature = "debugger")]
use std::fmt;

use log::debug;
use serde_derive::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::model::datatype::DataType;
use crate::model::input::InputInitializer::{Always, Once};
use crate::model::io::IO;
#[cfg(feature = "debugger")]
use crate::model::name::HasName;
#[cfg(feature = "debugger")]
use crate::model::name::Name;

#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
/// An `Input` can be initialized in one of two ways with an `InputInitializer`
#[must_use]
#[allow(clippy::module_name_repetitions)]
pub enum InputInitializer {
    /// A `ConstantInputInitializer` initializes an input "constantly".
    /// i.e. after each time the associated function is run
    Always(Value),
    /// 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
    Once(Value),
}

impl InputInitializer {
    /// Get the Value of the initializer
    #[must_use]
    pub fn get_value(&self) -> &Value {
        match self {
            Once(value) | Always(value) => value,
        }
    }
}

#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, Debug)]
/// An [Input] to a `RuntimeFunction`
pub struct Input {
    #[cfg(feature = "debugger")]
    #[serde(default, skip_serializing_if = "String::is_empty")]
    name: Name,

    /// `array_order` defines how many levels of arrays of non-array values does the destination accept
    #[serde(default, skip_serializing_if = "is_default_array_order")]
    array_order: i32,

    /// `generic` defines if the input accepts generic object types
    #[serde(default, skip_serializing_if = "is_not_generic")]
    generic: bool,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    // An optional `InputInitializer` associated with this input
    initializer: Option<InputInitializer>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    // An optional `InputInitializer` propagated from a flow input's initializer
    flow_initializer: Option<InputInitializer>,

    // The queue of values received so far as an ordered vector of entries,
    // with first will be at the head and last at the tail
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    received: Vec<Value>,

    // How many values at the front of `received` are from internal connections
    #[serde(skip)]
    internal_count: usize,

    // Remaining elements from an array initializer for gradual delivery
    #[serde(skip)]
    pending_init_elements: Vec<Value>,
}

#[allow(clippy::trivially_copy_pass_by_ref)] // As this is imposed on us by serde
fn is_default_array_order(order: &i32) -> bool {
    *order == 0
}

#[allow(clippy::trivially_copy_pass_by_ref)] // As this is imposed on us by serde
fn is_not_generic(generic: &bool) -> bool {
    !*generic
}

impl TryFrom<&IO> for Input {
    type Error = String;

    fn try_from(io: &IO) -> Result<Self, Self::Error> {
        let data_type = io.datatypes().first().ok_or("Could not get datatype")?;

        Ok(Input::new(
            #[cfg(feature = "debugger")]
            io.name(),
            data_type.type_array_order(),
            data_type.is_generic(),
            io.get_initializer().clone(),
            io.get_flow_initializer().clone(),
        ))
    }
}

#[cfg(feature = "debugger")]
impl fmt::Display for Input {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if !self.name.is_empty() {
            write!(f, "({}) ", self.name)?;
        }
        if !self.received.is_empty() {
            write!(f, "{:?}", self.received)?;
        }
        Ok(())
    }
}

impl Input {
    /// Create a new `Input` with an optional `InputInitializer`
    #[cfg(feature = "debugger")]
    pub fn new<S>(
        name: S,
        array_order: i32,
        generic: bool,
        initializer: Option<InputInitializer>,
        flow_initializer: Option<InputInitializer>,
    ) -> Self
    where
        S: Into<Name>,
    {
        Input {
            name: name.into(),
            array_order,
            generic,
            initializer,
            flow_initializer,
            received: Vec::new(),
            internal_count: 0,
            pending_init_elements: Vec::new(),
        }
    }

    /// Create a new `Input` with an optional `InputInitializer`
    #[cfg(not(feature = "debugger"))]
    #[must_use]
    pub fn new(
        array_order: i32,
        generic: bool,
        initializer: Option<InputInitializer>,
        flow_initializer: Option<InputInitializer>,
    ) -> Self {
        Input {
            array_order,
            generic,
            initializer,
            flow_initializer,
            received: Vec::new(),
            internal_count: 0,
            pending_init_elements: Vec::new(),
        }
    }

    #[cfg(feature = "debugger")]
    /// Reset the an `Input` - clearing all received values (only used while debugging)
    pub fn reset(&mut self) {
        self.received.clear();
    }

    #[cfg(feature = "debugger")]
    /// Return the name of the input
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return a reference to the initializer
    #[must_use]
    pub fn initializer(&self) -> &Option<InputInitializer> {
        &self.initializer
    }

    /// Return a reference to the flow initializer
    #[must_use]
    pub fn flow_initializer(&self) -> &Option<InputInitializer> {
        &self.flow_initializer
    }

    /// Check if an initializer value should be delivered gradually (one element per idle cycle)
    /// Returns true if the value is an array with higher array order than this input accepts
    fn should_deliver_gradually(&self, value: &Value) -> bool {
        !self.generic && value.is_array() && DataType::value_array_order(value) > self.array_order()
    }

    /// Send one element from an array initializer, keeping the rest for later
    fn send_one_element(&mut self, value: &Value) -> bool {
        if let Value::Array(elements) = value {
            if let Some(first) = elements.first() {
                self.send(first.clone());
                self.pending_init_elements = elements.iter().skip(1).cloned().collect();
                return true;
            }
        }
        false
    }

    /// Initialize an input with the `InputInitializer` if it has one, either on the function
    /// directly or via a connection from a flow input
    /// When called at start-up    it will initialize      if it's a `Once` or `Always` initializer
    /// When called after start-up it will initialize only if it's a           `Always` initializer
    #[allow(clippy::match_same_arms)]
    pub fn init(&mut self, first_time: bool, flow_gone_idle: bool) -> bool {
        // Deliver pending elements from a previous gradual init
        if flow_gone_idle && !self.pending_init_elements.is_empty() {
            let next = self.pending_init_elements.remove(0);
            self.send(next);
            return true;
        }

        match (first_time, flow_gone_idle, &self.initializer) {
            (true, false, Some(Once(one_time))) => {
                if self.should_deliver_gradually(one_time) {
                    return self.send_one_element(&one_time.clone());
                }
                self.send(one_time.clone());
                return true;
            }
            (_, false, Some(Always(constant))) => {
                self.send(constant.clone());
                return true;
            }
            (_, _, _) => {}
        }

        // Flow initializers will only be applied if a function initializer has not already been
        // applied
        match (first_time, flow_gone_idle, &self.flow_initializer) {
            (true, _, Some(Once(one_time))) => {
                if self.should_deliver_gradually(one_time) {
                    return self.send_one_element(&one_time.clone());
                }
                self.send(one_time.clone());
                return true;
            }
            (true, _, Some(Always(constant))) => {
                self.send(constant.clone());
                return true;
            }
            (_, true, Some(Always(constant))) => {
                self.send(constant.clone());
                return true;
            }
            (_, _, _) => {}
        }

        false
    }

    // return the array_order of this input
    fn array_order(&self) -> i32 {
        self.array_order
    }

    /// Send a Value or array of Values to this input
    pub(crate) fn send(&mut self, value: Value) -> bool {
        if self.generic {
            self.received.push(value);
        } else {
            match (
                DataType::value_array_order(&value) - self.array_order(),
                &value,
            ) {
                (0, _) => self.received.push(value),
                (1, Value::Array(array)) => self.send_array_elements(array.clone()),
                (2, Value::Array(array_2)) => {
                    for array in array_2 {
                        if let Value::Array(sub_array) = array {
                            self.send_array_elements(sub_array.clone());
                        }
                    }
                }
                (-1, _) => {
                    debug!(
                        "\t\tSending value '{value}' wrapped in an Array: '{}'",
                        json!([value])
                    );
                    self.received.push(json!([value]));
                }
                (-2, _) => {
                    debug!(
                        "\t\tSending value '{value}' wrapped in an Array of Array: '{}'",
                        json!([[value]])
                    );
                    self.received.push(json!([[value]]));
                }
                _ => return false,
            }
        }
        true // a value was sent!
    }

    // Send an array of values to this `Input`, by sending them one element at a time
    fn send_array_elements(&mut self, array: Vec<Value>) {
        debug!("\t\tSending Array as a series of Values");
        for value in array {
            debug!("\t\t\tSending array element as Value; '{value}'");
            self.received.push(value);
        }
    }

    /// Send a value from an internal connection — inserted before external values
    pub(crate) fn send_internal(&mut self, value: Value) -> bool {
        if self.generic {
            self.received.insert(self.internal_count, value);
            self.internal_count += 1;
        } else {
            match (
                DataType::value_array_order(&value) - self.array_order(),
                &value,
            ) {
                (0, _) => {
                    self.received.insert(self.internal_count, value);
                    self.internal_count += 1;
                }
                (1, Value::Array(array)) => {
                    for v in array {
                        self.received.insert(self.internal_count, v.clone());
                        self.internal_count += 1;
                    }
                }
                (2, Value::Array(array_2)) => {
                    for array in array_2 {
                        if let Value::Array(sub_array) = array {
                            for v in sub_array {
                                self.received.insert(self.internal_count, v.clone());
                                self.internal_count += 1;
                            }
                        }
                    }
                }
                (-1, _) => {
                    self.received.insert(self.internal_count, json!([value]));
                    self.internal_count += 1;
                }
                (-2, _) => {
                    self.received.insert(self.internal_count, json!([[value]]));
                    self.internal_count += 1;
                }
                _ => return false,
            }
        }
        true
    }

    /// Clear all internal values from this input, preserving external values
    pub fn clear_internal(&mut self) {
        if self.internal_count > 0 {
            self.received.drain(..self.internal_count);
            self.internal_count = 0;
        }
    }

    /// Take the first element from the Input and return it. Could panic!
    #[must_use]
    pub fn take(&mut self) -> Option<Value> {
        if self.received.is_empty() {
            return None;
        }

        if self.internal_count > 0 {
            self.internal_count -= 1;
        }
        Some(self.received.remove(0))
    }

    /// Return the total number of values queued up in this input
    #[must_use]
    pub fn values_available(&self) -> usize {
        self.received.len()
    }

    /// Return true if there are no more values available from this input
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values_available() == 0
    }

    /// Return true if this input has pending internal values
    #[must_use]
    pub fn has_internal(&self) -> bool {
        self.internal_count > 0
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod test {
    use crate::model::input::InputInitializer::{Always, Once};
    use serde_json::json;
    use serde_json::Value;

    use super::Input;

    #[test]
    fn no_inputs_initially() {
        let input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        assert!(input.is_empty());
    }

    #[test]
    fn take_from_empty_fails() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        assert!(input.take().is_none());
    }

    #[test]
    fn accepts_null() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        input.send(Value::Null);
        assert!(!input.is_empty());
    }

    #[test]
    fn accepts_array() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        input.send_array_elements(vec![json!(5), json!(10), json!(15)]);
        assert!(!input.is_empty());
    }

    #[test]
    fn take_empties() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        input.send(json!(10));
        assert!(!input.is_empty());
        let _value = input
            .take()
            .expect("Should have got a value from the input");
        assert!(input.is_empty());
    }

    #[cfg(feature = "debugger")]
    #[test]
    fn reset_empties() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            None,
            None,
        );
        input.send(json!(10));
        assert!(!input.is_empty());
        input.reset();
        assert!(input.is_empty());
    }

    #[test]
    fn init_first_time_once() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            Some(Once(json!(1))),
            None,
        );

        input.init(true, false);

        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());
    }

    #[test]
    fn init_first_time_always() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            Some(Always(json!(1))),
            None,
        );

        input.init(true, false);

        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());
    }

    #[test]
    fn init_later_once() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            Some(Once(json!(1))),
            None,
        );

        input.init(true, false);

        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());

        input.init(false, false);

        assert!(input.is_empty());
    }

    #[test]
    fn init_later_always() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            Some(Always(json!(1))),
            None,
        );

        input.init(true, false);

        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());

        input.init(false, false);

        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());
    }

    #[test]
    fn gradual_init_array_on_number_input() {
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            0,
            false,
            Some(Once(json!([1, 2, 3]))),
            None,
        );

        // First init: sends only element 0
        input.init(true, false);
        assert_eq!(input.take(), Some(json!(1)));
        assert!(input.is_empty());

        // Idle cycle: sends element 1
        input.init(false, true);
        assert_eq!(input.take(), Some(json!(2)));
        assert!(input.is_empty());

        // Idle cycle: sends element 2
        input.init(false, true);
        assert_eq!(input.take(), Some(json!(3)));
        assert!(input.is_empty());

        // No more elements
        input.init(false, true);
        assert!(input.is_empty());
    }

    #[test]
    fn non_gradual_array_on_array_input() {
        // array/number input receiving array/number value — same order, send whole array
        let mut input = Input::new(
            #[cfg(feature = "debugger")]
            "",
            1,
            false,
            Some(Once(json!([1, 2, 3]))),
            None,
        );

        input.init(true, false);
        assert_eq!(input.take(), Some(json!([1, 2, 3])));
        assert!(input.is_empty());
    }
}