diff --git a/src/lib.rs b/src/lib.rs index 8f7a463..1aacfb4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ //! //! Go to [plugins](plugins) module +#![warn(missing_docs)] + pub mod commands; pub mod plugins; pub mod tcp; diff --git a/src/plugins/loader.rs b/src/plugins/loader.rs index 038da42..28c7aee 100644 --- a/src/plugins/loader.rs +++ b/src/plugins/loader.rs @@ -19,6 +19,7 @@ pub trait Plugin: Any + Send + Sync { async fn on_plugin_unload(&self); } +/// Trait with function to register plugin. pub trait PluginRegistrar { /// Function to register the plugin fn register(&mut self, plugin: Box); @@ -32,11 +33,12 @@ impl PluginRegistrar for PluginManager { /// Plugin Manager pub struct PluginManager { + /// Vector with all loaded plugins. pub plugins: Vec>, } impl PluginManager { - /// Create empty `PluginManager` + /// Create empty `PluginManager`. pub fn new() -> Self { Self { plugins: Vec::new(), @@ -50,8 +52,10 @@ impl Default for PluginManager { } } +/// Plugin Manager Type pub type PluginManagerType = Arc; +/// Trait with command functions to implement on struct. #[async_trait] pub trait Command: Any + Send + Sync { /// Command name @@ -69,11 +73,12 @@ pub trait Command: Any + Send + Sync { /// Command Manager pub struct CommandManager { + /// Vector with all commands. pub commands: Vec>, } impl CommandManager { - /// Create empty `CommandManager` + /// Create empty `CommandManager`. pub fn new() -> Self { Self { commands: Vec::new(), @@ -87,10 +92,12 @@ impl Default for CommandManager { } } +/// Command Manager Type pub type CommandManagerType = Arc; +/// Trait with function to register command. pub trait CommandRegistrar { - /// Function to register the plugin and the commands in the plugin + /// Function to register the plugin and the commands in the plugin. fn register(&mut self, command: Box); } @@ -100,6 +107,7 @@ impl CommandRegistrar for CommandManager { } } +/// Trait with event functions to implement on struct. #[async_trait] pub trait Event: Any + Send + Sync { /// Event name (onConnect, onSend) @@ -110,6 +118,7 @@ pub trait Event: Any + Send + Sync { /// Event Manager pub struct EventManager { + /// Vector with all events loaded from plugins. pub events: Vec>, } @@ -126,10 +135,13 @@ impl Default for EventManager { } } + +/// Event Manager Type pub type EventManagerType = Arc; +/// Trait with function to register event. pub trait EventRegistrar { - /// Function to register the plugin and the commands in the plugin + /// Function to register the plugin and the commands in the plugin. fn register(&mut self, command: Box); } diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 373fed4..4fa8ee8 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -71,7 +71,7 @@ //! } //! } //! -//! /// Register plugin and command +//! /// Register plugin and event //! #[no_mangle] //! pub fn plugin_entry( //! plugin: &mut dyn PluginRegistrar, diff --git a/src/tcp/client.rs b/src/tcp/client.rs index 4e4b832..b9ed5e1 100644 --- a/src/tcp/client.rs +++ b/src/tcp/client.rs @@ -1,5 +1,6 @@ #![allow(clippy::unused_io_amount)] +/// Max size of a TCP packet pub const MAX_PACKET_LEN: usize = 65536; use std::{ @@ -7,8 +8,9 @@ use std::{ net::TcpStream, }; -/// TCP Client stream +/// TCP Client pub struct Client { + /// TCP stream of this client pub stream: TcpStream, } @@ -18,7 +20,7 @@ impl Client { Self { stream } } - /// Read message/buffer from Client + /// Read message/buffer from client pub fn read(&mut self) -> anyhow::Result { // allocate an empty buffer let mut buf = [0; MAX_PACKET_LEN]; @@ -26,13 +28,13 @@ impl Client { // read buffer from stream self.stream.read(&mut buf)?; - // encode &[u8] to a String and replace null spaces (empty `\0` bytes) + // encode &[u8] to a String and delete null bytes (empty `\0` bytes) let decoded = String::from_utf8(buf.to_vec())?.replace('\0', ""); Ok(decoded) } - /// Send message to Client + /// Send message to client pub fn send(&mut self, content: &str) -> io::Result<()> { // add a new line at the end of the content let content = format!("{content}\n\r"); diff --git a/src/tcp/handle_connection.rs b/src/tcp/handle_connection.rs index 2af5454..b97240a 100644 --- a/src/tcp/handle_connection.rs +++ b/src/tcp/handle_connection.rs @@ -24,8 +24,16 @@ pub async fn handle_connection( // run `onSend` events from plugins check_event(&mut client, &events, "onSend").await; - // split message by whitespace - let args: &Vec<&str> = &buf.split_ascii_whitespace().collect(); + // split message by whitespaces + let args: Vec<&str> = buf.split_ascii_whitespace().collect(); + + // client sent an empty buffer + if args.is_empty() { + client.send("empty buffer").expect("send message"); + + // don't execute the following commands because it causes panic + continue + } // get command from args let cmd = args[0];