use anyhow::Result;
use std::fmt::Debug;
pub trait Plugin: Debug + Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn on_init(&mut self) -> Result<()> {
Ok(())
}
fn on_command(&mut self, _command: &str, _args: &[&str]) -> Result<bool> {
Ok(false)
} }
#[derive(Default)]
pub struct PluginManager {
plugins: Vec<Box<dyn Plugin>>,
}
impl PluginManager {
#[must_use]
pub fn new() -> Self {
Self {
plugins: Vec::new(),
}
}
pub fn register<P: Plugin + 'static>(&mut self, mut plugin: P) -> Result<()> {
plugin.on_init()?;
self.plugins.push(Box::new(plugin));
Ok(())
}
pub fn handle_command(&mut self, command: &str, args: &[&str]) -> Result<bool> {
for plugin in &mut self.plugins {
if plugin.on_command(command, args)? {
return Ok(true);
}
}
Ok(false)
}
}