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