Finish unified stack refactor.
Adds support for operation state, which will be used to pass authentication information around. Added generic backend that uses a channel to communicate with a typed backend.
parent: tbd commit: d15581c
1 | |
2 | |
3 | |
4 | use ; |
5 | |
6 | use FutureExt; |
7 | use |
8 | , | OperationError
9 | , | Instance
10 | |
11 | AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, |
12 | , |
13 | , | ObjectBackend
14 | , |
15 | , | Repository
16 | , | User
17 | ; |
18 | use GiteratedBackend; |
19 | use ; |
20 | use Value; |
21 | use HandlerState; |
22 | use ; |
23 | use ; |
24 | |
25 | |
26 | |
27 | object_name: String, |
28 | operation_name: String, |
29 | |
30 | |
31 | |
32 | operations: , |
33 | get_object: , |
34 | |
35 | |
36 | |
37 | |
38 | Self |
39 | operations: new, |
40 | get_object: Vec new, |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | A, |
48 | O: GiteratedObject + Send + Sync, |
49 | D: + 'static, |
50 | H: + Send + Sync + 'static + Clone, |
51 | > |
52 | &mut self, |
53 | handler: H, |
54 | |
55 | let object_name = handler.object_name .to_string; |
56 | let operation_name = handler.operation_name .to_string; |
57 | |
58 | let wrapped = new; |
59 | |
60 | let pair = ObjectOperationPair |
61 | object_name, |
62 | operation_name, |
63 | ; |
64 | |
65 | assert!; |
66 | |
67 | self |
68 | |
69 | |
70 | |
71 | let closure = |
72 | async move |
73 | if from_str .is_ok |
74 | Ok |
75 | else |
76 | Err |
77 | |
78 | |
79 | .boxed |
80 | ; |
81 | |
82 | let wrapped = new; |
83 | |
84 | self.get_object.push; |
85 | |
86 | self |
87 | |
88 | |
89 | pub async |
90 | &self, |
91 | object: &O, |
92 | operation_name: &str, |
93 | operation: AnyOperation, |
94 | state: S, |
95 | operation_state: &StackOperationState, |
96 | |
97 | // TODO |
98 | let object = object.to_string; |
99 | |
100 | let object_name = |
101 | if from_str .is_ok |
102 | object_name |
103 | else if from_str .is_ok |
104 | object_name |
105 | else if from_str .is_ok |
106 | object_name |
107 | else |
108 | return Err; |
109 | |
110 | |
111 | .to_string; |
112 | |
113 | let target_handler = ObjectOperationPair |
114 | object_name, |
115 | operation_name: operation_name.to_string, |
116 | ; |
117 | |
118 | if let Some = self.operations.get |
119 | handler |
120 | .handle |
121 | AnyObject, |
122 | operation.clone, |
123 | state.clone, |
124 | operation_state, |
125 | |
126 | .await |
127 | else |
128 | Err |
129 | |
130 | |
131 | |
132 | pub async |
133 | &self, |
134 | instance: AnyObject, |
135 | request: ObjectRequest, |
136 | state: S, |
137 | operation_state: &StackOperationState, |
138 | |
139 | for handler in self.get_object.iter |
140 | if let Ok = handler |
141 | .handle |
142 | instance.clone, |
143 | AnyOperation, |
144 | state.clone, |
145 | operation_state, |
146 | |
147 | .await |
148 | |
149 | return Ok; |
150 | |
151 | |
152 | |
153 | Err |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | L, |
160 | O: GiteratedObject, |
161 | D: GiteratedOperation<O>, |
162 | S: Send + Sync + Clone, |
163 | > |
164 | |
165 | ; |
166 | ; |
167 | |
168 | async |
169 | &self, |
170 | object: &O, |
171 | operation: D, |
172 | state: S, |
173 | operation_state: &StackOperationState, |
174 | ; |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | F: FnMut( |
181 | &O, |
182 | D, |
183 | S, |
184 | ) -> |
185 | , |
186 | > + Send |
187 | + Sync |
188 | + Clone, |
189 | O: GiteratedObject + Send + Sync, |
190 | D: + 'static, |
191 | : Send, | Failure
192 | S: Send + Sync + Clone + 'static, |
193 | |
194 | |
195 | operation_name |
196 | |
197 | |
198 | |
199 | object_name |
200 | |
201 | |
202 | async |
203 | &self, |
204 | object: &O, |
205 | operation: D, |
206 | state: S, |
207 | _operation_state: &StackOperationState, |
208 | |
209 | self.clone .await |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | F: FnMut( |
217 | &O, |
218 | D, |
219 | S, |
220 | O1, |
221 | ) -> |
222 | , |
223 | > + Send |
224 | + Sync |
225 | + Clone, |
226 | O: GiteratedObject + Send + Sync, |
227 | D: + 'static, |
228 | : Send, | Failure
229 | S: Send + Sync + Clone + 'static, |
230 | O1: FromOperationState, |
231 | |
232 | |
233 | operation_name |
234 | |
235 | |
236 | |
237 | object_name |
238 | |
239 | |
240 | async |
241 | &self, |
242 | object: &O, |
243 | operation: D, |
244 | state: S, |
245 | operation_state: &StackOperationState, |
246 | |
247 | let o1 = O1 from_state |
248 | .await |
249 | .map_err?; |
250 | self.clone .await |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 | F: FnMut( |
258 | &O, |
259 | D, |
260 | S, |
261 | O1, |
262 | O2, |
263 | ) -> |
264 | , |
265 | > + Send |
266 | + Sync |
267 | + Clone, |
268 | O: GiteratedObject + Send + Sync, |
269 | D: + 'static, |
270 | : Send, | Failure
271 | S: Send + Sync + Clone + 'static, |
272 | O1: FromOperationState, |
273 | O2: FromOperationState, |
274 | |
275 | |
276 | operation_name |
277 | |
278 | |
279 | |
280 | object_name |
281 | |
282 | |
283 | async |
284 | &self, |
285 | object: &O, |
286 | operation: D, |
287 | state: S, |
288 | operation_state: &StackOperationState, |
289 | |
290 | let o1 = O1 from_state |
291 | .await |
292 | .map_err?; |
293 | let o2 = O2 from_state |
294 | .await |
295 | .map_err?; |
296 | self.clone .await |
297 | |
298 | |
299 | |
300 | |
301 | func: |
302 | dyn Fn( |
303 | AnyObject, |
304 | AnyOperation, |
305 | S, |
306 | StackOperationState, |
307 | ) |
308 | -> |
309 | + Send |
310 | + Sync, |
311 | >, |
312 | object_name: String, |
313 | |
314 | |
315 | |
316 | |
317 | A, |
318 | O: GiteratedObject + Send + Sync, |
319 | D: + 'static, |
320 | F: + Send + Sync + 'static + Clone, |
321 | > |
322 | handler: F, |
323 | |
324 | let handler = new; |
325 | Self |
326 | func: Box new |
327 | let handler = handler.clone; |
328 | async move |
329 | let handler = handler.clone; |
330 | let object: O = |
331 | map_err?; | from_object_str .
332 | let operation: D = from_value |
333 | .map_err?; |
334 | |
335 | let result = handler |
336 | .handle |
337 | .await; |
338 | result |
339 | .map |
340 | .map_err |
341 | => | Operation
342 | Operation |
343 | |
344 | => | Internal
345 | Internal |
346 | |
347 | => Unhandled, | Unhandled
348 | |
349 | |
350 | .boxed |
351 | , |
352 | object_name: object_name .to_string, |
353 | |
354 | |
355 | |
356 | async |
357 | &self, |
358 | object: AnyObject, |
359 | operation: AnyOperation, |
360 | state: S, |
361 | operation_state: &StackOperationState, |
362 | |
363 | .await |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | type Error: Serialize + DeserializeOwned; |
370 | |
371 | async ; |
372 | |
373 | |
374 | |
375 | |
376 | type Error = ; |
377 | |
378 | async |
379 | Ok |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | type Error = ; |
386 | |
387 | async |
388 | state: &StackOperationState, |
389 | |
390 | Ok |
391 | |
392 | |
393 | |
394 | |
395 | |
396 | pub giterated_backend: BackendWrapper, |
397 | |
398 | |
399 | |
400 | |
401 | sender: |
402 | , |
403 | WrappedOperation, |
404 | )>, |
405 | task: , |
406 | |
407 | |
408 | |
409 | object: AnyObject, |
410 | operation_payload: AnyOperation, |
411 | operation_name: String, |
412 | state: StackOperationState, |
413 | |
414 | |
415 | |
416 | |
417 | // Spawn listener task |
418 | |
419 | let = |
420 | , |
421 | WrappedOperation, |
422 | )>; |
423 | |
424 | let task = spawn |
425 | while let Some = recv.recv .await |
426 | let raw_result = backend |
427 | .object_operation |
428 | message.object, |
429 | &message.operation_name, |
430 | message.operation_payload, |
431 | &message.state, |
432 | |
433 | .await; |
434 | |
435 | responder.send .unwrap; |
436 | |
437 | error!; |
438 | ; |
439 | |
440 | Self |
441 | sender: send, |
442 | task: new, |
443 | |
444 | |
445 | |
446 | pub async |
447 | let = channel; |
448 | |
449 | self.sender |
450 | .send |
451 | .await |
452 | .map_err?; |
453 | |
454 | match response.await |
455 | Ok => Ok, |
456 | Err => Err, |
457 | |
458 | |
459 | |
460 | |
461 | use Debug; |
462 | |
463 | |
464 | |
465 | async |
466 | &self, |
467 | object: O, |
468 | operation: &str, |
469 | payload: D, |
470 | operation_state: &StackOperationState, |
471 | |
472 | |
473 | O: GiteratedObject + Debug, |
474 | D: + Debug, |
475 | |
476 | let operation = WrappedOperation |
477 | object: AnyObject, |
478 | operation_name: operation.to_string, |
479 | operation_payload: AnyOperation, |
480 | state: operation_state.clone, |
481 | ; |
482 | |
483 | let raw_result = self.call .await; |
484 | |
485 | match raw_result |
486 | Ok => Ok |
487 | .map_err?, |
488 | Err => match err |
489 | => | Internal
490 | warn! |
491 | "Internal Error: {:?}", |
492 | Internal |
493 | ; |
494 | |
495 | Err |
496 | |
497 | => Err, | Unhandled
498 | => Err | Operation
499 | from_value |
500 | .map_err?, |
501 | , |
502 | , |
503 | |
504 | |
505 | |
506 | async |
507 | &self, |
508 | object_str: &str, |
509 | operation_state: &StackOperationState, |
510 | |
511 | let operation = WrappedOperation |
512 | object: AnyObject, |
513 | operation_name: operation_name .to_string, |
514 | operation_payload: AnyOperation |
515 | unwrap, | to_value .
516 | , |
517 | state: operation_state.clone, |
518 | ; |
519 | |
520 | let raw_result = self.call .await; |
521 | |
522 | let object: ObjectResponse = match raw_result |
523 | Ok => Ok |
524 | .map_err?, |
525 | Err => match err |
526 | => | Internal
527 | warn! |
528 | "Internal Error: {:?}", |
529 | Internal |
530 | ; |
531 | |
532 | Err |
533 | |
534 | => Err, | Unhandled
535 | => Err | Operation
536 | from_value |
537 | .map_err?, |
538 | , |
539 | , |
540 | ?; |
541 | |
542 | unsafe |
543 | Ok |
544 | from_str |
545 | .map_err?, |
546 | self.clone, |
547 | |
548 | |
549 | |
550 | |
551 |