actor_sample/clients/
order_client.rs

1//! # Order Client
2//!
3//! Provides a high‑level API for interacting with the `Order` actor.
4//! It wraps a `ResourceClient<Order>` and handles orchestration logic.
5use crate::model::{Order, OrderId};
6use crate::order_actor::OrderError;
7use actor_framework::ActorClient;
8use actor_framework::{FrameworkError, ResourceClient};
9use async_trait::async_trait;
10use tracing::{debug, info, instrument};
11
12/// Client for interacting with the Order actor.
13///
14/// Orchestration logic (user validation, stock reservation) now happens
15/// in the Order actor's `on_create` hook.
16#[derive(Clone)]
17pub struct OrderClient {
18    inner: ResourceClient<Order>,
19}
20
21impl OrderClient {
22    pub fn new(inner: ResourceClient<Order>) -> Self {
23        Self { inner }
24    }
25
26    #[instrument(skip(self))]
27    pub async fn create_order(
28        &self,
29        params: crate::model::OrderCreate,
30    ) -> Result<OrderId, OrderError> {
31        debug!("create_order called");
32        info!("Sending create_order to actor");
33
34        // Create order - validation happens in Order::on_create
35        self.inner
36            .create(params)
37            .await
38            .map_err(|e| OrderError::ActorCommunicationError(e.to_string()))
39    }
40}
41
42#[async_trait]
43impl ActorClient<Order> for OrderClient {
44    type Error = OrderError;
45
46    fn inner(&self) -> &ResourceClient<Order> {
47        &self.inner
48    }
49
50    fn map_error(e: FrameworkError) -> Self::Error {
51        OrderError::ActorCommunicationError(e.to_string())
52    }
53}