flow_impl/
lib.rs

1#![deny(missing_docs)]
2//! `flow_impl` is a trait that flow function implementations must implement in order
3//! to be executed as part of a flow.
4//!
5use std::panic::{RefUnwindSafe, UnwindSafe};
6
7use serde_json::Value;
8
9/// Implementations should return a value of type `RunAgain` to indicate if it should be
10/// executed more times in the future.
11pub type RunAgain = bool;
12/// Use `RUN_AGAIN` to indicate that a function can be executed more times
13pub const RUN_AGAIN: RunAgain = true;
14/// Use `DONT_RUN_AGAIN` to indicate that a function should not be executed more times
15pub const DONT_RUN_AGAIN: RunAgain = false;
16
17/// An implementation runs with an array of inputs and returns a value (or null) and a
18/// bool indicating if it should be ran again.
19///
20/// Any 'implementation' of a function must implement this trait
21///
22/// # Examples
23///
24/// Here is an example implementation of this trait:
25///
26/// ```
27/// use flow_impl::{Implementation, RUN_AGAIN, RunAgain};
28/// use serde_json::Value;
29/// use serde_json::json;
30///
31/// #[derive(Debug)]
32/// pub struct Compare;
33///
34/// /*
35///     A compare operator that takes two numbers and outputs the comparisons between them
36/// */
37/// impl Implementation for Compare {
38///     fn run(&self, mut inputs: &[Value]) -> (Option<Value>, RunAgain) {
39///         let left = inputs[0].as_i64().unwrap();
40///         let right = inputs[1].as_i64().unwrap();
41///
42///         let output = json!({
43///                     "equal" : left == right,
44///                     "lt" : left < right,
45///                     "gt" : left > right,
46///                     "lte" : left <= right,
47///                     "gte" : left >= right
48///                 });
49///
50///         (None, RUN_AGAIN)
51///     }
52/// }
53/// ```
54pub trait Implementation : RefUnwindSafe + UnwindSafe + Sync + Send {
55    /// The `run` method is used to execute the implementation
56    fn run(&self, inputs: &[Value]) -> (Option<Value>, RunAgain);
57}