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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
pub type Result<T> = ::std::result::Result<T, crate::errors::Error>;
pub type ReceiveResult = ::std::result::Result<Vec<u8>, Box<dyn std::error::Error>>;
pub extern crate prost;
pub extern crate wapc_guest as wapc;
use crate::kv::DefaultKeyValueStore;
use crate::msg::DefaultMessageBroker;
use crate::objectstore::DefaultObjectStore;
use crate::raw::DefaultRawCapability;
use events::DefaultEventStreams;
use extras::DefaultExtras;
use std::collections::HashMap;
use wapc_guest::console_log;
use wascc_codec::blobstore::{Blob, BlobList, Container, Transfer};
use wascc_codec::eventstreams::Event;
pub fn protobytes(msg: impl prost::Message) -> Result<Vec<u8>> {
let mut buf = Vec::new();
msg.encode(&mut buf)?;
Ok(buf)
}
#[macro_export]
macro_rules! actor_receive {
($user_handler:ident) => {
use $crate::wapc::prelude::*;
wapc_handler!(handle_wapc);
fn handle_wapc(operation: &str, msg: &[u8]) -> CallResult {
let ctx = $crate::CapabilitiesContext::new();
$user_handler(&ctx, &operation, msg).map_err(|e| e.into())
}
};
}
pub trait KeyValueStore {
fn get(&self, key: &str) -> Result<Option<String>>;
fn set(&self, key: &str, value: &str, expires: Option<u32>) -> Result<()>;
fn atomic_add(&self, key: &str, value: i32) -> Result<i32>;
fn list_add(&self, key: &str, item: &str) -> Result<usize>;
fn list_del_item(&self, key: &str, item: &str) -> Result<usize>;
fn del_key(&self, key: &str) -> Result<()>;
fn list_range(&self, key: &str, start: isize, stop_inclusive: isize) -> Result<Vec<String>>;
fn list_clear(&self, key: &str) -> Result<()>;
fn set_add(&self, key: &str, value: &str) -> Result<usize>;
fn set_remove(&self, key: &str, value: &str) -> Result<usize>;
fn set_union(&self, keys: Vec<String>) -> Result<Vec<String>>;
fn set_intersect(&self, keys: Vec<String>) -> Result<Vec<String>>;
fn set_members(&self, key: &str) -> Result<Vec<String>>;
fn exists(&self, key: &str) -> Result<bool>;
}
pub trait Extras {
fn get_random(&self, min: u32, max: u32) -> Result<u32>;
fn get_guid(&self) -> Result<String>;
fn get_sequence_number(&self) -> Result<u64>;
}
pub trait EventStreams {
fn write_event(&self, stream: &str, values: HashMap<String, String>) -> Result<String>;
fn read_all(&self, stream: &str) -> Result<Vec<Event>>;
}
pub trait MessageBroker {
fn publish(&self, subject: &str, reply_to: Option<&str>, payload: &[u8]) -> Result<()>;
fn request(&self, subject: &str, payload: &[u8], timeout_ms: u64) -> Result<Vec<u8>>;
}
pub trait ObjectStore {
fn create_container(&self, name: &str) -> Result<Container>;
fn remove_container(&self, name: &str) -> Result<()>;
fn remove_object(&self, id: &str, container: &str) -> Result<()>;
fn list_objects(&self, container: &str) -> Result<BlobList>;
fn get_blob_info(&self, container: &str, id: &str) -> Result<Option<Blob>>;
fn start_upload(&self, blob: &Blob, chunk_size: u64, total_bytes: u64) -> Result<Transfer>;
fn upload_chunk(&self, transfer: &Transfer, offset: u64, bytes: &[u8]) -> Result<()>;
fn start_download(&self, blob: &Blob, chunk_size: u64) -> Result<Transfer>;
}
pub trait RawCapability {
fn call(&self, capid: &str, operation: &str, msg: &[u8]) -> Result<Vec<u8>>;
}
pub struct CapabilitiesContext {
kv: Box<dyn KeyValueStore>,
msg: Box<dyn MessageBroker>,
raw: Box<dyn RawCapability>,
blob: Box<dyn ObjectStore>,
extras: Box<dyn Extras>,
events: Box<dyn EventStreams>,
}
impl Default for CapabilitiesContext {
fn default() -> CapabilitiesContext {
CapabilitiesContext {
kv: Box::new(DefaultKeyValueStore::new()),
msg: Box::new(DefaultMessageBroker::new()),
raw: Box::new(DefaultRawCapability::new()),
blob: Box::new(DefaultObjectStore::new()),
extras: Box::new(DefaultExtras::new()),
events: Box::new(DefaultEventStreams::new()),
}
}
}
impl CapabilitiesContext {
pub fn new() -> CapabilitiesContext {
CapabilitiesContext {
kv: Box::new(DefaultKeyValueStore::new()),
msg: Box::new(DefaultMessageBroker::new()),
raw: Box::new(DefaultRawCapability::new()),
blob: Box::new(DefaultObjectStore::new()),
extras: Box::new(DefaultExtras::new()),
events: Box::new(DefaultEventStreams::new()),
}
}
pub fn custom(
kv: impl KeyValueStore + 'static,
msg: impl MessageBroker + 'static,
raw: impl RawCapability + 'static,
blob: impl ObjectStore + 'static,
extras: impl Extras + 'static,
events: impl EventStreams + 'static,
) -> Self {
CapabilitiesContext {
kv: Box::new(kv),
msg: Box::new(msg),
raw: Box::new(raw),
blob: Box::new(blob),
extras: Box::new(extras),
events: Box::new(events),
}
}
pub fn kv(&self) -> &dyn KeyValueStore {
self.kv.as_ref()
}
pub fn msg(&self) -> &dyn MessageBroker {
self.msg.as_ref()
}
pub fn raw(&self) -> &dyn RawCapability {
self.raw.as_ref()
}
pub fn objectstore(&self) -> &dyn ObjectStore {
self.blob.as_ref()
}
pub fn extras(&self) -> &dyn Extras {
self.extras.as_ref()
}
pub fn events(&self) -> &dyn EventStreams {
self.events.as_ref()
}
pub fn log(&self, msg: &str) {
console_log(msg);
}
}
pub mod errors;
pub mod events;
pub mod extras;
pub mod kv;
pub mod msg;
pub mod objectstore;
pub mod prelude;
pub mod raw;