Skip to main content

firewheel_graph/
error.rs

1use crate::graph::{Edge, EdgeID, PortIdx};
2use firewheel_core::{
3    channel_config::ChannelCount,
4    node::{NodeError, NodeID},
5};
6
7#[cfg(not(feature = "std"))]
8use bevy_platform::prelude::String;
9
10/// An error occurred while attempting to add an edge to the graph.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
12pub enum AddEdgeError {
13    /// The given source node was not found in the graph.
14    #[error("Could not add edge: could not find source node with ID {0:?}")]
15    SrcNodeNotFound(NodeID),
16    /// The given destination node was not found in the graph.
17    #[error("Could not add edge: could not find destination node with ID {0:?}")]
18    DstNodeNotFound(NodeID),
19    /// The given input port index is out of range.
20    #[error(
21        "Input port idx {port_idx:?} is out of range on node {node:?} with {num_in_ports:?} input ports"
22    )]
23    InPortOutOfRange {
24        node: NodeID,
25        port_idx: PortIdx,
26        num_in_ports: ChannelCount,
27    },
28    /// The given output port index is out of range.
29    #[error(
30        "Output port idx {port_idx:?} is out of range on node {node:?} with {num_out_ports:?} output ports"
31    )]
32    OutPortOutOfRange {
33        node: NodeID,
34        port_idx: PortIdx,
35        num_out_ports: ChannelCount,
36    },
37    /// This edge would have created a cycle in the graph.
38    #[error("Could not add edge: cycle was detected")]
39    CycleDetected,
40}
41
42/// An error occurred while attempting to compile the audio graph
43/// into a schedule.
44#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
45pub enum CompileGraphError {
46    /// A cycle was detected in the graph.
47    #[error("Failed to compile audio graph: a cycle was detected")]
48    CycleDetected,
49    /// The input data contained an edge referring to a non-existing node.
50    #[error(
51        "Failed to compile audio graph: input data contains an edge {0:?} referring to a non-existing node {1:?}"
52    )]
53    NodeOnEdgeNotFound(Edge, NodeID),
54    /// The input data contained multiple nodes with the same ID.
55    #[error(
56        "Failed to compile audio graph: input data contains multiple nodes with the same ID {0:?}"
57    )]
58    NodeIDNotUnique(NodeID),
59    /// The input data contained multiple edges with the same ID.
60    #[error(
61        "Failed to compile audio graph: input data contains multiple edges with the same ID {0:?}"
62    )]
63    EdgeIDNotUnique(EdgeID),
64    /// There was an error constructing the processor
65    #[error("Failed to construct a node's processor: {0}")]
66    ProcessorConstructionFailed(String),
67}
68
69/// An error occurred while attempting to activate a
70/// [`FirewheelContext`][crate::context::FirewheelContext].
71#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
72pub enum ActivateError {
73    /// The Firewheel context is already active. Either it has never been activated
74    /// or the [`FirewheelProcessor`][crate::processor::FirewheelProcessor] counterpart
75    /// has not been dropped yet.
76    ///
77    /// Note, in rare cases where the audio thread crashes without cleanly
78    /// dropping its contents, this may never succeed. Consider adding a
79    /// timeout to avoid deadlocking.
80    #[error("Failed to activate Firewheel context: The Firewheel context is already active")]
81    AlreadyActive,
82    /// The audio graph failed to compile.
83    #[error("Failed to activate Firewheel context: Audio graph failed to compile: {0}")]
84    GraphCompileError(#[from] CompileGraphError),
85}
86
87/// An error occurred while updating a [`FirewheelContext`][crate::context::FirewheelContext].
88#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
89pub enum UpdateError {
90    /// The context to processor message channel is full.
91    #[error("The Firewheel context to processor message channel is full")]
92    MsgChannelFull,
93    /// The audio graph failed to compile.
94    #[error("The audio graph failed to compile: {0}")]
95    GraphCompileError(#[from] CompileGraphError),
96}
97
98/// An error while removing a node in [`FirewheelContext`][crate::context::FirewheelContext].
99#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
100pub enum RemoveNodeError {
101    /// Removing the graph in node is not allowed.
102    #[error("Removing the graph in node is not allowed")]
103    CannotRemoveGraphInNode,
104    /// Removing the graph out node is not allowed.
105    #[error("Removing the graph out node is not allowed")]
106    CannotRemoveGraphOutNode,
107}
108
109/// An error occurred while deactivate a [`FirewheelContext`][crate::context::FirewheelContext].
110#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
111pub enum DeactivateError {
112    #[error("Timed out waiting for the Firewheel context to deactivate")]
113    TimedOut,
114}
115
116#[derive(Debug, thiserror::Error)]
117pub enum ModifyGraphError {
118    /// An error occured while adding a new node to the graph.
119    #[error("{0}")]
120    NodeError(NodeError),
121    #[error("{0}")]
122    /// An error occured while removing a node from the graph.
123    RemoveNodeError(#[from] RemoveNodeError),
124    /// An error occured while adding a new edge to the graph.
125    #[error("{0}")]
126    AddEdgeError(#[from] AddEdgeError),
127    /// An error occurred while updating a [`FirewheelContext`][crate::context::FirewheelContext].
128    #[error("{0}")]
129    UpdateError(#[from] UpdateError),
130    /// An error while trying to compile the graph, i.e. a
131    /// cycle was detected.
132    #[error("{0}")]
133    CompileGraphError(#[from] CompileGraphError),
134}
135
136impl From<NodeError> for ModifyGraphError {
137    fn from(e: NodeError) -> Self {
138        Self::NodeError(e)
139    }
140}