use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq)]
pub enum NetworkError {
#[error("Node index {0} out of bounds")]
NodeIndexOutOfBounds(usize),
#[error("Invalid edge: source={0}, target={1}")]
InvalidEdge(usize, usize),
#[error("Matrix dimension mismatch: expected {expected}, got {got}")]
DimensionMismatch { expected: usize, got: usize },
#[error("Network is disconnected")]
DisconnectedNetwork,
#[error("Computation failed: {0}")]
ComputationError(String),
#[error("Invalid parameter {parameter}: {value} (must be {constraint})")]
InvalidParameter {
parameter: String,
value: String,
constraint: String,
},
#[error("Operation requires non-empty network")]
EmptyNetwork,
#[error("Algorithm failed to converge after {iterations} iterations")]
ConvergenceFailure { iterations: usize },
#[error("Insufficient data: {reason}")]
InsufficientData { reason: String },
}
pub type NetworkResult<T> = Result<T, NetworkError>;
impl NetworkError {
pub fn computation<T: std::fmt::Display>(msg: T) -> Self {
NetworkError::ComputationError(msg.to_string())
}
pub fn invalid_param(param: &str, value: impl std::fmt::Display, constraint: &str) -> Self {
NetworkError::InvalidParameter {
parameter: param.to_string(),
value: value.to_string(),
constraint: constraint.to_string(),
}
}
pub fn insufficient_data(reason: &str) -> Self {
NetworkError::InsufficientData {
reason: reason.to_string(),
}
}
}