mirror of https://github.com/MedzikUser/servers
chore: add some comments in code
Moved `PluginsManager` and `PluginsManagerType` from `servers::plugin::manager` to the `servers::plugin` module. Added some comments in code. Implemented function `into()` to the `PluginsManager` struct.
This commit is contained in:
parent
501cbc74b7
commit
032ec88ed6
|
@ -1,3 +1,11 @@
|
|||
//! Default servers commands.
|
||||
//!
|
||||
//! List of commands:
|
||||
//! - /broadcast
|
||||
//! - /disconnect
|
||||
//! - /help
|
||||
//! - /id
|
||||
|
||||
mod broadcast;
|
||||
mod disconnect;
|
||||
mod help;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{fs, path::Path, sync::Arc};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use async_std::task;
|
||||
use libloading::{Library, Symbol};
|
||||
|
@ -12,6 +12,7 @@ use crate::{
|
|||
},
|
||||
};
|
||||
|
||||
/// Load all plugins, commands and events.
|
||||
pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
|
||||
// if plugins directory doesn't exists, create it
|
||||
if !Path::new(plugins_dir).exists() {
|
||||
|
@ -24,7 +25,7 @@ pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
|
|||
// init a plugins manager
|
||||
let mut plugins_manager = PluginsManager::new();
|
||||
|
||||
// add default commands
|
||||
// register default commands
|
||||
plugins_manager.commands = commands::register_commands();
|
||||
|
||||
for plugin_path in plugins_files {
|
||||
|
@ -59,5 +60,5 @@ pub fn loader(plugins_dir: &str) -> anyhow::Result<PluginsManagerType> {
|
|||
info!("Loaded plugin {}.", plugin.name());
|
||||
}
|
||||
|
||||
Ok(Arc::new(plugins_manager))
|
||||
Ok(plugins_manager.into())
|
||||
}
|
||||
|
|
|
@ -3,6 +3,10 @@ use std::sync::Arc;
|
|||
|
||||
use crate::plugins::prelude::*;
|
||||
|
||||
/// Plugins manager struct with Clone derive added by Arc.
|
||||
pub type PluginsManagerType = Arc<PluginsManager>;
|
||||
|
||||
/// A plugins manager that stores all plugins, commands and events.
|
||||
#[derive(Default)]
|
||||
pub struct PluginsManager {
|
||||
/// Vector with all loaded plugins.
|
||||
|
@ -14,14 +18,19 @@ pub struct PluginsManager {
|
|||
}
|
||||
|
||||
impl PluginsManager {
|
||||
/// Returns an empty PluginsManager
|
||||
pub fn new() -> PluginsManager {
|
||||
/// Returns an empty instance of [PluginsManager]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
plugins: Vec::new(),
|
||||
commands: Vec::new(),
|
||||
events: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the instance in [PluginsManagerType].
|
||||
pub fn into(self) -> PluginsManagerType {
|
||||
Arc::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for PluginsManager {
|
||||
|
@ -33,5 +42,3 @@ impl fmt::Debug for PluginsManager {
|
|||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub type PluginsManagerType = Arc<PluginsManager>;
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
//! Plugin infrastructure.
|
||||
|
||||
mod load;
|
||||
pub mod manager;
|
||||
mod manager;
|
||||
pub mod types;
|
||||
|
||||
pub use load::*;
|
||||
pub use manager::*;
|
||||
|
||||
/// Crates and types required in plugins.
|
||||
pub mod prelude {
|
||||
use super::*;
|
||||
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
//! Types used for creating plugins.
|
||||
|
||||
use std::any::Any;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{plugins::manager::PluginsManager, server::Client};
|
||||
|
||||
// A main plugin trait.
|
||||
/// A main plugin trait.
|
||||
#[async_trait]
|
||||
pub trait Plugin: Any + Send + Sync {
|
||||
/// Name of the plugin.
|
||||
|
@ -57,6 +59,7 @@ pub trait Event: Any + Send + Sync {
|
|||
async fn execute(&self, client: &Client, data: EventData) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
/// A plugin registrar trait.
|
||||
pub trait Registrar {
|
||||
/// Function to register plugins.
|
||||
fn register_plugins(&mut self, plugin: Box<dyn Plugin>);
|
||||
|
|
|
@ -10,8 +10,8 @@ use tungstenite::{accept, Message, WebSocket};
|
|||
|
||||
use super::run::PLUGINS_MANAGER;
|
||||
use crate::plugins::{
|
||||
manager::PluginsManagerType,
|
||||
prelude::{EventData, EventType},
|
||||
PluginsManagerType,
|
||||
};
|
||||
|
||||
/// Max length of a TCP and UDP packet
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//! Server infrastructure.
|
||||
|
||||
mod client;
|
||||
mod run;
|
||||
|
||||
|
|
|
@ -8,13 +8,14 @@ use tracing::{error, info, span, Level};
|
|||
use crate::{
|
||||
plugins::{
|
||||
self,
|
||||
manager::PluginsManagerType,
|
||||
prelude::{EventData, EventType},
|
||||
PluginsManagerType,
|
||||
},
|
||||
server::Client,
|
||||
CLIENTS, CLIENT_NEXT,
|
||||
};
|
||||
|
||||
/// Plugins directory.
|
||||
pub const PLUGINS_DIR: &str = "plugins";
|
||||
|
||||
lazy_static! {
|
||||
|
|
Loading…
Reference in New Issue