Better plugin structure
This commit is contained in:
+5
-32
@@ -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 {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user