omnia-runtime-macro
Procedural macros for generating WebAssembly Component Runtime infrastructure.
Overview
This crate provides the runtime! macro that generates the necessary runtime infrastructure for executing WebAssembly components with WASI capabilities. Instead of manually managing feature flags and conditional compilation, you declaratively specify which WASI interfaces and backends your runtime needs.
Usage
Add omnia to your dependencies (the runtime! macro is re-exported from the omnia crate):
[]
= { = true }
Then use the runtime! macro to generate your runtime infrastructure:
use runtime;
// Import the backend types you want to use
use WasiHttpCtx;
use DefaultOtel;
use Client as MongoDb;
use Client as Nats;
use Client as Azure;
// Generate runtime infrastructure
runtime!;
// The macro generates:
// - RuntimeContext struct with backend connections
// - RuntimeStoreCtx struct with per-instance contexts
// - State trait implementation
// - WASI view trait implementations
// - runtime_run() function
Configuration Format
The macro accepts a map-like syntax:
runtime!;
Supported Interfaces
-
http: HTTP client and server- Backend:
WasiHttpCtx(marker type, no backend connection needed)
- Backend:
-
otel: OpenTelemetry observability- Backend:
DefaultOtel(connects to OTEL collector)
- Backend:
-
blobstore: Object/blob storage- Backends:
MongoDborNats
- Backends:
-
keyvalue: Key-value storage- Backends:
NatsorRedis
- Backends:
-
messaging: Pub/sub messaging- Backends:
NatsorKafka
- Backends:
-
vault: Secrets management- Backend:
Azure(Azure Key Vault)
- Backend:
-
sql: SQL database- Backend:
Postgres
- Backend:
-
identity: Identity and authentication- Backend:
Azure(Azure Identity)
- Backend:
-
websocket: WebSocket connections- Backend:
WebSocketCtxImpl(default implementation for development use)
- Backend:
Generated Code
The macro generates the following:
RuntimeContext
A struct holding pre-instantiated components and backend connections:
RuntimeStoreCtx
Per-instance data shared between the WebAssembly runtime and host functions:
State Trait Implementation
Implements the State trait from the runtime crate, providing methods to create new store contexts and access the pre-instantiated component.
WASI View Implementations
Implements view traits for each configured WASI interface, allowing the WebAssembly guest to call host functions.
runtime_run() Function
A public async function that:
- Loads runtime configuration
- Compiles the WebAssembly component
- Links WASI interfaces
- Connects to backends
- Starts server interfaces (HTTP, messaging, WebSocket)
Example: Custom Initiator Configuration
You can create different runtime configurations for different use cases:
// Minimal HTTP server
// Full-featured runtime
Now you can declaratively specify your configuration:
This provides:
- Better readability: The configuration is explicit and self-documenting
- Less boilerplate: No need for complex feature flag combinations
- Type safety: Backend types are checked at compile time
- Flexibility: Easy to create multiple runtime configurations in the same binary
License
MIT OR Apache-2.0