Skip to main content

trustformers_core/plugins/
mod.rs

1//! Plugin system for TrustformeRS.
2//!
3//! This module provides a flexible plugin architecture that allows custom layers
4//! and components to be dynamically loaded and integrated into TrustformeRS models.
5//! The plugin system supports version compatibility, discovery, and runtime loading.
6//!
7//! # Overview
8//!
9//! The plugin system consists of:
10//!
11//! - [`Plugin`]: Core trait for plugin implementations
12//! - [`PluginRegistry`]: Central registry for plugin discovery and management
13//! - [`PluginInfo`]: Metadata about plugin capabilities and compatibility
14//! - [`PluginLoader`]: Dynamic loading of plugin implementations
15//!
16//! # Example
17//!
18//! ```no_run
19//! use trustformers_core::plugins::{PluginRegistry, PluginInfo, PluginManager};
20//!
21//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
22//! // Register a custom plugin
23//! let registry = PluginRegistry::new();
24//! registry.register("custom_attention", PluginInfo::new(
25//!     "custom_attention",
26//!     "1.0.0",
27//!     "Custom attention mechanism",
28//!     &["trustformers-core >= 0.1.0"]
29//! ))?;
30//!
31//! // List available plugins
32//! let plugins = registry.list_plugins();
33//! assert!(plugins.contains(&"custom_attention".to_string()));
34//! # Ok(())
35//! # }
36//! ```
37
38pub mod info;
39pub mod loader;
40pub mod registry;
41pub mod traits;
42
43pub use info::{Dependency, GpuRequirements, PluginInfo, SystemRequirements};
44pub use loader::PluginLoader;
45pub use registry::PluginRegistry;
46pub use traits::{Plugin, PluginContext, PluginEvent, PluginEventHandler};
47
48use crate::errors::Result;
49use std::collections::HashMap;
50
51/// Plugin discovery and management interface.
52///
53/// This trait provides methods for discovering available plugins,
54/// checking compatibility, and loading plugin instances.
55pub trait PluginManager {
56    /// Discovers all available plugins in the system.
57    ///
58    /// # Returns
59    ///
60    /// A map of plugin names to their information metadata.
61    fn discover_plugins(&self) -> Result<HashMap<String, PluginInfo>>;
62
63    /// Checks if a plugin is compatible with the current system.
64    ///
65    /// # Arguments
66    ///
67    /// * `name` - The name of the plugin to check
68    /// * `version` - The required version specification
69    ///
70    /// # Returns
71    ///
72    /// Returns `true` if the plugin is compatible, `false` otherwise.
73    fn is_compatible(&self, name: &str, version: &str) -> Result<bool>;
74
75    /// Loads a plugin by name.
76    ///
77    /// # Arguments
78    ///
79    /// * `name` - The name of the plugin to load
80    ///
81    /// # Returns
82    ///
83    /// Returns a boxed plugin instance ready for use.
84    fn load_plugin(&self, name: &str) -> Result<Box<dyn Plugin>>;
85
86    /// Lists all available plugin names.
87    ///
88    /// # Returns
89    ///
90    /// A vector of plugin names that can be loaded.
91    fn list_plugins(&self) -> Vec<String>;
92}