use serde_json::{Map, Value};
use uuid::Uuid;
pub fn data_info(data_ids: &[Uuid]) -> Value {
if data_ids.is_empty() {
Value::String("None".into())
} else {
Value::Array(
data_ids
.iter()
.map(|id| Value::String(id.to_string()))
.collect(),
)
}
}
pub fn run_info_for_running(data_ids: &[Uuid]) -> Value {
let mut m = Map::with_capacity(1);
m.insert("data".into(), data_info(data_ids));
Value::Object(m)
}
pub fn run_info_for_errored(data_ids: &[Uuid], error: &str) -> Value {
let mut m = Map::with_capacity(2);
m.insert("data".into(), data_info(data_ids));
m.insert("error".into(), Value::String(error.to_string()));
Value::Object(m)
}
pub fn run_info_for_initiated() -> Value {
Value::Object(Map::new())
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn empty_emits_string_none() {
assert_eq!(data_info(&[]), json!("None"));
}
#[test]
fn single_id_emits_one_element_array() {
let id = Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
assert_eq!(
data_info(&[id]),
json!(["00000000-0000-0000-0000-000000000001"])
);
}
#[test]
fn three_ids_preserve_order() {
let id1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap();
let id2 = Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap();
let id3 = Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap();
assert_eq!(
data_info(&[id1, id2, id3]),
json!([
"11111111-1111-1111-1111-111111111111",
"22222222-2222-2222-2222-222222222222",
"33333333-3333-3333-3333-333333333333",
])
);
}
#[test]
fn running_run_info_with_empty_data_emits_none_literal() {
let v = run_info_for_running(&[]);
assert_eq!(v.to_string(), "{\"data\":\"None\"}");
}
#[test]
fn running_run_info_with_single_id_matches_python_shape() {
let id =
Uuid::parse_str("00000000-0000-0000-0000-000000000001").expect("valid uuid literal");
let v = run_info_for_running(&[id]);
assert_eq!(
v.to_string(),
"{\"data\":[\"00000000-0000-0000-0000-000000000001\"]}"
);
}
#[test]
fn errored_run_info_with_empty_data_includes_error() {
let v = run_info_for_errored(&[], "boom");
assert_eq!(v.to_string(), "{\"data\":\"None\",\"error\":\"boom\"}");
}
#[test]
fn errored_run_info_includes_data_and_error() {
let id =
Uuid::parse_str("00000000-0000-0000-0000-000000000002").expect("valid uuid literal");
let v = run_info_for_errored(&[id], "boom");
let obj = v.as_object().expect("object");
let data = obj.get("data").expect("data key");
assert_eq!(data.as_array().expect("array").len(), 1);
assert_eq!(obj.get("error").and_then(Value::as_str), Some("boom"));
let keys: Vec<&str> = obj.keys().map(|k| k.as_str()).collect();
assert_eq!(keys, vec!["data", "error"]);
}
#[test]
fn initiated_run_info_is_empty_object() {
let v = run_info_for_initiated();
assert_eq!(v.to_string(), "{}");
assert!(v.as_object().expect("object").is_empty());
}
}