Skip to main content

waxosuit_codec/
capabilities.rs

1// Copyright 2015-2018 Capital One Services, LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Traits used for capabilities and capability providers
16//! 
17//! 
18
19use std::any::Any;
20use std::error::Error;
21
22/// The dispatcher is used by a capability provider to send commands to a guest module, expecting
23/// an event in return
24pub trait Dispatcher: Any + Send + Sync {
25    fn dispatch(&self, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;
26}
27
28/// The NullDispatcher is as its name implies--a dispatcher that does nothing. This is convenient for
29/// initializing a capability provider with a null dispatcher, and then swapping it for a real dispatcher
30/// when the host runtime provides one tethered with the appropriate channels
31#[derive(Default)]
32pub struct NullDispatcher {}
33
34impl NullDispatcher {
35    pub fn new() -> NullDispatcher {
36        NullDispatcher {}
37    }
38}
39
40impl Dispatcher for NullDispatcher {
41    fn dispatch(&self, _op: &str, _msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
42        unimplemented!()
43    }
44}
45
46/// Every capability provider must implement this trait
47pub trait CapabilityProvider: Any + Send + Sync {
48    /// This function will be called on the provider when the host runtime is ready and has configured a dispatcher
49    fn configure_dispatch(
50        &self,
51        dispatcher: Box<dyn Dispatcher>,
52        identity: ModuleIdentity,
53    ) -> Result<(), Box<dyn Error>>;
54    /// The capability provider will return either one of the well-known capability IDs or a custom capability ID using `category:id` notation
55    fn capability_id(&self) -> &'static str;
56    /// The human-readable, friendly name of this capability provider. By convention, the provider should include its specific implementation,
57    /// .e.g. include "Redis" in the name for a Redis-based capability provider.
58    fn name(&self) -> &'static str;
59    /// This function is called by the host runtime when a guest module is requesting a command be executed by the capability provider
60    fn handle_call(&self, op: &str, msg: &[u8]) -> Result<Vec<u8>, Box<dyn Error>>;
61}
62
63/// Represents a unique identity for a guest module
64#[derive(Debug, Serialize, Deserialize, Clone)]
65pub struct ModuleIdentity {
66    /// The primary key of the module in prefix-encoded format (will start with the letter 'M')
67    pub module: String,
68    /// The primary key of the issuer/signer of the module. This will start with any one of the valid signer prefixes
69    pub issuer: String,
70    /// A list of capability attestations for the module
71    pub capabilities: Vec<String>,
72}
73
74/// Wraps a constructor inside an FFI function to allow the `CapabilityProvider` trait implementation
75/// to be instantiated and used by the host runtime
76#[macro_export]
77macro_rules! capability_provider {
78    ($provider_type:ty, $constructor:path) => {
79        #[no_mangle]
80        pub extern "C" fn __capability_provider_create(
81        ) -> *mut $crate::capabilities::CapabilityProvider {
82            let constructor: fn() -> $provider_type = $constructor;
83            let object = constructor();
84            let boxed: Box<$crate::capabilities::CapabilityProvider> = Box::new(object);
85            Box::into_raw(boxed)
86        }
87    };
88}