#[derive(Debug, Clone)]
pub struct GuestInfo {
pub name: String,
pub version: String,
pub features: Vec<String>,
}
impl GuestInfo {
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
Self {
name: name.into(),
version: version.into(),
features: Vec::new(),
}
}
pub fn with_feature(mut self, feature: impl Into<String>) -> Self {
self.features.push(feature.into());
self
}
}
#[macro_export]
macro_rules! impl_wit_interface {
(
$impl_type:ty,
$name:expr,
fn register_handlers(&self, $dispatcher:ident: &mut $dispatcher_ty:ty) $block:block
) => {
impl $crate::wit_registry::WitInterface for $impl_type {
fn interface_name(&self) -> &'static str {
$name
}
fn register_handlers(&self, $dispatcher: &mut $dispatcher_ty) $block
}
};
}
#[macro_export]
macro_rules! simple_handler {
($command_type:ty, $handler:expr) => {{
struct Handler;
impl $crate::wit_registry::WitCommandHandler<$command_type> for Handler {
fn execute(
&mut self,
command: &$command_type,
) -> Result<<$command_type as $crate::wit_registry::WitCommand>::Response, String>
{
$handler(command)
}
}
Box::new(Handler) as Box<dyn $crate::wit_registry::WitCommandHandler<$command_type>>
}};
}
#[macro_export]
macro_rules! stateful_handler {
($state_type:ty, $command_type:ty, $handler:expr) => {{
struct Handler(std::sync::Arc<std::sync::Mutex<$state_type>>);
impl $crate::wit_registry::WitCommandHandler<$command_type> for Handler {
fn execute(
&mut self,
command: &$command_type,
) -> Result<<$command_type as $crate::wit_registry::WitCommand>::Response, String>
{
let mut state = self.0.lock().unwrap_or_else(|e| e.into_inner());
$handler(&mut state, command)
}
}
let state = std::sync::Arc::new(std::sync::Mutex::new(state));
Box::new(Handler(state)) as Box<dyn $crate::wit_registry::WitCommandHandler<$command_type>>
}};
}