use serde_json::{Value, json};
use std::{io, path::Path};
pub(crate) fn split_fixture(mut study: Value, input: &Path) -> (Value, Value) {
let mut runtime = json!({"interface":"headless","storage":{"backend":"filesystem","root":input.with_extension("results")},"workspace":{"root":input.with_extension("work"),"max_bytes":8_589_934_592u64}});
if let Some(object) = study.as_object_mut() {
for key in [
"threads",
"compute",
"disk",
"persistence",
"active_phases",
"reuse_from",
] {
if let Some(value) = object.remove(key) {
runtime[key] = value;
}
}
}
if runtime["reuse_from"].is_string() {
runtime["reuse_from"] = json!({"kind":"local","execution":runtime["reuse_from"]});
}
if let Some(reps) = study.get_mut("replicates")
&& let Some(count) = reps.get("count").cloned()
{
if let Some(s) = reps.get("scheduling") {
runtime["replicate_scheduling"] = s.clone();
}
if let Some(s) = reps.get("failure_policy") {
runtime["replicate_failure_policy"] = s.clone();
}
*reps = count;
}
(study, runtime)
}
pub(crate) fn write_fixture(path: impl AsRef<Path>, bytes: impl AsRef<[u8]>) -> io::Result<()> {
let path = path.as_ref();
std::fs::write(path, bytes)?;
let value = crate::config::read_config_json(path);
let input = path.parent().unwrap().parent().unwrap();
let (study, runtime) = split_fixture(value.unwrap_or(Value::Null), input);
std::fs::write(
path.with_file_name("runtime.json"),
serde_json::to_vec_pretty(&runtime)?,
)?;
if !study.is_null() {
std::fs::write(path, serde_json::to_vec_pretty(&study)?)?;
}
Ok(())
}
pub(crate) fn parse_manifest(
path: &Path,
value: Value,
) -> Result<crate::config::ParsedManifest, crate::config::ConfigError> {
let (study, runtime) = split_fixture(value, Path::new("/tmp/workflow-parser-input"));
let runtime = crate::config::RuntimeSettings::parse(
Path::new("/tmp/runtime.json"),
runtime,
Path::new("/tmp/workflow-parser-input"),
)?;
crate::config::parse_manifest(path, study, &runtime)
}