Remove unwrap statements and return OperationError's
parent: tbd commit: cf491a4
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 | // TODO: Check this |
370 | .ok_or?; |
371 | |
372 | let setting = |
373 | .as_internal_error_with_context |
374 | "deserializing setting {} for object {}" |
375 | operation.setting_name, object_type |
376 | )?; |
377 | |
378 | trace! |
379 | "Deserialized setting {} for object {}", |
380 | operation.setting_name, |
381 | object_type, |
382 | ; |
383 | |
384 | for provider in self.metadata_providers.iter |
385 | if provider.provides_for |
386 | trace! |
387 | "Resolved setting provider for setting {} for object {}", |
388 | operation.setting_name, |
389 | object_type, |
390 | ; |
391 | |
392 | let object_meta = self |
393 | .metadata |
394 | .objects |
395 | .get |
396 | .ok_or_else?; |
397 | |
398 | let raw_result = provider |
399 | .write |
400 | .await; |
401 | |
402 | return match raw_result |
403 | Ok => |
404 | |
405 | object, |
406 | setting, |
407 | operation_state.runtime.clone, |
408 | operation_state, |
409 | |
410 | .await; |
411 | |
412 | Ok |
413 | |
414 | Err => Err |
415 | "writing object {} setting {}" |
416 | object_type, operation.setting_name |
417 | ), |
418 | ; |
419 | |
420 | |
421 | trace! |
422 | "Failed to resolve setting provider for setting {} for object {}", |
423 | operation.setting_name, |
424 | object_type, |
425 | ; |
426 | |
427 | |
428 | |
429 | let target = ObjectOperationPair |
430 | object_name: &object_type, |
431 | operation_name: &message.operation, |
432 | ; |
433 | |
434 | // Resolve the target operations from the handlers table |
435 | let handler = self |
436 | .operation_handlers |
437 | .get |
438 | .ok_or_else?; |
439 | |
440 | trace! |
441 | "Resolved operation handler for network message {}::<{}>", |
442 | message.operation, |
443 | object_type |
444 | ; |
445 | |
446 | // Deserialize the operation |
447 | let meta = self |
448 | .metadata |
449 | .operations |
450 | .get |
451 | .ok_or_else?; |
452 | |
453 | let operation = |
454 | as_internal_error_with_context | .
455 | "deserializing operation {}::{}" |
456 | target.object_name, target.operation_name |
457 | )?; |
458 | |
459 | trace! |
460 | "Deserialized operation for network message {}::<{}>", |
461 | message.operation, |
462 | object_type |
463 | ; |
464 | |
465 | trace! |
466 | "Calling handler for network message {}::<{}>", |
467 | message.operation, |
468 | object_type |
469 | ; |
470 | |
471 | // Get the raw result of the operation, where the return values are boxed. |
472 | let raw_result = handler |
473 | .handle |
474 | .await; |
475 | |
476 | trace! |
477 | "Finished handling network message {}::<{}>", |
478 | message.operation, |
479 | object_type |
480 | ; |
481 | |
482 | // Deserialize the raw result for the network |
483 | match raw_result |
484 | Ok => Ok, |
485 | Err => Err |
486 | => | Operation
487 | Operation |
488 | |
489 | => | Internal
490 | Internal |
491 | "operation {}::{} handler outcome" |
492 | target.object_name, target.operation_name |
493 | ) |
494 | |
495 | => Unhandled, | Unhandled
496 | , |
497 | |
498 | |
499 | |
500 | pub async |
501 | &self, |
502 | object: AnyObject, |
503 | object_kind: String, |
504 | operation: GetValue, |
505 | operation_state: &StackOperationState, |
506 | |
507 | trace!; |
508 | |
509 | let value_meta = self |
510 | .metadata |
511 | .values |
512 | .get |
513 | object_kind: &object_kind, |
514 | value_kind: &operation.value_name, |
515 | |
516 | .ok_or_else?; |
517 | |
518 | for in self.value_getters.iter |
519 | if target.object_kind != object_kind |
520 | continue; |
521 | |
522 | |
523 | if target.value_kind != operation.value_name |
524 | continue; |
525 | |
526 | |
527 | return match getter |
528 | .handle |
529 | .await |
530 | |
531 | Ok => |
532 | // Serialize success, which is the value type itself |
533 | let serialized = .as_internal_error?; |
534 | |
535 | Ok |
536 | |
537 | Err => Err |
538 | => | Operation
539 | // Failure is sourced from GetValue operation, but this is hardcoded for now |
540 | let failure: &GetValueError = failure.0.downcast_ref .unwrap; |
541 | |
542 | Operation |
543 | |
544 | => Internal, | Internal
545 | => Unhandled, | Unhandled
546 | , |
547 | ; |
548 | |
549 | |
550 | Err |
551 | |
552 | |
553 | pub async |
554 | &self, |
555 | object: AnyObject, |
556 | object_kind: String, |
557 | operation: GetSetting, |
558 | _operation_state: &StackOperationState, |
559 | |
560 | trace! |
561 | "Handling network {}::get_setting for {}", |
562 | object_kind, |
563 | operation.setting_name |
564 | ; |
565 | |
566 | for provider in self.metadata_providers.iter |
567 | if provider.provides_for |
568 | let setting_meta = self |
569 | .metadata |
570 | .settings |
571 | .get |
572 | object_kind: &object_kind, |
573 | setting_name: &operation.setting_name, |
574 | |
575 | .ok_or_else?; |
576 | |
577 | let object_meta = self |
578 | .metadata |
579 | .objects |
580 | .get |
581 | .ok_or_else?; |
582 | |
583 | let result = provider |
584 | .read |
585 | .await |
586 | .as_internal_error_with_context |
587 | "reading setting {}" |
588 | operation.setting_name |
589 | )?; |
590 | |
591 | return .as_internal_error_with_context |
592 | "deserializing setting {}" |
593 | operation.setting_name |
594 | ); |
595 | |
596 | |
597 | |
598 | trace!; |
599 | |
600 | Err |
601 | |
602 | |
603 | pub async |
604 | &self, |
605 | object: AnyObject, |
606 | object_kind: String, |
607 | operation: SetSetting, |
608 | operation_state: &StackOperationState, |
609 | |
610 | trace! |
611 | "Handling network {}::set_setting for {}", |
612 | object_kind, |
613 | operation.setting_name |
614 | ; |
615 | |
616 | let target = ObjectSettingPair |
617 | object_kind: &object_kind, |
618 | setting_name: &operation.setting_name, |
619 | ; |
620 | |
621 | let handler = self.setting_change.get .unwrap; |
622 | |
623 | let setting_meta = self |
624 | .metadata |
625 | .settings |
626 | .get |
627 | object_kind: &object_kind, |
628 | setting_name: &operation.setting_name, |
629 | |
630 | .ok_or_else?; |
631 | |
632 | let setting = |
633 | as_internal_error_with_context | .
634 | "deserializing setting {} for object {}" |
635 | operation.setting_name, object_kind |
636 | )?; |
637 | |
638 | let raw_result = handler |
639 | .handle |
640 | .await; |
641 | |
642 | match raw_result |
643 | Ok => |
644 | // Serialize success, which is the value type itself |
645 | let serialized = to_vec .as_internal_error?; |
646 | |
647 | Ok |
648 | |
649 | Err => Err |
650 | => Internal, | Operation
651 | => Internal, | Internal
652 | => Unhandled, | Unhandled
653 | , |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | async |
661 | &self, |
662 | in_object: O, |
663 | operation_name: &str, |
664 | payload: D, |
665 | operation_state: &StackOperationState, |
666 | |
667 | |
668 | O: GiteratedObject + Debug + 'static, |
669 | D: + Debug + 'static, |
670 | : Clone, | Success
671 | : Clone, | Failure
672 | |
673 | // Erase object and operation types. |
674 | let object = AnyObject; |
675 | let operation = AnyOperation; |
676 | |
677 | // We need to hijack get_value, set_setting, and get_setting. |
678 | if operation_name == "get_value" |
679 | let get_value = operation |
680 | .0 |
681 | . |
682 | .ok_or_else?; |
683 | |
684 | let value_meta = self |
685 | .metadata |
686 | .values |
687 | .get |
688 | object_kind: object_name, |
689 | value_kind: &get_value.value_name, |
690 | |
691 | .ok_or_else?; |
692 | let value_name = value_meta.name.clone; |
693 | |
694 | trace! |
695 | "Handling get_value for {}::{}", |
696 | , | object_name
697 | value_name |
698 | ; |
699 | |
700 | for in self.value_getters.iter |
701 | if target.object_kind != object_name |
702 | continue; |
703 | |
704 | |
705 | if target.value_kind != value_name |
706 | continue; |
707 | |
708 | |
709 | trace! |
710 | "Calling handler for get_value {}::{}", |
711 | , | object_name
712 | value_name |
713 | ; |
714 | |
715 | return match getter |
716 | .handle |
717 | .await |
718 | |
719 | Ok => Ok |
720 | as |
721 | .downcast |
722 | .unwrap, |
723 | Err => Err |
724 | => Operation | Operation
725 | failure.0. .unwrap .clone, |
726 | , |
727 | => | Internal
728 | Internal |
729 | "{}::get_value::<{}> handler outcome" |
730 | , | object_name
731 | value_name |
732 | ) |
733 | |
734 | => Unhandled, | Unhandled
735 | , |
736 | ; |
737 | |
738 | else if operation.0. |
739 | let get_setting: &GetSetting = operation.0.downcast_ref .unwrap; |
740 | let setting_name = get_setting.setting_name.clone; |
741 | |
742 | let raw_result = self |
743 | .get_setting |
744 | object, |
745 | to_string, | object_name .
746 | get_setting.clone, |
747 | operation_state, |
748 | |
749 | .await; |
750 | |
751 | return match raw_result |
752 | Ok => |
753 | // Success is the setting type, serialize it |
754 | // let serialized = (setting_meta.serialize)(success).unwrap(); |
755 | |
756 | // Ok(serde_json::to_vec(&serialized).unwrap()) |
757 | Ok |
758 | |
759 | Err => Err |
760 | => | Operation
761 | // We know this is the right type |
762 | Operation |
763 | |
764 | => | Internal
765 | Internal |
766 | "{}::get_setting::<{}> handler outcome" |
767 | , | object_name
768 | setting_name |
769 | ) |
770 | |
771 | => Unhandled, | Unhandled
772 | , |
773 | ; |
774 | else if operation.0. |
775 | todo! |
776 | else if operation.0. |
777 | todo! |
778 | |
779 | |
780 | // Resolve the operation from the known operations table. |
781 | let operation_type = |
782 | let mut operation_type = None; |
783 | |
784 | for in self.metadata.operations.iter |
785 | // Skip elements that we know will not match |
786 | if target.object_name != object_name |
787 | continue; |
788 | |
789 | |
790 | if target.operation_name != operation_name |
791 | continue; |
792 | |
793 | |
794 | if |
795 | operation_type = Some; |
796 | break; |
797 | |
798 | |
799 | |
800 | operation_type |
801 | |
802 | .ok_or_else?; |
803 | |
804 | // Resolve the handler from our handler tree |
805 | let handler_tree = self |
806 | .operation_handlers |
807 | .get |
808 | .ok_or_else?; |
809 | |
810 | let raw_result = handler_tree |
811 | .handle |
812 | .await; |
813 | |
814 | // Convert the dynamic result back into its concrete type |
815 | match raw_result |
816 | Ok => Ok, |
817 | Err => Err |
818 | => | Internal
819 | Internal |
820 | "operation {}::{} handler outcome" |
821 | operation_type.object_name, operation_type.operation_name |
822 | ) |
823 | |
824 | => Operation | Operation
825 | boxed_error.0. .unwrap .clone, |
826 | , |
827 | => Unhandled, | Unhandled
828 | , |
829 | |
830 | |
831 | |
832 | async |
833 | &self, |
834 | object_str: &str, |
835 | _operation_state: &StackOperationState, |
836 | |
837 | |
838 | O: GiteratedObject + Debug + 'static, |
839 | |
840 | // TODO: Authorization? |
841 | for in self.metadata.objects.iter |
842 | if let Ok = |
843 | return Ok |
844 | new_unchecked |
845 | object.0. .unwrap .clone, |
846 | self.clone, |
847 | |
848 | ; |
849 | |
850 | |
851 | |
852 | Err |
853 | |
854 | |
855 | |
856 | /// Defines a type that is a valid Giterated runtime state. |
857 | /// |
858 | /// This allows for extraction of state in handlers, based on a |
859 | /// [`FromOperationState<S>`] impl on (what is in this case) [`Self`]. |
860 | |
861 | |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | T: , |
868 | |
869 | type Error = MissingValue; |
870 | |
871 | async |
872 | required_parameters: &R, |
873 | operation_state: &S, |
874 | |
875 | Ok |
876 | .await |
877 | .ok |
878 | |
879 | |
880 |