use std::sync::Arc;
use async_trait::async_trait;
use crate::{EnvVar, InChannels, Node, NodeId, NodeName, NodeTable, OutChannels, Output};
#[async_trait]
pub trait Condition: Send + Sync {
async fn run(
&self,
in_channels: &mut InChannels,
out_channels: &OutChannels,
env: Arc<EnvVar>,
) -> bool;
}
pub struct ConditionalNode {
id: NodeId,
name: NodeName,
condition: Box<dyn Condition>,
in_channels: InChannels,
out_channels: OutChannels,
}
impl ConditionalNode {
pub fn with_condition(
name: NodeName,
condition: impl Condition + 'static,
node_table: &mut NodeTable,
) -> Self {
Self {
id: node_table.alloc_id_for(&name),
name,
condition: Box::new(condition),
in_channels: InChannels::default(),
out_channels: OutChannels::default(),
}
}
}
#[async_trait]
impl Node for ConditionalNode {
fn id(&self) -> NodeId {
self.id
}
fn name(&self) -> NodeName {
self.name.clone()
}
fn input_channels(&mut self) -> &mut InChannels {
&mut self.in_channels
}
fn output_channels(&mut self) -> &mut OutChannels {
&mut self.out_channels
}
async fn run(&mut self, env: Arc<EnvVar>) -> Output {
Output::ConditionResult(
self.condition
.run(&mut self.in_channels, &self.out_channels, env)
.await,
)
}
fn is_condition(&self) -> bool {
true
}
}