use serde_json::Value;
use flowcore::errors::Result;
use flowcore::{RunAgain, RUN_AGAIN};
use flowmacro::flow_function;
#[flow_function]
fn inner_remove(value: &Value, array: &Value) -> Result<(Option<Value>, RunAgain)> {
let mut input_array = array.clone();
let output = if let Some(array) = input_array.as_array_mut() {
array.retain(|val| val != value);
Value::Array(array.clone())
} else {
input_array
};
Ok((Some(output), RUN_AGAIN))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod test {
use serde_json::{json, Value};
use super::inner_remove;
#[test]
fn remove_1() {
let array: Value = json!([1, 2]);
let value = json!(1);
let (result, _) = inner_remove(&value, &array).expect("_remove() failed");
assert_eq!(
result.expect("Could not get the Value from the output"),
json!([2])
);
}
#[test]
fn remove_repeated_entry() {
let array: Value = json!([1, 2, 2, 3, 4]);
let value = json!(2);
let (result, _) = inner_remove(&value, &array).expect("_remove() failed");
assert_eq!(
result.expect("Could not get the Value from the output"),
json!([1, 3, 4])
);
}
#[test]
fn not_remove_3() {
let array: Value = json!([1, 2]);
let value = json!(3);
let (result, _) = inner_remove(&value, &array).expect("_remove() failed");
assert_eq!(
result.expect("Could not get the Value from the output"),
json!([1, 2])
);
}
#[test]
fn try_to_remove_from_empty_array() {
let array: Value = json!([]);
let value = json!(3);
let (result, _) = inner_remove(&value, &array).expect("_remove() failed");
assert_eq!(
result.expect("Could not get the Value from the output"),
json!([])
);
}
#[test]
fn try_to_remove_non_existent_entry() {
let array: Value = json!([1, 2, 3, 5, 7, 8, 9]);
let value = json!(6);
let (result, _) = inner_remove(&value, &array).expect("_remove() failed");
assert_eq!(
result.expect("Could not get the Value from the output"),
json!([1, 2, 3, 5, 7, 8, 9])
);
}
}