bzzz-core 0.1.0

Bzzz core library - Declarative orchestration engine for AI Agents
Documentation
//! Runtime Factory
//!
//! Creates runtime adapters based on RuntimeKind.

use crate::{DockerRuntime, HttpRuntime, LocalRuntime, RunError, RuntimeAdapter, RuntimeKind};
use std::sync::Arc;

/// Factory for creating runtime adapters
pub struct RuntimeFactory {
    /// Default runtime kind when not specified
    default_kind: RuntimeKind,
}

impl Default for RuntimeFactory {
    fn default() -> Self {
        RuntimeFactory {
            default_kind: RuntimeKind::Local,
        }
    }
}

impl RuntimeFactory {
    /// Create a new runtime factory with Local as default
    pub fn new() -> Self {
        RuntimeFactory::default()
    }

    /// Create a runtime factory with a specific default
    pub fn with_default(kind: RuntimeKind) -> Self {
        RuntimeFactory { default_kind: kind }
    }

    /// Create a runtime adapter for the given kind
    pub fn create(&self, kind: RuntimeKind) -> Result<Arc<dyn RuntimeAdapter>, RunError> {
        match kind {
            RuntimeKind::Local => Ok(Arc::new(LocalRuntime::new())),
            RuntimeKind::Docker => Ok(Arc::new(DockerRuntime::new())),
            RuntimeKind::Http => Ok(Arc::new(HttpRuntime::new())),
            RuntimeKind::Cloud => Err(RunError::InvalidConfig {
                message: "Cloud runtime not yet implemented".into(),
            }),
        }
    }

    /// Create the default runtime adapter
    pub fn create_default(&self) -> Result<Arc<dyn RuntimeAdapter>, RunError> {
        self.create(self.default_kind)
    }

    /// Get the default runtime kind
    pub fn default_kind(&self) -> RuntimeKind {
        self.default_kind
    }
}

/// Convenience function to create a runtime for the given kind
pub fn create_runtime(kind: RuntimeKind) -> Result<Arc<dyn RuntimeAdapter>, RunError> {
    RuntimeFactory::new().create(kind)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_factory_creation() {
        let factory = RuntimeFactory::new();
        assert_eq!(factory.default_kind(), RuntimeKind::Local);
    }

    #[test]
    fn test_factory_with_default() {
        let factory = RuntimeFactory::with_default(RuntimeKind::Docker);
        assert_eq!(factory.default_kind(), RuntimeKind::Docker);
    }

    #[tokio::test]
    async fn test_create_local_runtime() {
        let factory = RuntimeFactory::new();
        let runtime = factory.create(RuntimeKind::Local).unwrap();
        assert_eq!(runtime.kind(), RuntimeKind::Local);
    }

    #[tokio::test]
    async fn test_create_docker_runtime() {
        let factory = RuntimeFactory::new();
        let runtime = factory.create(RuntimeKind::Docker).unwrap();
        assert_eq!(runtime.kind(), RuntimeKind::Docker);
    }

    #[tokio::test]
    async fn test_create_http_runtime() {
        let factory = RuntimeFactory::new();
        let runtime = factory.create(RuntimeKind::Http).unwrap();
        assert_eq!(runtime.kind(), RuntimeKind::Http);
    }

    #[tokio::test]
    async fn test_create_default_runtime() {
        let factory = RuntimeFactory::new();
        let runtime = factory.create_default().unwrap();
        assert_eq!(runtime.kind(), RuntimeKind::Local);
    }
}