use crate::graph::{NodeId, PortIdx};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum GraphError {
#[error("cycle detected in graph; unresolved nodes: {nodes:?}")]
CycleDetected {
nodes: Vec<NodeId>,
},
#[error("port {port} not found on node {node}")]
PortNotFound {
node: NodeId,
port: PortIdx,
},
#[error("port direction mismatch: {from:?} -> {to:?} (expected Output -> Input)")]
PortDirectionMismatch {
from: (NodeId, PortIdx),
to: (NodeId, PortIdx),
},
#[error("ports incompatible: {from:?} -> {to:?} (channels or sample format differ)")]
PortIncompatible {
from: (NodeId, PortIdx),
to: (NodeId, PortIdx),
},
#[error("graph is not compiled; call Graph::compile first")]
NotCompiled,
#[error("graph is already compiled")]
AlreadyCompiled,
#[error("node {0} not found")]
NodeNotFound(NodeId),
}
#[cfg(test)]
mod tests {
use super::GraphError;
#[test]
fn cycle_detected_display_lists_nodes() {
let e = GraphError::CycleDetected { nodes: vec![2, 3] };
assert_eq!(
e.to_string(),
"cycle detected in graph; unresolved nodes: [2, 3]"
);
}
#[test]
fn port_not_found_display() {
let e = GraphError::PortNotFound { node: 1, port: 2 };
assert_eq!(e.to_string(), "port 2 not found on node 1");
}
#[test]
fn port_direction_mismatch_display() {
let e = GraphError::PortDirectionMismatch {
from: (0, 1),
to: (2, 0),
};
assert!(e.to_string().contains("Output -> Input"));
}
#[test]
fn port_incompatible_display() {
let e = GraphError::PortIncompatible {
from: (0, 0),
to: (1, 0),
};
assert!(e.to_string().contains("incompatible"));
}
#[test]
fn not_compiled_display() {
let e = GraphError::NotCompiled;
assert_eq!(
e.to_string(),
"graph is not compiled; call Graph::compile first"
);
}
#[test]
fn already_compiled_display() {
let e = GraphError::AlreadyCompiled;
assert_eq!(e.to_string(), "graph is already compiled");
}
#[test]
fn node_not_found_display() {
let e = GraphError::NodeNotFound(5);
assert_eq!(e.to_string(), "node 5 not found");
}
#[test]
fn clone_and_eq_hold() {
let a = GraphError::NodeNotFound(3);
assert_eq!(a.clone(), a);
assert_ne!(GraphError::NodeNotFound(1), GraphError::NotCompiled);
}
}