actor_sample/product_actor/
mod.rs

1//! # Product Actor
2//!
3//! This module implements the Product resource actor with inventory management and custom actions.
4//!
5//! ## Overview
6//!
7//! The Product actor demonstrates how to add custom domain-specific actions beyond CRUD operations.
8//! It manages product catalog and inventory, with actions for checking and reserving stock.
9//!
10//! ## Structure
11//!
12//! - [`entity`] - [`ActorEntity`](actor_framework::ActorEntity) implementation for [`Product`]
13//! - [`error`] - [`ProductError`] type for type-safe error handling
14//! - [`actions`] - [`ProductAction`] and [`ProductActionResult`] for stock management
15//! - [`new()`] - Factory function that creates the actor and client
16//!
17//! ## Custom Actions
18//!
19//! The Product actor showcases the Action pattern for domain-specific operations:
20//!
21//! ```rust,ignore
22//! // Check current stock level (read-only)
23//! let stock = product_client.check_stock(product_id).await?;
24//!
25//! // Reserve stock for an order (mutating, can fail)
26//! product_client.reserve_stock(product_id, quantity).await?;
27//! ```
28//!
29//! ## Usage
30//!
31//! ```rust
32//! use actor_sample::product_actor;
33//! use actor_sample::clients::ProductClient;
34//! use actor_sample::model::ProductCreate;
35//!
36//! #[tokio::main]
37//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
38//!     // Create actor and client
39//!     let (actor, generic_client) = product_actor::new();
40//!     let client = ProductClient::new(generic_client);
41//!
42//!     // Start the actor (no dependencies)
43//!     tokio::spawn(actor.run(()));
44//!
45//!     // Create a product
46//!     let params = ProductCreate {
47//!         name: "Widget".to_string(),
48//!         price: 29.99,
49//!         quantity: 100,
50//!     };
51//!     let id = client.create_product(params).await?;
52//!
53//!     // Reserve stock
54//!     client.reserve_stock(id, 5).await?;
55//!     Ok(())
56//! }
57//! ```
58//!
59//! ## Key Features
60//!
61//! - **Custom actions**: Stock management via [`ProductAction`]
62//! - **Business logic validation**: `reserve_stock` fails if insufficient inventory
63//! - **Type-safe results**: Actions return strongly-typed [`ProductActionResult`]
64
65pub mod actions;
66pub mod entity;
67pub mod error;
68
69pub use actions::*;
70pub use error::*;
71
72use crate::model::Product;
73use actor_framework::{ResourceActor, ResourceClient};
74
75/// Creates a new Product actor and its client.
76pub fn new() -> (ResourceActor<Product>, ResourceClient<Product>) {
77    ResourceActor::new(32)
78}