use super::core_traits::Plugin;
use super::types_config::{LogLevel, PluginConfig, PluginMetadata, PluginParameter};
use crate::error::Result;
pub trait PluginFactory: Send + Sync {
fn create_plugin(&self, config: &PluginConfig) -> Result<Box<dyn Plugin>>;
fn metadata(&self) -> PluginMetadata;
fn validate_config(&self, config: &PluginConfig) -> Result<()>;
fn default_config(&self) -> PluginConfig {
PluginConfig::default()
}
fn config_schema(&self) -> std::collections::HashMap<String, String> {
std::collections::HashMap::new()
}
}
#[derive(Debug, Clone)]
pub struct PluginConfigBuilder {
config: PluginConfig,
}
impl PluginConfigBuilder {
pub fn new() -> Self {
Self {
config: PluginConfig::default(),
}
}
pub fn with_parameter(mut self, key: &str, value: PluginParameter) -> Self {
self.config.parameters.insert(key.to_string(), value);
self
}
pub fn with_parameters(
mut self,
params: std::collections::HashMap<String, PluginParameter>,
) -> Self {
self.config.parameters.extend(params);
self
}
pub fn with_threads(mut self, threads: usize) -> Self {
self.config.runtime_settings.num_threads = Some(threads);
self
}
pub fn with_memory_limit(mut self, limit: usize) -> Self {
self.config.runtime_settings.memory_limit = Some(limit);
self
}
pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
self.config.runtime_settings.timeout_ms = Some(timeout_ms);
self
}
pub fn with_gpu(mut self, use_gpu: bool) -> Self {
self.config.runtime_settings.use_gpu = use_gpu;
self
}
pub fn with_log_level(mut self, level: LogLevel) -> Self {
self.config.runtime_settings.log_level = level;
self
}
pub fn with_setting(mut self, key: &str, value: &str) -> Self {
self.config
.plugin_settings
.insert(key.to_string(), value.to_string());
self
}
pub fn with_settings(mut self, settings: std::collections::HashMap<String, String>) -> Self {
self.config.plugin_settings.extend(settings);
self
}
pub fn build(self) -> PluginConfig {
self.config
}
pub fn config(&self) -> &PluginConfig {
&self.config
}
pub fn validate(&self) -> Result<()> {
if self.config.runtime_settings.num_threads == Some(0) {
return Err(crate::error::SklearsError::InvalidOperation(
"Number of threads cannot be zero".to_string(),
));
}
if let Some(timeout) = self.config.runtime_settings.timeout_ms {
if timeout == 0 {
return Err(crate::error::SklearsError::InvalidOperation(
"Timeout cannot be zero".to_string(),
));
}
}
Ok(())
}
pub fn clone_builder(&self) -> Self {
self.clone()
}
}
impl Default for PluginConfigBuilder {
fn default() -> Self {
Self::new()
}
}
impl PluginConfigBuilder {
pub fn cpu_optimized() -> Self {
Self::new()
.with_threads(num_cpus::get())
.with_gpu(false)
.with_log_level(LogLevel::Info)
}
pub fn gpu_optimized() -> Self {
Self::new()
.with_gpu(true)
.with_threads(2) .with_log_level(LogLevel::Info)
}
pub fn development() -> Self {
Self::new()
.with_log_level(LogLevel::Debug)
.with_timeout(300000) .with_threads(1) }
pub fn production() -> Self {
Self::new()
.with_log_level(LogLevel::Warn)
.with_timeout(30000) .with_memory_limit(4 * 1024 * 1024 * 1024) .with_threads(num_cpus::get())
}
}