nemo_flow_adaptive/acg/
plugin_registry.rs1use std::collections::HashMap;
14use std::sync::Arc;
15
16use crate::acg::error::{AcgError, Result};
17use crate::acg::plugin::ProviderPlugin;
18
19pub struct PluginRegistry {
24 plugins: HashMap<String, Arc<dyn ProviderPlugin>>,
25}
26
27impl PluginRegistry {
28 pub fn new() -> Self {
30 Self {
31 plugins: HashMap::new(),
32 }
33 }
34
35 pub fn register(&mut self, plugin: Arc<dyn ProviderPlugin>) -> Result<()> {
40 let id = plugin.plugin_id().to_string();
41 if self.plugins.contains_key(&id) {
42 return Err(AcgError::PluginAlreadyRegistered(id));
43 }
44 self.plugins.insert(id, plugin);
45 Ok(())
46 }
47
48 pub fn get(&self, plugin_id: &str) -> Option<Arc<dyn ProviderPlugin>> {
50 self.plugins.get(plugin_id).cloned()
51 }
52
53 pub fn list_plugin_ids(&self) -> Vec<String> {
55 let mut ids: Vec<String> = self.plugins.keys().cloned().collect();
56 ids.sort();
57 ids
58 }
59
60 pub fn deregister(&mut self, plugin_id: &str) -> bool {
62 self.plugins.remove(plugin_id).is_some()
63 }
64
65 pub fn len(&self) -> usize {
67 self.plugins.len()
68 }
69
70 pub fn is_empty(&self) -> bool {
72 self.plugins.is_empty()
73 }
74}
75
76impl Default for PluginRegistry {
77 fn default() -> Self {
78 Self::new()
79 }
80}
81
82impl std::fmt::Debug for PluginRegistry {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 f.debug_struct("PluginRegistry")
85 .field("plugin_ids", &self.list_plugin_ids())
86 .finish()
87 }
88}
89
90#[cfg(test)]
91#[path = "../../tests/unit/acg/plugin_registry_tests.rs"]
92mod tests;