actor_sample/clients/
user_client.rs

1//! # User Client
2//!
3//! Provides a high‑level API for interacting with the `User` actor.
4//! It wraps a `ResourceClient<User>` and exposes domain‑specific methods.
5use crate::model::{User, UserCreate, UserId, UserUpdate};
6use crate::user_actor::UserError;
7use actor_framework::ActorClient;
8use actor_framework::{FrameworkError, ResourceClient};
9use async_trait::async_trait;
10use tracing::{debug, instrument};
11
12/// Client for interacting with the User actor.
13#[derive(Clone)]
14pub struct UserClient {
15    inner: ResourceClient<User>,
16}
17
18impl UserClient {
19    pub fn new(inner: ResourceClient<User>) -> Self {
20        Self { inner }
21    }
22}
23
24#[async_trait]
25impl ActorClient<User> for UserClient {
26    type Error = UserError;
27
28    fn inner(&self) -> &ResourceClient<User> {
29        &self.inner
30    }
31
32    fn map_error(e: FrameworkError) -> Self::Error {
33        UserError::ActorCommunicationError(e.to_string())
34    }
35}
36
37impl UserClient {
38    // Custom create method as it needs specific payload conversion
39
40    #[instrument(skip(self))]
41    pub async fn create_user(&self, params: UserCreate) -> Result<UserId, UserError> {
42        debug!("Sending request");
43        self.inner
44            .create(params)
45            .await
46            .map_err(|e| UserError::ActorCommunicationError(e.to_string()))
47    }
48
49    // New method utilizing the generic update
50    #[instrument(skip(self))]
51    #[allow(dead_code)]
52    pub async fn update_user(&self, id: UserId, update: UserUpdate) -> Result<User, UserError> {
53        debug!("Sending request");
54        self.inner
55            .update(id, update)
56            .await
57            .map_err(|e| UserError::ActorCommunicationError(e.to_string()))
58    }
59}