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 -->
## [Unreleased]
### Features
- **plugins**: add `Result<()>` in `fn execute()` (Event and Command traits)
## [0.1.0] - 2022-06-17
### Default commands

View File

@ -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(())
}
}

View File

@ -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(())
}
}

View File

@ -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(())
//! }
//! }
//!

View File

@ -4,6 +4,9 @@ use async_trait::async_trait;
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.
#[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.

View File

@ -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");
},
}
}
}
}