Skip to main content

fetchium_core/plugin/
registry.rs

1//! Plugin registry — in-process store for loaded plugins.
2
3use crate::error::FetchiumError;
4use crate::plugin::manifest::PluginManifest;
5use std::collections::HashMap;
6
7/// Metadata about an installed plugin (no dynamic dispatch — pure registry).
8#[derive(Debug, Clone, serde::Serialize)]
9pub struct PluginEntry {
10    pub manifest: PluginManifest,
11    /// Absolute path to the plugin directory.
12    pub path: std::path::PathBuf,
13    pub status: PluginStatus,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
17#[serde(rename_all = "snake_case")]
18pub enum PluginStatus {
19    Loaded,
20    Disabled,
21    Error { message: String },
22}
23
24/// Central registry of all installed plugins.
25#[derive(Default)]
26pub struct PluginRegistry {
27    entries: HashMap<String, PluginEntry>,
28}
29
30impl PluginRegistry {
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Scan the plugin directory and register all found manifests.
36    pub fn discover(&mut self, plugin_dir: &std::path::Path) -> Result<usize, FetchiumError> {
37        if !plugin_dir.exists() {
38            return Ok(0);
39        }
40        let mut count = 0;
41        for entry in std::fs::read_dir(plugin_dir)? {
42            let entry = entry?;
43            let path = entry.path();
44            if path.is_dir() {
45                let manifest_path = path.join("plugin.toml");
46                if manifest_path.exists() {
47                    match PluginManifest::load(&manifest_path) {
48                        Ok(manifest) => {
49                            let name = manifest.name.clone();
50                            self.entries.insert(
51                                name,
52                                PluginEntry {
53                                    manifest,
54                                    path,
55                                    status: PluginStatus::Loaded,
56                                },
57                            );
58                            count += 1;
59                        }
60                        Err(e) => {
61                            tracing::warn!(
62                                "Failed to load plugin manifest at {:?}: {e}",
63                                manifest_path
64                            );
65                        }
66                    }
67                }
68            }
69        }
70        Ok(count)
71    }
72
73    pub fn get(&self, name: &str) -> Option<&PluginEntry> {
74        self.entries.get(name)
75    }
76
77    pub fn all(&self) -> Vec<&PluginEntry> {
78        let mut v: Vec<&PluginEntry> = self.entries.values().collect();
79        v.sort_by_key(|e| &e.manifest.name);
80        v
81    }
82
83    pub fn remove(&mut self, name: &str) -> Option<PluginEntry> {
84        self.entries.remove(name)
85    }
86
87    /// Default plugin directory: `~/.fetchium/plugins/`.
88    pub fn default_dir() -> std::path::PathBuf {
89        dirs::home_dir()
90            .unwrap_or_else(|| std::path::PathBuf::from("."))
91            .join(".fetchium")
92            .join("plugins")
93    }
94}