use std::sync::Arc;
use async_trait::async_trait;
use serde_json::Value;
use crate::event::Event;
use crate::recorder::{noop_recorder, RunRecorder};
use crate::result::StepResult;
use crate::state::State;
pub struct WorkflowContext {
pub branch_id: String,
pub config: Value,
pub instance_metadata: Value,
pub state: Arc<dyn State>,
pub recorder: Arc<dyn RunRecorder>,
pub trigger_kind: String,
}
impl WorkflowContext {
pub fn new(branch_id: impl Into<String>, state: Arc<dyn State>) -> Self {
Self {
branch_id: branch_id.into(),
config: Value::Object(Default::default()),
instance_metadata: Value::Object(Default::default()),
state,
recorder: noop_recorder(),
trigger_kind: "event".into(),
}
}
pub fn with_config(mut self, config: Value) -> Self {
self.config = config;
self
}
pub fn with_recorder(mut self, recorder: Arc<dyn RunRecorder>) -> Self {
self.recorder = recorder;
self
}
pub fn with_trigger_kind(mut self, trigger_kind: impl Into<String>) -> Self {
self.trigger_kind = trigger_kind.into();
self
}
pub fn scoped(&self, key: &str) -> String {
format!("{}.{}", self.branch_id, key)
}
}
#[async_trait]
pub trait StepNode: Send + Sync {
fn produces_fan_out(&self) -> bool {
false
}
async fn process(&self, event: &Event, ctx: &WorkflowContext) -> StepResult;
}