wasmcloud-provider-wit-bindgen

Crates.iowasmcloud-provider-wit-bindgen
lib.rswasmcloud-provider-wit-bindgen
version0.1.1
sourcesrc
created_at2024-03-27 19:21:06.967316
updated_at2024-03-27 19:21:06.967316
descriptionMacro for performing bindgen while building wasmCloud capability providers written in Rust
homepage
repositoryhttps://github.com/wasmCloud/wasmCloud
max_upload_size
id1188322
size6,660
wasmCloud Automation Bot (wasmcloud-automation)

documentation

README

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.

Quickstart

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 Traits, structs, 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.

Re-exports

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},
};

Special case: re-using 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,
}
Commit count: 5943

cargo fmt