doc: update

This commit is contained in:
MedzikUser 2022-06-21 12:33:13 +02:00
parent d4f7fb650b
commit b9af11c34e
No known key found for this signature in database
GPG key ID: A5FAC1E185C112DB
4 changed files with 66 additions and 42 deletions

View file

@ -6,7 +6,7 @@ use servers::{
struct PluginTest; struct PluginTest;
/// Create a new plugin. /// Create a new plugin
#[async_trait] #[async_trait]
impl Plugin for PluginTest { impl Plugin for PluginTest {
/// Name of the plugin. /// Name of the plugin.
@ -19,7 +19,7 @@ impl Plugin for PluginTest {
async fn on_plugin_load(&self) {} async fn on_plugin_load(&self) {}
} }
/// Create a new command. /// Create a new command
#[async_trait] #[async_trait]
impl Command for PluginTest { impl Command for PluginTest {
/// Command name /// Command name

View file

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

View file

@ -1,6 +1,6 @@
//! Plugins loader //! Plugins loader
//! //!
//! ## Writing plugins //! # Writing a plugins
//! //!
//! Create a new project `cargo new --lib plugin` //! Create a new project `cargo new --lib plugin`
//! //!
@ -19,20 +19,15 @@
//! servers = "0.1.0" //! servers = "0.1.0"
//! ``` //! ```
//! //!
//! ### Command plugin
//!
//! In file `src/lib.rs` //! In file `src/lib.rs`
//! //!
//! ```no_run //! ```no_run
//! use async_trait::async_trait; //! use async_trait::async_trait;
//! use servers::{ //! use servers::{plugins::{Plugin, Registrar}, tcp::Client};
//! plugins::{Command, Plugin, PluginManagerType, Registrar},
//! tcp::Client,
//! };
//! //!
//! struct PluginTest; //! struct PluginTest;
//! //!
//! /// Create a new plugin. //! /// Create a new plugin
//! #[async_trait] //! #[async_trait]
//! impl Plugin for PluginTest { //! impl Plugin for PluginTest {
//! /// Name of the plugin. //! /// Name of the plugin.
@ -45,7 +40,37 @@
//! async fn on_plugin_load(&self) {} //! async fn on_plugin_load(&self) {}
//! } //! }
//! //!
//! /// Create a new command. //! /// Register plugin
//! #[no_mangle]
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
//! registrar.register_plugin(Box::new(PluginTest));
//! }
//! ```
//!
//! ## Add command
//!
//! ```no_run
//! use async_trait::async_trait;
//! use servers::{
//! plugins::{Command, PluginManagerType, Registrar},
//! tcp::Client,
//! };
//! #
//! # struct PluginTest;
//! #
//! # #[async_trait]
//! # impl servers::plugins::Plugin for PluginTest {
//! # /// Name of the plugin.
//! # fn name(&self) -> &'static str {
//! # "test"
//! # }
//! #
//! # /// A function will be executed when plugin loading.
//! # /// Usally used for initialization.
//! # async fn on_plugin_load(&self) {}
//! # }
//!
//! /// Create a new command
//! #[async_trait] //! #[async_trait]
//! impl Command for PluginTest { //! impl Command for PluginTest {
//! /// Command name //! /// Command name
@ -60,43 +85,42 @@
//! //!
//! /// 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) {
//! client.send("content").expect("send message") //! client.send("Command executed!").expect("send message")
//! } //! }
//! } //! }
//! //!
//! /// Register plugin //! /// Register plugin
//! #[no_mangle] //! #[no_mangle]
//! pub fn plugin_entry(registrar: &mut dyn Registrar) { //! pub fn plugin_entry(registrar: &mut dyn Registrar) {
//! registrar.register_plugin(Box::new(PluginTest)); //! # registrar.register_plugin(Box::new(PluginTest));
//! registrar.register_command(Box::new(PluginTest)); //! registrar.register_command(Box::new(PluginTest));
//! } //! }
//! ``` //! ```
//! //!
//! ### Event plugin //! ## Add event
//! //!
//! In file `src/lib.rs` //! In file `src/lib.rs`
//! //!
//! ```no_run //! ```no_run
//! use async_trait::async_trait; //! use async_trait::async_trait;
//! use servers::{ //! use servers::{
//! plugins::{Event, Plugin, PluginManagerType, Registrar}, //! plugins::{Event, Registrar},
//! tcp::Client, //! tcp::Client,
//! }; //! };
//! //! #
//! struct PluginTest; //! # struct PluginTest;
//! //! #
//! /// Create a new plugin. //! # #[async_trait]
//! #[async_trait] //! # impl servers::plugins::Plugin for PluginTest {
//! impl Plugin for PluginTest { //! # /// Name of the plugin.
//! /// Name of the plugin. //! # fn name(&self) -> &'static str {
//! fn name(&self) -> &'static str { //! # "test"
//! "test" //! # }
//! } //! #
//! //! # /// A function will be executed when plugin loading.
//! /// A function will be executed when plugin loading. //! # /// Usally used for initialization.
//! /// Usally used for initialization. //! # async fn on_plugin_load(&self) {}
//! async fn on_plugin_load(&self) {} //! # }
//! }
//! //!
//! /// Create a new event //! /// Create a new event
//! #[async_trait] //! #[async_trait]
@ -117,7 +141,7 @@
//! /// Register plugin //! /// Register plugin
//! #[no_mangle] //! #[no_mangle]
//! pub fn plugin_entry(registrar: &mut dyn Registrar) { //! pub fn plugin_entry(registrar: &mut dyn Registrar) {
//! registrar.register_plugin(Box::new(PluginTest)); //! # registrar.register_plugin(Box::new(PluginTest));
//! registrar.register_event(Box::new(PluginTest)); //! registrar.register_event(Box::new(PluginTest));
//! } //! }
//! ``` //! ```
@ -126,9 +150,9 @@
//! //!
//! To build plugin run command: `cargo build --release` //! To build plugin run command: `cargo build --release`
//! //!
//! The compiled plugin can be found in `./target/release/libplugin.so` //! The compiled plugin can be found in `target/release/libplugin.so`
//! //!
//! Move compiled plugin to the `plugin` directory where servers is located //! Move (or create a symlink) the built plugin to the `plugin/` directory in the server root directory.
mod loader; mod loader;
mod types; mod types;

View file

@ -14,7 +14,7 @@ pub trait Plugin: Any + Send + Sync {
async fn on_plugin_load(&self); async fn on_plugin_load(&self);
} }
/// Add a new command /// Add a command to the plugin.
#[async_trait] #[async_trait]
pub trait Command: Any + Send + Sync { pub trait Command: Any + Send + Sync {
/// Name of the command. /// Name of the command.
@ -30,7 +30,7 @@ pub trait Command: Any + Send + Sync {
); );
} }
/// Add a new function that will be executed when the event occurs /// Add a new function that will be executed when the event occurs.
#[async_trait] #[async_trait]
pub trait Event: Any + Send + Sync { pub trait Event: Any + Send + Sync {
/// Event name (onConnect or onSend) /// Event name (onConnect or onSend)
@ -39,13 +39,13 @@ pub trait Event: Any + Send + Sync {
async fn execute(&self, client: &mut Client); async fn execute(&self, client: &mut Client);
} }
/// Plugin Manager /// Plugin Manager with all plugins features.
pub struct PluginManager { pub struct PluginManager {
/// Vector with loaded plugins. /// Array with loaded plugins.
pub plugins: Vec<Box<dyn Plugin>>, pub plugins: Vec<Box<dyn Plugin>>,
/// Vector with all commands. /// Array with all commands.
pub commands: Vec<Box<dyn Command>>, pub commands: Vec<Box<dyn Command>>,
/// Vector with all events. /// Array with all events.
pub events: Vec<Box<dyn Event>>, pub events: Vec<Box<dyn Event>>,
} }
@ -66,7 +66,7 @@ impl Default for PluginManager {
} }
} }
/// Type of the [PluginManager] /// Arc type of the [PluginManager].
pub type PluginManagerType = Arc<PluginManager>; pub type PluginManagerType = Arc<PluginManager>;
/// Plugin Registrar /// Plugin Registrar