From ec574f32687bf894c70d19f2d6f5e3baf87c9c21 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Thu, 23 Jun 2022 11:07:42 +0200 Subject: [PATCH] feat(plugins): add `Result<()>` in `fn execute()` (Event and Command traits) --- CHANGELOG.md | 2 ++ plugin_test/src/lib.rs | 21 ++++++++++++++------- src/commands/help.rs | 10 +++++----- src/plugins/mod.rs | 17 ++++++++++------- src/plugins/types.rs | 7 +++++-- src/tcp/handle_connection.rs | 24 +++++++++++++++++++++--- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3f7ee..d0b6cc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Features +- **plugins**: add `Result<()>` in `fn execute()` (Event and Command traits) ## [0.1.0] - 2022-06-17 ### Default commands diff --git a/plugin_test/src/lib.rs b/plugin_test/src/lib.rs index fa5e5b5..dd447dd 100644 --- a/plugin_test/src/lib.rs +++ b/plugin_test/src/lib.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use servers::{ - plugins::{Command, Event, Plugin, PluginManagerType, Registrar}, + plugins::{Command, Event, Plugin, PluginManagerType, Registrar, Result}, tcp::Client, }; @@ -33,8 +33,15 @@ impl Command for PluginTest { } /// Command function - async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) { - client.send("content").expect("send message") + async fn execute( + &self, + client: &mut Client, + _args: Vec<&str>, + _commands: &PluginManagerType, + ) -> Result<()> { + client.send("content")?; + + Ok(()) } } @@ -47,10 +54,10 @@ impl Event for PluginTest { } /// Event function - async fn execute(&self, client: &mut Client) { - client - .send(&format!("Welcome {}", client.stream.peer_addr().unwrap())) - .expect("send message") + async fn execute(&self, client: &mut Client) -> Result<()> { + client.send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))?; + + Ok(()) } } diff --git a/src/commands/help.rs b/src/commands/help.rs index 9ebc0ff..f3fae59 100644 --- a/src/commands/help.rs +++ b/src/commands/help.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use crate::{ - plugins::{Command, PluginManagerType}, + plugins::{Command, PluginManagerType, Result}, tcp::Client, }; @@ -22,11 +22,11 @@ impl Command for CommandHelp { client: &mut Client, _args: Vec<&str>, plugin_manager: &PluginManagerType, - ) { + ) -> Result<()> { for command in plugin_manager.commands.iter() { - client - .send(&format!("{} - {}", command.name(), command.help())) - .expect("send message"); + client.send(&format!("{} - {}", command.name(), command.help()))?; } + + Ok(()) } } diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 21224da..a5253c0 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -52,7 +52,7 @@ //! ```no_run //! use async_trait::async_trait; //! use servers::{ -//! plugins::{Command, PluginManagerType, Registrar}, +//! plugins::{Command, PluginManagerType, Registrar, Result}, //! tcp::Client, //! }; //! # @@ -84,8 +84,10 @@ //! } //! //! /// Command function -//! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) { -//! client.send("Command executed!").expect("send message") +//! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) -> Result<()> { +//! client.send("Command executed!")?; +//! +//! Ok(()) //! } //! } //! @@ -104,7 +106,7 @@ //! ```no_run //! use async_trait::async_trait; //! use servers::{ -//! plugins::{Event, Registrar}, +//! plugins::{Event, Registrar, Result}, //! tcp::Client, //! }; //! # @@ -131,10 +133,11 @@ //! } //! //! /// Event function -//! async fn execute(&self, client: &mut Client) { +//! async fn execute(&self, client: &mut Client) -> Result<()> { //! client -//! .send(&format!("Welcome {}", client.stream.peer_addr().unwrap())) -//! .expect("send message") +//! .send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))?; +//! +//! Ok(()) //! } //! } //! diff --git a/src/plugins/types.rs b/src/plugins/types.rs index c8bc5b9..0908b77 100644 --- a/src/plugins/types.rs +++ b/src/plugins/types.rs @@ -4,6 +4,9 @@ use async_trait::async_trait; use crate::tcp::Client; +/// Custom Result alias, imported from [anyhow::Result]. +pub type Result = anyhow::Result; + /// A plugin wich allows you to add extra functionality. #[async_trait] pub trait Plugin: Any + Send + Sync { @@ -27,7 +30,7 @@ pub trait Command: Any + Send + Sync { client: &mut Client, args: Vec<&str>, plugin_manager: &PluginManagerType, - ); + ) -> Result<()>; } /// Add a new function that will be executed when the event occurs. @@ -36,7 +39,7 @@ pub trait Event: Any + Send + Sync { /// Event name (onConnect or onSend) fn name(&self) -> &'static str; /// Event function - async fn execute(&self, client: &mut Client); + async fn execute(&self, client: &mut Client) -> Result<()>; } /// Plugin Manager with all plugins features. diff --git a/src/tcp/handle_connection.rs b/src/tcp/handle_connection.rs index 089b7d7..dd13d38 100644 --- a/src/tcp/handle_connection.rs +++ b/src/tcp/handle_connection.rs @@ -1,6 +1,6 @@ use std::io::Write; -use log::trace; +use log::{trace, error}; use crate::plugins::PluginManagerType; @@ -44,10 +44,19 @@ pub async fn handle_connection( trace!("Executing a command `{}`", command.name()); // execute command - command + let out = command .execute(&mut client, args[1..args.len()].to_vec(), &plugin_manager) .await; + match out { + Ok(_) => (), + Err(err) => { + error!("failed to execute command `{cmd}`, error message = `{err}`"); + + client.send(&format!("error: {err}")).expect("send message to client"); + }, + } + // don't search for more commands break; } @@ -71,7 +80,16 @@ async fn check_event(client: &mut Client, events: &PluginManagerType, event_name trace!("Executing a event `{}`", event.name()); // execute event - event.execute(client).await; + let out = event.execute(client).await; + + match out { + Ok(_) => (), + Err(err) => { + error!("failed to execute event `{event_name}`, error message = `{err}`"); + + client.send(&format!("error: {err}")).expect("send message to client"); + }, + } } } }