1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2015-2018 Capital One Services, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Raw capability provider interface
//! 
//! This module contains the raw capability struct. Use this facility
//! if you are building your own internal or proprietary capability provider that is not one of
//! the provider types exposed as part of the standard guest SDK. You might want to provide your own 
//! SDK that depends on this and provide a wrapper around the raw capability provider interface.

use std::rc::Rc;
use super::SOURCE_GUEST;
use crate::HostRuntimeInterface;
use crate::Result;
use wascap_codec as codec;
use codec::core::Event;

/// An implementation of the opaque capability provider which depends upon the host runtime
pub struct RawCapability {
    hri: Rc<&'static dyn HostRuntimeInterface>,
}

impl RawCapability {
    pub(crate) fn new(hri: Rc<&'static dyn HostRuntimeInterface>) -> RawCapability {
        RawCapability {
            hri,
        }
    }

    /// Performs a host call using the supplied payload as the protobuf `Any` message within 
    /// a core `Command`, returning an `Event`. You must supply a `type_url` that corresponds
    /// to the type of message contained in the `Any` protobuf. The value for `target_cap` 
    /// must _not_ use the `wascap:` prefix, instead you should supply your own prefix
    pub fn call(&self, target_cap: &str, payload: &[u8], type_url: &str) -> Result<Event> {        
        let any = prost_types::Any {
            type_url: type_url.to_string(),
            value: payload.to_vec(),
        };
        let cmd = codec::core::Command {
            source: SOURCE_GUEST.to_string(),
            target_cap: target_cap.to_string(),
            payload: Some(any),
        };        
        self.hri.do_host_call(&cmd)            
    }
}