async_dag/graph/
error.rs

1//! The error types.
2
3use super::NodeIndex;
4use crate::any::TypeInfo;
5use crate::tuple::TupleIndex;
6
7/// Errors that can happen during graph construction.
8#[derive(Debug)]
9#[allow(variant_size_differences)]
10pub enum Error {
11    /// The specified dependent node has started running its task and can't have its dependency modified.
12    HasStarted(NodeIndex),
13    /// The specified dependency index is greater than or equal to the dependent node's task's number of inputs.
14    OutOfRange(TupleIndex),
15    /// The dependent node's task has `input` type at specified index, but the depended node's task has a different `output` type.
16    TypeMismatch {
17        /// The input type for the child.
18        input: TypeInfo,
19        /// The output type from the parent.
20        output: TypeInfo,
21    },
22    /// Adding the specified dependency would have caused the graph to cycle.
23    WouldCycle,
24}
25
26impl std::fmt::Display for Error {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Self::HasStarted(index) => f.debug_tuple("Error::HasStarted").field(index).finish(),
30            Self::OutOfRange(len) => f.debug_tuple("Error::OutOfRange").field(len).finish(),
31            Self::TypeMismatch { input, output } => f
32                .debug_struct("Error::TypeMismatch")
33                .field("input", input)
34                .field("output", output)
35                .finish(),
36            Self::WouldCycle => f.debug_tuple("Error::WouldCycle").finish(),
37        }
38    }
39}
40
41impl std::error::Error for Error {}
42
43/// An [`Error`] and a [`TryTask`](crate::task::TryTask).
44#[derive(Debug)]
45pub struct ErrorWithTask<T> {
46    /// The error.
47    pub error: Error,
48    /// The task.
49    pub task: T,
50}
51
52impl<T: std::fmt::Debug> std::fmt::Display for ErrorWithTask<T> {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.debug_struct("ErrorWithTask")
55            .field("error", &self.error)
56            .field("task", &self.task)
57            .finish()
58    }
59}
60
61impl<T: std::fmt::Debug> std::error::Error for ErrorWithTask<T> {}