use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;
#[async_trait]
pub trait Executor: Send + Sync + 'static {
async fn execute(&self, payload: Bytes) -> Result<Bytes> {
self.execute_method("dispatch", payload).await
}
async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes>;
async fn execute_value(
&self,
method: &str,
payload: serde_json::Value,
) -> Result<serde_json::Value> {
let bytes = serde_json::to_vec(&payload)?;
let result = self.execute_method(method, Bytes::from(bytes)).await?;
Ok(serde_json::from_slice(&result)?)
}
}
#[async_trait]
impl<T: Executor + ?Sized> Executor for Arc<T> {
async fn execute_method(&self, method: &str, payload: Bytes) -> Result<Bytes> {
(**self).execute_method(method, payload).await
}
async fn execute_value(
&self,
method: &str,
payload: serde_json::Value,
) -> Result<serde_json::Value> {
(**self).execute_value(method, payload).await
}
}