use thiserror::Error;
pub type Result<T> = std::result::Result<T, NeuralDynamicsError>;
#[derive(Error, Debug, Clone)]
pub enum NeuralDynamicsError {
#[error("Invalid parameter {parameter}: {reason} (value: {value})")]
InvalidParameter {
parameter: String,
value: f64,
reason: String,
},
#[error("Invalid neuron index {index} (max: {max})")]
InvalidNeuronIndex { index: usize, max: usize },
#[error("Invalid population index {index} (max: {max})")]
InvalidPopulationIndex { index: usize, max: usize },
#[error("Invalid projection index {index} (max: {max})")]
InvalidProjectionIndex { index: usize, max: usize },
#[error("Population size mismatch: expected {expected}, got {actual}")]
SizeMismatch { expected: usize, actual: usize },
#[error("Population is empty")]
EmptyPopulation,
#[error("Network is empty (no populations)")]
EmptyNetwork,
#[error("Connectivity error: {reason}")]
ConnectivityError { reason: String },
#[error("Simulation error: {reason}")]
SimulationError { reason: String },
#[error("Non-finite value encountered in {location}: {value}")]
NonFiniteValue { location: String, value: f64 },
#[error("Integration error: {reason}")]
IntegrationError { reason: String },
#[error("Recording error: {reason}")]
RecordingError { reason: String },
#[error("Analysis error: {reason}")]
AnalysisError { reason: String },
#[error("IO error: {0}")]
IoError(String),
#[error("Hodgkin-Huxley error: {0}")]
HodgkinHuxleyError(String),
#[error("Synapse error: {0}")]
SynapseError(String),
}
impl From<hodgkin_huxley::error::HHError> for NeuralDynamicsError {
fn from(err: hodgkin_huxley::error::HHError) -> Self {
NeuralDynamicsError::HodgkinHuxleyError(err.to_string())
}
}
impl From<synapse_models::error::SynapseError> for NeuralDynamicsError {
fn from(err: synapse_models::error::SynapseError) -> Self {
NeuralDynamicsError::SynapseError(err.to_string())
}
}
impl From<std::io::Error> for NeuralDynamicsError {
fn from(err: std::io::Error) -> Self {
NeuralDynamicsError::IoError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = NeuralDynamicsError::InvalidParameter {
parameter: "weight".to_string(),
value: -1.0,
reason: "must be non-negative".to_string(),
};
assert!(err.to_string().contains("Invalid parameter"));
assert!(err.to_string().contains("weight"));
}
#[test]
fn test_error_conversions() {
let hh_err = hodgkin_huxley::error::HHError::InvalidParameter {
parameter: "c_m".to_string(),
value: -1.0,
reason: "negative".to_string(),
};
let nd_err: NeuralDynamicsError = hh_err.into();
assert!(matches!(nd_err, NeuralDynamicsError::HodgkinHuxleyError(_)));
}
}