diff --git a/plugin_test/src/lib.rs b/plugin_test/src/lib.rs index a7412e5..c13a3fe 100644 --- a/plugin_test/src/lib.rs +++ b/plugin_test/src/lib.rs @@ -35,5 +35,5 @@ impl Command for PluginTest { #[no_mangle] pub fn plugin_entry(registrar: &mut dyn PluginRegistrar, command: &mut dyn CommandRegistrar) { registrar.register_plugin(Box::new(PluginTest)); - command.register_plugin(Box::new(PluginTest)); + command.register_command(Box::new(PluginTest)); } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3f47a22..b5c4349 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,6 +4,8 @@ pub use help::*; use crate::plugins::Command; +/// Register default commands pub fn register_commands() -> Vec> { + // create Vector with Commands vec![Box::new(CommandHelp)] } diff --git a/src/main.rs b/src/main.rs index cf4a6be..a556f54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ struct Cli { } fn main() -> anyhow::Result<()> { + // init logger SimpleLogger::new().init()?; // parse cli args diff --git a/src/plugins/loader.rs b/src/plugins/loader.rs index 49601a1..76cbe8f 100644 --- a/src/plugins/loader.rs +++ b/src/plugins/loader.rs @@ -19,6 +19,18 @@ pub trait Plugin: Any + Send + Sync { async fn on_plugin_unload(&self); } +pub trait PluginRegistrar { + /// Function to register the plugin + fn register_plugin(&mut self, plugin: Box); +} + +impl PluginRegistrar for PluginManager { + fn register_plugin(&mut self, plugin: Box) { + self.plugins.push(plugin) + } +} + +/// Plugin Manager pub struct PluginManager { pub plugins: Vec>, } @@ -38,15 +50,7 @@ impl Default for PluginManager { } } -pub trait PluginRegistrar { - fn register_plugin(&mut self, plugin: Box); -} - -impl PluginRegistrar for PluginManager { - fn register_plugin(&mut self, plugin: Box) { - self.plugins.push(plugin) - } -} +pub type PluginManagerType = Arc; #[async_trait] pub trait Command: Any + Send + Sync { @@ -63,12 +67,13 @@ pub trait Command: Any + Send + Sync { ); } +/// Command Manager pub struct CommandManager { - /// Vector with plugins pub commands: Vec>, } impl CommandManager { + /// Create empty `CommandManager` pub fn new() -> Self { Self { commands: Vec::new(), @@ -76,25 +81,27 @@ impl CommandManager { } } -pub type CommandManagerType = Arc; - impl Default for CommandManager { fn default() -> Self { Self::new() } } +pub type CommandManagerType = Arc; + pub trait CommandRegistrar { - fn register_plugin(&mut self, command: Box); + /// Function to register the plugin and the commands in the plugin + fn register_command(&mut self, command: Box); } impl CommandRegistrar for CommandManager { - fn register_plugin(&mut self, command: Box) { + fn register_command(&mut self, command: Box) { self.commands.push(command) } } -pub fn loader() -> anyhow::Result<(Arc, Arc)> { +/// Plugins and Commands loader +pub fn loader() -> anyhow::Result<(CommandManagerType, PluginManagerType)> { // get path to .so lib from command argument let config_dir = "./plugins"; let paths = fs::read_dir(config_dir)?; @@ -138,5 +145,6 @@ pub fn loader() -> anyhow::Result<(Arc, Arc)> { } } + // return CommandManager and PluginManager Ok((Arc::new(command_manager), Arc::new(plugin_manager))) } diff --git a/src/tcp/handle_connection.rs b/src/tcp/handle_connection.rs index 2d08710..2b33c06 100644 --- a/src/tcp/handle_connection.rs +++ b/src/tcp/handle_connection.rs @@ -6,6 +6,7 @@ use crate::plugins::CommandManagerType; use super::Client; +/// Handle Client connection pub async fn handle_connection( mut client: Client, commands: CommandManagerType,