use std::any::Any;
use std::error::Error;
pub trait Dispatcher: Any + Send + Sync {
fn dispatch(&self, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;
}
#[derive(Default)]
pub struct NullDispatcher {}
impl NullDispatcher {
pub fn new() -> NullDispatcher {
NullDispatcher {}
}
}
impl Dispatcher for NullDispatcher {
fn dispatch(&self, _op: &str, _msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
unimplemented!()
}
}
pub trait CapabilityProvider: Any + Send + Sync {
fn configure_dispatch(
&self,
dispatcher: Box<dyn Dispatcher>,
identity: ModuleIdentity,
) -> Result<(), Box<dyn Error>>;
fn capability_id(&self) -> &'static str;
fn name(&self) -> &'static str;
fn handle_call(&self, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModuleIdentity {
pub module: String,
pub issuer: String,
pub capabilities: Vec<String>,
}
#[macro_export]
macro_rules! capability_provider {
($provider_type:ty, $constructor:path) => {
#[no_mangle]
pub extern "C" fn __capability_provider_create(
) -> *mut $crate::capabilities::CapabilityProvider {
let constructor: fn() -> $provider_type = $constructor;
let object = constructor();
let boxed: Box<$crate::capabilities::CapabilityProvider> = Box::new(object);
Box::into_raw(boxed)
}
};
}