vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Image renderer abstraction.

mod constants;
mod dummy;
mod python;

#[cfg(feature = "comfy-renderer")]
mod comfyui;

// Ekspor konstanta publik
pub use constants::OUTPUT_FILENAME;

// Implementasi renderer tetap publik agar dapat diuji dan diekstensi
pub use dummy::DummyRenderer;
pub use python::PythonRenderer;

#[cfg(feature = "comfy-renderer")]
pub use comfyui::ComfyUIRenderer;

// PythonConfig dikonsolidasi dari config::model
pub use crate::config::model::PythonConfig;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::{Path, PathBuf};
use vc_shared::hashing::Sha256Digest; // <--- TAMBAHKAN PathBuf

// ============================================================
// RendererKind
// ============================================================
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RendererKind {
    Dummy,
    Python,
    Comfy,
}

impl fmt::Display for RendererKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RendererKind::Dummy => write!(f, "dummy"),
            RendererKind::Python => write!(f, "python"),
            RendererKind::Comfy => write!(f, "comfy"),
        }
    }
}

// ============================================================
// RenderOutput
// ============================================================
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct RenderOutput {
    pub image_path: PathBuf,
    pub image_hash: Sha256Digest,
    pub duration_ms: u64,
    pub backend: RendererKind,
    pub seed: u64,
}

// ============================================================
// RenderParams
// ============================================================
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct RenderParams {
    pub width: u32,
    pub height: u32,
    pub steps: u32,
    pub cfg: f64,
    pub backend_options: serde_json::Value,
}

impl RenderParams {
    /// Create new RenderParams with default backend_options.
    pub fn new(width: u32, height: u32, steps: u32, cfg: f64) -> Self {
        Self {
            width,
            height,
            steps,
            cfg,
            backend_options: serde_json::Value::Object(serde_json::Map::new()),
        }
    }

    /// Set custom backend options.
    pub fn with_backend_options(mut self, options: serde_json::Value) -> Self {
        self.backend_options = options;
        self
    }
}

impl Default for RenderParams {
    fn default() -> Self {
        Self::new(1024, 1024, 30, 7.0)
    }
}

// ============================================================
// ImageRenderer Trait
// ============================================================
pub trait ImageRenderer: Send + Sync {
    fn render(
        &self,
        prompt: &str,
        negative_prompt: &str,
        seed: u64,
        output_dir: &Path,
        params: &RenderParams,
    ) -> Result<RenderOutput>;
}