actor_framework/message.rs
1//! # Generic Messages
2//!
3//! This module defines the generic message types used for communication between
4//! the `ResourceClient` and `ResourceActor`.
5
6use crate::entity::ActorEntity;
7use crate::error::FrameworkError;
8use tokio::sync::oneshot;
9
10/// Type alias for the one-shot response channel used by actors.
11pub type Response<T> = oneshot::Sender<Result<T, FrameworkError>>;
12
13/// Internal message type sent to the actor to request operations.
14///
15/// # Resource-Oriented Architecture
16/// This enum implements a **Resource-Oriented** design pattern where each actor manages a specific
17/// type of resource (the [`ActorEntity`]). Instead of defining ad-hoc messages for every operation,
18/// we standardize around a set of lifecycle operations that apply to almost any persistent resource.
19///
20/// # The CRUD Pattern
21/// The variants of this enum map directly to standard **CRUD** (Create, Read, Update, Delete) operations,
22/// plus a custom `Action` variant for resource-specific logic that doesn't fit the CRUD model.
23///
24/// - **Create**: Lifecycle start. Uses [`ActorEntity::Create`] to initialize a new resource.
25/// - **Get (Read)**: Retrieval. Fetches the current state of the resource by ID.
26/// - **Update**: State mutation. Uses [`ActorEntity::Update`] to modify an existing resource.
27/// - **Delete**: Lifecycle end. Removes the resource.
28/// - **Action**: Extensibility. Executes a custom [`ActorEntity::Action`].
29///
30/// # Entity Interaction
31/// This type is generic over `T: ActorEntity`. It uses the associated types defined in the [`ActorEntity`] trait
32/// (like `Create`, `Update`, `Action`) to ensure type safety for every operation.
33/// This guarantees that you can't send a "User Create" payload to a "Product" actor.
34#[derive(Debug)]
35pub enum ResourceRequest<T: ActorEntity> {
36 Create {
37 params: T::Create,
38 respond_to: Response<T::Id>,
39 },
40 Get {
41 id: T::Id,
42 respond_to: Response<Option<T>>,
43 },
44 Update {
45 id: T::Id,
46 update: T::Update,
47 respond_to: Response<T>,
48 },
49 #[allow(dead_code)]
50 Delete { id: T::Id, respond_to: Response<()> },
51 Action {
52 id: T::Id,
53 action: T::Action,
54 respond_to: Response<T::ActionResult>,
55 },
56}