use serde::de::DeserializeOwned;
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<T, F>(&mut self, label: &str, predicate: F) -> Result<bool, EngineError>
where
T: DeserializeOwned,
F: FnOnce(&T) -> bool,
{
let input: T = self.input().await?;
let value = predicate(&input);
if let Some(plan) = self.plan().cloned() {
lock_plan(&plan).set_condition(ConditionResult::Evaluated {
expression: label.to_string(),
value,
});
}
Ok(value)
}
pub fn when_dynamic(&mut self, label: &str, value: bool) -> bool {
if let Some(plan) = self.plan().cloned() {
lock_plan(&plan).set_condition(ConditionResult::Unevaluable {
expression: label.to_string(),
reason: DYNAMIC_REASON.to_string(),
});
}
value
}
}