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