feat(plugins): add `Result<()>` in `fn execute()` (Event and Command traits)

This commit is contained in:
MedzikUser 2022-06-23 11:07:42 +02:00
parent 9e06f31ff2
commit ec574f3268
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
6 changed files with 57 additions and 24 deletions

View File

@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header --> <!-- next-header -->
## [Unreleased] ## [Unreleased]
### Features
- **plugins**: add `Result<()>` in `fn execute()` (Event and Command traits)
## [0.1.0] - 2022-06-17 ## [0.1.0] - 2022-06-17
### Default commands ### Default commands

View File

@ -1,6 +1,6 @@
use async_trait::async_trait; use async_trait::async_trait;
use servers::{ use servers::{
plugins::{Command, Event, Plugin, PluginManagerType, Registrar}, plugins::{Command, Event, Plugin, PluginManagerType, Registrar, Result},
tcp::Client, tcp::Client,
}; };
@ -33,8 +33,15 @@ impl Command for PluginTest {
} }
/// Command function /// Command function
async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) { async fn execute(
client.send("content").expect("send message") &self,
client: &mut Client,
_args: Vec<&str>,
_commands: &PluginManagerType,
) -> Result<()> {
client.send("content")?;
Ok(())
} }
} }
@ -47,10 +54,10 @@ impl Event for PluginTest {
} }
/// Event function /// Event function
async fn execute(&self, client: &mut Client) { async fn execute(&self, client: &mut Client) -> Result<()> {
client client.send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))?;
.send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))
.expect("send message") Ok(())
} }
} }

View File

@ -1,7 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::{ use crate::{
plugins::{Command, PluginManagerType}, plugins::{Command, PluginManagerType, Result},
tcp::Client, tcp::Client,
}; };
@ -22,11 +22,11 @@ impl Command for CommandHelp {
client: &mut Client, client: &mut Client,
_args: Vec<&str>, _args: Vec<&str>,
plugin_manager: &PluginManagerType, plugin_manager: &PluginManagerType,
) { ) -> Result<()> {
for command in plugin_manager.commands.iter() { for command in plugin_manager.commands.iter() {
client client.send(&format!("{} - {}", command.name(), command.help()))?;
.send(&format!("{} - {}", command.name(), command.help()))
.expect("send message");
} }
Ok(())
} }
} }

View File

@ -52,7 +52,7 @@
//! ```no_run //! ```no_run
//! use async_trait::async_trait; //! use async_trait::async_trait;
//! use servers::{ //! use servers::{
//! plugins::{Command, PluginManagerType, Registrar}, //! plugins::{Command, PluginManagerType, Registrar, Result},
//! tcp::Client, //! tcp::Client,
//! }; //! };
//! # //! #
@ -84,8 +84,10 @@
//! } //! }
//! //!
//! /// Command function //! /// Command function
//! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) { //! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) -> Result<()> {
//! client.send("Command executed!").expect("send message") //! client.send("Command executed!")?;
//!
//! Ok(())
//! } //! }
//! } //! }
//! //!
@ -104,7 +106,7 @@
//! ```no_run //! ```no_run
//! use async_trait::async_trait; //! use async_trait::async_trait;
//! use servers::{ //! use servers::{
//! plugins::{Event, Registrar}, //! plugins::{Event, Registrar, Result},
//! tcp::Client, //! tcp::Client,
//! }; //! };
//! # //! #
@ -131,10 +133,11 @@
//! } //! }
//! //!
//! /// Event function //! /// Event function
//! async fn execute(&self, client: &mut Client) { //! async fn execute(&self, client: &mut Client) -> Result<()> {
//! client //! client
//! .send(&format!("Welcome {}", client.stream.peer_addr().unwrap())) //! .send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))?;
//! .expect("send message") //!
//! Ok(())
//! } //! }
//! } //! }
//! //!

View File

@ -4,6 +4,9 @@ use async_trait::async_trait;
use crate::tcp::Client; use crate::tcp::Client;
/// Custom Result alias, imported from [anyhow::Result].
pub type Result<T> = anyhow::Result<T>;
/// A plugin wich allows you to add extra functionality. /// A plugin wich allows you to add extra functionality.
#[async_trait] #[async_trait]
pub trait Plugin: Any + Send + Sync { pub trait Plugin: Any + Send + Sync {
@ -27,7 +30,7 @@ pub trait Command: Any + Send + Sync {
client: &mut Client, client: &mut Client,
args: Vec<&str>, args: Vec<&str>,
plugin_manager: &PluginManagerType, plugin_manager: &PluginManagerType,
); ) -> Result<()>;
} }
/// Add a new function that will be executed when the event occurs. /// 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) /// Event name (onConnect or onSend)
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
/// Event function /// Event function
async fn execute(&self, client: &mut Client); async fn execute(&self, client: &mut Client) -> Result<()>;
} }
/// Plugin Manager with all plugins features. /// Plugin Manager with all plugins features.

View File

@ -1,6 +1,6 @@
use std::io::Write; use std::io::Write;
use log::trace; use log::{trace, error};
use crate::plugins::PluginManagerType; use crate::plugins::PluginManagerType;
@ -44,10 +44,19 @@ pub async fn handle_connection(
trace!("Executing a command `{}`", command.name()); trace!("Executing a command `{}`", command.name());
// execute command // execute command
command let out = command
.execute(&mut client, args[1..args.len()].to_vec(), &plugin_manager) .execute(&mut client, args[1..args.len()].to_vec(), &plugin_manager)
.await; .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 // don't search for more commands
break; break;
} }
@ -71,7 +80,16 @@ async fn check_event(client: &mut Client, events: &PluginManagerType, event_name
trace!("Executing a event `{}`", event.name()); trace!("Executing a event `{}`", event.name());
// execute event // 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");
},
}
} }
} }
} }