use fluxframe_core::EffectMetadata;
use serde::Deserialize;
use crate::state::EffectInventory;
pub fn parse_inventory(data: &serde_json::Value) -> Result<EffectInventory, String> {
let obj = data
.as_object()
.ok_or_else(|| "list_effects payload is not an object".to_string())?;
let mut sections = std::collections::BTreeMap::new();
for key in ["mask", "background", "foreground", "post"] {
let arr = obj
.get(key)
.and_then(serde_json::Value::as_array)
.ok_or_else(|| format!("list_effects missing section '{key}'"))?;
let mut effects = Vec::with_capacity(arr.len());
for item in arr {
let meta = WireMetadata::deserialize(item)
.map_err(|e| format!("section '{key}': metadata decode failed: {e}"))?;
effects.push(
meta.into_owned()
.map_err(|e| format!("section '{key}': {e}"))?,
);
}
sections.insert(key.to_string(), effects);
}
let build_features = obj
.get("build_features")
.and_then(serde_json::Value::as_array)
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default();
Ok(EffectInventory {
sections,
build_features,
})
}
pub fn parse_presets(data: &serde_json::Value) -> Result<Vec<String>, String> {
let arr = data
.as_array()
.ok_or_else(|| "list_presets payload is not an array".to_string())?;
Ok(arr
.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect())
}
#[derive(Debug, serde::Deserialize)]
struct WireMetadata {
name: String,
help: String,
params: Vec<WireParam>,
}
#[derive(Debug, serde::Deserialize)]
struct WireParam {
name: String,
kind: serde_json::Value,
help: String,
commit: serde_json::Value,
}
impl WireMetadata {
fn into_owned(self) -> Result<EffectMetadata, String> {
let mut params_owned = Vec::with_capacity(self.params.len());
for param in self.params {
params_owned.push(param.into_owned()?);
}
let params = params_owned.leak();
Ok(EffectMetadata {
name: leak_str(self.name),
help: leak_str(self.help),
params,
})
}
}
impl WireParam {
fn into_owned(self) -> Result<fluxframe_core::ParamDescriptor, String> {
let name = self.name;
let kind = WireKind::deserialize(&self.kind)
.map_err(|e| format!("param '{name}': kind decode failed: {e}"))?
.into_owned();
let commit = WireCommit::deserialize(&self.commit)
.map_err(|e| format!("param '{name}': commit decode failed: {e}"))?
.into_owned();
Ok(fluxframe_core::ParamDescriptor {
name: leak_str(name),
kind,
help: leak_str(self.help),
commit,
})
}
}
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum WireKind {
Float {
default: f32,
min: f32,
max: f32,
step: f32,
scale: WireScale,
},
Integer {
default: i64,
min: i64,
max: i64,
step: i64,
scale: WireScale,
},
Bool {
default: bool,
},
Color {
default: [u8; 3],
},
Path {
#[serde(default)]
default: Option<String>,
#[serde(default)]
extensions: Vec<String>,
required: bool,
},
Enum {
default: String,
variants: Vec<String>,
},
}
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
enum WireScale {
Linear,
Logarithmic,
}
impl WireScale {
fn into_owned(self) -> fluxframe_core::Scale {
match self {
Self::Linear => fluxframe_core::Scale::Linear,
Self::Logarithmic => fluxframe_core::Scale::Logarithmic,
}
}
}
impl WireKind {
fn into_owned(self) -> fluxframe_core::ParamKind {
match self {
Self::Float {
default,
min,
max,
step,
scale,
} => fluxframe_core::ParamKind::Float {
default,
min,
max,
step,
scale: scale.into_owned(),
},
Self::Integer {
default,
min,
max,
step,
scale,
} => fluxframe_core::ParamKind::Integer {
default,
min,
max,
step,
scale: scale.into_owned(),
},
Self::Bool { default } => fluxframe_core::ParamKind::Bool { default },
Self::Color { default } => fluxframe_core::ParamKind::Color { default },
Self::Path {
default,
extensions,
required,
} => fluxframe_core::ParamKind::Path {
default: default.map(leak_str),
extensions: leak_vec_of_str(extensions),
required,
},
Self::Enum { default, variants } => fluxframe_core::ParamKind::Enum {
default: leak_str(default),
variants: leak_vec_of_str(variants),
},
}
}
}
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case")]
enum WireCommit {
Live { debounce_ms: u32 },
OnCommit,
Instant,
}
impl WireCommit {
fn into_owned(self) -> fluxframe_core::CommitStrategy {
match self {
Self::Live { debounce_ms } => fluxframe_core::CommitStrategy::Live { debounce_ms },
Self::OnCommit => fluxframe_core::CommitStrategy::OnCommit,
Self::Instant => fluxframe_core::CommitStrategy::Instant,
}
}
}
fn leak_str(s: String) -> &'static str {
Box::leak(s.into_boxed_str())
}
fn leak_vec_of_str(v: Vec<String>) -> &'static [&'static str] {
let leaked: Vec<&'static str> = v.into_iter().map(leak_str).collect();
Box::leak(leaked.into_boxed_slice())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_inventory_round_trips_minimal_payload() {
let payload = serde_json::json!({
"mask": [{
"name": "threshold",
"help": "Binarise",
"params": [{
"name": "level",
"kind": {"type": "float", "default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01, "scale": "linear"},
"help": "cutoff",
"commit": {"mode": "live", "debounce_ms": 50}
}]
}],
"background": [],
"foreground": [],
"post": [],
"build_features": ["ml"]
});
let inv = parse_inventory(&payload).expect("inventory parses");
assert_eq!(inv.sections["mask"].len(), 1);
assert_eq!(inv.sections["mask"][0].name, "threshold");
assert_eq!(inv.build_features, vec!["ml".to_string()]);
}
#[test]
fn parse_inventory_rejects_missing_section() {
let payload = serde_json::json!({
"mask": [],
"background": [],
"foreground": []
});
let err = parse_inventory(&payload).expect_err("missing section is an error");
assert!(err.contains("'post'"), "got: {err}");
}
#[test]
fn parse_inventory_rejects_unknown_param_type() {
let payload = serde_json::json!({
"mask": [{
"name": "weird",
"help": "",
"params": [{
"name": "x",
"kind": {"type": "bogus"},
"help": "",
"commit": {"mode": "instant"}
}]
}],
"background": [],
"foreground": [],
"post": []
});
let err =
parse_inventory(&payload).expect_err("unknown kind type must surface as an error");
assert!(
err.contains("'x'"),
"error should mention the offending param name, got: {err}"
);
assert!(
err.contains("kind decode failed"),
"error should describe the failure, got: {err}"
);
}
#[test]
fn parse_presets_extracts_names() {
let payload = serde_json::json!(["alpha", "beta", "gamma"]);
let names = parse_presets(&payload).expect("parses");
assert_eq!(names, vec!["alpha", "beta", "gamma"]);
}
}