1#![forbid(unsafe_code)]
2
3use std::{error::Error, fmt};
4
5#[cfg(feature = "bellman_ford")]
6pub mod bellman_ford;
7pub use bellman_ford::*;
8
9#[cfg(feature = "dijkstra")]
10pub mod dijkstra;
11pub use dijkstra::*;
12
13#[cfg(feature = "floyd_warshall")]
14pub mod floyd_warshall;
15pub use floyd_warshall::*;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum GraphError {
20 NegativeWeightCycle,
22
23 MissingStartNode,
25}
26
27impl Error for GraphError {}
28
29impl fmt::Display for GraphError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 write!(f, "{:?}", self)
41 }
42}
43
44pub trait GraphAlgorithm {
46 type Node;
48
49 type Weight;
51
52 fn run(&self, start: Option<Self::Node>) -> Result<Self::Weight, GraphError>;
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_graph_error() {
70 assert_eq!(
71 format!("{}", GraphError::NegativeWeightCycle),
72 "NegativeWeightCycle"
73 );
74
75 assert_eq!(
76 format!("{}", GraphError::MissingStartNode),
77 "MissingStartNode"
78 );
79 }
80}