actor_sample/model/user.rs
1use serde::{Deserialize, Serialize};
2
3/// Represents a registered user in the system.
4///
5/// # Actor Framework
6/// This struct implements the [`ActorEntity`](actor_framework::ActorEntity) trait,
7/// allowing it to be managed by a [`ResourceActor`](actor_framework::ResourceActor).
8///
9/// See [`impl ActorEntity for User`](#impl-ActorEntity-for-User) for details on:
10/// - Creation parameters ([`UserCreate`])
11/// - Update parameters ([`UserUpdate`])
12use std::fmt::Display;
13
14/// Type-safe identifier for Users.
15#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
16pub struct UserId(pub u32);
17
18impl From<u32> for UserId {
19 fn from(id: u32) -> Self {
20 Self(id)
21 }
22}
23
24impl Display for UserId {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "user_{}", self.0)
27 }
28}
29
30#[derive(Debug, Clone, PartialEq)]
31pub struct User {
32 pub id: UserId,
33 pub name: String,
34 pub email: String,
35}
36
37/// Payload for creating a new user.
38#[derive(Debug, Clone)]
39pub struct UserCreate {
40 pub name: String,
41 pub email: String,
42}
43
44/// Payload for updating an existing user.
45/// DTOs for User updates.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct UserUpdate {
48 pub name: Option<String>,
49 pub email: Option<String>,
50}
51
52impl User {
53 /// Creates a new User instance.
54 ///
55 /// # Arguments
56 /// * `name` - User's display name
57 /// * `email` - User's email address
58 ///
59 /// # Notes
60 /// The `id` field is initialized as an empty string and will be set by the actor system.
61 pub fn new(name: impl Into<String>, email: impl Into<String>) -> Self {
62 Self {
63 id: UserId(0),
64 name: name.into(),
65 email: email.into(),
66 }
67 }
68}