use crate::actor::state::{ActorStateClient, ActorStateManager};
use crate::actor::types::ActorKey;
use std::sync::Arc;
use uuid::Uuid;
#[derive(Clone)]
pub struct ActorContext {
actor_key: ActorKey,
state_client: Arc<ActorStateClient>,
execution_id: String,
workflow_execution_id: Option<String>,
}
impl ActorContext {
pub fn new(
actor_key: ActorKey,
state_client: Arc<ActorStateClient>,
execution_id: String,
) -> Self {
Self {
actor_key,
state_client,
execution_id,
workflow_execution_id: None,
}
}
pub fn actor_name(&self) -> &str {
self.actor_key.actor_name()
}
pub fn key(&self) -> &str {
self.actor_key.key()
}
pub fn execution_id(&self) -> &str {
&self.execution_id
}
pub fn actor_key(&self) -> &ActorKey {
&self.actor_key
}
pub fn state(&self) -> ActorStateManager {
ActorStateManager::new(
self.actor_key.clone(),
self.state_client.clone(),
self.execution_id.clone(),
)
}
pub fn with_workflow_execution_id(mut self, workflow_execution_id: String) -> Self {
self.workflow_execution_id = Some(workflow_execution_id);
self
}
pub fn workflow_execution_id(&self) -> Option<&str> {
self.workflow_execution_id.as_deref()
}
pub fn deterministic_uuid(&self, seed: &str) -> Uuid {
let namespace = Uuid::new_v5(
&Uuid::NAMESPACE_DNS,
format!("{}:{}", self.actor_name(), self.key()).as_bytes(),
);
Uuid::new_v5(&namespace, seed.as_bytes())
}
}
#[derive(Clone)]
pub struct SharedActorContext {
inner: ActorContext,
}
impl SharedActorContext {
pub fn new(
actor_key: ActorKey,
state_client: Arc<ActorStateClient>,
execution_id: String,
) -> Self {
Self {
inner: ActorContext::new(actor_key, state_client, execution_id),
}
}
pub fn actor_name(&self) -> &str {
self.inner.actor_name()
}
pub fn key(&self) -> &str {
self.inner.key()
}
pub fn execution_id(&self) -> &str {
self.inner.execution_id()
}
pub fn actor_key(&self) -> &ActorKey {
self.inner.actor_key()
}
pub fn state(&self) -> ActorStateManager {
self.inner.state()
}
pub fn deterministic_uuid(&self, seed: &str) -> Uuid {
self.inner.deterministic_uuid(seed)
}
pub fn workflow_execution_id(&self) -> Option<&str> {
self.inner.workflow_execution_id()
}
}
impl From<ActorContext> for SharedActorContext {
fn from(ctx: ActorContext) -> Self {
Self { inner: ctx }
}
}
pub struct ActorContextBuilder {
actor_key: ActorKey,
state_client: Arc<ActorStateClient>,
execution_id: Option<String>,
workflow_execution_id: Option<String>,
}
impl ActorContextBuilder {
pub fn new(actor_key: ActorKey, state_client: Arc<ActorStateClient>) -> Self {
Self {
actor_key,
state_client,
execution_id: None,
workflow_execution_id: None,
}
}
pub fn execution_id(mut self, execution_id: impl Into<String>) -> Self {
self.execution_id = Some(execution_id.into());
self
}
pub fn workflow_execution_id(mut self, workflow_execution_id: impl Into<String>) -> Self {
self.workflow_execution_id = Some(workflow_execution_id.into());
self
}
pub fn build(self) -> ActorContext {
let execution_id = self
.execution_id
.unwrap_or_else(|| Uuid::new_v4().to_string());
let mut ctx = ActorContext::new(self.actor_key, self.state_client, execution_id);
if let Some(workflow_execution_id) = self.workflow_execution_id {
ctx = ctx.with_workflow_execution_id(workflow_execution_id);
}
ctx
}
pub fn build_shared(self) -> SharedActorContext {
SharedActorContext::from(self.build())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_actor_context_creation() {
let actor_key = ActorKey::new("ShoppingCart", "user-123");
let state_client = Arc::new(ActorStateClient::new_mock());
let execution_id = "exec-456".to_string();
let ctx = ActorContext::new(actor_key.clone(), state_client, execution_id.clone());
assert_eq!(ctx.actor_name(), "ShoppingCart");
assert_eq!(ctx.key(), "user-123");
assert_eq!(ctx.execution_id(), "exec-456");
}
#[tokio::test]
async fn test_shared_context_from_actor_context() {
let actor_key = ActorKey::new("ShoppingCart", "user-123");
let state_client = Arc::new(ActorStateClient::new_mock());
let ctx = ActorContext::new(actor_key, state_client, "exec-123".to_string());
let shared_ctx = SharedActorContext::from(ctx);
assert_eq!(shared_ctx.actor_name(), "ShoppingCart");
assert_eq!(shared_ctx.key(), "user-123");
}
#[tokio::test]
async fn test_context_builder() {
let actor_key = ActorKey::new("BankAccount", "account-789");
let state_client = Arc::new(ActorStateClient::new_mock());
let ctx = ActorContextBuilder::new(actor_key, state_client)
.execution_id("exec-999")
.workflow_execution_id("workflow-111")
.build();
assert_eq!(ctx.actor_name(), "BankAccount");
assert_eq!(ctx.key(), "account-789");
assert_eq!(ctx.execution_id(), "exec-999");
assert_eq!(ctx.workflow_execution_id(), Some("workflow-111"));
}
#[tokio::test]
async fn test_deterministic_uuid() {
let actor_key = ActorKey::new("ShoppingCart", "user-123");
let state_client = Arc::new(ActorStateClient::new_mock());
let ctx = ActorContext::new(actor_key, state_client, "exec-123".to_string());
let uuid1 = ctx.deterministic_uuid("order");
let uuid2 = ctx.deterministic_uuid("order");
assert_eq!(uuid1, uuid2);
let uuid3 = ctx.deterministic_uuid("invoice");
assert_ne!(uuid1, uuid3);
}
#[tokio::test]
async fn test_context_builder_default_execution_id() {
let actor_key = ActorKey::new("ShoppingCart", "user-123");
let state_client = Arc::new(ActorStateClient::new_mock());
let ctx = ActorContextBuilder::new(actor_key, state_client).build();
assert!(!ctx.execution_id().is_empty());
assert!(Uuid::parse_str(ctx.execution_id()).is_ok());
}
}