actor_sample/user_actor/
entity.rs

1//! Entity trait implementation for the User resource type.
2//!
3//! This module contains the [`ActorEntity`] trait implementation
4//! that enables [`User`] to be managed by the generic [`actor_framework::ResourceActor`].
5//!
6//! See the trait implementation on [`User`] for method documentation.
7
8use crate::model::{User, UserCreate, UserId, UserUpdate};
9use crate::user_actor::UserError;
10use actor_framework::ActorEntity;
11use async_trait::async_trait;
12
13#[derive(Debug)]
14pub enum UserAction {
15    // No custom actions for now
16}
17
18/// Marker constant to ensure module documentation is rendered.
19#[doc(hidden)]
20/// Marker constant to verify ActorEntity trait implementation exists at compile time.
21/// This is used by the framework to ensure proper trait implementation.
22#[allow(dead_code)]
23pub const ENTITY_IMPL_PRESENT: bool = true;
24
25#[async_trait]
26impl ActorEntity for User {
27    type Id = UserId;
28    type Create = UserCreate;
29    type Update = UserUpdate;
30    type Action = UserAction;
31    type ActionResult = ();
32    type Context = ();
33    type Error = UserError;
34
35    // fn id(&self) -> &String { &self.id }
36
37    /// Creates a new User from creation parameters.
38    fn from_create_params(id: UserId, params: UserCreate) -> Result<Self, Self::Error> {
39        Ok(User {
40            id,
41            name: params.name,
42            email: params.email,
43        })
44    }
45
46    /// Handles updates to the User entity.
47    ///
48    /// # Fields Updated
49    /// - `name`: User's display name
50    /// - `email`: User's email address
51    async fn on_update(
52        &mut self,
53        update: UserUpdate,
54        _ctx: &Self::Context,
55    ) -> Result<(), Self::Error> {
56        if let Some(name) = update.name {
57            self.name = name;
58        }
59        if let Some(email) = update.email {
60            self.email = email;
61        }
62        Ok(())
63    }
64
65    async fn handle_action(
66        &mut self,
67        _action: UserAction,
68        _ctx: &Self::Context,
69    ) -> Result<(), Self::Error> {
70        Ok(())
71    }
72}