1 unstable release

0.1.1 Mar 27, 2024

#1258 in WebAssembly

Download history 113/week @ 2024-03-22 36/week @ 2024-03-29 4/week @ 2024-04-05

153 downloads per month

Apache-2.0

92KB
1.5K SLoC

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

Dependencies

~32–48MB
~887K SLoC