Skip to main content

Crate zlayer_sdk

Crate zlayer_sdk 

Source
Expand description

ZLayer SDK for building WASM plugins in Rust.

This crate provides type-safe bindings to ZLayer host functions and convenient macros for implementing plugins.

§Example

#![no_std]
extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;
use zlayer_sdk::bindings::{
    export_handler,
    exports::zlayer::plugin::handler::{
        Guest, Capabilities, HandleResult, InitError, PluginInfo, PluginRequest,
    },
    zlayer::plugin::{
        config, logging,
        plugin_metadata::Version,
        request_types::PluginResponse,
    },
};

struct MyPlugin;

impl Guest for MyPlugin {
    fn init() -> Result<Capabilities, InitError> {
        logging::info("Initializing...");
        Ok(Capabilities::CONFIG | Capabilities::LOGGING)
    }

    fn info() -> PluginInfo {
        PluginInfo {
            id: String::from("my:plugin"),
            name: String::from("My Plugin"),
            version: Version { major: 0, minor: 1, patch: 0, pre_release: None },
            description: String::from("A custom plugin"),
            author: String::from("Author"),
            license: None,
            homepage: None,
            metadata: Vec::new(),
        }
    }

    fn handle(request: PluginRequest) -> HandleResult {
        logging::info("Handling request");
        HandleResult::Response(PluginResponse {
            status: 200,
            headers: Vec::new(),
            body: b"Hello from ZLayer!".to_vec(),
        })
    }

    fn shutdown() {
        logging::info("Shutting down...");
    }
}

// Note: `with_types_in` is required when using the SDK from an external crate
export_handler!(MyPlugin with_types_in zlayer_sdk::bindings);

§Features

  • Type-safe bindings - Generated from WIT definitions
  • Host function access - Config, KV storage, logging, secrets, metrics
  • HTTP capabilities - Make outbound HTTP requests
  • no_std compatible - Minimal runtime dependencies

§no_std Support

This crate is no_std by default for WASM plugin compilation. Enable the std feature for native testing or non-WASM targets.

Modules§

bindings
Generated bindings from WIT definitions.
config
Configuration access for plugins.
kv
Key-value storage for plugin state.
log
Structured logging for plugins.
metadata
Helper module for working with plugin metadata.
metrics
Metrics emission for observability.
prelude
Prelude module for convenient imports.
response
Helper module for building plugin responses.
secrets
Secure secret access for plugins.

Macros§

zlayer_plugin
Register a plugin implementation with the ZLayer runtime.

Structs§

ConfigError
Error returned by config operations
SecretError
Error returned by secret operations

Enums§

KvError
Error returned by key-value operations

Traits§

ZLayerPlugin
Trait for implementing ZLayer plugins.

Functions§

kv_pair
Create a KeyValue pair.
version
Create a Version struct.
version_pre
Create a Version struct with pre-release tag.