flowstdlib 1.0.0

The standard library of functions and flows for 'flow' programs
Documentation
use serde_json::json;
use serde_json::Value;

use flowcore::errors::Result;
use flowcore::{RunAgain, RUN_AGAIN};
use flowmacro::flow_function;

#[flow_function]
fn inner_zip(left: &Value, right: &Value) -> Result<(Option<Value>, RunAgain)> {
    let left = left.as_array().ok_or("Could not get left array")?;
    let right = right.as_array().ok_or("Could not get right array")?;
    let tuples = left.iter().zip(right.iter());
    let tuples_vec: Vec<(&Value, &Value)> = tuples.collect();
    Ok((Some(json!(tuples_vec)), RUN_AGAIN))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod test {
    use serde_json::{json, Value};

    use super::inner_zip;

    #[test]
    fn zip_empty() {
        let left = Value::Array(vec![]);
        let right = Value::Array(vec![]);

        let (result, _) = inner_zip(&left, &right).expect("_zip() failed");

        let zipped_array = result.expect("Could not get the value from the output");

        assert_eq!(zipped_array, Value::Array(vec!()));
    }

    #[test]
    fn zip_happy() {
        let left = json!(vec![1, 2]);
        let right = json!(vec![3, 4]);

        let (result, _) = inner_zip(&left, &right).expect("_zip() failed");

        let zipped_array = result.expect("Could not get the value from the output");

        assert_eq!(zipped_array, json!(vec![(1, 3), (2, 4)]));
    }

    #[test]
    fn zip_invalid_left() {
        let left = json!(1);
        let right = json!(vec![3, 4]);

        assert!(inner_zip(&left, &right).is_err());
    }

    #[test]
    fn zip_invalid_right() {
        let left = json!(vec![1, 2]);
        let right = json!(3);

        assert!(inner_zip(&left, &right).is_err());
    }

    #[test]
    fn zip_unequal() {
        let left = json!(vec![1, 2]);
        let right = json!(vec![3]);

        let (result, _) = inner_zip(&left, &right).expect("_zip() failed");

        let zipped_array = result.expect("Could not get the value from the output");

        assert_eq!(zipped_array, json!(vec![(1, 3)]));
    }
}