trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Plugin registry for discovery and management.

use crate::errors::{Result, TrustformersError};
use crate::plugins::{Plugin, PluginInfo, PluginLoader, PluginManager};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

/// Central registry for plugin discovery and management.
///
/// The `PluginRegistry` maintains a catalog of available plugins,
/// handles plugin loading and unloading, and provides compatibility
/// checking and version management.
///
/// # Thread Safety
///
/// The registry is thread-safe and can be shared across multiple threads.
/// It uses read-write locks to allow concurrent reads while ensuring
/// exclusive access for modifications.
///
/// # Example
///
/// ```no_run
/// use trustformers_core::plugins::{PluginRegistry, PluginInfo, PluginManager};
/// use std::path::Path;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let registry = PluginRegistry::new();
///
/// // Register a plugin
/// let info = PluginInfo::new(
///     "custom_attention",
///     "1.0.0",
///     "Custom attention mechanism",
///     &["trustformers-core >= 0.1.0"]
/// );
/// registry.register("custom_attention", info)?;
///
/// // List all available plugins
/// let plugins = registry.list_plugins();
/// assert!(plugins.contains(&"custom_attention".to_string()));
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct PluginRegistry {
    /// Plugin information cache
    plugins: Arc<RwLock<HashMap<String, PluginInfo>>>,
    /// Loaded plugin instances
    loaded: Arc<RwLock<HashMap<String, Box<dyn Plugin>>>>,
    /// Plugin search paths
    search_paths: Arc<RwLock<Vec<PathBuf>>>,
    /// Plugin loader
    loader: Arc<PluginLoader>,
    /// Registry configuration
    config: RegistryConfig,
}

impl PluginRegistry {
    /// Creates a new plugin registry.
    ///
    /// # Returns
    ///
    /// A new registry instance with default configuration.
    pub fn new() -> Self {
        Self {
            plugins: Arc::new(RwLock::new(HashMap::new())),
            loaded: Arc::new(RwLock::new(HashMap::new())),
            search_paths: Arc::new(RwLock::new(Vec::new())),
            loader: Arc::new(PluginLoader::new()),
            config: RegistryConfig::default(),
        }
    }

    /// Creates a new plugin registry with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Registry configuration
    ///
    /// # Returns
    ///
    /// A new registry instance.
    pub fn with_config(config: RegistryConfig) -> Self {
        Self {
            plugins: Arc::new(RwLock::new(HashMap::new())),
            loaded: Arc::new(RwLock::new(HashMap::new())),
            search_paths: Arc::new(RwLock::new(Vec::new())),
            loader: Arc::new(PluginLoader::new()),
            config,
        }
    }

    /// Registers a plugin in the registry.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name (must be unique)
    /// * `info` - Plugin metadata
    ///
    /// # Returns
    ///
    /// `Ok(())` on success, error if registration fails.
    ///
    /// # Errors
    ///
    /// - Plugin name already exists
    /// - Invalid plugin information
    pub fn register(&self, name: &str, info: PluginInfo) -> Result<()> {
        info.validate()?;

        let mut plugins = self.plugins.write().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire write lock".to_string())
        })?;

        if plugins.contains_key(name) {
            return Err(TrustformersError::plugin_error(format!(
                "Plugin '{}' is already registered",
                name
            )));
        }

        plugins.insert(name.to_string(), info);
        Ok(())
    }

    /// Unregisters a plugin from the registry.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name to unregister
    ///
    /// # Returns
    ///
    /// `Ok(())` on success, error if the plugin is not found.
    pub fn unregister(&self, name: &str) -> Result<()> {
        // Unload the plugin if it's currently loaded
        if self.is_loaded(name) {
            self.unload_plugin(name)?;
        }

        let mut plugins = self.plugins.write().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire write lock".to_string())
        })?;

        plugins.remove(name).ok_or_else(|| {
            TrustformersError::plugin_error(format!("Plugin '{}' not found", name))
        })?;

        Ok(())
    }

    /// Gets information about a registered plugin.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name
    ///
    /// # Returns
    ///
    /// Plugin information if found.
    pub fn get_plugin_info(&self, name: &str) -> Result<PluginInfo> {
        let plugins = self.plugins.read().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire read lock".to_string())
        })?;

        plugins
            .get(name)
            .cloned()
            .ok_or_else(|| TrustformersError::plugin_error(format!("Plugin '{}' not found", name)))
    }

    /// Checks if a plugin is currently loaded.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name
    ///
    /// # Returns
    ///
    /// `true` if the plugin is loaded, `false` otherwise.
    pub fn is_loaded(&self, name: &str) -> bool {
        self.loaded.read().map(|loaded| loaded.contains_key(name)).unwrap_or(false)
    }

    /// Unloads a plugin from memory.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name to unload
    ///
    /// # Returns
    ///
    /// `Ok(())` on success.
    pub fn unload_plugin(&self, name: &str) -> Result<()> {
        let mut loaded = self.loaded.write().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire write lock".to_string())
        })?;

        if let Some(mut plugin) = loaded.remove(name) {
            plugin.cleanup()?;
        }

        Ok(())
    }

    /// Adds a directory to the plugin search path.
    ///
    /// # Arguments
    ///
    /// * `path` - Directory path to add
    pub fn add_search_path<P: AsRef<Path>>(&self, path: P) {
        if let Ok(mut paths) = self.search_paths.write() {
            paths.push(path.as_ref().to_path_buf());
        }
    }

    /// Removes a directory from the plugin search path.
    ///
    /// # Arguments
    ///
    /// * `path` - Directory path to remove
    pub fn remove_search_path<P: AsRef<Path>>(&self, path: P) {
        if let Ok(mut paths) = self.search_paths.write() {
            paths.retain(|p| p != path.as_ref());
        }
    }

    /// Scans all search paths for plugins and registers them.
    ///
    /// # Returns
    ///
    /// Number of plugins discovered and registered.
    pub fn scan_for_plugins(&self) -> Result<usize> {
        let search_paths = self
            .search_paths
            .read()
            .map_err(|_| TrustformersError::lock_error("Failed to acquire read lock".to_string()))?
            .clone();

        let mut count = 0;

        for path in &search_paths {
            if let Ok(entries) = std::fs::read_dir(path) {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.is_file() && self.is_plugin_file(&path) {
                        if let Ok(info) = self.loader.load_plugin_info(&path) {
                            let name = info.name().to_string();
                            if self.register(&name, info).is_ok() {
                                count += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok(count)
    }

    /// Checks if a file is a plugin file based on extension.
    ///
    /// # Arguments
    ///
    /// * `path` - File path to check
    ///
    /// # Returns
    ///
    /// `true` if it's a plugin file.
    fn is_plugin_file(&self, path: &Path) -> bool {
        if let Some(ext) = path.extension() {
            let ext = ext.to_string_lossy().to_lowercase();
            matches!(ext.as_str(), "so" | "dll" | "dylib" | "wasm")
        } else {
            false
        }
    }

    /// Validates plugin dependencies.
    ///
    /// # Arguments
    ///
    /// * `name` - Plugin name to validate
    ///
    /// # Returns
    ///
    /// `Ok(())` if all dependencies are satisfied.
    pub fn validate_dependencies(&self, name: &str) -> Result<()> {
        let info = self.get_plugin_info(name)?;

        for dep in info.dependencies() {
            if !dep.optional {
                let dep_info = self.get_plugin_info(&dep.name)?;
                if !dep.requirement.matches(dep_info.version()) {
                    return Err(TrustformersError::plugin_error(format!(
                        "Plugin '{}' requires '{}' {} but found {}",
                        name,
                        dep.name,
                        dep.requirement,
                        dep_info.version()
                    )));
                }
            }
        }

        Ok(())
    }

    /// Exports the registry to a configuration file.
    ///
    /// # Arguments
    ///
    /// * `path` - File path to write the configuration
    ///
    /// # Returns
    ///
    /// `Ok(())` on success.
    pub fn export_config<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let plugins = self.plugins.read().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire read lock".to_string())
        })?;

        let config = RegistryConfig {
            plugins: plugins.clone(),
            ..self.config.clone()
        };

        let json = serde_json::to_string_pretty(&config)
            .map_err(|e| TrustformersError::serialization_error(e.to_string()))?;

        std::fs::write(path, json).map_err(|e| TrustformersError::io_error(e.to_string()))?;

        Ok(())
    }

    /// Imports registry configuration from a file.
    ///
    /// # Arguments
    ///
    /// * `path` - File path to read the configuration from
    ///
    /// # Returns
    ///
    /// `Ok(())` on success.
    pub fn import_config<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let json = std::fs::read_to_string(path)
            .map_err(|e| TrustformersError::io_error(e.to_string()))?;

        let config: RegistryConfig = serde_json::from_str(&json)
            .map_err(|e| TrustformersError::serialization_error(e.to_string()))?;

        let mut plugins = self.plugins.write().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire write lock".to_string())
        })?;

        for (name, info) in config.plugins {
            plugins.insert(name, info);
        }

        Ok(())
    }

    /// Gets registry statistics.
    ///
    /// # Returns
    ///
    /// Registry statistics.
    pub fn stats(&self) -> Result<RegistryStats> {
        let plugins = self.plugins.read().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire read lock".to_string())
        })?;
        let loaded = self.loaded.read().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire read lock".to_string())
        })?;

        Ok(RegistryStats {
            total_plugins: plugins.len(),
            loaded_plugins: loaded.len(),
            search_paths: self.search_paths.read().map(|paths| paths.len()).unwrap_or(0),
        })
    }
}

impl PluginManager for PluginRegistry {
    fn discover_plugins(&self) -> Result<HashMap<String, PluginInfo>> {
        let plugins = self.plugins.read().map_err(|_| {
            TrustformersError::lock_error("Failed to acquire read lock".to_string())
        })?;
        Ok(plugins.clone())
    }

    fn is_compatible(&self, name: &str, version: &str) -> Result<bool> {
        let info = self.get_plugin_info(name)?;
        Ok(info.is_compatible_with("trustformers-core", version))
    }

    fn load_plugin(&self, name: &str) -> Result<Box<dyn Plugin>> {
        // Check if already loaded
        {
            let loaded = self.loaded.read().map_err(|_| {
                TrustformersError::lock_error("Failed to acquire read lock".to_string())
            })?;
            if let Some(plugin) = loaded.get(name) {
                return Ok(plugin.clone());
            }
        }

        // Validate dependencies
        self.validate_dependencies(name)?;

        // Get plugin info
        let info = self.get_plugin_info(name)?;

        // Load the plugin
        let mut plugin = self.loader.load_plugin(&info)?;
        plugin.initialize()?;

        // Store in loaded cache
        let plugin_clone = plugin.clone();
        {
            let mut loaded = self.loaded.write().map_err(|_| {
                TrustformersError::lock_error("Failed to acquire write lock".to_string())
            })?;
            loaded.insert(name.to_string(), plugin);
        }

        Ok(plugin_clone)
    }

    fn list_plugins(&self) -> Vec<String> {
        self.plugins
            .read()
            .map(|plugins| plugins.keys().cloned().collect())
            .unwrap_or_default()
    }
}

impl Default for PluginRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Registry configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryConfig {
    /// Registered plugins
    #[serde(default)]
    pub plugins: HashMap<String, PluginInfo>,
    /// Maximum number of loaded plugins
    #[serde(default = "default_max_loaded")]
    pub max_loaded_plugins: usize,
    /// Enable automatic plugin discovery
    #[serde(default = "default_auto_discovery")]
    pub auto_discovery: bool,
    /// Plugin cache directory
    #[serde(default)]
    pub cache_dir: Option<PathBuf>,
    /// Plugin load timeout in seconds
    #[serde(default = "default_load_timeout")]
    pub load_timeout_secs: u64,
}

impl Default for RegistryConfig {
    fn default() -> Self {
        Self {
            plugins: HashMap::new(),
            max_loaded_plugins: default_max_loaded(),
            auto_discovery: default_auto_discovery(),
            cache_dir: None,
            load_timeout_secs: default_load_timeout(),
        }
    }
}

fn default_max_loaded() -> usize {
    100
}
fn default_auto_discovery() -> bool {
    true
}
fn default_load_timeout() -> u64 {
    30
}

/// Registry statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryStats {
    /// Total number of registered plugins
    pub total_plugins: usize,
    /// Number of currently loaded plugins
    pub loaded_plugins: usize,
    /// Number of search paths
    pub search_paths: usize,
}