The long awaited, exhalted huge networking stack change.
parent: tbd commit: 21b6a72
1 | use Any; |
2 | |
3 | use Debug; |
4 | use PhantomData; |
5 | use Deref; |
6 | use FromStr; |
7 | use ; |
8 | |
9 | use Error; |
10 | use IntoInternalError; |
11 | use Instance; |
12 | use ObjectResponse; |
13 | use ; |
14 | |
15 | use GetValue; |
16 | use |
17 | , | OperationError
18 | , |
19 | , | ObjectBackend
20 | , | GiteratedOperation
21 | , |
22 | ; |
23 | |
24 | use ; |
25 | |
26 | use crate HandlerTree; |
27 | use crate MetadataProvider; |
28 | use crate:: |
29 | AnyFailure, AnyObject, AnyOperation, AnySetting, AnySuccess, AnyValue, HandlerResolvable, |
30 | HandlerWrapper, MissingValue, ObjectOperationPair, ObjectSettingPair, ObjectValuePair, |
31 | RuntimeMetadata, StackOperationState, SubstackBuilder, |
32 | ; |
33 | |
34 | pub type OperationHandler<OS> = |
35 | ; |
36 | |
37 | pub type ValueGetter<OS> = ; |
38 | |
39 | pub type SettingGetter<OS> = ; |
40 | |
41 | pub type ValueChange<OS> = ; |
42 | |
43 | pub type SettingChange<OS> = ; |
44 | |
45 | |
46 | |
47 | pub inner: , |
48 | |
49 | |
50 | |
51 | type Target = ; |
52 | |
53 | |
54 | &self.inner |
55 | |
56 | |
57 | |
58 | |
59 | operation_handlers: , |
60 | value_getters: , |
61 | setting_getters: , |
62 | value_change: , |
63 | setting_change: , |
64 | metadata_providers: , |
65 | pub metadata: RuntimeMetadata, |
66 | _marker: , |
67 | |
68 | |
69 | |
70 | |
71 | Self |
72 | operation_handlers: Default default, |
73 | value_getters: Default default, |
74 | setting_getters: Default default, |
75 | value_change: Default default, |
76 | setting_change: Default default, |
77 | metadata_providers: Default default, |
78 | metadata: Default default, |
79 | _marker: Default default, |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | f.debug_struct .finish |
87 | |
88 | |
89 | |
90 | |
91 | inner: , |
92 | |
93 | |
94 | |
95 | |
96 | : , |
97 | : , | Error
98 | |
99 | |
100 | let mut stack = Self |
101 | inner: Default default, |
102 | ; |
103 | |
104 | |
105 | ; |
106 | |
107 | let mut builder: = |
108 | ; | new
109 | |
110 | builder.; |
111 | |
112 | stack.merge_builder; |
113 | |
114 | stack |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | &mut self, |
121 | mut builder: , |
122 | |
123 | for in builder.operation_handlers |
124 | let tree = self.get_or_create_tree; |
125 | |
126 | tree.push; |
127 | |
128 | |
129 | for in builder.value_getters |
130 | assert!; |
131 | |
132 | |
133 | for in builder.setting_getters |
134 | assert!; |
135 | |
136 | |
137 | for in builder.value_change |
138 | self.inner.value_change.insert; |
139 | |
140 | |
141 | for in builder.setting_change |
142 | self.inner.setting_change.insert; |
143 | |
144 | |
145 | self.inner |
146 | .metadata_providers |
147 | .append; |
148 | |
149 | self.inner.metadata.append; |
150 | |
151 | self |
152 | |
153 | |
154 | |
155 | &mut self, |
156 | target: & , |
157 | |
158 | if self.inner.operation_handlers.contains_key |
159 | self.inner.operation_handlers.get_mut .unwrap |
160 | else |
161 | self.inner |
162 | .operation_handlers |
163 | .insert; |
164 | |
165 | self.inner.operation_handlers.get_mut .unwrap |
166 | |
167 | |
168 | |
169 | |
170 | GiteratedStack |
171 | inner: new, |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | pub async |
178 | |
179 | O: GiteratedObject + 'static, |
180 | |
181 | trace! |
182 | "value updated {}::{}", |
183 | , | object_name
184 | new_value.kind .value_kind |
185 | ; |
186 | let object = new; |
187 | |
188 | // First, resolve a handler for the exact object value pair |
189 | if let Some = self.inner.value_change.get |
190 | // TODO |
191 | let _ = handler |
192 | .handle |
193 | .await; |
194 | |
195 | |
196 | // We need to resolve for `any` object and `any` value combination |
197 | let target = ObjectValuePair |
198 | object_kind: "any", |
199 | value_kind: new_value.kind .value_kind, |
200 | ; |
201 | if let Some = self.inner.value_change.get |
202 | // TODO |
203 | let _ = handler |
204 | .handle |
205 | .await; |
206 | |
207 | |
208 | let target = ObjectValuePair |
209 | object_kind: object_name, |
210 | value_kind: "any", |
211 | ; |
212 | if let Some = self.inner.value_change.get |
213 | // TODO |
214 | let _ = handler |
215 | .handle |
216 | .await; |
217 | |
218 | |
219 | // Now resolve for both `any` |
220 | |
221 | let target = ObjectValuePair |
222 | object_kind: "any", |
223 | value_kind: "any", |
224 | ; |
225 | if let Some = self.inner.value_change.get |
226 | // TODO |
227 | let _ = handler |
228 | .handle |
229 | .await; |
230 | |
231 | |
232 | |
233 | pub async |
234 | |
235 | O: GiteratedObject + 'static, |
236 | S: Setting + 'static, |
237 | |
238 | trace!; |
239 | let target = ; |
240 | |
241 | if let Some = self.inner.setting_change.get |
242 | let _ = handler |
243 | .handle |
244 | , |
245 | operation_state.clone, |
246 | |
247 | .await; |
248 | |
249 | |
250 | |
251 | pub async |
252 | |
253 | O: GiteratedObject, |
254 | |
255 | // TODO |
256 | |
257 | |
258 | /// Writes a setting for the specified object. |
259 | pub async |
260 | &self, |
261 | object: &O, |
262 | setting: S, |
263 | |
264 | |
265 | O: GiteratedObject + 'static + Clone, |
266 | S: Setting + 'static + Clone, |
267 | |
268 | for provider in self.inner.metadata_providers.iter |
269 | if provider.provides_for |
270 | let setting_meta = self |
271 | .inner |
272 | .metadata |
273 | .settings |
274 | .get |
275 | .ok_or_else?; |
276 | |
277 | let object_meta = self |
278 | .inner |
279 | .metadata |
280 | .objects |
281 | .get |
282 | .ok_or_else?; |
283 | |
284 | let result = provider |
285 | .write |
286 | , | new
287 | object_meta, |
288 | , |
289 | setting_meta, |
290 | |
291 | .await |
292 | .as_internal_error_with_context; |
293 | |
294 | return result; |
295 | |
296 | |
297 | |
298 | Err |
299 | |
300 | |
301 | /// Gets a setting for the specified object. |
302 | pub async |
303 | |
304 | O: GiteratedObject + 'static + Clone, |
305 | S: Setting + 'static, |
306 | |
307 | for provider in self.inner.metadata_providers.iter |
308 | if provider.provides_for |
309 | trace! |
310 | "Resolving setting {} for object {} from provider.", |
311 | , | name
312 | object_name |
313 | ; |
314 | |
315 | let setting_meta = self |
316 | .inner |
317 | .metadata |
318 | .settings |
319 | .get |
320 | .ok_or_else?; |
321 | |
322 | let object_meta = self |
323 | .inner |
324 | .metadata |
325 | .objects |
326 | .get |
327 | .ok_or_else?; |
328 | |
329 | let value = provider |
330 | .read |
331 | .await |
332 | .as_internal_error_with_context?; |
333 | |
334 | return from_value |
335 | .as_internal_error_with_context; |
336 | |
337 | |
338 | trace! |
339 | "No provider registered for setting {} and object {}", |
340 | , | name
341 | object_name |
342 | ; |
343 | |
344 | Err |
345 | |
346 | |
347 | |
348 | |
349 | pub async |
350 | &self, |
351 | object: AnyObject, |
352 | object_kind: String, |
353 | operation: GetSetting, |
354 | _operation_state: &OS, |
355 | |
356 | trace! |
357 | "Handling network {}::get_setting for {}", |
358 | object_kind, |
359 | operation.setting_name |
360 | ; |
361 | |
362 | for provider in self.inner.metadata_providers.iter |
363 | if provider.provides_for |
364 | let setting_meta = self |
365 | .inner |
366 | .metadata |
367 | .settings |
368 | .get |
369 | object_kind: &object_kind, |
370 | setting_name: &operation.setting_name, |
371 | |
372 | .ok_or_else?; |
373 | |
374 | let object_meta = self |
375 | .inner |
376 | .metadata |
377 | .objects |
378 | .get |
379 | .ok_or_else?; |
380 | |
381 | let result = provider |
382 | .read |
383 | .await |
384 | .as_internal_error_with_context |
385 | "reading setting {}" |
386 | operation.setting_name |
387 | )?; |
388 | |
389 | return .as_internal_error_with_context |
390 | "deserializing setting {}" |
391 | operation.setting_name |
392 | ); |
393 | |
394 | |
395 | |
396 | trace!; |
397 | |
398 | Err |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | async |
405 | &self, |
406 | in_object: O, |
407 | _operation_name: &str, |
408 | payload: D, |
409 | operation_state: &OS, |
410 | |
411 | |
412 | O: GiteratedObject + Debug + 'static, |
413 | D: + Debug + 'static, |
414 | : Clone, | Success
415 | : Clone, | Failure
416 | |
417 | trace! |
418 | "Object operation for {}::{}", |
419 | , | object_name
420 | operation_name |
421 | ; |
422 | // Erase object and operation types. |
423 | let object = new; |
424 | let operation = new; |
425 | |
426 | let raw_result = self |
427 | .new_operation_func |
428 | .await; |
429 | |
430 | // Convert the dynamic result back into its concrete type |
431 | match raw_result |
432 | Ok => Ok, |
433 | Err => Err |
434 | => | Internal
435 | Internal |
436 | "operation {}::{} handler outcome" |
437 | , | object_name
438 | operation_name |
439 | ) |
440 | |
441 | => Operation | Operation
442 | boxed_error.0. .unwrap .clone, |
443 | , |
444 | => Unhandled, | Unhandled
445 | , |
446 | |
447 | |
448 | |
449 | async |
450 | &self, |
451 | object_str: &str, |
452 | operation_state: &OS, |
453 | |
454 | |
455 | O: GiteratedObject + Debug + 'static, |
456 | |
457 | // TODO: Authorization? |
458 | for in self.inner.metadata.objects.iter |
459 | if object_name != object_name |
460 | continue; |
461 | |
462 | |
463 | if let Ok = |
464 | return Ok |
465 | new_unchecked |
466 | ; |
467 | |
468 | |
469 | |
470 | if let Some = self.operation_handlers.get |
471 | object_name: "any", |
472 | operation_name: "any", |
473 | |
474 | let result = handler |
475 | .handle |
476 | |
477 | , | new
478 | , | new
479 | , |
480 | operation_state.clone, |
481 | |
482 | .await; |
483 | |
484 | match result |
485 | Ok => |
486 | let object: &ObjectResponse = success.0.downcast_ref .unwrap; |
487 | |
488 | return Ok |
489 | new_unchecked |
490 | ; |
491 | |
492 | Err => match err |
493 | => | Operation
494 | let failure: &ObjectRequestError = failure.0.downcast_ref .unwrap; |
495 | |
496 | return Err; |
497 | |
498 | => | Internal
499 | return Err |
500 | |
501 | => | Unhandled
502 | , |
503 | ; |
504 | |
505 | |
506 | Err |
507 | |
508 | |
509 | |
510 | // Placeholder |
511 | |
512 | pub async |
513 | &self, |
514 | object: AnyObject, |
515 | operation: AnyOperation, |
516 | operation_state: OS, |
517 | |
518 | let operation_name = operation.kind .operation_name; |
519 | |
520 | if let Some = self.inner.operation_handlers.get |
521 | object_name: "any", |
522 | operation_name: "any", |
523 | |
524 | match handler_tree |
525 | .handle |
526 | .await |
527 | |
528 | Ok => return Ok, |
529 | Err => match err |
530 | => | Operation
531 | return Err |
532 | |
533 | => | Internal
534 | return Err |
535 | |
536 | => | Unhandled
537 | , |
538 | |
539 | |
540 | |
541 | // We need to hijack get_value, set_setting, and get_setting. |
542 | if operation_name == "get_value" |
543 | let get_value = operation |
544 | . |
545 | .ok_or_else?; |
546 | |
547 | let value_meta = self |
548 | .inner |
549 | .metadata |
550 | .values |
551 | .get |
552 | object_kind: object.kind, |
553 | value_kind: &get_value.value_name, |
554 | |
555 | .ok_or_else?; |
556 | let value_name = value_meta.name.clone; |
557 | |
558 | trace!; |
559 | |
560 | if let Some = self.inner.value_getters.get |
561 | object_kind: "any", |
562 | value_kind: &get_value.value_name, |
563 | |
564 | match handler |
565 | .handle |
566 | , |
567 | operation_state.clone, |
568 | |
569 | .await |
570 | |
571 | Ok => |
572 | // self.value_update(in_object, success.clone(), operation_state) |
573 | // .await; |
574 | |
575 | return Ok |
576 | as |
577 | .downcast |
578 | .unwrap; |
579 | |
580 | Err => |
581 | match err |
582 | => | Operation
583 | return Err; |
584 | |
585 | => | Internal
586 | // This DOES NOT result in an early return |
587 | warn!; |
588 | |
589 | => | Unhandled
590 | // This DOES NOT result in an early return |
591 | |
592 | |
593 | |
594 | |
595 | |
596 | |
597 | if let Some = self.inner.value_getters.get |
598 | object_kind: object.kind, |
599 | value_kind: "any", |
600 | |
601 | match handler |
602 | .handle |
603 | , |
604 | operation_state.clone, |
605 | |
606 | .await |
607 | |
608 | Ok => |
609 | // self.value_update(in_object, success.clone(), operation_state) |
610 | // .await; |
611 | |
612 | return Ok |
613 | as |
614 | .downcast |
615 | .unwrap; |
616 | |
617 | Err => |
618 | match err |
619 | => | Operation
620 | return Err; |
621 | |
622 | => | Internal
623 | // This DOES NOT result in an early return |
624 | warn!; |
625 | |
626 | => | Unhandled
627 | // This DOES NOT result in an early return |
628 | |
629 | |
630 | |
631 | |
632 | |
633 | |
634 | if let Some = self.inner.value_getters.get |
635 | object_kind: "any", |
636 | value_kind: "any", |
637 | |
638 | match handler |
639 | .handle |
640 | , |
641 | operation_state.clone, |
642 | |
643 | .await |
644 | |
645 | Ok => |
646 | // self.value_update(in_object, success.clone(), operation_state) |
647 | // .await; |
648 | |
649 | return Ok |
650 | as |
651 | .downcast |
652 | .unwrap; |
653 | |
654 | Err => |
655 | match err |
656 | => | Operation
657 | return Err; |
658 | |
659 | => | Internal
660 | // This DOES NOT result in an early return |
661 | warn!; |
662 | |
663 | => | Unhandled
664 | // This DOES NOT result in an early return |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | if let Some = self.inner.value_getters.get |
672 | object_kind: object.kind, |
673 | value_kind: &get_value.value_name, |
674 | |
675 | match handler |
676 | .handle |
677 | , |
678 | operation_state.clone, |
679 | |
680 | .await |
681 | |
682 | Ok => |
683 | // self.value_update(in_object, success.clone(), operation_state) |
684 | // .await; |
685 | |
686 | return Ok |
687 | |
688 | .as_internal_error_with_context?, |
689 | ; |
690 | |
691 | Err => |
692 | match err |
693 | => | Operation
694 | return Err; |
695 | |
696 | => | Internal
697 | // This DOES NOT result in an early return |
698 | warn!; |
699 | |
700 | => | Unhandled
701 | // This DOES NOT result in an early return |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | else if operation. |
708 | let get_setting: &GetSetting = operation.downcast_ref .unwrap; |
709 | let setting_name = get_setting.setting_name.clone; |
710 | |
711 | let raw_result = self |
712 | .get_setting |
713 | object.clone, |
714 | object.kind .to_string, |
715 | get_setting.clone, |
716 | &operation_state, |
717 | |
718 | .await; |
719 | |
720 | let setting_meta = self |
721 | .metadata |
722 | .settings |
723 | .get |
724 | object_kind: object.kind, |
725 | setting_name: &get_setting.setting_name, |
726 | |
727 | .ok_or_else?; |
728 | |
729 | return match raw_result |
730 | Ok => |
731 | // Success is the setting type, serialize it |
732 | // let serialized = (setting_meta.serialize)(success).unwrap(); |
733 | |
734 | // Ok(serde_json::to_vec(&serialized).unwrap()) |
735 | // Ok(success.downcast_ref::<D::Success>().unwrap().clone()) |
736 | Ok |
737 | unwrap, | .
738 | |
739 | |
740 | Err => Err |
741 | => | Operation
742 | // We know this is the right type |
743 | Operation |
744 | |
745 | => | Internal
746 | Internal |
747 | "{}::get_setting::<{}> handler outcome" |
748 | object.kind, |
749 | setting_name |
750 | ) |
751 | |
752 | => Unhandled, | Unhandled
753 | , |
754 | ; |
755 | else if operation. |
756 | let operation: &SetSetting = operation.downcast_ref .unwrap; |
757 | let object_type = object.kind; |
758 | |
759 | trace! |
760 | "Handling {}::set_setting for {}", |
761 | object_type, |
762 | operation.setting_name |
763 | ; |
764 | |
765 | let setting_meta = self |
766 | .metadata |
767 | .settings |
768 | .get |
769 | object_kind: &object_type, |
770 | setting_name: &operation.setting_name, |
771 | |
772 | // TODO: Check this |
773 | .ok_or |
774 | InvalidSetting |
775 | operation.setting_name.clone, |
776 | object_type.to_string .clone, |
777 | , |
778 | ?; |
779 | |
780 | let setting = |
781 | .as_internal_error_with_context |
782 | "deserializing setting {} for object {}" |
783 | operation.setting_name, object_type |
784 | )?; |
785 | |
786 | trace! |
787 | "Deserialized setting {} for object {}", |
788 | operation.setting_name, |
789 | object_type, |
790 | ; |
791 | |
792 | for provider in self.metadata_providers.iter |
793 | if provider.provides_for |
794 | trace! |
795 | "Resolved setting provider for setting {} for object {}", |
796 | operation.setting_name, |
797 | object_type, |
798 | ; |
799 | |
800 | let object_meta = self |
801 | .metadata |
802 | .objects |
803 | .get |
804 | .ok_or_else?; |
805 | |
806 | let raw_result = provider |
807 | .write |
808 | .await; |
809 | |
810 | return match raw_result |
811 | Ok => |
812 | warn!; |
813 | // (setting_meta.setting_updated)( |
814 | // object, |
815 | // setting, |
816 | // self.clone(), |
817 | // operation_state, |
818 | // ) |
819 | // .await; |
820 | |
821 | Ok |
822 | |
823 | Err => Err |
824 | "writing object {} setting {}" |
825 | object_type, operation.setting_name |
826 | ), |
827 | ; |
828 | |
829 | |
830 | trace! |
831 | "Failed to resolve setting provider for setting {} for object {}", |
832 | operation.setting_name, |
833 | object_type, |
834 | ; |
835 | |
836 | else if operation. |
837 | let object_request: &ObjectRequest = operation.downcast_ref .unwrap; |
838 | trace!; |
839 | |
840 | // TODO: Authorization? |
841 | for in self.inner.metadata.objects.iter |
842 | if object_meta.name == "networked_object" |
843 | // TODO: HACK |
844 | continue; |
845 | |
846 | |
847 | if let Ok = |
848 | trace!; |
849 | |
850 | return Ok |
851 | object, |
852 | ; |
853 | |
854 | |
855 | |
856 | if let Some = self.operation_handlers.get |
857 | object_name: "any", |
858 | operation_name: "any", |
859 | |
860 | let result = handler |
861 | .handle |
862 | |
863 | , | new
864 | , | new
865 | , |
866 | operation_state.clone, |
867 | |
868 | .await; |
869 | |
870 | match result |
871 | Ok => |
872 | let object: &ObjectResponse = success.0.downcast_ref .unwrap; |
873 | |
874 | return Ok; |
875 | |
876 | Err => |
877 | todo! |
878 | |
879 | |
880 | |
881 | |
882 | return Err; |
883 | |
884 | |
885 | trace! |
886 | "Object operation for {}::{} is not special case", |
887 | object.kind, |
888 | operation_name |
889 | ; |
890 | |
891 | // Resolve the handler from our handler tree |
892 | let handler_tree = self |
893 | .inner |
894 | .operation_handlers |
895 | .get |
896 | .ok_or_else?; |
897 | |
898 | trace! |
899 | "Object operation for {}::{} handler tree resolved", |
900 | object.kind, |
901 | operation_name |
902 | ; |
903 | |
904 | handler_tree |
905 | .handle |
906 | .await |
907 | |
908 | |
909 | |
910 | /// Defines a type that is a valid Giterated runtime state. |
911 | /// |
912 | /// This allows for extraction of state in handlers, based on a |
913 | /// [`FromOperationState<S>`] impl on (what is in this case) [`Self`]. |
914 | |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | T: , |
922 | |
923 | type Error = MissingValue; |
924 | |
925 | async |
926 | required_parameters: &R, |
927 | operation_state: &S, |
928 | |
929 | Ok |
930 | .await |
931 | .ok |
932 | |
933 | |
934 |