1use std::sync::Arc;
6
7use anyhow::Result;
8use async_trait::async_trait;
9use bytes::Bytes;
10
11#[async_trait]
15pub trait Executor: Send + Sync + 'static {
16 async fn execute(&self, payload: Bytes) -> Result<Bytes> {
19 self.execute_method("dispatch", payload).await
20 }
21
22 async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes>;
24
25 async fn execute_value(
31 &self,
32 method: &str,
33 payload: serde_json::Value,
34 ) -> Result<serde_json::Value> {
35 let bytes = serde_json::to_vec(&payload)?;
36 let result = self.execute_method(method, Bytes::from(bytes)).await?;
37 Ok(serde_json::from_slice(&result)?)
38 }
39
40 async fn execute_value_traced(
47 &self,
48 method: &str,
49 payload: serde_json::Value,
50 ) -> Result<(serde_json::Value, Arc<str>)> {
51 let value = self.execute_value(method, payload).await?;
52 Ok((value, Arc::from("")))
53 }
54}
55
56#[async_trait]
58impl<T: Executor + ?Sized> Executor for Arc<T> {
59 async fn execute(&self, payload: Bytes) -> Result<Bytes> {
60 (**self).execute(payload).await
61 }
62
63 async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
64 (**self).execute_method(method, payload).await
65 }
66
67 async fn execute_value(
68 &self,
69 method: &str,
70 payload: serde_json::Value,
71 ) -> Result<serde_json::Value> {
72 (**self).execute_value(method, payload).await
73 }
74
75 async fn execute_value_traced(
76 &self,
77 method: &str,
78 payload: serde_json::Value,
79 ) -> Result<(serde_json::Value, Arc<str>)> {
80 (**self).execute_value_traced(method, payload).await
81 }
82}