Skip to main content

cuenv_task_graph/
error.rs

1//! Error types for task graph operations.
2
3use std::fmt;
4
5/// Result type for task graph operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during task graph operations.
9#[derive(Debug, Clone)]
10pub enum Error {
11    /// A dependency cycle was detected in the graph.
12    CycleDetected {
13        /// Human-readable description of the cycle.
14        message: String,
15    },
16
17    /// A task depends on another task that doesn't exist.
18    MissingDependency {
19        /// The task that has the missing dependency.
20        task: String,
21        /// The name of the missing dependency.
22        dependency: String,
23    },
24
25    /// Multiple missing dependencies were found.
26    MissingDependencies {
27        /// List of (task, missing_dependency) pairs.
28        missing: Vec<(String, String)>,
29    },
30
31    /// Failed to perform topological sort.
32    TopologicalSortFailed {
33        /// Reason for the failure.
34        reason: String,
35    },
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::CycleDetected { message } => {
42                write!(f, "Cycle detected in task graph: {message}")
43            }
44            Self::MissingDependency { task, dependency } => {
45                write!(f, "Task '{task}' depends on missing task '{dependency}'")
46            }
47            Self::MissingDependencies { missing } => {
48                let list = missing
49                    .iter()
50                    .map(|(task, dep)| format!("Task '{task}' depends on missing task '{dep}'"))
51                    .collect::<Vec<_>>()
52                    .join(", ");
53                write!(f, "Missing dependencies: {list}")
54            }
55            Self::TopologicalSortFailed { reason } => {
56                write!(f, "Failed to sort tasks topologically: {reason}")
57            }
58        }
59    }
60}
61
62impl std::error::Error for Error {}