use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum MeshError {
InvalidConfig(String),
InvalidRouterConfig {
field: &'static str,
reason: String,
},
NeuronCountMismatch {
expected: usize,
got: usize,
context: String,
},
IndexOutOfBounds { index: usize, max: usize },
TopologyError(String),
DelayError(String),
NonFiniteSignal { index: usize, context: String },
NonFiniteNeuromodulator { field: &'static str },
OutOfRangeNeuromodulator { field: &'static str, value: f32 },
}
impl MeshError {
pub(crate) fn invalid_router_config(field: &'static str, reason: impl Into<String>) -> Self {
Self::InvalidRouterConfig {
field,
reason: reason.into(),
}
}
}
impl fmt::Display for MeshError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MeshError::InvalidConfig(msg) => write!(f, "invalid configuration: {msg}"),
MeshError::InvalidRouterConfig { field, reason } => {
write!(f, "invalid router config ({field}): {reason}")
}
MeshError::NeuronCountMismatch {
expected,
got,
context,
} => write!(
f,
"neuron count mismatch in {context}: expected {expected}, got {got}"
),
MeshError::IndexOutOfBounds { index, max } => {
write!(f, "index {index} out of bounds (max {max})")
}
MeshError::TopologyError(msg) => write!(f, "topology error: {msg}"),
MeshError::DelayError(msg) => write!(f, "delay error: {msg}"),
MeshError::NonFiniteSignal { index, context } => {
write!(f, "non-finite signal in {context}[{index}]")
}
MeshError::NonFiniteNeuromodulator { field } => {
write!(f, "non-finite neuromodulator field {field}")
}
MeshError::OutOfRangeNeuromodulator { field, value } => {
write!(
f,
"neuromodulator field {field} must be in [0, 1], got {value}"
)
}
}
}
}
impl std::error::Error for MeshError {}
pub type Result<T> = std::result::Result<T, MeshError>;