1use anyhow::Context;
2
3pub fn parse_mv_contexts(val: serde_json::Value) -> anyhow::Result<Vec<Vec<Vec<f32>>>> {
10 match val {
11 serde_json::Value::Array(arr) if arr.is_empty() => {
12 anyhow::bail!("context must be a non-empty array")
13 }
14 serde_json::Value::Array(arr) => {
15 let first = arr.first().unwrap();
16 if !first.is_array() {
17 let ctx = serde_json::from_value::<Vec<f32>>(serde_json::Value::Array(arr))
19 .context("context must be a JSON array of numbers")?;
20 Ok(vec![vec![ctx]])
21 } else if first
22 .as_array()
23 .and_then(|a| a.first())
24 .map(|v| v.is_array())
25 .unwrap_or(false)
26 {
27 arr.into_iter()
29 .enumerate()
30 .map(|(i, batch_item)| {
31 serde_json::from_value::<Vec<Vec<f32>>>(batch_item)
32 .with_context(|| format!("context[{i}] must be an array of variate arrays"))
33 })
34 .collect()
35 } else {
36 arr.into_iter()
38 .enumerate()
39 .map(|(i, v)| {
40 let series = serde_json::from_value::<Vec<f32>>(v)
41 .with_context(|| format!("context[{i}] must be an array of numbers"))?;
42 Ok(vec![series])
43 })
44 .collect()
45 }
46 }
47 _ => anyhow::bail!("context must be a JSON array"),
48 }
49}