Skip to main content

Plugin

Trait Plugin 

Source
pub trait Plugin:
    Send
    + Sync
    + 'static {
    // Required methods
    fn on_start<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = PluginResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn handle_request<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
        type_tag: &'life2 str,
        payload: &'life3 [u8],
    ) -> Pin<Box<dyn Future<Output = PluginResult<Vec<u8>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait;
    fn on_stop<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 PluginContext,
    ) -> Pin<Box<dyn Future<Output = PluginResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn metadata(&self) -> Option<PluginMetadata> { ... }
    fn supported_types(&self) -> Vec<&'static str> { ... }
}
Expand description

Main trait for implementing rustbridge plugins

Plugins must implement this trait to define their behavior. The async methods are executed on the Tokio runtime.

§Example

use rustbridge_core::prelude::*;

struct MyPlugin;

#[async_trait::async_trait]
impl Plugin for MyPlugin {
    async fn on_start(&self, ctx: &PluginContext) -> PluginResult<()> {
        // Initialize resources
        Ok(())
    }

    async fn handle_request(
        &self,
        ctx: &PluginContext,
        type_tag: &str,
        payload: &[u8],
    ) -> PluginResult<Vec<u8>> {
        match type_tag {
            "echo" => Ok(payload.to_vec()),
            _ => Err(PluginError::UnknownMessageType(type_tag.to_string())),
        }
    }

    async fn on_stop(&self, ctx: &PluginContext) -> PluginResult<()> {
        // Cleanup resources
        Ok(())
    }
}

Required Methods§

Source

fn on_start<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = PluginResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when the plugin is starting up

Use this to initialize resources, connections, etc. The plugin transitions to Active state after this returns successfully.

Source

fn handle_request<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, type_tag: &'life2 str, payload: &'life3 [u8], ) -> Pin<Box<dyn Future<Output = PluginResult<Vec<u8>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Handle an incoming request

  • type_tag: Message type identifier (e.g., “user.create”)
  • payload: JSON-encoded request payload

Returns JSON-encoded response payload

Source

fn on_stop<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 PluginContext, ) -> Pin<Box<dyn Future<Output = PluginResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when the plugin is shutting down

Use this to cleanup resources, close connections, etc. The plugin transitions to Stopped state after this returns.

Provided Methods§

Source

fn metadata(&self) -> Option<PluginMetadata>

Get plugin metadata

Override this to provide plugin information

Source

fn supported_types(&self) -> Vec<&'static str>

List supported message types

Override this to provide a list of supported type tags

Implementors§