Expand description
WASM Plugin Interface Types for rustledger
This crate provides the type definitions for rustledger’s WASM plugin interface. Use it as a dependency in your plugin crate to ensure type compatibility with the rustledger host.
§Quick Start
Add this to your plugin’s Cargo.toml:
[dependencies]
rustledger-plugin-types = "0.10"
rmp-serde = "1"Then in your plugin:
use rustledger_plugin_types::*;
#[no_mangle]
pub extern "C" fn process(input_ptr: u32, input_len: u32) -> u64 {
let input_bytes = unsafe {
std::slice::from_raw_parts(input_ptr as *const u8, input_len as usize)
};
let input: PluginInput = rmp_serde::from_slice(input_bytes).unwrap();
// Process directives...
let output = PluginOutput {
directives: input.directives,
errors: vec![],
};
let output_bytes = rmp_serde::to_vec(&output).unwrap();
let output_ptr = alloc(output_bytes.len() as u32);
unsafe {
std::ptr::copy_nonoverlapping(
output_bytes.as_ptr(),
output_ptr,
output_bytes.len(),
);
}
((output_ptr as u64) << 32) | (output_bytes.len() as u64)
}
#[no_mangle]
pub extern "C" fn alloc(size: u32) -> *mut u8 {
let layout = std::alloc::Layout::from_size_align(size as usize, 1).unwrap();
unsafe { std::alloc::alloc(layout) }
}§Serialization Format
Plugins communicate with the host via MessagePack serialization. The host
calls process(ptr, len) with a pointer to MessagePack-encoded PluginInput.
The plugin returns a packed u64 containing a pointer and length to
MessagePack-encoded PluginOutput.
§Memory Management
Plugins must export an alloc(size: u32) -> *mut u8 function. The host uses
this to allocate memory in the WASM linear memory for passing input data.
The plugin uses it to allocate memory for output data.
Optionally, plugins can export a dealloc(ptr: *mut u8, size: u32) function
to free memory. This is not required by the host but can be useful for
memory management within longer-running plugin operations.
§Version Compatibility
Plugin types are versioned with rustledger. For best compatibility, use the
same minor version of rustledger-plugin-types as the rustledger host you’re
targeting (e.g., 0.10.x for rustledger 0.10.x).
§Building
Build your plugin for the WASM target:
rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown --releaseThe output will be in target/wasm32-unknown-unknown/release/your_plugin.wasm
Structs§
- Amount
Data - Amount data for serialization.
- Balance
Data - Balance assertion data.
- Close
Data - Close account data.
- Commodity
Data - Commodity declaration data.
- Cost
Data - Cost data for serialization.
- Custom
Data - Custom directive data.
- Directive
Wrapper - A wrapper around directives for serialization.
- Document
Data - Document data.
- Event
Data - Event data.
- Note
Data - Note data.
- Open
Data - Open account data.
- PadData
- Pad directive data.
- Plugin
Error - Error generated by a plugin.
- Plugin
Input - Input passed to a plugin.
- Plugin
Options - Ledger options passed to plugins.
- Plugin
Output - Output returned from a plugin.
- Posting
Data - Posting data for serialization.
- Price
Annotation Data - Price annotation data.
- Price
Data - Price directive data.
- Query
Data - Query directive data.
- Transaction
Data - Transaction data for serialization.
Enums§
- Directive
Data - Directive-specific data.
- Meta
Value Data - Metadata value for serialization.
- Plugin
Error Severity - Severity of a plugin error.
Functions§
- sort_
directives - Sort directives using beancount’s standard ordering.