waxosuit_codec/
lib.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//! # wascap-codec
16//! 
17//! This library provides the core set of types and associated functions used to facilitate guest module
18//! and host runtime communication for Waxosuit. 
19
20#[macro_use]
21extern crate serde_derive;
22
23pub mod http {
24    //! # HTTP server capability data structures
25    //! 
26    //! This module contains data types for the `wascap:http_server` capability.
27    include!(concat!(env!("OUT_DIR"), "/http.rs"));
28
29    use prost::Message;
30    use serde::ser::Serialize;
31    use std::collections::HashMap;
32
33    pub const OP_PERFORM_REQUEST: &str = "wascap:httpclient!PerformRequest";
34    pub const OP_HANDLE_REQUEST: &str = "wascap:httpserver!HandleRequest";
35
36    impl Into<Request> for &[u8] {
37        fn into(self) -> Request {
38            Request::decode(self).unwrap()
39        }
40    }
41
42    impl Into<Response> for &[u8] {
43        fn into(self) -> Response {
44            Response::decode(self).unwrap()
45        }
46    }
47
48    impl Response {
49        /// Creates a response with a given status code and serializes the given payload as JSON
50        pub fn json<T>(payload: T, status_code: u32, status: &str) -> Response
51        where
52            T: Serialize,
53        {
54            Response {
55                body: serde_json::to_string(&payload).unwrap().into_bytes(),
56                header: HashMap::new(),
57                status: status.to_string(),
58                status_code,
59            }
60        }
61
62        /// Handy shortcut for creating a 404/Not Found response
63        pub fn not_found() -> Response {
64            Response {
65                status: "Not Found".to_string(),
66                status_code: 404,
67                ..Default::default()
68            }
69        }
70
71        /// Useful shortcut for creating a 200/OK response
72        pub fn ok() -> Response {
73            Response {
74                status: "OK".to_string(),
75                status_code: 200,
76                ..Default::default()
77            }
78        }
79
80        /// Useful shortcut for creating a 500/Internal Server Error response
81        pub fn internal_server_error(msg: &str) -> Response {
82            Response {
83                status: "Internal Server Error".to_string(),
84                status_code: 500,
85                body: msg.to_string().as_bytes().into(),
86                ..Default::default()
87            }
88        }
89
90        /// Shortcut for creating a 400/Bad Request response
91        pub fn bad_request() -> Response {
92            Response {
93                status: "Bad Request".to_string(),
94                status_code: 400,
95                ..Default::default()
96            }
97        }
98    }
99}
100
101pub mod core {
102//! # Core data types
103//! 
104//! This module contains data types used for Wascap guest module and host runtime communications
105    include!(concat!(env!("OUT_DIR"), "/core.rs"));
106
107    pub const OP_PERFORM_LIVE_UPDATE: &str = "wascap:httpsrv!PerformLiveUpdate";
108    pub const OP_HEALTH_REQUEST: &str = "wascap:httpsrv!HealthRequest";    
109
110}
111
112pub mod messaging {
113//! # Message Broker Data Types
114//! 
115//! This module contains data types for the `wascap:messaging` capability provider
116    use prost::Message;
117
118    include!(concat!(env!("OUT_DIR"), "/messaging.rs"));
119
120    pub const OP_PUBLISH_MESSAGE: &str = "wascap:messaging!Publish"; 
121    pub const OP_DELIVER_MESSAGE: &str = "wascap:messaging!DeliverMessage";   
122    pub const OP_PERFORM_REQUEST: &str = "wascap:messaging!Request";
123    
124    impl Into<DeliverMessage> for &[u8] {
125        fn into(self) -> DeliverMessage {
126            DeliverMessage::decode(self).unwrap()
127        }
128    }
129
130    impl Into<PublishMessage> for &[u8] {
131        fn into(self) -> PublishMessage {
132            PublishMessage::decode(self).unwrap()
133        }
134    } 
135
136    impl Into<RequestMessage> for &[u8] {
137        fn into(self) -> RequestMessage {
138            RequestMessage::decode(self).unwrap()
139        }
140    }   
141}
142
143pub mod keyvalue {
144//! # Key-Value Store Data Types
145//! 
146//! This module contains data types for the `wascap:keyvalue` capability provider
147    include!(concat!(env!("OUT_DIR"), "/keyvalue.rs"));
148
149    pub const OP_ADD: &str = "wascap:keyvalue!Add";
150    pub const OP_GET: &str = "wascap:keyvalue!Get";
151    pub const OP_SET: &str = "wascap:keyvalue!Set";
152    pub const OP_DEL: &str = "wascap:keyvalue!Del";
153    pub const OP_CLEAR: &str = "wascap:keyvalue!Clear";
154    pub const OP_RANGE: &str = "wascap:keyvalue!Range";
155    pub const OP_PUSH: &str = "wascap:keyvalue!Push";  
156    pub const OP_LIST_DEL: &str = "wascap:keyvalue!ListItemDelete";  
157
158    pub const OP_SET_ADD: &str = "wascap:keyvalue!SetAdd";
159    pub const OP_SET_REMOVE: &str = "wascap:keyvalue!SetRemove";
160    pub const OP_SET_UNION: &str = "wascap:keyvalue!SetUnion";
161    pub const OP_SET_INTERSECT: &str = "wascap:keyvalue!SetIntersection";
162    pub const OP_SET_QUERY: &str = "wascap:keyvalue!SetQuery";
163    pub const OP_KEY_EXISTS: &str = "wascap:keyvalue!KeyExists";
164}
165
166
167pub mod capabilities;
168