vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Python backend factory.

#[cfg(feature = "python-renderer")]
use crate::backend::BuildContext;
#[cfg(feature = "python-renderer")]
use crate::config::model::PythonConfig;
#[cfg(feature = "python-renderer")]
use crate::renderer::{ImageRenderer, PythonRenderer};

use super::FactoryError;

#[cfg(feature = "python-renderer")]
pub fn build(
    cfg: &PythonConfig,
    ctx: &BuildContext,
) -> Result<Box<dyn ImageRenderer>, FactoryError> {
    let python_ctx = ctx.python().ok_or_else(|| FactoryError::BuildFailed {
        backend: "python".to_string(),
        source: anyhow::anyhow!("PythonContext is required for Python backend"),
    })?;

    // Extract interpreter path as Option<&str>
    // If interpreter is None, pass None to PythonRenderer::new()
    let interpreter = python_ctx
        .interpreter
        .as_deref()
        .map(|p| {
            p.to_str().ok_or_else(|| FactoryError::BuildFailed {
                backend: "python".to_string(),
                source: anyhow::anyhow!("Interpreter path contains invalid UTF-8"),
            })
        })
        .transpose()?;

    // Pure construction — no I/O, no process spawning
    let renderer = PythonRenderer::new(&cfg.script_path, interpreter);
    Ok(Box::new(renderer))
}

#[cfg(not(feature = "python-renderer"))]
pub fn build(
    _cfg: &crate::config::model::PythonConfig,
    _ctx: &crate::backend::BuildContext,
) -> Result<Box<dyn crate::renderer::ImageRenderer>, FactoryError> {
    Err(FactoryError::BuildFailed {
        backend: "python".to_string(),
        source: anyhow::anyhow!(
            "Python backend not available (feature 'python-renderer' not enabled)"
        ),
    })
}