Skip to main content

minion_engine/plugins/
registry.rs

1use std::collections::HashMap;
2
3use super::PluginStep;
4
5/// Registry that stores and retrieves plugins by name
6#[allow(dead_code)]
7pub struct PluginRegistry {
8    plugins: HashMap<String, Box<dyn PluginStep>>,
9}
10
11#[allow(dead_code)]
12impl PluginRegistry {
13    pub fn new() -> Self {
14        Self {
15            plugins: HashMap::new(),
16        }
17    }
18
19    /// Register a plugin. If a plugin with the same name already exists, it is replaced.
20    pub fn register(&mut self, plugin: Box<dyn PluginStep>) {
21        let name = plugin.name().to_string();
22        self.plugins.insert(name, plugin);
23    }
24
25    /// Look up a plugin by name
26    pub fn get(&self, name: &str) -> Option<&dyn PluginStep> {
27        self.plugins.get(name).map(|b| b.as_ref())
28    }
29
30    /// Return the number of registered plugins
31    pub fn len(&self) -> usize {
32        self.plugins.len()
33    }
34
35    /// Return true if no plugins are registered
36    pub fn is_empty(&self) -> bool {
37        self.plugins.is_empty()
38    }
39}
40
41impl Default for PluginRegistry {
42    fn default() -> Self {
43        Self::new()
44    }
45}