flowstdlib/data/count/
count.rs

1use flowcore::errors::*;
2use flowmacro::flow_function;
3use serde_json::json;
4use serde_json::Value;
5use flowcore::RunAgain;
6use flowcore::RUN_AGAIN;
7
8#[flow_function]
9fn _count(inputs: &[Value]) -> Result<(Option<Value>, RunAgain)> {
10    let mut count = inputs[1].as_i64().ok_or("Could not get count")?;
11    count += 1;
12
13    Ok((Some(json!(count)), RUN_AGAIN))
14}
15
16#[cfg(test)]
17mod test {
18    use serde_json::json;
19
20    use super::_count;
21
22    #[test]
23    fn count_returns_value() {
24        let data = json!(42);
25        let previous_count = json!(0);
26        let inputs = vec![data, previous_count];
27
28        let (result, _) = _count(&inputs).expect("_count() failed");
29        let output = result.expect("Could not get the Value from the output");
30
31        assert_eq!(output.pointer("")
32                       .expect("Could not get the /count from the output"), &json!(1));
33    }
34}