Better plugin structure

This commit is contained in:
2025-08-29 16:22:53 +02:00
parent b3a1c0bcff
commit 494ecd6204
4 changed files with 40 additions and 36 deletions
+5 -32
View File
@@ -1,41 +1,20 @@
use std::path::Path; use std::path::Path;
#[cfg(feature = "loader")]
use wasmtime::{Instance, Store};
#[cfg(feature = "loader")]
use wasmtime_wasi::preview1::WasiP1Ctx;
#[cfg(feature = "loader")] #[cfg(feature = "loader")]
pub mod loader; pub mod loader;
pub mod macros; pub mod macros;
pub mod plugin;
pub mod vfs; pub mod vfs;
pub use anyhow::Result; pub use anyhow::Result;
pub trait PluginApi { use crate::plugin::{Plugin, PluginInstance};
fn init(&mut self);
}
pub struct ServerConfig {} pub struct ServerConfig {}
pub enum PluginInstance { impl Default for ServerConfig {
#[cfg(feature = "loader")] fn default() -> Self {
Wasm(Instance, Store<WasiP1Ctx>), Self {}
StatiC(Box<dyn PluginApi>),
}
impl PluginApi for PluginInstance {
fn init(&mut self) {
match self {
#[cfg(feature = "loader")]
PluginInstance::Wasm(instance, store) => {
let init = instance
.get_typed_func::<(), ()>(&mut *store, "init")
.unwrap();
init.call(store, ()).unwrap();
}
PluginInstance::StatiC(plugin) => plugin.init(),
}
} }
} }
@@ -56,9 +35,3 @@ impl ServerConfig {
Ok(()) Ok(())
} }
} }
impl Default for ServerConfig {
fn default() -> Self {
Self {}
}
}
+29
View File
@@ -0,0 +1,29 @@
#[cfg(feature = "loader")]
use wasmtime::{Instance, Store};
#[cfg(feature = "loader")]
use wasmtime_wasi::preview1::WasiP1Ctx;
pub trait Plugin {
fn init(&mut self);
}
pub enum PluginInstance {
#[cfg(feature = "loader")]
Wasm(Instance, Store<WasiP1Ctx>),
Static(Box<dyn Plugin>),
}
impl Plugin for PluginInstance {
fn init(&mut self) {
match self {
#[cfg(feature = "loader")]
PluginInstance::Wasm(instance, store) => {
let init = instance
.get_typed_func::<(), ()>(&mut *store, "init")
.unwrap();
init.call(store, ()).unwrap();
}
PluginInstance::Static(plugin) => plugin.init(),
}
}
}
+2 -2
View File
@@ -1,9 +1,9 @@
use voxa_server::{PluginApi, export_plugin}; use voxa_server::{export_plugin, plugin::Plugin};
#[derive(Default)] #[derive(Default)]
pub struct MyPlugin; pub struct MyPlugin;
impl PluginApi for MyPlugin { impl Plugin for MyPlugin {
fn init(&mut self) { fn init(&mut self) {
println!("MyPlugin initialized!"); println!("MyPlugin initialized!");
} }
+4 -2
View File
@@ -1,7 +1,9 @@
use voxa_server::PluginInstance; use std::path::PathBuf;
use voxa_server::plugin::PluginInstance;
fn main() -> voxa_server::Result<()> { fn main() -> voxa_server::Result<()> {
let root = PathBuf::from("../");
let config = voxa_server::ServerConfig::default(); let config = voxa_server::ServerConfig::default();
let plugin = test_plugin::MyPlugin::default(); let plugin = test_plugin::MyPlugin::default();
config.start_with(vec![PluginInstance::StatiC(Box::new(plugin))]) config.start_with(vec![PluginInstance::Static(Box::new(plugin))], &root)
} }