wasmcloud_provider_core/
lib.rs

1#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
2
3//! # wasmcloud Provider Core
4//!
5//! This library provides the core set of types and associated functions used for
6//! the common set of functionality required for the wasmcloud host to manipulate
7//! capability providers and for developers to create their own providers.
8//!
9//! # Example
10//! The following illustrates an example of the simplest capability provider
11//!```
12//!
13//! use wasmcloud_provider_core as provider;
14//! use wasmcloud_actor_core as actor;
15//! use provider::{CapabilityProvider, Dispatcher, NullDispatcher, serialize,
16//!             core::{OP_BIND_ACTOR, OP_HEALTH_REQUEST, OP_REMOVE_ACTOR, SYSTEM_ACTOR}};
17//! use actor::{CapabilityConfiguration, HealthCheckResponse};
18//! use std::sync::{Arc, RwLock};
19//! use std::error::Error;
20//!
21//! // Hello world implementation of the `demo:hello` capability provider
22//! #[derive(Clone)]
23//! pub struct HelloProvider {
24//!     dispatcher: Arc<RwLock<Box<dyn Dispatcher>>>,
25//! }
26//!
27//! const OP_HELLO: &str = "DoHello";
28//!
29//! impl Default for HelloProvider {
30//!     fn default() -> Self {
31//!         HelloProvider {
32//!             dispatcher: Arc::new(RwLock::new(Box::new(NullDispatcher::new()))),
33//!         }
34//!     }
35//! }
36//!
37//! impl CapabilityProvider for HelloProvider {
38//!     // Invoked by the runtime host to give this provider plugin the ability to communicate
39//!     // with actors
40//!     fn configure_dispatch(
41//!         &self,
42//!         dispatcher: Box<dyn Dispatcher>,
43//!         ) -> Result<(), Box<dyn Error + Sync + Send>> {
44//!        
45//!         let mut lock = self.dispatcher.write().unwrap();
46//!         *lock = dispatcher;
47//!         Ok(())
48//!     }
49//!
50//!     // Invoked by host runtime to allow an actor to make use of the capability
51//!     // All providers MUST handle the "configure" message, even if no work will be done
52//!     fn handle_call(
53//!            &self,
54//!            actor: &str,
55//!            op: &str,
56//!            msg: &[u8],
57//!        ) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>> {
58//!
59//!        match op {
60//!            OP_BIND_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
61//!            OP_REMOVE_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
62//!            OP_HEALTH_REQUEST if actor == SYSTEM_ACTOR =>
63//!                Ok(serialize(HealthCheckResponse {
64//!                  healthy: true,
65//!                  message: "".to_string(),
66//!                })
67//!               .unwrap()),
68//!            OP_HELLO => Ok(b"Hello, World".to_vec()),
69//!            _ => Err(format!("Unknown operation: {}", op).into()),
70//!         }
71//!     }
72//!
73//!        // No cleanup needed on stop
74//!        fn stop(&self) {}
75//!    }
76//!
77//!```
78//!
79
80pub use capabilities::*;
81
82use rmp_serde::{Deserializer, Serializer};
83use serde::{Deserialize, Serialize};
84use std::io::Cursor;
85
86/// The agreed-upon standard for payload serialization (message pack)
87pub fn serialize<T>(
88    item: T,
89) -> ::std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>
90where
91    T: Serialize,
92{
93    let mut buf = Vec::new();
94    item.serialize(&mut Serializer::new(&mut buf).with_struct_map())?;
95    Ok(buf)
96}
97
98/// The agreed-upon standard for payload de-serialization (message pack)
99pub fn deserialize<'de, T: Deserialize<'de>>(
100    buf: &[u8],
101) -> ::std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
102    let mut de = Deserializer::new(Cursor::new(buf));
103    match Deserialize::deserialize(&mut de) {
104        Ok(t) => Ok(t),
105        Err(e) => Err(format!("Failed to de-serialize: {}", e).into()),
106    }
107}
108
109pub mod capabilities;
110pub mod core;