From 6149cb7744f8c4763c220212a9d280b296085559 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 29 Aug 2025 13:52:04 +0200 Subject: [PATCH] Better structure --- src/lib.rs | 37 +++++++++++++++++++++++++------------ src/macros.rs | 15 +++++++++++++++ src/main.rs | 12 ++---------- test-plugin/Cargo.toml | 2 +- test-plugin/src/main.rs | 7 +++++++ 5 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 src/macros.rs create mode 100644 test-plugin/src/main.rs diff --git a/src/lib.rs b/src/lib.rs index 8ab748c..d6c7289 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,11 +5,16 @@ use wasmtime_wasi::preview1::WasiP1Ctx; #[cfg(feature = "loader")] pub mod loader; +pub mod macros; + +pub use anyhow::Result; pub trait PluginApi { fn init(&mut self); } +pub struct ServerConfig {} + pub enum PluginInstance { #[cfg(feature = "loader")] Wasm(Instance, Store), @@ -31,18 +36,26 @@ impl PluginApi for PluginInstance { } } -#[macro_export] -macro_rules! export_plugin { - ($plugin_type:ty) => { - static PLUGIN: std::sync::Mutex> = std::sync::Mutex::new(None); +impl ServerConfig { + pub fn start(&self) -> Result<()> { + let mut plugins: Vec = Vec::new(); + #[cfg(feature = "loader")] + loader::load_plugins(&mut plugins, std::path::Path::new("./plugins")); - #[unsafe(no_mangle)] - pub extern "C" fn init() { - let mut plugin = PLUGIN.lock().unwrap(); - *plugin = Some(<$plugin_type>::default()); - if let Some(plugin) = plugin.as_mut() { - plugin.init(); - } + self.start_with(plugins) + } + + pub fn start_with(&self, mut plugins: Vec) -> Result<()> { + for plugin in &mut plugins { + plugin.init(); } - }; + + Ok(()) + } +} + +impl Default for ServerConfig { + fn default() -> Self { + Self {} + } } diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..1fc78f2 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,15 @@ +#[macro_export] +macro_rules! export_plugin { + ($plugin_type:ty) => { + static PLUGIN: std::sync::Mutex> = std::sync::Mutex::new(None); + + #[unsafe(no_mangle)] + pub extern "C" fn init() { + let mut plugin = PLUGIN.lock().unwrap(); + *plugin = Some(<$plugin_type>::default()); + if let Some(plugin) = plugin.as_mut() { + plugin.init(); + } + } + }; +} diff --git a/src/main.rs b/src/main.rs index 211d06c..9ccfd8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,4 @@ -use voxa_server::PluginApi; - fn main() -> anyhow::Result<()> { - let mut plugins = Vec::new(); - voxa_server::loader::load_plugins(&mut plugins, std::path::Path::new("./plugins")); - - for plugin in &mut plugins { - plugin.init(); - } - - Ok(()) + let config = voxa_server::ServerConfig::default(); + config.start() } diff --git a/test-plugin/Cargo.toml b/test-plugin/Cargo.toml index 7f3c64b..05cdd38 100644 --- a/test-plugin/Cargo.toml +++ b/test-plugin/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] [dependencies] voxa-server = { path = "../", default-features = false } \ No newline at end of file diff --git a/test-plugin/src/main.rs b/test-plugin/src/main.rs new file mode 100644 index 0000000..6860144 --- /dev/null +++ b/test-plugin/src/main.rs @@ -0,0 +1,7 @@ +use voxa_server::PluginInstance; + +fn main() -> voxa_server::Result<()> { + let config = voxa_server::ServerConfig::default(); + let plugin = test_plugin::MyPlugin::default(); + config.start_with(vec![PluginInstance::StatiC(Box::new(plugin))]) +}