use serde::{Deserialize, Serialize};
use crate::validation::{ProtocolLimits, Validate, ValidationError, check_finite};
use super::de::de_loss;
use super::{
ConfigPayload, EmbeddingBatch, GradientBatch, NeuromodulatorSnapshot, SpikeBatch,
StimulusBatch, TraceBatch,
};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum IpcMessage {
Spikes(SpikeBatch),
Embeddings(EmbeddingBatch),
Stimuli(StimulusBatch),
Neuromodulators(NeuromodulatorSnapshot),
Loss(#[serde(deserialize_with = "de_loss")] f32),
ConfigUpdate(ConfigPayload),
GradientUpdate(GradientBatch),
EligibilityTraces(TraceBatch),
TrainingComplete,
Shutdown,
Ping,
}
impl Validate for IpcMessage {
fn validate_with(&self, limits: ProtocolLimits) -> Result<(), ValidationError> {
match self {
Self::Spikes(batch) => batch.validate_with(limits),
Self::Embeddings(batch) => batch.validate_with(limits),
Self::Stimuli(batch) => batch.validate_with(limits),
Self::Neuromodulators(snapshot) => snapshot.validate_with(limits),
Self::ConfigUpdate(payload) => payload.validate_with(limits),
Self::GradientUpdate(batch) => batch.validate_with(limits),
Self::EligibilityTraces(batch) => batch.validate_with(limits),
Self::Loss(value) => check_finite("Loss", *value),
Self::TrainingComplete | Self::Shutdown | Self::Ping => Ok(()),
}
}
}