use std::sync::Arc;
use async_trait::async_trait;
use crate::{
EnvVar, InChannels, OutChannels, Output,
action::Action,
connection::{in_channel::TypedInChannels, out_channel::TypedOutChannels},
};
#[async_trait]
pub trait TypedAction: Send + Sync {
type I: Send + Sync + 'static;
type O: Send + Sync + 'static;
fn make_typed_in_channels(&self, in_channels: &InChannels) -> TypedInChannels<Self::I> {
TypedInChannels(
in_channels.0.clone(),
in_channels.1.clone(),
Default::default(),
)
}
fn make_typed_out_channels(&self, out_channels: &OutChannels) -> TypedOutChannels<Self::O> {
TypedOutChannels(out_channels.0.clone(), Default::default())
}
async fn run(
&self,
in_channels: TypedInChannels<Self::I>,
out_channels: TypedOutChannels<Self::O>,
env: Arc<EnvVar>,
) -> Output;
}
#[async_trait]
impl<T: TypedAction> Action for T {
async fn run(
&self,
in_channels: &mut InChannels,
out_channels: &mut OutChannels,
env: Arc<EnvVar>,
) -> Output {
let in_channels = self.make_typed_in_channels(in_channels);
let out_channels = self.make_typed_out_channels(out_channels);
self.run(in_channels, out_channels, env).await
}
}