vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Workflow validator.

use super::error::WorkflowError;
use super::template::WorkflowTemplate;
use serde_json::Value;

const SUPPORTED_VERSIONS: &[u32] = &[1];

pub struct WorkflowValidator;

impl WorkflowValidator {
    pub fn validate(template: &WorkflowTemplate) -> Result<(), WorkflowError> {
        let metadata = template.metadata();

        // 1. Version check
        if !SUPPORTED_VERSIONS.contains(&metadata.version) {
            return Err(WorkflowError::UnsupportedVersion {
                version: metadata.version,
                supported: SUPPORTED_VERSIONS.to_vec(),
            });
        }

        // 2. Required bindings - use values() to avoid unused variable and clippy lint
        for binding in metadata.bindings.values() {
            if binding.required {
                if !Self::node_exists(template.workflow_body(), &binding.node) {
                    return Err(WorkflowError::MissingNode { node: binding.node.clone() });
                }
                if !Self::input_exists(template.workflow_body(), &binding.node, &binding.input) {
                    return Err(WorkflowError::MissingInput {
                        node: binding.node.clone(),
                        input: binding.input.clone(),
                    });
                }
            }
        }

        // 3. Output bindings - use values()
        for output in metadata.outputs.values() {
            if !Self::node_exists(template.workflow_body(), &output.node) {
                return Err(WorkflowError::MissingNode { node: output.node.clone() });
            }
        }

        Ok(())
    }

    fn node_exists(workflow: &Value, node_id: &str) -> bool {
        workflow.get(node_id).is_some()
    }

    fn input_exists(workflow: &Value, node_id: &str, input_name: &str) -> bool {
        workflow
            .get(node_id)
            .and_then(|v| v.get("inputs"))
            .and_then(|v| v.get(input_name))
            .is_some()
    }
}