comment code

This commit is contained in:
MedzikUser 2022-06-05 22:10:23 +02:00
parent 969be3498b
commit 5fbb153f8e
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
5 changed files with 28 additions and 16 deletions

View File

@ -35,5 +35,5 @@ impl Command for PluginTest {
#[no_mangle] #[no_mangle]
pub fn plugin_entry(registrar: &mut dyn PluginRegistrar, command: &mut dyn CommandRegistrar) { pub fn plugin_entry(registrar: &mut dyn PluginRegistrar, command: &mut dyn CommandRegistrar) {
registrar.register_plugin(Box::new(PluginTest)); registrar.register_plugin(Box::new(PluginTest));
command.register_plugin(Box::new(PluginTest)); command.register_command(Box::new(PluginTest));
} }

View File

@ -4,6 +4,8 @@ pub use help::*;
use crate::plugins::Command; use crate::plugins::Command;
/// Register default commands
pub fn register_commands() -> Vec<Box<dyn Command>> { pub fn register_commands() -> Vec<Box<dyn Command>> {
// create Vector with Commands
vec![Box::new(CommandHelp)] vec![Box::new(CommandHelp)]
} }

View File

@ -27,6 +27,7 @@ struct Cli {
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
// init logger
SimpleLogger::new().init()?; SimpleLogger::new().init()?;
// parse cli args // parse cli args

View File

@ -19,6 +19,18 @@ pub trait Plugin: Any + Send + Sync {
async fn on_plugin_unload(&self); async fn on_plugin_unload(&self);
} }
pub trait PluginRegistrar {
/// Function to register the plugin
fn register_plugin(&mut self, plugin: Box<dyn Plugin>);
}
impl PluginRegistrar for PluginManager {
fn register_plugin(&mut self, plugin: Box<dyn Plugin>) {
self.plugins.push(plugin)
}
}
/// Plugin Manager
pub struct PluginManager { pub struct PluginManager {
pub plugins: Vec<Box<dyn Plugin>>, pub plugins: Vec<Box<dyn Plugin>>,
} }
@ -38,15 +50,7 @@ impl Default for PluginManager {
} }
} }
pub trait PluginRegistrar { pub type PluginManagerType = Arc<PluginManager>;
fn register_plugin(&mut self, plugin: Box<dyn Plugin>);
}
impl PluginRegistrar for PluginManager {
fn register_plugin(&mut self, plugin: Box<dyn Plugin>) {
self.plugins.push(plugin)
}
}
#[async_trait] #[async_trait]
pub trait Command: Any + Send + Sync { pub trait Command: Any + Send + Sync {
@ -63,12 +67,13 @@ pub trait Command: Any + Send + Sync {
); );
} }
/// Command Manager
pub struct CommandManager { pub struct CommandManager {
/// Vector with plugins
pub commands: Vec<Box<dyn Command>>, pub commands: Vec<Box<dyn Command>>,
} }
impl CommandManager { impl CommandManager {
/// Create empty `CommandManager`
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
commands: Vec::new(), commands: Vec::new(),
@ -76,25 +81,27 @@ impl CommandManager {
} }
} }
pub type CommandManagerType = Arc<CommandManager>;
impl Default for CommandManager { impl Default for CommandManager {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
pub type CommandManagerType = Arc<CommandManager>;
pub trait CommandRegistrar { pub trait CommandRegistrar {
fn register_plugin(&mut self, command: Box<dyn Command>); /// Function to register the plugin and the commands in the plugin
fn register_command(&mut self, command: Box<dyn Command>);
} }
impl CommandRegistrar for CommandManager { impl CommandRegistrar for CommandManager {
fn register_plugin(&mut self, command: Box<dyn Command>) { fn register_command(&mut self, command: Box<dyn Command>) {
self.commands.push(command) self.commands.push(command)
} }
} }
pub fn loader() -> anyhow::Result<(Arc<CommandManager>, Arc<PluginManager>)> { /// Plugins and Commands loader
pub fn loader() -> anyhow::Result<(CommandManagerType, PluginManagerType)> {
// get path to .so lib from command argument // get path to .so lib from command argument
let config_dir = "./plugins"; let config_dir = "./plugins";
let paths = fs::read_dir(config_dir)?; let paths = fs::read_dir(config_dir)?;
@ -138,5 +145,6 @@ pub fn loader() -> anyhow::Result<(Arc<CommandManager>, Arc<PluginManager>)> {
} }
} }
// return CommandManager and PluginManager
Ok((Arc::new(command_manager), Arc::new(plugin_manager))) Ok((Arc::new(command_manager), Arc::new(plugin_manager)))
} }

View File

@ -6,6 +6,7 @@ use crate::plugins::CommandManagerType;
use super::Client; use super::Client;
/// Handle Client connection
pub async fn handle_connection( pub async fn handle_connection(
mut client: Client, mut client: Client,
commands: CommandManagerType, commands: CommandManagerType,