Merge branch dyn_inherent into master
This is a squashed commit of the following: commit 6b7c04bc1aae470d9c95b9c5f4e5698bd8c8fef0 Author: Amber <[email protected]> Date: Tue Sep 26 14:03:52 2023 -0500 Give meta types better ergonomics and location
parent: tbd commit: f94279e
1 | use ; |
2 | |
3 | use FutureExt; |
4 | use |
5 | , | AuthenticatedPayload
6 | , |
7 | , | Instance
8 | , | GiteratedMessage
9 | |
10 | AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, |
11 | , |
12 | , | ObjectBackend
13 | , |
14 | , |
15 | , |
16 | ; |
17 | |
18 | use trace; |
19 | |
20 | use crate:: |
21 | GiteratedOperationHandler, ObjectMeta, ObjectOperationPair, ObjectValuePair, OperationMeta, |
22 | OperationWrapper, SettingMeta, StackOperationState, ValueMeta, |
23 | ; |
24 | |
25 | /// Temporary name for the next generation of Giterated stack |
26 | |
27 | |
28 | operation_handlers: , |
29 | value_getters: , |
30 | setting_getters: , |
31 | metadata: RuntimeMetadata, |
32 | |
33 | |
34 | |
35 | |
36 | &mut self, |
37 | builder: , |
38 | |
39 | for in builder.operation_handlers |
40 | let tree = self.get_or_create_tree; |
41 | |
42 | tree.push; |
43 | |
44 | |
45 | for in builder.value_getters |
46 | assert!; |
47 | |
48 | |
49 | for in builder.setting_getters |
50 | assert!; |
51 | |
52 | |
53 | self.metadata.append; |
54 | |
55 | self |
56 | |
57 | |
58 | |
59 | if self.operation_handlers.contains_key |
60 | self.operation_handlers.get_mut .unwrap |
61 | else |
62 | self.operation_handlers |
63 | .insert; |
64 | |
65 | self.operation_handlers.get_mut .unwrap |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | elements: , |
72 | |
73 | |
74 | |
75 | |
76 | Self |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | self.elements.push; |
83 | |
84 | |
85 | pub async |
86 | &self, |
87 | object: & , |
88 | operation: & , |
89 | operation_state: &StackOperationState, |
90 | |
91 | for handler in self.elements.iter |
92 | match handler.handle .await |
93 | Ok => return Ok, |
94 | Err => match err |
95 | => | Operation
96 | return Err |
97 | |
98 | => return Err, | Internal
99 | _ => |
100 | continue; |
101 | |
102 | , |
103 | |
104 | |
105 | |
106 | Err |
107 | |
108 | |
109 | |
110 | /// Stores runtime metadata for all in-use Giterated protocol types. |
111 | |
112 | |
113 | objects: , |
114 | operations: , |
115 | values: , |
116 | settings: , |
117 | |
118 | |
119 | /// Defines a type that is a valid Giterated runtime state. |
120 | /// |
121 | /// This allows for extraction of state in handlers, based on a |
122 | /// [`FromOperationState<S>`] impl on (what is in this case) [`Self`]. |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | operation_handlers: , |
129 | value_getters: , |
130 | setting_getters: , |
131 | metadata: RuntimeMetadata, |
132 | state: S, |
133 | |
134 | |
135 | |
136 | |
137 | Self |
138 | operation_handlers: Default default, |
139 | value_getters: Default default, |
140 | setting_getters: Default default, |
141 | metadata: Default default, |
142 | state, |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | /// Insert an operation handler into the runtime builder. |
149 | /// |
150 | /// # Type Registration |
151 | /// Inserting the handler will automatically, if required, register the operation type of the |
152 | /// handler. It will **not** register the object type automatically. |
153 | |
154 | |
155 | O: GiteratedObject + 'static, |
156 | D: + 'static + Clone, |
157 | H: + 'static + Clone + Send + Sync, |
158 | : Send + Sync, | Failure
159 | : Send + Sync, | Success
160 | |
161 | let object_name = handler.object_name .to_string; |
162 | let operation_name = handler.operation_name .to_string; |
163 | |
164 | let wrapped = new; |
165 | |
166 | let pair = ObjectOperationPair |
167 | object_name, |
168 | operation_name, |
169 | ; |
170 | |
171 | self.operation_handlers.insert; |
172 | |
173 | self.metadata.; |
174 | |
175 | self |
176 | |
177 | |
178 | /// Register a [`GiteratedObject`] type with the runtime. |
179 | /// |
180 | /// # Type Registration |
181 | /// This will register the provided object type. |
182 | |
183 | self.metadata.; |
184 | |
185 | // Insert handler so ObjectRequest is handled properly |
186 | let handler = move |_object: &Instance, |
187 | operation: ObjectRequest, |
188 | _state: S, |
189 | _operation_state: StackOperationState, |
190 | stack: | |
191 | async move |
192 | for in stack.metadata.objects.iter |
193 | if .is_ok |
194 | return Ok; |
195 | |
196 | |
197 | |
198 | Err |
199 | |
200 | .boxed_local |
201 | ; |
202 | |
203 | self.operation; |
204 | |
205 | self |
206 | |
207 | |
208 | /// Register a [`Setting`] type with the runtime. |
209 | /// |
210 | /// # Type Registration |
211 | /// This will register the provided setting type. |
212 | |
213 | self.metadata.; |
214 | |
215 | self |
216 | |
217 | |
218 | /// Register a [`GiteratedObjectValue<O>`] type with the runtime, providing |
219 | /// its associated handler for [`GetValue`]. |
220 | /// |
221 | /// # Type Registration |
222 | /// This will register the provided [`GiteratedObjectValue`] type for its matching / specified |
223 | /// object type. It will **not** register the object type automatically. |
224 | |
225 | |
226 | O: GiteratedObject + 'static, |
227 | V: + 'static + Clone, |
228 | F: + Clone + 'static + Send + Sync, |
229 | |
230 | let object_name = handler.object_name .to_string; |
231 | let value_name = value_name .to_string; |
232 | |
233 | let wrapped = new; |
234 | |
235 | let handler_object_name = object_name.clone; |
236 | let handler_value_name = value_name.clone; |
237 | |
238 | // Insert handler so GetValue is handled properly |
239 | let _handler = move |object: &O, |
240 | operation: , |
241 | _state: S, |
242 | operation_state: StackOperationState, |
243 | stack: | |
244 | let stack = stack.clone; |
245 | let object_name = handler_object_name; |
246 | let value_name = handler_value_name; |
247 | let object = object.clone; |
248 | async move |
249 | for in stack.value_getters.iter |
250 | if target.object_kind != object_name |
251 | continue; |
252 | |
253 | |
254 | if target.value_kind != value_name |
255 | continue; |
256 | |
257 | |
258 | return match getter |
259 | .handle |
260 | &, |
261 | & |
262 | value_name: operation.value_name, |
263 | ty: Default default, |
264 | as , |
265 | &operation_state, |
266 | |
267 | .await |
268 | Ok => Ok, |
269 | Err => Err |
270 | => Operation, | Operation
271 | => Internal, | Internal
272 | => Unhandled, | Unhandled
273 | , |
274 | |
275 | |
276 | |
277 | Err |
278 | |
279 | .boxed_local |
280 | ; |
281 | |
282 | assert! |
283 | .value_getters |
284 | .insert |
285 | ObjectValuePair |
286 | object_kind: object_name, |
287 | value_kind: value_name |
288 | , |
289 | wrapped |
290 | |
291 | .is_none; |
292 | |
293 | self.metadata.; |
294 | |
295 | self |
296 | |
297 | |
298 | /// Register a handler for [`GetSetting`] for it's associated object type. |
299 | |
300 | |
301 | O: GiteratedObject + 'static, |
302 | F: + Clone + 'static + Send + Sync, |
303 | |
304 | let object_name = handler.object_name .to_string; |
305 | |
306 | let wrapped = new; |
307 | |
308 | assert!; |
309 | |
310 | self |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | let object_name = object_name .to_string; |
317 | |
318 | let object_meta = ; |
319 | |
320 | if self.objects.insert .is_some |
321 | trace! |
322 | "Registration of object {} overwrote previous registration.", |
323 | object_name |
324 | ; |
325 | else |
326 | trace! |
327 | |
328 | |
329 | |
330 | |
331 | &mut self, |
332 | |
333 | let object_name = object_name .to_string; |
334 | let operation_name = operation_name .to_string; |
335 | |
336 | if self |
337 | .operations |
338 | .insert |
339 | ObjectOperationPair |
340 | object_name: object_name.clone, |
341 | operation_name: operation_name.clone, |
342 | , |
343 | , |
344 | |
345 | .is_some |
346 | |
347 | trace! |
348 | "Registration of object operation {}<{}> overwrote previous registration.", |
349 | , | operation_name
350 | object_name |
351 | ; |
352 | else |
353 | trace! |
354 | "Registration of object operation {}<{}>.", |
355 | , | operation_name
356 | object_name |
357 | |
358 | |
359 | |
360 | |
361 | |
362 | O: GiteratedObject + 'static, |
363 | V: + 'static, |
364 | > |
365 | &mut self, |
366 | |
367 | let object_name = object_name .to_string; |
368 | let value_name = value_name .to_string; |
369 | |
370 | if self |
371 | .values |
372 | .insert |
373 | ObjectValuePair |
374 | object_kind: object_name.clone, |
375 | value_kind: value_name.clone, |
376 | , |
377 | , |
378 | |
379 | .is_some |
380 | |
381 | trace! |
382 | "Registration of value <{}>::{} overwrote previous registration.", |
383 | , | object_name
384 | value_name |
385 | ; |
386 | else |
387 | trace! |
388 | "Registration of value <{}>::{}.", |
389 | , | object_name
390 | value_name |
391 | ; |
392 | |
393 | |
394 | |
395 | |
396 | let setting_name = name .to_string; |
397 | |
398 | if self |
399 | .settings |
400 | .insert |
401 | .is_some |
402 | |
403 | trace! |
404 | "Registration of setting {} overwrote previous registration.", |
405 | name |
406 | ; |
407 | else |
408 | trace!; |
409 | |
410 | |
411 | |
412 | |
413 | self.objects.extend; |
414 | self.operations.extend; |
415 | self.values.extend; |
416 | self.settings.extend; |
417 | |
418 | |
419 | |
420 | /// Handles a giterated network message, returning either a raw success |
421 | /// payload or a serialized error payload. |
422 | pub async |
423 | &self, |
424 | message: AuthenticatedPayload, |
425 | operation_state: &StackOperationState, |
426 | |
427 | let message: = message.into_message; |
428 | |
429 | // Deserialize the object, also getting the object type's name |
430 | let = |
431 | let mut result = None; |
432 | |
433 | for in self.metadata.objects.iter |
434 | if let Ok = |
435 | result = Some; |
436 | break; |
437 | |
438 | |
439 | |
440 | result |
441 | |
442 | .ok_or_else?; |
443 | |
444 | trace! |
445 | "Handling network message {}::<{}>", |
446 | message.operation, |
447 | object_type |
448 | ; |
449 | |
450 | if message.operation == "get_value" |
451 | // Special case |
452 | let operation: GetValue = from_slice .unwrap; |
453 | |
454 | return self |
455 | .network_get_value |
456 | .await; |
457 | else if message.operation == "get_setting" |
458 | let operation: GetSetting = from_slice .unwrap; |
459 | let setting_meta = self |
460 | .metadata |
461 | .settings |
462 | .get |
463 | .ok_or_else?; |
464 | let raw_result = self |
465 | .get_setting |
466 | .await; |
467 | return match raw_result |
468 | Ok => |
469 | // Success is the setting type, serialize it |
470 | let serialized = .unwrap; |
471 | |
472 | Ok |
473 | |
474 | Err => Err |
475 | => | Operation
476 | // We know how to resolve this type |
477 | let failure: GetSettingError = *failure.downcast .unwrap; |
478 | |
479 | Operation |
480 | |
481 | => Internal, | Internal
482 | => Unhandled, | Unhandled
483 | , |
484 | ; |
485 | |
486 | |
487 | let target = ObjectOperationPair |
488 | object_name: object_type.clone, |
489 | operation_name: message.operation.clone, |
490 | ; |
491 | |
492 | // Resolve the target operations from the handlers table |
493 | let handler = self |
494 | .operation_handlers |
495 | .get |
496 | .ok_or_else?; |
497 | |
498 | trace! |
499 | "Resolved operation handler for network message {}::<{}>", |
500 | message.operation, |
501 | object_type |
502 | ; |
503 | |
504 | // Deserialize the operation |
505 | let meta = self |
506 | .metadata |
507 | .operations |
508 | .get |
509 | .ok_or_else?; |
510 | |
511 | let operation = |
512 | .map_err?; |
513 | |
514 | trace! |
515 | "Deserialized operation for network message {}::<{}>", |
516 | message.operation, |
517 | object_type |
518 | ; |
519 | |
520 | trace! |
521 | "Calling handler for network message {}::<{}>", |
522 | message.operation, |
523 | object_type |
524 | ; |
525 | |
526 | // Get the raw result of the operation, where the return values are boxed. |
527 | let raw_result = handler.handle .await; |
528 | |
529 | trace! |
530 | "Finished handling network message {}::<{}>", |
531 | message.operation, |
532 | object_type |
533 | ; |
534 | |
535 | // Deserialize the raw result for the network |
536 | match raw_result |
537 | Ok => Ok |
538 | .map_err?, |
539 | Err => Err |
540 | => Operation | Operation
541 | |
542 | .map_err?, |
543 | , |
544 | => Internal, | Internal
545 | => Unhandled, | Unhandled
546 | , |
547 | |
548 | |
549 | |
550 | pub async |
551 | &self, |
552 | object: , |
553 | object_kind: String, |
554 | operation: GetValue, |
555 | operation_state: &StackOperationState, |
556 | |
557 | trace!; |
558 | |
559 | let value_meta = self |
560 | .metadata |
561 | .values |
562 | .get |
563 | object_kind: object_kind.clone, |
564 | value_kind: operation.value_name.clone, |
565 | |
566 | .ok_or_else?; |
567 | |
568 | for in self.value_getters.iter |
569 | if target.object_kind != object_kind |
570 | continue; |
571 | |
572 | |
573 | if target.value_kind != operation.value_name |
574 | continue; |
575 | |
576 | |
577 | return match getter |
578 | .handle |
579 | .await |
580 | |
581 | Ok => |
582 | // Serialize success, which is the value type itself |
583 | let serialized = |
584 | .map_err?; |
585 | |
586 | Ok |
587 | |
588 | Err => Err |
589 | => | Operation
590 | // Failure is sourced from GetValue operation, but this is hardcoded for now |
591 | let failure: GetValueError = *failure.downcast .unwrap; |
592 | |
593 | Operation |
594 | to_vec |
595 | .map_err?, |
596 | |
597 | |
598 | => Internal, | Internal
599 | => Unhandled, | Unhandled
600 | , |
601 | ; |
602 | |
603 | |
604 | Err |
605 | |
606 | |
607 | pub async |
608 | &self, |
609 | object: , |
610 | object_kind: String, |
611 | operation: GetSetting, |
612 | operation_state: &StackOperationState, |
613 | |
614 | trace! |
615 | "Handling network {}::get_setting for {}", |
616 | object_kind, |
617 | operation.setting_name |
618 | ; |
619 | |
620 | let setting_getter = self |
621 | .setting_getters |
622 | .get |
623 | .ok_or_else?; |
624 | |
625 | setting_getter |
626 | .handle |
627 | .await |
628 | |
629 | |
630 | pub async |
631 | &self, |
632 | _operation: SetSetting, |
633 | _operation_state: &StackOperationState, |
634 | |
635 | todo! |
636 | |
637 | |
638 | |
639 | use Debug; |
640 | |
641 | |
642 | |
643 | async |
644 | &self, |
645 | in_object: O, |
646 | operation_name: &str, |
647 | payload: D, |
648 | operation_state: &StackOperationState, |
649 | |
650 | |
651 | O: GiteratedObject + Debug + 'static, |
652 | D: + Debug + 'static, |
653 | |
654 | // Erase object and operation types. |
655 | let object = Box new as ; |
656 | let operation = Box new as ; |
657 | |
658 | // We need to hijack get_value, set_setting, and get_setting. |
659 | if operation_name == "get_value" |
660 | let mut value_meta = None; |
661 | for in self.metadata.values.iter |
662 | if |
663 | value_meta = Some; |
664 | break; |
665 | |
666 | |
667 | |
668 | let value_meta = value_meta.ok_or_else?; |
669 | |
670 | let value_name = value_meta.name.clone; |
671 | |
672 | trace! |
673 | "Handling get_value for {}::{}", |
674 | , | object_name
675 | value_name |
676 | ; |
677 | |
678 | for in self.value_getters.iter |
679 | if target.object_kind != object_name |
680 | continue; |
681 | |
682 | |
683 | if target.value_kind != value_name |
684 | continue; |
685 | |
686 | |
687 | return match getter |
688 | .handle |
689 | .await |
690 | |
691 | Ok => Ok, |
692 | Err => Err |
693 | => | Operation
694 | Operation |
695 | |
696 | => Internal, | Internal
697 | => Unhandled, | Unhandled
698 | , |
699 | ; |
700 | |
701 | |
702 | return Err; |
703 | else if operation. |
704 | let get_setting: = operation.downcast .unwrap; |
705 | |
706 | let raw_result = self |
707 | .get_setting |
708 | object, |
709 | to_string, | object_name .
710 | *get_setting, |
711 | operation_state, |
712 | |
713 | .await; |
714 | |
715 | return match raw_result |
716 | Ok => |
717 | // Success is the setting type, serialize it |
718 | // let serialized = (setting_meta.serialize)(success).unwrap(); |
719 | |
720 | // Ok(serde_json::to_vec(&serialized).unwrap()) |
721 | Ok |
722 | |
723 | Err => Err |
724 | => | Operation
725 | // We know this is the right type |
726 | Operation |
727 | |
728 | => Internal, | Internal
729 | => Unhandled, | Unhandled
730 | , |
731 | ; |
732 | else if operation. |
733 | todo! |
734 | else if operation. |
735 | todo! |
736 | |
737 | |
738 | // Resolve the operation from the known operations table. |
739 | let operation_type = |
740 | let mut operation_type = None; |
741 | |
742 | for in self.metadata.operations.iter |
743 | // Skip elements that we know will not match |
744 | if target.object_name != object_name |
745 | continue; |
746 | |
747 | |
748 | if target.operation_name != operation_name |
749 | continue; |
750 | |
751 | |
752 | if |
753 | operation_type = Some; |
754 | break; |
755 | |
756 | |
757 | |
758 | operation_type |
759 | |
760 | .ok_or_else?; |
761 | |
762 | // Resolve the handler from our handler tree |
763 | let handler_tree = self |
764 | .operation_handlers |
765 | .get |
766 | .ok_or_else?; |
767 | |
768 | let raw_result = handler_tree |
769 | .handle |
770 | .await; |
771 | |
772 | // Convert the dynamic result back into its concrete type |
773 | match raw_result |
774 | Ok => Ok, |
775 | Err => Err |
776 | => Internal, | Internal
777 | => | Operation
778 | Operation |
779 | |
780 | => Unhandled, | Unhandled
781 | , |
782 | |
783 | |
784 | |
785 | async |
786 | &self, |
787 | object_str: &str, |
788 | _operation_state: &StackOperationState, |
789 | |
790 | // TODO: Authorization? |
791 | for in self.metadata.objects.iter |
792 | if let Ok = |
793 | return Ok |
794 | new_unchecked |
795 | ; |
796 | |
797 | |
798 | |
799 | Err |
800 | |
801 | |
802 |