Fix settings
parent: tbd commit: c518454
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 ; |
19 | use trace; |
20 | |
21 | use crate:: |
22 | , GiteratedOperationHandler, MissingValue, ObjectMeta, | MetadataProvider
23 | ObjectOperationPair, ObjectSettingPair, ObjectValuePair, OperationMeta, OperationWrapper, |
24 | SettingMeta, SettingUpdate, StackOperationState, ValueMeta, |
25 | ; |
26 | |
27 | /// Temporary name for the next generation of Giterated stack |
28 | |
29 | |
30 | operation_handlers: , |
31 | value_getters: , |
32 | setting_getters: , |
33 | value_change: , |
34 | setting_change: , |
35 | metadata_providers: , |
36 | metadata: RuntimeMetadata, |
37 | |
38 | |
39 | |
40 | |
41 | f.debug_struct .finish |
42 | |
43 | |
44 | |
45 | |
46 | ; |
47 | |
48 | |
49 | |
50 | &mut self, |
51 | mut builder: , |
52 | |
53 | for in builder.operation_handlers |
54 | let tree = self.get_or_create_tree; |
55 | |
56 | tree.push; |
57 | |
58 | |
59 | for in builder.value_getters |
60 | assert!; |
61 | |
62 | |
63 | for in builder.setting_getters |
64 | assert!; |
65 | |
66 | |
67 | for in builder.value_change |
68 | self.value_change.insert; |
69 | |
70 | |
71 | for in builder.setting_change |
72 | self.setting_change.insert; |
73 | |
74 | |
75 | self.metadata_providers |
76 | .append; |
77 | |
78 | self.metadata.append; |
79 | |
80 | self |
81 | |
82 | |
83 | pub async |
84 | &self, |
85 | object: O, |
86 | new_value: V, |
87 | operation_state: &StackOperationState, |
88 | |
89 | O: GiteratedObject + 'static, |
90 | V: + 'static, |
91 | |
92 | trace!; |
93 | let target = ; |
94 | |
95 | if let Some = self.value_change.get |
96 | // TODO |
97 | let _ = handler |
98 | .handle |
99 | &, |
100 | &, |
101 | operation_state, |
102 | |
103 | .await; |
104 | |
105 | |
106 | |
107 | pub async |
108 | &self, |
109 | object: O, |
110 | new_setting: S, |
111 | operation_state: &StackOperationState, |
112 | |
113 | O: GiteratedObject + 'static, |
114 | S: Setting + 'static, |
115 | |
116 | trace!; |
117 | let target = ; |
118 | |
119 | if let Some = self.setting_change.get |
120 | let _ = handler |
121 | .handle |
122 | &, |
123 | &, |
124 | operation_state, |
125 | |
126 | .await; |
127 | |
128 | |
129 | |
130 | pub async |
131 | |
132 | O: GiteratedObject, |
133 | |
134 | // TODO |
135 | |
136 | |
137 | /// Writes a setting for the specified object. |
138 | pub async |
139 | &self, |
140 | object: &O, |
141 | setting: S, |
142 | |
143 | |
144 | O: GiteratedObject + 'static, |
145 | S: Setting + 'static + Clone, |
146 | |
147 | for provider in self.metadata_providers.iter |
148 | if provider.provides_for |
149 | let setting_meta = self |
150 | .metadata |
151 | .settings |
152 | .get |
153 | object_kind: object_name .to_string, |
154 | setting_name: name .to_string, |
155 | |
156 | .ok_or_else?; |
157 | |
158 | let object_meta = self |
159 | .metadata |
160 | .objects |
161 | .get |
162 | .ok_or_else?; |
163 | |
164 | let result = provider |
165 | .write |
166 | .await |
167 | .as_internal_error_with_context; |
168 | |
169 | return result; |
170 | |
171 | |
172 | |
173 | Err |
174 | |
175 | |
176 | /// Gets a setting for the specified object. |
177 | pub async |
178 | |
179 | O: GiteratedObject + 'static, |
180 | S: Setting + 'static, |
181 | |
182 | for provider in self.metadata_providers.iter |
183 | if provider.provides_for |
184 | trace! |
185 | "Resolving setting {} for object {} from provider.", |
186 | , | name
187 | object_name |
188 | ; |
189 | |
190 | let setting_meta = self |
191 | .metadata |
192 | .settings |
193 | .get |
194 | object_kind: object_name .to_string, |
195 | setting_name: name .to_string, |
196 | |
197 | .ok_or_else?; |
198 | |
199 | let object_meta = self |
200 | .metadata |
201 | .objects |
202 | .get |
203 | .ok_or_else?; |
204 | |
205 | let value = provider |
206 | .read |
207 | .await |
208 | .as_internal_error_with_context?; |
209 | |
210 | return from_value |
211 | .as_internal_error_with_context; |
212 | |
213 | |
214 | trace! |
215 | "No provider registered for setting {} and object {}", |
216 | , | name
217 | object_name |
218 | ; |
219 | |
220 | Err |
221 | |
222 | |
223 | |
224 | if self.operation_handlers.contains_key |
225 | self.operation_handlers.get_mut .unwrap |
226 | else |
227 | self.operation_handlers |
228 | .insert; |
229 | |
230 | self.operation_handlers.get_mut .unwrap |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | elements: , |
238 | |
239 | |
240 | |
241 | |
242 | self.elements.push; |
243 | |
244 | |
245 | pub async |
246 | &self, |
247 | object: & , |
248 | operation: & , |
249 | operation_state: &StackOperationState, |
250 | |
251 | for handler in self.elements.iter |
252 | match handler.handle .await |
253 | Ok => return Ok, |
254 | Err => match err |
255 | => | Operation
256 | return Err |
257 | |
258 | => return Err, | Internal
259 | _ => |
260 | continue; |
261 | |
262 | , |
263 | |
264 | |
265 | |
266 | Err |
267 | |
268 | |
269 | |
270 | /// Stores runtime metadata for all in-use Giterated protocol types. |
271 | |
272 | |
273 | objects: , |
274 | operations: , |
275 | values: , |
276 | settings: , |
277 | |
278 | |
279 | /// Defines a type that is a valid Giterated runtime state. |
280 | /// |
281 | /// This allows for extraction of state in handlers, based on a |
282 | /// [`FromOperationState<S>`] impl on (what is in this case) [`Self`]. |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | operation_handlers: , |
289 | value_getters: , |
290 | setting_getters: , |
291 | metadata: RuntimeMetadata, |
292 | value_change: , |
293 | metadata_providers: , |
294 | setting_change: , |
295 | |
296 | state: S, |
297 | |
298 | |
299 | |
300 | |
301 | Self |
302 | operation_handlers: Default default, |
303 | value_getters: Default default, |
304 | setting_getters: Default default, |
305 | metadata: Default default, |
306 | value_change: Default default, |
307 | metadata_providers: Default default, |
308 | setting_change: Default default, |
309 | state, |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | /// Insert an operation handler into the runtime builder. |
316 | /// |
317 | /// # Type Registration |
318 | /// Inserting the handler will automatically, if required, register the operation type of the |
319 | /// handler. It will **not** register the object type automatically. |
320 | |
321 | |
322 | O: GiteratedObject + 'static, |
323 | D: + 'static + Clone, |
324 | H: + 'static + Clone + Send + Sync, |
325 | : Send + Sync, | Failure
326 | : Send + Sync, | Success
327 | |
328 | let object_name = handler.object_name .to_string; |
329 | let operation_name = handler.operation_name .to_string; |
330 | |
331 | let wrapped = new; |
332 | |
333 | let pair = ObjectOperationPair |
334 | object_name, |
335 | operation_name, |
336 | ; |
337 | |
338 | self.operation_handlers.insert; |
339 | |
340 | self.metadata.; |
341 | |
342 | self |
343 | |
344 | |
345 | /// Register a [`GiteratedObject`] type with the runtime. |
346 | /// |
347 | /// # Type Registration |
348 | /// This will register the provided object type. |
349 | |
350 | self.metadata.; |
351 | |
352 | // Insert handler so ObjectRequest is handled properly |
353 | let handler = move |_object: &Instance, |
354 | operation: ObjectRequest, |
355 | _state: S, |
356 | _operation_state: StackOperationState, |
357 | stack: | |
358 | async move |
359 | for in stack.metadata.objects.iter |
360 | if .is_ok |
361 | return Ok; |
362 | |
363 | |
364 | |
365 | Err |
366 | |
367 | .boxed_local |
368 | ; |
369 | |
370 | self.operation; |
371 | |
372 | self |
373 | |
374 | |
375 | /// Register a [`Setting`] type with the runtime. |
376 | /// |
377 | /// # Type Registration |
378 | /// This will register the provided setting type. |
379 | |
380 | self.metadata.; |
381 | |
382 | self |
383 | |
384 | |
385 | /// Register a [`GiteratedObjectValue`] that is also a [`Setting`], which |
386 | /// allows for automatic value updates. |
387 | |
388 | O: GiteratedObject + 'static + Clone, |
389 | T: + Setting + 'static + Clone, |
390 | > |
391 | &mut self, |
392 | |
393 | self.metadata.; |
394 | self.metadata.; |
395 | |
396 | self.setting_change.insert |
397 | ObjectSettingPair |
398 | object_kind: object_name .to_string, |
399 | setting_name: name .to_string, |
400 | , |
401 | new |
402 | move |object: &O, |
403 | setting: , |
404 | _state: , |
405 | operation_state: StackOperationState, |
406 | stack: | |
407 | trace! |
408 | "value setting updated {}::{}", |
409 | , | object_name
410 | value_name |
411 | ; |
412 | let object = object.clone; |
413 | async move |
414 | stack |
415 | .value_update |
416 | .await; |
417 | Ok |
418 | |
419 | .boxed_local |
420 | , |
421 | , |
422 | , |
423 | ; |
424 | |
425 | let wrapped = new |
426 | |
427 | |
428 | state: _, |
429 | operation_state: StackOperationState, |
430 | stack: | |
431 | let object = object.clone; |
432 | async move |
433 | match stack |
434 | .get_setting |
435 | Box new as _, |
436 | to_string, | object_name .
437 | GetSetting |
438 | setting_name: name .to_string, |
439 | , |
440 | &operation_state, |
441 | |
442 | .await |
443 | |
444 | Ok => Ok, |
445 | Err => |
446 | panic!; |
447 | |
448 | |
449 | |
450 | .boxed_local |
451 | , |
452 | self.state.clone, |
453 | ; |
454 | |
455 | self.value_getters.insert |
456 | ObjectValuePair |
457 | object_kind: object_name .to_string, |
458 | value_kind: value_name .to_string, |
459 | , |
460 | wrapped, |
461 | ; |
462 | |
463 | self |
464 | |
465 | |
466 | /// Register a [`GiteratedObjectValue<O>`] type with the runtime, providing |
467 | /// its associated handler for [`GetValue`]. |
468 | /// |
469 | /// # Type Registration |
470 | /// This will register the provided [`GiteratedObjectValue`] type for its matching / specified |
471 | /// object type. It will **not** register the object type automatically. |
472 | |
473 | |
474 | O: GiteratedObject + 'static, |
475 | V: + 'static + Clone, |
476 | F: + Clone + 'static + Send + Sync, |
477 | |
478 | let object_name = handler.object_name .to_string; |
479 | let value_name = value_name .to_string; |
480 | |
481 | let wrapped = new; |
482 | |
483 | let handler_object_name = object_name.clone; |
484 | let handler_value_name = value_name.clone; |
485 | |
486 | // Insert handler so GetValue is handled properly |
487 | let _handler = move |object: &O, |
488 | operation: , |
489 | _state: S, |
490 | operation_state: StackOperationState, |
491 | stack: | |
492 | let stack = stack; |
493 | let object_name = handler_object_name; |
494 | let value_name = handler_value_name; |
495 | let object = object.clone; |
496 | async move |
497 | for in stack.value_getters.iter |
498 | if target.object_kind != object_name |
499 | continue; |
500 | |
501 | |
502 | if target.value_kind != value_name |
503 | continue; |
504 | |
505 | |
506 | return match getter |
507 | .handle |
508 | &, |
509 | & |
510 | value_name: operation.value_name, |
511 | ty: Default default, |
512 | as , |
513 | &operation_state, |
514 | |
515 | .await |
516 | Ok => Ok, |
517 | Err => Err |
518 | => Operation, | Operation
519 | => Internal, | Internal
520 | => Unhandled, | Unhandled
521 | , |
522 | |
523 | |
524 | |
525 | Err |
526 | |
527 | .boxed_local |
528 | ; |
529 | |
530 | assert! |
531 | .value_getters |
532 | .insert |
533 | ObjectValuePair |
534 | object_kind: object_name, |
535 | value_kind: value_name |
536 | , |
537 | wrapped |
538 | |
539 | .is_none; |
540 | |
541 | self.metadata.; |
542 | |
543 | self |
544 | |
545 | |
546 | /// Register a handler for [`GetSetting`] for it's associated object type. |
547 | |
548 | |
549 | O: GiteratedObject + 'static, |
550 | F: + Clone + 'static + Send + Sync, |
551 | |
552 | let object_name = handler.object_name .to_string; |
553 | |
554 | let wrapped = new; |
555 | |
556 | assert!; |
557 | |
558 | self |
559 | |
560 | |
561 | |
562 | |
563 | O: GiteratedObject + 'static, |
564 | F: + Clone + 'static + Send + Sync, |
565 | V: + Clone + 'static, |
566 | |
567 | let object_name = handler.object_name .to_string; |
568 | |
569 | let wrapped = new; |
570 | |
571 | assert!; |
572 | |
573 | self |
574 | |
575 | |
576 | |
577 | self.metadata_providers.push; |
578 | |
579 | self |
580 | |
581 | |
582 | |
583 | |
584 | |
585 | new_value: V, |
586 | |
587 | |
588 | |
589 | |
590 | O: GiteratedObject, |
591 | V: , |
592 | |
593 | type Success = V; |
594 | |
595 | type Failure = MissingValue; |
596 | |
597 | |
598 | |
599 | |
600 | let object_name = object_name .to_string; |
601 | |
602 | let object_meta = ; |
603 | |
604 | if self.objects.insert .is_some |
605 | trace! |
606 | "Registration of object {} overwrote previous registration.", |
607 | object_name |
608 | ; |
609 | else |
610 | trace! |
611 | |
612 | |
613 | |
614 | |
615 | &mut self, |
616 | |
617 | let object_name = object_name .to_string; |
618 | let operation_name = operation_name .to_string; |
619 | |
620 | if self |
621 | .operations |
622 | .insert |
623 | ObjectOperationPair |
624 | object_name: object_name, |
625 | operation_name: operation_name, |
626 | , |
627 | , |
628 | |
629 | .is_some |
630 | |
631 | trace! |
632 | "Registration of object operation {}<{}> overwrote previous registration.", |
633 | , | operation_name
634 | object_name |
635 | ; |
636 | else |
637 | trace! |
638 | "Registration of object operation {}<{}>.", |
639 | , | operation_name
640 | object_name |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | O: GiteratedObject + 'static, |
647 | V: + 'static, |
648 | > |
649 | &mut self, |
650 | |
651 | let object_name = object_name .to_string; |
652 | let value_name = value_name .to_string; |
653 | |
654 | if self |
655 | .values |
656 | .insert |
657 | ObjectValuePair |
658 | object_kind: object_name, |
659 | value_kind: value_name, |
660 | , |
661 | , |
662 | |
663 | .is_some |
664 | |
665 | trace! |
666 | "Registration of value <{}>::{} overwrote previous registration.", |
667 | , | object_name
668 | value_name |
669 | ; |
670 | else |
671 | trace! |
672 | "Registration of value <{}>::{}.", |
673 | , | object_name
674 | value_name |
675 | ; |
676 | |
677 | |
678 | |
679 | |
680 | if self |
681 | .settings |
682 | .insert |
683 | ObjectSettingPair |
684 | object_kind: object_name .to_string, |
685 | setting_name: name .to_string, |
686 | , |
687 | , |
688 | |
689 | .is_some |
690 | |
691 | trace! |
692 | "Registration of setting {} overwrote previous registration.", |
693 | name |
694 | ; |
695 | else |
696 | trace!; |
697 | |
698 | |
699 | |
700 | |
701 | self.objects.extend; |
702 | self.operations.extend; |
703 | self.values.extend; |
704 | self.settings.extend; |
705 | |
706 | |
707 | |
708 | /// Handles a giterated network message, returning either a raw success |
709 | /// payload or a serialized error payload. |
710 | pub async |
711 | &self, |
712 | message: AuthenticatedPayload, |
713 | operation_state: &StackOperationState, |
714 | |
715 | let message: = message.into_message; |
716 | |
717 | // Deserialize the object, also getting the object type's name |
718 | let = |
719 | let mut result = None; |
720 | |
721 | for in self.metadata.objects.iter |
722 | if let Ok = |
723 | result = Some; |
724 | break; |
725 | |
726 | |
727 | |
728 | result |
729 | |
730 | .ok_or_else?; |
731 | |
732 | trace! |
733 | "Handling network message {}::<{}>", |
734 | message.operation, |
735 | object_type |
736 | ; |
737 | |
738 | if message.operation == "get_value" |
739 | // Special case |
740 | let operation: GetValue = from_slice .unwrap; |
741 | |
742 | let result = self |
743 | .network_get_value |
744 | object, |
745 | object_type.clone, |
746 | operation.clone, |
747 | operation_state, |
748 | |
749 | .await; |
750 | |
751 | // In the case of internal errors, attach context |
752 | let result = result.map_err |
753 | => Operation, | Operation
754 | => | Internal
755 | Internal |
756 | "{}::get_value::<{}> outcome" |
757 | object_type, operation.value_name |
758 | ) |
759 | |
760 | => Unhandled, | Unhandled
761 | ; |
762 | |
763 | return result; |
764 | else if message.operation == "get_setting" |
765 | let operation: GetSetting = from_slice .unwrap; |
766 | let setting_meta = self |
767 | .metadata |
768 | .settings |
769 | .get |
770 | object_kind: object_type.clone, |
771 | setting_name: operation.setting_name.clone, |
772 | |
773 | .ok_or_else?; |
774 | let raw_result = self |
775 | .get_setting |
776 | .await; |
777 | return match raw_result |
778 | Ok => |
779 | // Success is the setting type, serialize it |
780 | let serialized = .unwrap; |
781 | |
782 | Ok |
783 | |
784 | Err => Err |
785 | => | Operation
786 | // We know how to resolve this type |
787 | let failure: GetSettingError = *failure.downcast .unwrap; |
788 | |
789 | Operation |
790 | |
791 | => | Internal
792 | Internal |
793 | "{}::get_setting::<{}> handler outcome" |
794 | object_type, setting_meta.name |
795 | ) |
796 | |
797 | => Unhandled, | Unhandled
798 | , |
799 | ; |
800 | else if message.operation == "set_setting" |
801 | let operation: SetSetting = from_slice .unwrap; |
802 | |
803 | trace! |
804 | "Handling network {}::set_setting for {}", |
805 | object_type, |
806 | operation.setting_name |
807 | ; |
808 | |
809 | let setting_meta = self |
810 | .metadata |
811 | .settings |
812 | .get |
813 | object_kind: object_type.clone, |
814 | setting_name: operation.setting_name.clone, |
815 | |
816 | .unwrap; |
817 | |
818 | let setting = |
819 | .as_internal_error_with_context |
820 | "deserializing setting {} for object {}" |
821 | operation.setting_name, object_type |
822 | )?; |
823 | |
824 | trace! |
825 | "Deserialized setting {} for object {}", |
826 | operation.setting_name, |
827 | object_type, |
828 | ; |
829 | |
830 | for provider in self.metadata_providers.iter |
831 | if provider.provides_for |
832 | trace! |
833 | "Resolved setting provider for setting {} for object {}", |
834 | operation.setting_name, |
835 | object_type, |
836 | ; |
837 | |
838 | let object_meta = self |
839 | .metadata |
840 | .objects |
841 | .get |
842 | .ok_or_else?; |
843 | |
844 | let raw_result = provider |
845 | .write |
846 | .await; |
847 | |
848 | return match raw_result |
849 | Ok => |
850 | |
851 | object, |
852 | setting, |
853 | operation_state.runtime.clone, |
854 | operation_state, |
855 | |
856 | .await; |
857 | |
858 | Ok |
859 | |
860 | Err => Err |
861 | "writing object {} setting {}" |
862 | object_type, operation.setting_name |
863 | ), |
864 | ; |
865 | |
866 | |
867 | trace! |
868 | "Failed to resolve setting provider for setting {} for object {}", |
869 | operation.setting_name, |
870 | object_type, |
871 | ; |
872 | |
873 | |
874 | |
875 | let target = ObjectOperationPair |
876 | object_name: object_type.clone, |
877 | operation_name: message.operation.clone, |
878 | ; |
879 | |
880 | // Resolve the target operations from the handlers table |
881 | let handler = self |
882 | .operation_handlers |
883 | .get |
884 | .ok_or_else?; |
885 | |
886 | trace! |
887 | "Resolved operation handler for network message {}::<{}>", |
888 | message.operation, |
889 | object_type |
890 | ; |
891 | |
892 | // Deserialize the operation |
893 | let meta = self |
894 | .metadata |
895 | .operations |
896 | .get |
897 | .ok_or_else?; |
898 | |
899 | let operation = |
900 | as_internal_error_with_context | .
901 | "deserializing operation {}::{}" |
902 | target.object_name, target.operation_name |
903 | )?; |
904 | |
905 | trace! |
906 | "Deserialized operation for network message {}::<{}>", |
907 | message.operation, |
908 | object_type |
909 | ; |
910 | |
911 | trace! |
912 | "Calling handler for network message {}::<{}>", |
913 | message.operation, |
914 | object_type |
915 | ; |
916 | |
917 | // Get the raw result of the operation, where the return values are boxed. |
918 | let raw_result = handler.handle .await; |
919 | |
920 | trace! |
921 | "Finished handling network message {}::<{}>", |
922 | message.operation, |
923 | object_type |
924 | ; |
925 | |
926 | // Deserialize the raw result for the network |
927 | match raw_result |
928 | Ok => Ok, |
929 | Err => Err |
930 | => | Operation
931 | Operation |
932 | |
933 | => | Internal
934 | Internal |
935 | "operation {}::{} handler outcome" |
936 | target.object_name, target.operation_name |
937 | ) |
938 | |
939 | => Unhandled, | Unhandled
940 | , |
941 | |
942 | |
943 | |
944 | pub async |
945 | &self, |
946 | object: , |
947 | object_kind: String, |
948 | operation: GetValue, |
949 | operation_state: &StackOperationState, |
950 | |
951 | trace!; |
952 | |
953 | let value_meta = self |
954 | .metadata |
955 | .values |
956 | .get |
957 | object_kind: object_kind.clone, |
958 | value_kind: operation.value_name.clone, |
959 | |
960 | .ok_or_else?; |
961 | |
962 | for in self.value_getters.iter |
963 | if target.object_kind != object_kind |
964 | continue; |
965 | |
966 | |
967 | if target.value_kind != operation.value_name |
968 | continue; |
969 | |
970 | |
971 | return match getter |
972 | .handle |
973 | .await |
974 | |
975 | Ok => |
976 | // Serialize success, which is the value type itself |
977 | let serialized = .as_internal_error?; |
978 | |
979 | Ok |
980 | |
981 | Err => Err |
982 | => | Operation
983 | // Failure is sourced from GetValue operation, but this is hardcoded for now |
984 | let failure: GetValueError = *failure.downcast .unwrap; |
985 | |
986 | Operation |
987 | |
988 | => Internal, | Internal
989 | => Unhandled, | Unhandled
990 | , |
991 | ; |
992 | |
993 | |
994 | Err |
995 | |
996 | |
997 | pub async |
998 | &self, |
999 | object: , |
1000 | object_kind: String, |
1001 | operation: GetSetting, |
1002 | operation_state: &StackOperationState, |
1003 | |
1004 | trace! |
1005 | "Handling network {}::get_setting for {}", |
1006 | object_kind, |
1007 | operation.setting_name |
1008 | ; |
1009 | |
1010 | for provider in self.metadata_providers.iter |
1011 | if provider.provides_for |
1012 | let setting_meta = self |
1013 | .metadata |
1014 | .settings |
1015 | .get |
1016 | object_kind: object_kind.clone, |
1017 | setting_name: operation.setting_name.clone, |
1018 | |
1019 | .ok_or_else?; |
1020 | |
1021 | let object_meta = self |
1022 | .metadata |
1023 | .objects |
1024 | .get |
1025 | .ok_or_else?; |
1026 | |
1027 | let result = provider |
1028 | .read |
1029 | .await |
1030 | .as_internal_error_with_context |
1031 | "reading setting {}" |
1032 | operation.setting_name |
1033 | )?; |
1034 | |
1035 | return Ok |
1036 | as_internal_error_with_context | .
1037 | "deserializing setting {}" |
1038 | operation.setting_name |
1039 | )?, |
1040 | ; |
1041 | |
1042 | |
1043 | |
1044 | todo! |
1045 | |
1046 | |
1047 | pub async |
1048 | &self, |
1049 | object: , |
1050 | object_kind: String, |
1051 | operation: SetSetting, |
1052 | operation_state: &StackOperationState, |
1053 | |
1054 | trace! |
1055 | "Handling network {}::set_setting for {}", |
1056 | object_kind, |
1057 | operation.setting_name |
1058 | ; |
1059 | |
1060 | let target = ObjectSettingPair |
1061 | object_kind: object_kind.clone, |
1062 | setting_name: operation.setting_name.clone, |
1063 | ; |
1064 | |
1065 | let handler = self.setting_change.get .unwrap; |
1066 | |
1067 | let raw_result = handler |
1068 | .handle |
1069 | .await; |
1070 | |
1071 | match raw_result |
1072 | Ok => |
1073 | // Serialize success, which is the value type itself |
1074 | let serialized = to_vec .as_internal_error?; |
1075 | |
1076 | Ok |
1077 | |
1078 | Err => Err |
1079 | => | Operation
1080 | // Failure is sourced from GetValue operation, but this is hardcoded for now |
1081 | let failure: GetValueError = *failure.downcast .unwrap; |
1082 | |
1083 | Operation |
1084 | |
1085 | => Internal, | Internal
1086 | => Unhandled, | Unhandled
1087 | , |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | use Debug; |
1093 | |
1094 | |
1095 | |
1096 | async |
1097 | &self, |
1098 | in_object: O, |
1099 | operation_name: &str, |
1100 | payload: D, |
1101 | operation_state: &StackOperationState, |
1102 | |
1103 | |
1104 | O: GiteratedObject + Debug + 'static, |
1105 | D: + Debug + 'static, |
1106 | |
1107 | // Erase object and operation types. |
1108 | let object = Box new as ; |
1109 | let operation = Box new as ; |
1110 | |
1111 | // We need to hijack get_value, set_setting, and get_setting. |
1112 | if operation_name == "get_value" |
1113 | let mut value_meta = None; |
1114 | for in self.metadata.values.iter |
1115 | if |
1116 | value_meta = Some; |
1117 | break; |
1118 | |
1119 | |
1120 | |
1121 | let value_meta = value_meta.ok_or_else?; |
1122 | |
1123 | let value_name = value_meta.name.clone; |
1124 | |
1125 | trace! |
1126 | "Handling get_value for {}::{}", |
1127 | , | object_name
1128 | value_name |
1129 | ; |
1130 | |
1131 | for in self.value_getters.iter |
1132 | if target.object_kind != object_name |
1133 | continue; |
1134 | |
1135 | |
1136 | if target.value_kind != value_name |
1137 | continue; |
1138 | |
1139 | |
1140 | return match getter |
1141 | .handle |
1142 | .await |
1143 | |
1144 | Ok => Ok, |
1145 | Err => Err |
1146 | => | Operation
1147 | Operation |
1148 | |
1149 | => | Internal
1150 | Internal |
1151 | "{}::get_value::<{}> handler outcome" |
1152 | , | object_name
1153 | value_name |
1154 | ) |
1155 | |
1156 | => Unhandled, | Unhandled
1157 | , |
1158 | ; |
1159 | |
1160 | |
1161 | return Err; |
1162 | else if operation. |
1163 | let get_setting: = operation.downcast .unwrap; |
1164 | let setting_name = get_setting.setting_name.clone; |
1165 | |
1166 | let raw_result = self |
1167 | .get_setting |
1168 | object, |
1169 | to_string, | object_name .
1170 | *get_setting, |
1171 | operation_state, |
1172 | |
1173 | .await; |
1174 | |
1175 | return match raw_result |
1176 | Ok => |
1177 | // Success is the setting type, serialize it |
1178 | // let serialized = (setting_meta.serialize)(success).unwrap(); |
1179 | |
1180 | // Ok(serde_json::to_vec(&serialized).unwrap()) |
1181 | Ok |
1182 | |
1183 | Err => Err |
1184 | => | Operation
1185 | // We know this is the right type |
1186 | Operation |
1187 | |
1188 | => | Internal
1189 | Internal |
1190 | "{}::get_setting::<{}> handler outcome" |
1191 | , | object_name
1192 | setting_name |
1193 | ) |
1194 | |
1195 | => Unhandled, | Unhandled
1196 | , |
1197 | ; |
1198 | else if operation. |
1199 | todo! |
1200 | else if operation. |
1201 | todo! |
1202 | |
1203 | |
1204 | // Resolve the operation from the known operations table. |
1205 | let operation_type = |
1206 | let mut operation_type = None; |
1207 | |
1208 | for in self.metadata.operations.iter |
1209 | // Skip elements that we know will not match |
1210 | if target.object_name != object_name |
1211 | continue; |
1212 | |
1213 | |
1214 | if target.operation_name != operation_name |
1215 | continue; |
1216 | |
1217 | |
1218 | if |
1219 | operation_type = Some; |
1220 | break; |
1221 | |
1222 | |
1223 | |
1224 | operation_type |
1225 | |
1226 | .ok_or_else?; |
1227 | |
1228 | // Resolve the handler from our handler tree |
1229 | let handler_tree = self |
1230 | .operation_handlers |
1231 | .get |
1232 | .ok_or_else?; |
1233 | |
1234 | let raw_result = handler_tree |
1235 | .handle |
1236 | .await; |
1237 | |
1238 | // Convert the dynamic result back into its concrete type |
1239 | match raw_result |
1240 | Ok => Ok, |
1241 | Err => Err |
1242 | => | Internal
1243 | Internal |
1244 | "operation {}::{} handler outcome" |
1245 | operation_type.object_name, operation_type.operation_name |
1246 | ) |
1247 | |
1248 | => | Operation
1249 | Operation |
1250 | |
1251 | => Unhandled, | Unhandled
1252 | , |
1253 | |
1254 | |
1255 | |
1256 | async |
1257 | &self, |
1258 | object_str: &str, |
1259 | _operation_state: &StackOperationState, |
1260 | |
1261 | // TODO: Authorization? |
1262 | for in self.metadata.objects.iter |
1263 | if let Ok = |
1264 | return Ok |
1265 | new_unchecked |
1266 | ; |
1267 | |
1268 | |
1269 | |
1270 | Err |
1271 | |
1272 | |
1273 |