use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginMetadata {
pub name: String,
pub version: String,
pub description: String,
pub author: String,
}
pub trait Command: Send + Sync {
fn name(&self) -> &str;
#[allow(dead_code)]
fn description(&self) -> &str;
fn execute(&self, args: &[String]) -> Result<()>;
}
pub struct PluginRegistry {
commands: HashMap<String, Box<dyn Command>>,
}
impl PluginRegistry {
pub fn new() -> Self {
Self {
commands: HashMap::new(),
}
}
pub fn register_command(&mut self, command: Box<dyn Command>) {
let name = command.name().to_string();
self.commands.insert(name, command);
}
pub fn get_command(&self, name: &str) -> Option<&dyn Command> {
self.commands.get(name).map(|cmd| cmd.as_ref())
}
#[allow(dead_code)]
pub fn list_commands(&self) -> Vec<&str> {
self.commands.keys().map(|s| s.as_str()).collect()
}
}
impl Default for PluginRegistry {
fn default() -> Self {
Self::new()
}
}