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