#webassembly #wasm #codec #wasmcloud #provider

wasmcloud-provider-core

Core types and traits used for building wasmcloud capability providers

2 releases

0.1.1 Mar 26, 2021
0.1.0 Feb 25, 2021

#23 in #wasmcloud

Download history 11/week @ 2021-02-20 63/week @ 2021-02-27 91/week @ 2021-03-06 252/week @ 2021-03-13 159/week @ 2021-03-20 161/week @ 2021-03-27 64/week @ 2021-04-03 245/week @ 2021-04-10 158/week @ 2021-04-17 117/week @ 2021-04-24 85/week @ 2021-05-01 78/week @ 2021-05-08 163/week @ 2021-05-15 31/week @ 2021-05-22 62/week @ 2021-05-29 28/week @ 2021-06-05

496 downloads per month
Used in 14 crates (12 directly)

Apache-2.0

10KB
105 lines

wasmcloud Provider Core

This crate provides shared traits and primitives used for building capability providers. For more information on creating capability providers, see our documentation at wasmcloud.dev.


lib.rs:

wasmcloud Provider Core

This library provides the core set of types and associated functions used for the common set of functionality required for the wasmcloud host to manipulate capability providers and for developers to create their own providers.

Example

The following illustrates an example of the simplest capability provider


 use wasmcloud_provider_core as provider;
 use wasmcloud_actor_core as actor;
 use provider::{CapabilityProvider, Dispatcher, NullDispatcher, serialize,
             core::{OP_BIND_ACTOR, OP_HEALTH_REQUEST, OP_REMOVE_ACTOR, SYSTEM_ACTOR}};
 use actor::{CapabilityConfiguration, HealthCheckResponse};
 use std::sync::{Arc, RwLock};
 use std::error::Error;

 // Hello world implementation of the `demo:hello` capability provider
 #[derive(Clone)]
 pub struct HelloProvider {
     dispatcher: Arc<RwLock<Box<dyn Dispatcher>>>,
 }

 const OP_HELLO: &str = "DoHello";

 impl Default for HelloProvider {
     fn default() -> Self {
         HelloProvider {
             dispatcher: Arc::new(RwLock::new(Box::new(NullDispatcher::new()))),
         }
     }
 }

 impl CapabilityProvider for HelloProvider {
     // Invoked by the runtime host to give this provider plugin the ability to communicate
     // with actors
     fn configure_dispatch(
         &self,
         dispatcher: Box<dyn Dispatcher>,
         ) -> Result<(), Box<dyn Error + Sync + Send>> {
        
         let mut lock = self.dispatcher.write().unwrap();
         *lock = dispatcher;
         Ok(())
     }

     // Invoked by host runtime to allow an actor to make use of the capability
     // All providers MUST handle the "configure" message, even if no work will be done
     fn handle_call(
            &self,
            actor: &str,
            op: &str,
            msg: &[u8],
        ) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>> {

        match op {
            OP_BIND_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
            OP_REMOVE_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
            OP_HEALTH_REQUEST if actor == SYSTEM_ACTOR =>
                Ok(serialize(HealthCheckResponse {
                  healthy: true,
                  message: "".to_string(),
                })
               .unwrap()),
            OP_HELLO => Ok(b"Hello, World".to_vec()),
            _ => Err(format!("Unknown operation: {}", op).into()),
         }
     }

        // No cleanup needed on stop
        fn stop(&self) {}
    }

Dependencies

~1–1.6MB
~36K SLoC