use serde_json::Value;
use crate::context::WorkflowContext;
use crate::error::EngineError;
use crate::plan::{ConditionResult, lock_plan};
const DYNAMIC_REASON: &str =
"depends on a previous step's output, which is synthetic while planning";
impl WorkflowContext {
pub async fn when<F>(&mut self, expression: &str, predicate: F) -> Result<bool, EngineError>
where
F: FnOnce(&Value) -> bool,
{
let payload = self.payload().await?;
let value = predicate(&payload);
if let Some(plan) = self.plan().cloned() {
lock_plan(&plan).set_condition(ConditionResult::Evaluated {
expression: expression.to_string(),
value,
});
}
Ok(value)
}
pub fn when_dynamic(&mut self, expression: &str, value: bool) -> bool {
if let Some(plan) = self.plan().cloned() {
lock_plan(&plan).set_condition(ConditionResult::Unevaluable {
expression: expression.to_string(),
reason: DYNAMIC_REASON.to_string(),
});
}
value
}
}