vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! BuildContext — dependency container for backend construction.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

#[cfg(feature = "comfy-renderer")]
use crate::services::comfy::ValidatedWorkflowTemplate;

// ============================================================
// ComfyContext (M2.8B)
// ============================================================

#[cfg(feature = "comfy-renderer")]
#[derive(Clone)]
pub struct ComfyContext {
    pub workflow: Arc<ValidatedWorkflowTemplate>,
    pub runtime_handle: tokio::runtime::Handle,
}

#[cfg(feature = "comfy-renderer")]
impl ComfyContext {
    pub fn new(
        workflow: ValidatedWorkflowTemplate,
        runtime_handle: tokio::runtime::Handle,
    ) -> Self {
        Self { workflow: Arc::new(workflow), runtime_handle }
    }
}

// ============================================================
// PythonContext (M2.8C.1 — diperbaiki untuk menerima Option)
// ============================================================

/// Context for Python backend.
/// Contains only external resources, not constructed services.
#[derive(Clone)]
pub struct PythonContext {
    /// Path to the Python interpreter (if explicitly specified).
    pub interpreter: Option<Arc<PathBuf>>,

    /// Root directory for Python scripts (if explicitly specified).
    pub script_root: Option<Arc<PathBuf>>,

    /// Environment variables for the Python process (if any).
    pub environment: Option<Arc<HashMap<String, String>>>,
}

impl PythonContext {
    pub fn new(
        interpreter: Option<PathBuf>,
        script_root: Option<PathBuf>,
        environment: Option<HashMap<String, String>>,
    ) -> Self {
        Self {
            interpreter: interpreter.map(Arc::new),
            script_root: script_root.map(Arc::new),
            environment: environment.map(Arc::new),
        }
    }
}

// ============================================================
// BuildContext & Builder
// ============================================================

#[derive(Clone, Default)]
pub struct BuildContext {
    #[cfg(feature = "comfy-renderer")]
    comfy: Option<ComfyContext>,

    python: Option<PythonContext>,
    // Future: add other backend contexts here (invoke, local, etc.)
}

impl BuildContext {
    pub fn builder() -> BuildContextBuilder {
        BuildContextBuilder::default()
    }

    #[cfg(feature = "comfy-renderer")]
    pub fn comfy(&self) -> Option<&ComfyContext> {
        self.comfy.as_ref()
    }

    pub fn python(&self) -> Option<&PythonContext> {
        self.python.as_ref()
    }
}

#[derive(Default)]
pub struct BuildContextBuilder {
    #[cfg(feature = "comfy-renderer")]
    comfy: Option<ComfyContext>,

    python: Option<PythonContext>,
}

impl BuildContextBuilder {
    #[cfg(feature = "comfy-renderer")]
    pub fn with_comfy(mut self, ctx: ComfyContext) -> Self {
        self.comfy = Some(ctx);
        self
    }

    pub fn with_python(mut self, ctx: PythonContext) -> Self {
        self.python = Some(ctx);
        self
    }

    pub fn build(self) -> BuildContext {
        BuildContext {
            #[cfg(feature = "comfy-renderer")]
            comfy: self.comfy,
            python: self.python,
        }
    }
}