Crates.io | wasmcloud-provider-wit-bindgen |
lib.rs | wasmcloud-provider-wit-bindgen |
version | 0.1.1 |
source | src |
created_at | 2024-03-27 19:21:06.967316 |
updated_at | 2024-03-27 19:21:06.967316 |
description | Macro for performing bindgen while building wasmCloud capability providers written in Rust |
homepage | |
repository | https://github.com/wasmCloud/wasmCloud |
max_upload_size | |
id | 1188322 |
size | 6,660 |
wasmcloud-provider-wit-bindgen
This crate contains a Rust compile time procedural macro which makes it easy to develop WIT-based wasmcloud capbility provider binaries written in Rust.
It all starts with wasmcloud_provider_wit_bindgen::generate
, for example:
wasmcloud_provider_wit_bindgen::generate!({
impl_struct: KvRedisProvider,
contract: "wasmcloud:keyvalue",
wit_bindgen_cfg: "provider-kvredis"
});
Assuming a struct named KvRedisProvider
exists in your source code and you have a provider-kvredis
world defined in your wit/
folder, the above macro will expand to Trait
s, struct
s, and other required machinery to implement a wasmcloud capability provider.
[!NOTE] For a full example, see the
kv-redis
provider in wasmcloud
By using the generate
macro, you'll be required to write implementation blocks like the following:
impl WasmcloudCapabilityProvider for KvRedisProvider {
async fn put_link(&self, ld: &LinkDefinition) -> bool { ... }
async fn delete_link(&self, actor_id: &str) { ... }
async fn shutdown(&self) { ... }
}
impl WasmcloudKeyvalueKeyValue for KvRedisProvider {
async fn get(&self, ctx: Context, arg: String) -> ProviderInvocationResult<GetResponse> { ... }
async fn set(&self, ctx: Context, arg: SetRequest) -> ProviderInvocationResult<()> { ... }
async fn del(&self, ctx: Context, arg: String) -> ProviderInvocationResult<bool> { ... }
...
}
Don't worry, types like SetRequest
and GetResponse
above will be provided by the expanded macro code.
Note that wasmcloud-provider-wit-bindgen
re-exports many dependencies in order to ensure that they match and are usable together:
It's recommended to use these dependencies in your code to avoid duplicating dependencies which could lead to all sorts of problems. For example the following use
block:
use wasmcloud_provider_wit_bindgen::deps::{
async_trait::async_trait,
serde::Deserialize,
serde_json,
wasmcloud_provider_sdk::core::LinkDefinition,
wasmcloud_provider_sdk::{load_host_data, start_provider, Context},
};
serde
When re-using a re-exported serde
, a known issue exists which requires that you must use the #[serde(crate = "...")]
directive:
#[derive(Deserialize)]
#[serde(crate = "wasmcloud_provider_wit_bindgen::deps::serde")]
struct ExampleStruct {
/// Some string that is part of this struct
#[serde(alias = "WORDS", alias = "Words")]
words: String,
}