Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 82 additions & 22 deletions crates/api/src/yaml/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,35 +220,46 @@ fn compile_dag(
.map(|(name, def)| {
let mut params = def.params;

if def.kind == "audio::mixer" && mode != EngineMode::Dynamic {
if let Some(count) = incoming_counts.get(&name) {
if *count > 1 {
if let Some(serde_json::Value::Object(ref mut map)) = params {
let should_inject = matches!(
map.get("num_inputs"),
Some(serde_json::Value::Null) | None
);
if should_inject {
if def.kind == "audio::mixer" {
if let Some(ref p) = params {
if !p.is_object() {
return Err(format!(
"audio::mixer node '{name}' params must be an object, got {}",
value_type_name(p),
));
}
}

if mode != EngineMode::Dynamic {
if let Some(count) = incoming_counts.get(&name) {
if *count > 1 {
if let Some(serde_json::Value::Object(ref mut map)) = params {
let should_inject = matches!(
map.get("num_inputs"),
Some(serde_json::Value::Null) | None
);
if should_inject {
map.insert(
"num_inputs".to_string(),
serde_json::Value::Number((*count).into()),
);
}
} else if params.is_none() {
let mut map = serde_json::Map::new();
map.insert(
"num_inputs".to_string(),
serde_json::Value::Number((*count).into()),
);
params = Some(serde_json::Value::Object(map));
}
} else if params.is_none() {
let mut map = serde_json::Map::new();
map.insert(
"num_inputs".to_string(),
serde_json::Value::Number((*count).into()),
);
params = Some(serde_json::Value::Object(map));
}
}
}
}

(name, Node { kind: def.kind, params, state: None })
Ok((name, Node { kind: def.kind, params, state: None }))
})
.collect();
.collect::<Result<IndexMap<_, _>, _>>()?;
Comment on lines +260 to +262
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Collecting into a Result preserves successful pipeline construction semantics

Changing the node-map construction from collecting bare (name, Node) pairs to collecting Result<(name, Node), String> only introduces early return on the new validation failure. The connection list and incoming counts are fully built before this point, and user_nodes.into_iter() is still consumed exactly once, so successful compilations keep the same node ordering and connection data as before.

Open in Devin Review (Staging)

Was this helpful? React with 👍 or 👎 to provide feedback.

Debug

Playground


Ok(Pipeline {
name,
Expand All @@ -262,6 +273,17 @@ fn compile_dag(
})
}

fn value_type_name(v: &serde_json::Value) -> &'static str {
match v {
serde_json::Value::Null => "null",
serde_json::Value::Bool(_) => "bool",
serde_json::Value::Number(_) => "number",
serde_json::Value::String(_) => "string",
serde_json::Value::Array(_) => "array",
serde_json::Value::Object(_) => "object",
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -335,7 +357,7 @@ mod tests {
}

#[test]
fn compile_dag_oneshot_audio_mixer_skips_inject_when_params_is_non_object() {
fn compile_dag_audio_mixer_rejects_non_object_params() {
let mixer = UserNode {
kind: "audio::mixer".to_string(),
params: Some(serde_json::Value::String("scalar".into())),
Expand All @@ -346,8 +368,46 @@ mod tests {
("b", user_node("core::source", Needs::None)),
("mixer", mixer),
]);
let compiled = compile(dag_pipeline(nodes, EngineMode::OneShot)).expect("compile");
let mixer = compiled.nodes.get("mixer").expect("mixer present");
assert_eq!(mixer.params.as_ref().and_then(serde_json::Value::as_str), Some("scalar"));
let err = compile(dag_pipeline(nodes, EngineMode::OneShot))
.expect_err("non-object params should be rejected");
assert!(
err.contains("audio::mixer node 'mixer' params must be an object"),
"error should mention the node name and requirement: {err}",
);
assert!(err.contains("string"), "error should mention the actual type: {err}");
}

#[test]
fn compile_dag_dynamic_audio_mixer_also_rejects_non_object_params() {
let mixer = UserNode {
kind: "audio::mixer".to_string(),
params: Some(serde_json::Value::Number(42.into())),
needs: mixer_needs(),
};
let nodes = dag_nodes(vec![
("a", user_node("core::source", Needs::None)),
("b", user_node("core::source", Needs::None)),
("mixer", mixer),
]);
let err = compile(dag_pipeline(nodes, EngineMode::Dynamic))
.expect_err("non-object params should be rejected even in dynamic mode");
assert!(err.contains("number"), "error should mention the actual type: {err}");
}

#[test]
fn compile_dag_audio_mixer_rejects_null_params() {
let mixer = UserNode {
kind: "audio::mixer".to_string(),
params: Some(serde_json::Value::Null),
needs: mixer_needs(),
};
let nodes = dag_nodes(vec![
("a", user_node("core::source", Needs::None)),
("b", user_node("core::source", Needs::None)),
("mixer", mixer),
]);
let err = compile(dag_pipeline(nodes, EngineMode::OneShot))
.expect_err("null params should be rejected");
assert!(err.contains("got null"), "error should mention null type: {err}");
}
}
Loading