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.
§Two subsystems
Rustledger has two distinct WASM plugin subsystems, and this crate hosts the shared types for both:
- Directive plugins transform the directive stream after parsing
(tagging, dedup, categorization). Required export:
process. Host loader:rustledger-plugin. The Quick Start below covers this case. - WASM importers turn bank-statement files into directives.
Required exports:
metadata,identify,extract,extract_enriched. Host loader:rustledger-importer::WasmImporter. Use thewasm_importer_main!macro (behind theguestfeature) to generate the boilerplate. See theguestmodule for details.
§Directive-Plugin Quick Start
Use the wasm_plugin_main! macro (behind the guest feature) to
generate the required alloc + process exports from a single
user fn. Add this to your plugin’s Cargo.toml:
[dependencies]
rustledger-plugin-types = { version = "0.15", features = ["guest"] }Then in your plugin:
use rustledger_plugin_types::{
PluginInput, PluginOutput, wasm_plugin_main,
};
fn process(input: PluginInput) -> PluginOutput {
// Simplest case: keep every input unchanged.
PluginOutput::passthrough(input.directives.len())
}
wasm_plugin_main! {
process: process,
}See the guest module for the full macro reference (including
the once-per-crate constraint on the wasm32 target). If you need
to write the extern "C" exports manually — for finer control or
to avoid the guest feature — see the “Without the macro” section
in the crate README.
§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.15.x for rustledger 0.15.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
§WASM-Importer Quick Start
Importers read source files (CSV, OFX, …) and emit directives. The host
loader lives in rustledger-importer; the wire format and a
boilerplate-eliminating macro live here.
Enable the guest feature, then use wasm_importer_main!:
[dependencies]
rustledger-plugin-types = { version = "0.15", features = ["guest"] }use rustledger_plugin_types::{
DirectiveData, DirectiveWrapper, ImporterInput, ImporterOutput,
OpenData, wasm_importer_main,
};
fn identify(path: &str) -> bool {
path.ends_with(".mybank")
}
fn extract(input: ImporterInput) -> ImporterOutput {
// Parse input.content; emit DirectiveWrapper values.
ImporterOutput::new(vec![/* … */])
}
wasm_importer_main! {
name: "my-bank",
description: "MyBank CSV statements",
identify: identify,
extract: extract,
// `extract_enriched` is auto-generated as a Default-categorization
// passthrough. Add `extract_enriched: my_fn` to override.
}Importer ABI types defined in this crate: ImporterInput,
IdentifyInput, IdentifyOutput, ImporterOutput,
EnrichedImporterOutput, MetadataOutput, EnrichmentWrapper,
AlternativeWrapper.
Wire-format method strings for EnrichmentWrapper::method: "rule",
"merchant-dict" (hyphen, not underscore), "ml", "llm", "manual",
"default". Unknown values trigger a host warning and fall back to
Default.
Structs§
- Alternative
Wrapper - Wire-format counterpart to
rustledger_ops::enrichment::Alternative. - 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.
- Enriched
Importer Output - Wire-format output returned from a WASM importer’s
extract_enriched. Each directive is paired with per-directive categorization metadata. - Enrichment
Wrapper - Wire-format counterpart to
rustledger_ops::enrichment::Enrichment. - Event
Data - Event data.
- Identify
Input - Wire-format input to a WASM importer’s
identifyentry point. - Identify
Output - Wire-format output from a WASM importer’s
identify. - Importer
Input - Wire-format input passed from the host to a WASM importer’s
extract/extract_enrichedentry point. - Importer
Output - Wire-format output returned from a WASM importer’s
extract. - Metadata
Output - Wire-format output from a WASM importer’s
metadataentry point. Returned once at load time and cached by the host registry — used forImporter::name()andImporter::description()on the wrapper. - 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.
- Source
Span - Source-location metadata for a posting that the host parsed from a beancount file.
- Transaction
Data - Transaction data for serialization.
Enums§
- Cost
Number Data - The numeric component of a
CostData. - Directive
Data - Directive-specific data.
- Meta
Value Data - Metadata value for serialization.
- Plugin
Error Severity - Severity of a plugin error.
- Plugin
Op - One operation in a
PluginOutput’s ordered op list. - Price
Annotation View - Typed view of a
PriceAnnotationData.
Constants§
- ABI_
VERSION - Version of the host/guest WASM ABI defined by this crate.
- ABI_
VERSION_ EXPORT - The WASM export symbol a guest uses to advertise
ABI_VERSION. Thewasm_*_main!macros emit it; the host looks it up by this name. Kept here as the single source of truth shared by both sides.
Functions§
- sort_
directives - Sort directives using beancount’s standard ordering.
- validate_
op_ coverage - Validate that
opsform a complete, non-overlapping cover of the input.