firewheel_graph/graph/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Audio graph compilation algorithm adapted from:
// https://github.com/m-hilgendorf/audio-graph/tree/39c254073a73780335606f83e069afda230f0d3f

use std::error::Error;
use std::fmt;

use super::{
    compiler::{Edge, EdgeID, InPortIdx, OutPortIdx},
    NodeID,
};

/// An error occurred while attempting to add an edge to the graph.
#[derive(Debug, Clone)]
pub enum AddEdgeError {
    /// The given source node was not found in the graph.
    SrcNodeNotFound(NodeID),
    /// The given destination node was not found in the graph.
    DstNodeNotFound(NodeID),
    /// The given input port index is out of range.
    InPortOutOfRange {
        node: NodeID,
        port_idx: InPortIdx,
        num_in_ports: u32,
    },
    /// The given output port index is out of range.
    OutPortOutOfRange {
        node: NodeID,
        port_idx: OutPortIdx,
        num_out_ports: u32,
    },
    /// The edge already exists in the graph.
    EdgeAlreadyExists,
    /// The input port is already connected.
    InputPortAlreadyConnected(NodeID, InPortIdx),
    /// This edge would have created a cycle in the graph.
    CycleDetected,
}

impl Error for AddEdgeError {}

impl fmt::Display for AddEdgeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SrcNodeNotFound(node_id) => {
                write!(
                    f,
                    "Could not add edge: could not find source node with ID {:?}",
                    node_id
                )
            }
            Self::DstNodeNotFound(node_id) => {
                write!(
                    f,
                    "Could not add edge: could not find destination node with ID {:?}",
                    node_id
                )
            }
            Self::InPortOutOfRange {
                node,
                port_idx,
                num_in_ports,
            } => {
                write!(
                    f,
                    "Input port idx {:?} is out of range on node {:?} with {} input ports",
                    port_idx, node, num_in_ports,
                )
            }
            Self::OutPortOutOfRange {
                node,
                port_idx,
                num_out_ports,
            } => {
                write!(
                    f,
                    "Output port idx {:?} is out of range on node {:?} with {} output ports",
                    port_idx, node, num_out_ports,
                )
            }
            Self::EdgeAlreadyExists => {
                write!(f, "Could not add edge: edge already exists in the graph",)
            }
            Self::InputPortAlreadyConnected(node_id, port_id) => {
                write!(
                    f,
                    "Could not add edge: input port with ID {:?} on node with ID {:?} is already connected",
                    port_id,
                    node_id,
                )
            }
            Self::CycleDetected => {
                write!(f, "Could not add edge: cycle was detected")
            }
        }
    }
}

/// An error occurred while attempting to compile the audio graph
/// into a schedule.
#[derive(Debug)]
pub enum CompileGraphError {
    /// A cycle was detected in the graph.
    CycleDetected,
    /// The input data contained an edge referring to a non-existing node.
    NodeOnEdgeNotFound(Edge, NodeID),
    /// The input data contained multiple nodes with the same ID.
    NodeIDNotUnique(NodeID),
    /// The input data contained multiple edges with the same ID.
    EdgeIDNotUnique(EdgeID),
    /// The input port has more than one connection.
    ManyToOneError(NodeID, InPortIdx),
    /// An audio node failed to activate.
    NodeActivationFailed(NodeID, Box<dyn Error>),
    /// The message channel is full.
    MessageChannelFull,
}

impl Error for CompileGraphError {}

impl fmt::Display for CompileGraphError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CycleDetected => {
                write!(f, "Failed to compile audio graph: a cycle was detected")
            }
            Self::NodeOnEdgeNotFound(edge, node_id) => {
                write!(f, "Failed to compile audio graph: input data contains an edge {:?} referring to a non-existing node {:?}", edge, node_id)
            }
            Self::NodeIDNotUnique(node_id) => {
                write!(f, "Failed to compile audio graph: input data contains multiple nodes with the same ID {:?}", node_id)
            }
            Self::EdgeIDNotUnique(edge_id) => {
                write!(f, "Failed to compile audio graph: input data contains multiple edges with the same ID {:?}", edge_id)
            }
            Self::ManyToOneError(node_id, port_id) => {
                write!(f, "Failed to compile audio graph: input data contains multiple edges that go to the same input port with ID {:?} on node with id {:?}", port_id, node_id)
            }
            Self::NodeActivationFailed(node_id, e) => {
                write!(
                    f,
                    "Failed to compile audio graph: Node with ID {:?} failed to activate: {}",
                    node_id, e
                )
            }
            Self::MessageChannelFull => {
                write!(f, "Failed to compile audio graph: Message channel is full")
            }
        }
    }
}