#![forbid(unsafe_code)]
use std::{error::Error, fmt};
#[cfg(feature = "bellman_ford")]
pub mod bellman_ford;
pub use bellman_ford::*;
#[cfg(feature = "dijkstra")]
pub mod dijkstra;
pub use dijkstra::*;
#[cfg(feature = "floyd_warshall")]
pub mod floyd_warshall;
pub use floyd_warshall::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GraphError {
NegativeWeightCycle,
MissingStartNode,
}
impl Error for GraphError {}
impl fmt::Display for GraphError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub trait GraphAlgorithm {
type Node;
type Weight;
fn run(&self, start: Option<Self::Node>) -> Result<Self::Weight, GraphError>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph_error() {
assert_eq!(
format!("{}", GraphError::NegativeWeightCycle),
"NegativeWeightCycle"
);
assert_eq!(
format!("{}", GraphError::MissingStartNode),
"MissingStartNode"
);
}
}