use crate::{DockerRuntime, HttpRuntime, LocalRuntime, RunError, RuntimeAdapter, RuntimeKind};
use std::sync::Arc;
pub struct RuntimeFactory {
default_kind: RuntimeKind,
}
impl Default for RuntimeFactory {
fn default() -> Self {
RuntimeFactory {
default_kind: RuntimeKind::Local,
}
}
}
impl RuntimeFactory {
pub fn new() -> Self {
RuntimeFactory::default()
}
pub fn with_default(kind: RuntimeKind) -> Self {
RuntimeFactory { default_kind: 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(),
}),
}
}
pub fn create_default(&self) -> Result<Arc<dyn RuntimeAdapter>, RunError> {
self.create(self.default_kind)
}
pub fn default_kind(&self) -> RuntimeKind {
self.default_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);
}
}