use serde::{Deserialize, Serialize};
#[cfg(feature = "auto-register")]
use crate::actor::executor::ActorExecutor;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationMetadata {
pub actor_name: String,
pub operation_name: String,
pub mode: OperationMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OperationMode {
Exclusive,
Shared,
}
#[derive(Debug, Clone)]
pub struct ActorDefinition {
pub actor_name: &'static str,
pub operations: &'static [OperationMetadata],
}
#[cfg(feature = "auto-register")]
inventory::collect!(ActorDefinition);
impl ActorDefinition {
pub const fn new(actor_name: &'static str, operations: &'static [OperationMetadata]) -> Self {
Self {
actor_name,
operations,
}
}
pub fn actor_name(&self) -> &str {
self.actor_name
}
pub fn operations(&self) -> &[OperationMetadata] {
self.operations
}
pub fn operation_count(&self) -> usize {
self.operations.len()
}
}
#[cfg(feature = "auto-register")]
pub type ActorHandlerRegistrationFn = fn(&ActorExecutor);
#[cfg(feature = "auto-register")]
#[derive(Clone, Copy)]
pub struct ActorHandlerRegistration {
pub register_fn: ActorHandlerRegistrationFn,
}
#[cfg(feature = "auto-register")]
inventory::collect!(ActorHandlerRegistration);
#[cfg(feature = "auto-register")]
impl ActorHandlerRegistration {
pub const fn new(register_fn: ActorHandlerRegistrationFn) -> Self {
Self { register_fn }
}
pub fn register(&self, executor: &ActorExecutor) {
(self.register_fn)(executor);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_actor_definition_accessors() {
let definition = ActorDefinition {
actor_name: "TestActor",
operations: &[],
};
assert_eq!(definition.actor_name(), "TestActor");
assert_eq!(definition.operation_count(), 0);
}
#[test]
fn test_operation_mode_serialization() {
let mode = OperationMode::Exclusive;
let json = serde_json::to_string(&mode).unwrap();
let deserialized: OperationMode = serde_json::from_str(&json).unwrap();
assert_eq!(mode, deserialized);
}
#[test]
fn test_operation_metadata() {
let metadata = OperationMetadata {
actor_name: "ShoppingCart".to_string(),
operation_name: "add_item".to_string(),
mode: OperationMode::Exclusive,
};
assert_eq!(metadata.actor_name, "ShoppingCart");
assert_eq!(metadata.operation_name, "add_item");
assert_eq!(metadata.mode, OperationMode::Exclusive);
}
}