amari_network/
error.rs

1//! Error types for network analysis operations
2
3use thiserror::Error;
4
5/// Error types for network analysis operations
6#[derive(Error, Debug, Clone, PartialEq)]
7pub enum NetworkError {
8    /// Node index is out of bounds
9    #[error("Node index {0} out of bounds")]
10    NodeIndexOutOfBounds(usize),
11
12    /// Invalid edge specification
13    #[error("Invalid edge: source={0}, target={1}")]
14    InvalidEdge(usize, usize),
15
16    /// Matrix dimension mismatch
17    #[error("Matrix dimension mismatch: expected {expected}, got {got}")]
18    DimensionMismatch { expected: usize, got: usize },
19
20    /// Network is disconnected when connected network required
21    #[error("Network is disconnected")]
22    DisconnectedNetwork,
23
24    /// Computation failed with detailed message
25    #[error("Computation failed: {0}")]
26    ComputationError(String),
27
28    /// Invalid parameter value
29    #[error("Invalid parameter {parameter}: {value} (must be {constraint})")]
30    InvalidParameter {
31        parameter: String,
32        value: String,
33        constraint: String,
34    },
35
36    /// Empty network when non-empty required
37    #[error("Operation requires non-empty network")]
38    EmptyNetwork,
39
40    /// Convergence failure in iterative algorithm
41    #[error("Algorithm failed to converge after {iterations} iterations")]
42    ConvergenceFailure { iterations: usize },
43
44    /// Insufficient data for analysis
45    #[error("Insufficient data: {reason}")]
46    InsufficientData { reason: String },
47}
48
49/// Result type for network operations
50pub type NetworkResult<T> = Result<T, NetworkError>;
51
52impl NetworkError {
53    /// Create a computation error with formatted message
54    pub fn computation<T: std::fmt::Display>(msg: T) -> Self {
55        NetworkError::ComputationError(msg.to_string())
56    }
57
58    /// Create an invalid parameter error
59    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    /// Create an insufficient data error
68    pub fn insufficient_data(reason: &str) -> Self {
69        NetworkError::InsufficientData {
70            reason: reason.to_string(),
71        }
72    }
73}