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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#![deny(missing_docs)]
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::time::Duration;
use protobuf::well_known_types::Any;
use protobuf::Message;
pub use function_registry::FunctionRegistry;
use statefun_proto::http_function::Address as ProtoAddress;
mod function_registry;
mod invocation_bridge;
pub mod io;
pub mod transport;
#[derive(Debug)]
pub struct Context<'a> {
state: &'a HashMap<String, Any>,
self_address: &'a ProtoAddress,
caller_address: &'a ProtoAddress,
}
impl<'a> Context<'a> {
fn new(
state: &'a HashMap<String, Any>,
self_address: &'a ProtoAddress,
caller_address: &'a ProtoAddress,
) -> Self {
Context {
state,
self_address,
caller_address,
}
}
pub fn self_address(&self) -> Address {
Address::from_proto(self.self_address)
}
pub fn caller_address(&self) -> Address {
Address::from_proto(self.caller_address)
}
pub fn get_state<T: Message>(&self, name: &str) -> Option<T> {
let state = self.state.get(name);
state.and_then(|serialized_state| {
let unpacked_state: Option<T> = unpack_state(name, serialized_state);
unpacked_state
})
}
}
fn unpack_state<T: Message>(state_name: &str, packed_state: &Any) -> Option<T> {
log::debug!("Packed state for {}: {:?}", state_name, packed_state);
let unpacked_state: Option<T> = packed_state
.unpack()
.expect("Could not unpack state from Any.");
unpacked_state
}
#[derive(Debug, PartialEq)]
pub struct Address {
pub function_type: FunctionType,
pub id: String,
}
impl Display for Address {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Address {}/{}", self.function_type, self.id)
}
}
impl Address {
pub fn new(function_type: FunctionType, id: &str) -> Self {
Address {
function_type,
id: id.to_owned(),
}
}
fn from_proto(proto_address: &ProtoAddress) -> Self {
Address {
function_type: FunctionType::new(
proto_address.get_namespace(),
proto_address.get_field_type(),
),
id: proto_address.get_id().to_owned(),
}
}
fn into_proto(self) -> ProtoAddress {
let mut result = ProtoAddress::new();
result.set_namespace(self.function_type.namespace);
result.set_field_type(self.function_type.name);
result.set_id(self.id);
result
}
}
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct FunctionType {
namespace: String,
name: String,
}
impl FunctionType {
pub fn new(namespace: &str, name: &str) -> FunctionType {
FunctionType {
namespace: namespace.to_string(),
name: name.to_string(),
}
}
}
impl Display for FunctionType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "FunctionType {}/{}", self.namespace, self.name)
}
}
#[derive(Default, Debug)]
pub struct Effects {
invocations: Vec<(Address, Any)>,
delayed_invocations: Vec<(Address, Duration, Any)>,
egress_messages: Vec<(EgressIdentifier, Any)>,
state_updates: Vec<StateUpdate>,
}
impl Effects {
pub fn new() -> Effects {
Effects {
invocations: Vec::new(),
delayed_invocations: Vec::new(),
egress_messages: Vec::new(),
state_updates: Vec::new(),
}
}
pub fn send<M: Message>(&mut self, address: Address, message: M) {
let packed_message = Any::pack(&message).unwrap();
self.invocations.push((address, packed_message));
}
pub fn send_after<M: Message>(&mut self, address: Address, delay: Duration, message: M) {
let packed_message = Any::pack(&message).unwrap();
self.delayed_invocations
.push((address, delay, packed_message));
}
pub fn egress<M: Message>(&mut self, identifier: EgressIdentifier, message: M) {
let packed_message = Any::pack(&message).unwrap();
self.egress_messages.push((identifier, packed_message));
}
pub fn delete_state(&mut self, name: &str) {
self.state_updates
.push(StateUpdate::Delete(name.to_owned()));
}
pub fn update_state<T: Message>(&mut self, name: &str, value: &T) {
self.state_updates.push(StateUpdate::Update(
name.to_owned(),
Any::pack(value).expect("Could not pack state update."),
));
}
}
#[derive(Debug)]
enum StateUpdate {
Update(String, Any),
Delete(String),
}
#[derive(Debug)]
pub struct EgressIdentifier {
namespace: String,
name: String,
}
impl EgressIdentifier {
pub fn new(namespace: &str, name: &str) -> EgressIdentifier {
EgressIdentifier {
namespace: namespace.to_string(),
name: name.to_string(),
}
}
}
impl Display for EgressIdentifier {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "EgressIdentifier {}/{}", self.namespace, self.name)
}
}