use async_trait::async_trait;
use coreon_core::{processor::Predicate, Exchange, Processor, Result};
use std::sync::Arc;
pub struct WhenClause {
predicate: Arc<dyn Predicate>,
then: Arc<dyn Processor>,
}
impl WhenClause {
pub fn new(predicate: Arc<dyn Predicate>, then: Arc<dyn Processor>) -> Self {
Self { predicate, then }
}
}
pub struct Choice {
clauses: Vec<WhenClause>,
otherwise: Option<Arc<dyn Processor>>,
}
impl Choice {
pub fn new(clauses: Vec<WhenClause>, otherwise: Option<Arc<dyn Processor>>) -> Arc<Self> {
Arc::new(Self { clauses, otherwise })
}
}
#[async_trait]
impl Processor for Choice {
async fn process(&self, exchange: &mut Exchange) -> Result<()> {
for clause in &self.clauses {
if clause.predicate.matches(exchange) {
return clause.then.process(exchange).await;
}
}
if let Some(o) = &self.otherwise {
return o.process(exchange).await;
}
Ok(())
}
}