actor_sample/model/product.rs
1/// Represents a product in the inventory.
2///
3/// # Actor Framework
4/// This struct implements the [`ActorEntity`](actor_framework::ActorEntity) trait,
5/// allowing it to be managed by a [`ResourceActor`](actor_framework::ResourceActor).
6///
7/// See [`impl ActorEntity for Product`](#impl-ActorEntity-for-Product) for details on:
8/// - Creation parameters ([`ProductCreate`](crate::model::ProductCreate))
9/// - Update parameters ([`ProductUpdate`](crate::model::ProductUpdate))
10/// - Custom actions ([`ProductAction`](crate::product_actor::actions::ProductAction))
11use serde::{Deserialize, Serialize};
12
13use std::fmt::Display;
14
15/// Type-safe identifier for Products.
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub struct ProductId(pub u32);
18
19impl From<u32> for ProductId {
20 fn from(id: u32) -> Self {
21 Self(id)
22 }
23}
24
25impl Display for ProductId {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 write!(f, "product_{}", self.0)
28 }
29}
30
31#[derive(Debug, Clone)]
32pub struct Product {
33 #[allow(dead_code)]
34 pub id: ProductId,
35 pub name: String,
36 pub price: f64,
37 pub quantity: u32,
38}
39
40impl Product {
41 /// Creates a new Product instance.
42 ///
43 /// # Arguments
44 /// * `id` - Unique identifier (typically set by the actor system)
45 /// * `name` - Product name
46 /// * `price` - Product price
47 /// * `quantity` - Available stock quantity
48 pub fn new(id: ProductId, name: impl Into<String>, price: f64, quantity: u32) -> Self {
49 Self {
50 id,
51 name: name.into(),
52 price,
53 quantity,
54 }
55 }
56}
57
58/// DTOs for Product creation and updates.
59#[derive(Debug, Clone)]
60pub struct ProductCreate {
61 pub name: String,
62 pub price: f64,
63 pub quantity: u32,
64}
65
66// DTOs for Product updates.
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct ProductUpdate {
69 pub price: Option<f64>,
70 pub quantity: Option<u32>,
71}