use crate::{RuleAction, Result, RulesError, LogLevel};
use crate::context::ExecutionContext;
pub struct ActionExecutor;
impl ActionExecutor {
pub fn new() -> Self {
Self
}
pub async fn execute_action(&self, action: &RuleAction, context: &mut ExecutionContext) -> Result<()> {
match action {
RuleAction::SetField { field, value } => {
context.set_variable(field.clone(), value.clone());
Ok(())
}
RuleAction::Log { level, message } => {
match level {
LogLevel::Trace => tracing::trace!("{}", message),
LogLevel::Debug => tracing::debug!("{}", message),
LogLevel::Info => tracing::info!("{}", message),
LogLevel::Warn => tracing::warn!("{}", message),
LogLevel::Error => tracing::error!("{}", message),
}
Ok(())
}
RuleAction::ModifyContext { modifications } => {
for (key, value) in modifications {
context.set_variable(key.clone(), value.clone());
}
Ok(())
}
RuleAction::Abort { reason } => {
Err(RulesError::ActionExecution(format!("Execution aborted: {}", reason)))
}
_ => {
tracing::warn!("Unhandled action type: {:?}", action);
Ok(())
}
}
}
}
impl Default for ActionExecutor {
fn default() -> Self {
Self::new()
}
}