use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphError {
NodeNotFound(usize),
EdgeCreationError { source: usize, target: usize },
EdgeNotFoundError { source: usize, target: usize },
GraphContainsCycle,
GraphNotFrozen,
GraphIsFrozen,
RootNodeAlreadyExists,
AlgorithmError(&'static str),
}
impl fmt::Display for GraphError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NodeNotFound(index) => {
write!(
f,
"Node with index {index} not found; it may be out of bounds or have been removed."
)
}
Self::EdgeCreationError { source, target } => {
write!(
f,
"Edge from {source} to {target} could not be created; a node may not exist or the edge already exists."
)
}
Self::EdgeNotFoundError { source, target } => {
write!(f, "Edge from {source} to {target} not found.")
}
Self::GraphContainsCycle => {
write!(f, "Operation failed because the graph contains a cycle.")
}
Self::GraphNotFrozen => {
write!(
f,
"Operation not possible because the graph is not frozen. Call graph.freeze() first."
)
}
Self::GraphIsFrozen => {
write!(
f,
"Operation not possible because the graph is frozen and cannot be mutated. Call graph.unfreeze() first."
)
}
Self::RootNodeAlreadyExists => {
write!(f, "Root node already exists")
}
Self::AlgorithmError(e) => {
write!(f, "AlgorithmError: {e}")
}
}
}
}
impl std::error::Error for GraphError {}