vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Workflow injector (new architecture).

use super::error::WorkflowError;
use super::runtime::RuntimeBindings;
use super::template::ValidatedWorkflowTemplate;
use serde_json::Value;

/// Injects runtime bindings into a validated workflow template.
pub struct WorkflowInjector;

impl WorkflowInjector {
    pub fn inject(
        template: &ValidatedWorkflowTemplate,
        bindings: &RuntimeBindings,
    ) -> Result<Value, WorkflowError> {
        let mut workflow = (*template.workflow_body()).clone();

        for (key, binding) in template.metadata().bindings.iter() {
            let value = bindings.lookup(key)?;
            // Traverse the workflow to the specified node and input
            if let Some(node) = workflow.get_mut(&binding.node).and_then(|v| v.as_object_mut()) {
                if let Some(inputs) = node.get_mut("inputs").and_then(|v| v.as_object_mut()) {
                    inputs.insert(binding.input.clone(), value);
                } else {
                    return Err(WorkflowError::MissingInput {
                        node: binding.node.clone(),
                        input: binding.input.clone(),
                    });
                }
            } else {
                return Err(WorkflowError::MissingNode { node: binding.node.clone() });
            }
        }

        Ok(workflow)
    }
}