ff_cli/
plugin.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct PluginMetadata {
7    pub name: String,
8    pub version: String,
9    pub description: String,
10    pub author: String,
11}
12
13pub trait Command: Send + Sync {
14    fn name(&self) -> &str;
15    #[allow(dead_code)]
16    fn description(&self) -> &str;
17    fn execute(&self, args: &[String]) -> Result<()>;
18}
19
20pub struct PluginRegistry {
21    commands: HashMap<String, Box<dyn Command>>,
22}
23
24impl PluginRegistry {
25    pub fn new() -> Self {
26        Self {
27            commands: HashMap::new(),
28        }
29    }
30
31    pub fn register_command(&mut self, command: Box<dyn Command>) {
32        let name = command.name().to_string();
33        self.commands.insert(name, command);
34    }
35
36    pub fn get_command(&self, name: &str) -> Option<&dyn Command> {
37        self.commands.get(name).map(|cmd| cmd.as_ref())
38    }
39
40    #[allow(dead_code)]
41    pub fn list_commands(&self) -> Vec<&str> {
42        self.commands.keys().map(|s| s.as_str()).collect()
43    }
44}
45
46impl Default for PluginRegistry {
47    fn default() -> Self {
48        Self::new()
49    }
50}