Skip to main content

ZLayerPlugin

Trait ZLayerPlugin 

Source
pub trait ZLayerPlugin: Default {
    // Required methods
    fn info(&self) -> PluginInfo;
    fn handle(
        &self,
        event_type: &str,
        payload: &[u8],
    ) -> Result<Vec<u8>, String>;

    // Provided methods
    fn init(&self) -> Result<(), String> { ... }
    fn shutdown(&self) -> Result<(), String> { ... }
    fn capabilities(&self) -> Capabilities { ... }
}
Expand description

Trait for implementing ZLayer plugins.

This trait provides the core interface that all plugins must implement. The zlayer_plugin! macro generates the necessary WIT bindings glue code.

§Example

use zlayer_sdk::prelude::*;
use zlayer_sdk::{ZLayerPlugin, zlayer_plugin};

struct MyPlugin;

impl Default for MyPlugin {
    fn default() -> Self {
        Self
    }
}

impl ZLayerPlugin for MyPlugin {
    fn info(&self) -> PluginInfo {
        PluginInfo {
            id: "my-org:my-plugin".into(),
            name: "My Plugin".into(),
            version: Version { major: 1, minor: 0, patch: 0, pre_release: None },
            description: "A simple plugin".into(),
            author: "My Organization".into(),
            license: Some("MIT".into()),
            homepage: None,
            metadata: vec![],
        }
    }

    fn handle(&self, event_type: &str, payload: &[u8]) -> Result<Vec<u8>, String> {
        Ok(payload.to_vec())
    }
}

zlayer_plugin!(MyPlugin);

Required Methods§

Source

fn info(&self) -> PluginInfo

Return plugin metadata.

This information is used for logging, metrics, and management.

Source

fn handle(&self, event_type: &str, payload: &[u8]) -> Result<Vec<u8>, String>

Handle an incoming event or request.

§Arguments
  • event_type - The type of event (e.g., “GET”, “POST”, “message”, “timer”)
  • payload - The event payload as bytes
§Returns
  • Ok(response) - The response payload as bytes
  • Err(message) - An error message if handling failed
§Errors

Returns an error message string if handling fails.

Provided Methods§

Source

fn init(&self) -> Result<(), String>

Initialize the plugin.

Called once when the plugin is loaded. Override to perform initialization tasks like reading configuration or connecting to services.

Return Ok(()) on success or Err(message) to abort loading.

§Errors

Returns an error message string if initialization fails.

Source

fn shutdown(&self) -> Result<(), String>

Graceful shutdown hook.

Called when the plugin is being unloaded. Override to clean up resources like database connections or file handles.

§Errors

Returns an error message string if shutdown fails.

Source

fn capabilities(&self) -> Capabilities

Return the capabilities this plugin requires.

Override to request additional capabilities like HTTP client access.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§