diff --git a/Cargo.lock b/Cargo.lock index f83a4e1759..51fc8237d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3754,7 +3754,7 @@ dependencies = [ ] [[package]] -name = "plugin_proc" +name = "plugin-api-derive" version = "0.1.0" dependencies = [ "proc-macro2 1.0.24", @@ -5602,7 +5602,7 @@ version = "0.1.0" dependencies = [ "bincode", "common-api", - "plugin_proc", + "plugin-api-derive", "serde", ] diff --git a/assets/plugins/hello.plugin.tar b/assets/plugins/hello.plugin.tar index f48fa4cfcd..358e51550a 100644 Binary files a/assets/plugins/hello.plugin.tar and b/assets/plugins/hello.plugin.tar differ diff --git a/common/src/plugin/mod.rs b/common/src/plugin/mod.rs index 193776a155..4865f2d3c2 100644 --- a/common/src/plugin/mod.rs +++ b/common/src/plugin/mod.rs @@ -4,12 +4,7 @@ pub mod module; use crate::assets::ASSETS_PATH; use serde::{Deserialize, Serialize}; -use std::{ - collections::HashMap, - fs, - io::Read, - path::{Path, PathBuf}, -}; +use std::{collections::{HashMap, HashSet}, fs, io::Read, path::{Path, PathBuf}}; use tracing::{error, info}; use common_api::Event; @@ -19,8 +14,8 @@ use self::{ errors::PluginError, module::{PluginModule, PreparedEventQuery}}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct PluginData { name: String, - modules: Vec<PathBuf>, - dependencies: Vec<String>, + modules: HashSet<PathBuf>, + dependencies: HashSet<String>, } #[derive(Clone)] @@ -93,9 +88,12 @@ impl PluginMgr { } pub fn execute_prepared<T>(&self, event_name: &str,event: &PreparedEventQuery<T>) -> Result<Vec<T::Response>, PluginError> where T: Event { - Ok(self.plugins.iter().map(|plugin| { - plugin.execute_prepared(event_name, event) - }).collect::<Result<Vec<Vec<T::Response>>, _>>()?.into_iter().flatten().collect::<Vec<T::Response>>()) + let mut out = Vec::new(); + for plugin in &self.plugins { + let exe = plugin.execute_prepared(event_name, event)?; + out.extend(exe); + } + Ok(out) } pub fn execute_event<T>(&self, event_name: &str,event: &T) -> Result<Vec<T::Response>, PluginError> where T: Event { diff --git a/common/src/plugin/module.rs b/common/src/plugin/module.rs index e025da71be..6a44471958 100644 --- a/common/src/plugin/module.rs +++ b/common/src/plugin/module.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, sync::Arc}; +use std::{collections::HashSet, marker::PhantomData, sync::Arc}; use error::RuntimeError; use parking_lot::Mutex; @@ -11,15 +11,15 @@ use common_api::{Action, Event}; pub type Function<'a> = Func<'a, (i32, u32), i32>; #[derive(Clone)] -// This tructure represent the WASM State of the plugin. +// This structure represent the WASM State of the plugin. pub struct PluginModule { wasm_instance: Arc<Mutex<Instance>>, - events: Vec<String>, + events: HashSet<String>, } impl PluginModule { - // This function take bytes from a WASM File and compile them + // This function takes bytes from a WASM File and compile them pub fn new(wasm_data: &Vec<u8>) -> Result<Self,PluginModuleError> { let module = compile(&wasm_data).map_err(|e| PluginModuleError::Compile(e))?; let instance = module @@ -33,7 +33,7 @@ impl PluginModule { }) } - // This function try to execute an event for the current module will return None if the event doesn't exists + // This function tries to execute an event for the current module. Will return None if the event doesn't exists pub fn try_execute<T>( &self, event_name: &str, @@ -42,7 +42,7 @@ impl PluginModule { where T: Event, { - if !self.events.iter().any(|x| x == event_name) { + if !self.events.contains(event_name) { return None; } let bytes = { @@ -61,14 +61,14 @@ impl PluginModule { } } -// This structure represent a Pre-encoded event object (Usefull to avoid reencoding for each module in every plugin) +// This structure represent a Pre-encoded event object (Useful to avoid reencoding for each module in every plugin) pub struct PreparedEventQuery<T> { bytes: Vec<u8>, _phantom: PhantomData<T>, } impl<T: Event> PreparedEventQuery<T> { - // Create a prepared query from a event reference (Encode to bytes the struct) + // Create a prepared query from an event reference (Encode to bytes the struct) // This Prepared Query is used by the `try_execute` method in `PluginModule` pub fn new(event: &T) -> Result<Self, PluginError> where diff --git a/plugin-api/Cargo.toml b/plugin-api/Cargo.toml index 3e50ed0f76..21e49dd52f 100644 --- a/plugin-api/Cargo.toml +++ b/plugin-api/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] common-api = { path = "../common/common-api" } -plugin_proc = { path = "./plugin_proc"} +plugin-api-derive = { path = "./plugin-api-derive"} serde = {version = "1.0.118", features = ["derive"]} bincode = "1.3.1" diff --git a/plugin-api/plugin_proc/Cargo.toml b/plugin-api/plugin-api-derive/Cargo.toml similarity index 88% rename from plugin-api/plugin_proc/Cargo.toml rename to plugin-api/plugin-api-derive/Cargo.toml index 44709430b5..cec091ffe7 100644 --- a/plugin-api/plugin_proc/Cargo.toml +++ b/plugin-api/plugin-api-derive/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "plugin_proc" +name = "plugin-api-derive" version = "0.1.0" authors = ["ccgauche <gaucheron.laurent@gmail.com>"] edition = "2018" diff --git a/plugin-api/plugin_proc/src/lib.rs b/plugin-api/plugin-api-derive/src/lib.rs similarity index 100% rename from plugin-api/plugin_proc/src/lib.rs rename to plugin-api/plugin-api-derive/src/lib.rs diff --git a/plugin-api/src/lib.rs b/plugin-api/src/lib.rs index c2d72a9773..cd910fca26 100644 --- a/plugin-api/src/lib.rs +++ b/plugin-api/src/lib.rs @@ -1,13 +1,12 @@ #![feature(const_fn)] -pub extern crate plugin_proc; +pub extern crate plugin_api_derive; pub extern crate common_api; pub use common_api::*; - -pub use plugin_proc::*; +pub use plugin_api_derive::*; use serde::de::DeserializeOwned; use serde::Serialize;