pub trait Plugin:
Send
+ Sync
+ Debug {
// Required methods
fn id(&self) -> &str;
fn metadata(&self) -> PluginMetadata;
fn initialize(&mut self, config: &PluginConfig) -> Result<()>;
fn is_compatible(&self, input_type: TypeId) -> bool;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn validate_config(&self, config: &PluginConfig) -> Result<()>;
fn cleanup(&mut self) -> Result<()>;
}Expand description
Core trait that all plugins must implement
This trait defines the fundamental interface that every plugin in the sklears ecosystem must provide. It ensures consistency across all plugin types and enables the plugin system to manage plugins in a type-safe manner.
§Examples
use sklears_core::plugin::{Plugin, PluginMetadata, PluginConfig};
use sklears_core::error::Result;
use std::any::{Any, TypeId};
#[derive(Debug)]
struct MyPlugin {
name: String,
}
impl Plugin for MyPlugin {
fn id(&self) -> &str {
&self.name
}
fn metadata(&self) -> PluginMetadata {
PluginMetadata::default()
}
fn initialize(&mut self, _config: &PluginConfig) -> Result<()> {
Ok(())
}
fn is_compatible(&self, _input_type: TypeId) -> bool {
true
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn validate_config(&self, _config: &PluginConfig) -> Result<()> {
Ok(())
}
fn cleanup(&mut self) -> Result<()> {
Ok(())
}
}Required Methods§
Sourcefn id(&self) -> &str
fn id(&self) -> &str
Unique identifier for the plugin
This should be a unique string that identifies the plugin within the system. It’s used for plugin discovery and registration.
Sourcefn metadata(&self) -> PluginMetadata
fn metadata(&self) -> PluginMetadata
Plugin metadata
Returns comprehensive metadata about the plugin including name, version, description, capabilities, and dependencies.
Sourcefn initialize(&mut self, config: &PluginConfig) -> Result<()>
fn initialize(&mut self, config: &PluginConfig) -> Result<()>
Initialize the plugin with configuration
This method is called when the plugin is loaded and provides the plugin with its configuration. The plugin should perform any necessary initialization here.
Sourcefn is_compatible(&self, input_type: TypeId) -> bool
fn is_compatible(&self, input_type: TypeId) -> bool
Check if the plugin is compatible with the given input type
This method allows the plugin system to determine if a plugin can handle a particular data type before attempting to use it.
Sourcefn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Get the plugin as Any for downcasting
This enables type-safe downcasting to the concrete plugin type when needed for specialized operations.
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Get the plugin as mutable Any for downcasting
This enables mutable access to the concrete plugin type when needed for specialized operations.
Sourcefn validate_config(&self, config: &PluginConfig) -> Result<()>
fn validate_config(&self, config: &PluginConfig) -> Result<()>
Validate plugin configuration
This method should check if the provided configuration is valid for this plugin. It’s called before initialization to catch configuration errors early.