1use thiserror::Error;
4
5#[derive(Error, Debug, Clone, PartialEq)]
7pub enum NetworkError {
8 #[error("Node index {0} out of bounds")]
10 NodeIndexOutOfBounds(usize),
11
12 #[error("Invalid edge: source={0}, target={1}")]
14 InvalidEdge(usize, usize),
15
16 #[error("Matrix dimension mismatch: expected {expected}, got {got}")]
18 DimensionMismatch { expected: usize, got: usize },
19
20 #[error("Network is disconnected")]
22 DisconnectedNetwork,
23
24 #[error("Computation failed: {0}")]
26 ComputationError(String),
27
28 #[error("Invalid parameter {parameter}: {value} (must be {constraint})")]
30 InvalidParameter {
31 parameter: String,
32 value: String,
33 constraint: String,
34 },
35
36 #[error("Operation requires non-empty network")]
38 EmptyNetwork,
39
40 #[error("Algorithm failed to converge after {iterations} iterations")]
42 ConvergenceFailure { iterations: usize },
43
44 #[error("Insufficient data: {reason}")]
46 InsufficientData { reason: String },
47}
48
49pub type NetworkResult<T> = Result<T, NetworkError>;
51
52impl NetworkError {
53 pub fn computation<T: std::fmt::Display>(msg: T) -> Self {
55 NetworkError::ComputationError(msg.to_string())
56 }
57
58 pub fn invalid_param(param: &str, value: impl std::fmt::Display, constraint: &str) -> Self {
60 NetworkError::InvalidParameter {
61 parameter: param.to_string(),
62 value: value.to_string(),
63 constraint: constraint.to_string(),
64 }
65 }
66
67 pub fn insufficient_data(reason: &str) -> Self {
69 NetworkError::InsufficientData {
70 reason: reason.to_string(),
71 }
72 }
73}