use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub enum Error {
CycleDetected {
message: String,
},
MissingDependency {
task: String,
dependency: String,
},
MissingDependencies {
missing: Vec<(String, String)>,
},
TopologicalSortFailed {
reason: String,
},
DuplicateNodeName {
name: String,
existing_kind: String,
new_kind: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CycleDetected { message } => {
write!(f, "Cycle detected in task graph: {message}")
}
Self::MissingDependency { task, dependency } => {
write!(f, "Task '{task}' depends on missing task '{dependency}'")
}
Self::MissingDependencies { missing } => {
let list = missing
.iter()
.map(|(task, dep)| format!("Task '{task}' depends on missing task '{dep}'"))
.collect::<Vec<_>>()
.join(", ");
write!(f, "Missing dependencies: {list}")
}
Self::TopologicalSortFailed { reason } => {
write!(f, "Failed to sort tasks topologically: {reason}")
}
Self::DuplicateNodeName {
name,
existing_kind,
new_kind,
} => {
write!(
f,
"Node name '{name}' is already used by a {existing_kind}; cannot add as {new_kind}"
)
}
}
}
}
impl std::error::Error for Error {}