JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Re-expose Operation State in generics.

This is the worst code I have ever written. I hate the way this changes everything. ugh.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90db3e2

⁨giterated-stack/src/dynamic.rs⁩ - ⁨4651⁩ bytes
Raw
1 use std::{any::Any, ops::Deref, sync::Arc};
2
3 use giterated_models::{
4 object::GiteratedObject, operation::GiteratedOperation, settings::Setting,
5 value::GiteratedObjectValue,
6 };
7
8 use crate::{ObjectOperationPair, ObjectSettingPair, ObjectValuePair, ValueMeta};
9
10 #[derive(Clone)]
11 pub struct AnyObject {
12 inner: Arc<dyn Any + Send + Sync>,
13 kind: &'static str,
14 }
15
16 impl AnyObject {
17 pub fn new<O: GiteratedObject + 'static>(object: O) -> Self {
18 Self {
19 inner: Arc::new(object) as _,
20 kind: O::object_name(),
21 }
22 }
23
24 pub fn new_raw(_object: Arc<dyn Any + Send + Sync>, _kind: &'static str) -> Self {
25 todo!()
26 }
27
28 pub fn kind(&self) -> &'static str {
29 self.kind
30 }
31 }
32
33 impl Deref for AnyObject {
34 type Target = dyn Any + Send + Sync;
35
36 fn deref(&self) -> &Self::Target {
37 self.inner.as_ref()
38 }
39 }
40
41 #[derive(Clone)]
42 pub struct AnyOperation {
43 inner: Arc<dyn Any + Send + Sync>,
44 kind: ObjectOperationPair<'static>,
45 }
46
47 impl AnyOperation {
48 pub fn new<O: GiteratedObject, D: GiteratedOperation<O> + 'static>(operation: D) -> Self {
49 Self {
50 inner: Arc::new(operation) as _,
51 kind: ObjectOperationPair::from_types::<O, D>(),
52 }
53 }
54
55 pub fn new_raw(
56 _operation: Arc<dyn Any + Send + Sync>,
57 _kind: ObjectOperationPair<'static>,
58 ) -> Self {
59 todo!()
60 }
61
62 pub fn kind(&self) -> ObjectOperationPair<'static> {
63 self.kind
64 }
65 }
66
67 impl Deref for AnyOperation {
68 type Target = dyn Any + Send + Sync;
69
70 fn deref(&self) -> &Self::Target {
71 self.inner.as_ref()
72 }
73 }
74
75 #[derive(Clone)]
76 pub struct AnyValue {
77 inner: Arc<dyn Any + Send + Sync>,
78 kind: ObjectValuePair<'static>,
79 }
80
81 impl AnyValue {
82 pub fn new<O: GiteratedObject, V: GiteratedObjectValue<Object = O> + 'static>(
83 value: V,
84 ) -> Self {
85 Self {
86 inner: Arc::new(value) as _,
87 kind: ObjectValuePair::from_types::<O, V>(),
88 }
89 }
90
91 pub fn new_raw(_value: Arc<dyn Any + Send + Sync>, _kind: ObjectValuePair<'static>) -> Self {
92 todo!()
93 }
94
95 pub fn kind(&self) -> ObjectValuePair<'static> {
96 self.kind
97 }
98 }
99
100 impl Deref for AnyValue {
101 type Target = dyn Any + Send + Sync;
102
103 fn deref(&self) -> &Self::Target {
104 self.inner.as_ref()
105 }
106 }
107
108 #[derive(Clone)]
109 pub struct AnySetting {
110 inner: Arc<dyn Any + Send + Sync>,
111 kind: ObjectSettingPair<'static>,
112 }
113
114 impl AnySetting {
115 pub fn new<O: GiteratedObject, S: Setting + 'static>(setting: S) -> Self {
116 Self {
117 inner: Arc::new(setting) as _,
118 kind: ObjectSettingPair::from_types::<O, S>(),
119 }
120 }
121
122 pub fn new_raw(
123 _setting: Arc<dyn Any + Send + Sync>,
124 _kind: ObjectSettingPair<'static>,
125 ) -> Self {
126 todo!()
127 }
128
129 pub fn kind(&self) -> ObjectSettingPair<'static> {
130 self.kind
131 }
132 }
133
134 impl Deref for AnySetting {
135 type Target = dyn Any + Send + Sync;
136
137 fn deref(&self) -> &Self::Target {
138 self.inner.as_ref()
139 }
140 }
141
142 #[derive(Clone)]
143 pub struct AnySuccess(pub Arc<dyn Any + Send>);
144
145 #[derive(Clone)]
146 pub struct AnyFailure(pub Arc<dyn Any + Send>);
147
148 /// Should be renamed.
149 ///
150 /// Allows accepting object types that are either GiteratedObject types or
151 /// AnyObject.
152 pub trait MaybeDynamicObject: Clone {
153 fn from_any(object: &AnyObject) -> Self;
154
155 fn object_name() -> &'static str;
156 }
157
158 impl<O: GiteratedObject> MaybeDynamicObject for O {
159 fn from_any(_object: &AnyObject) -> Self {
160 todo!()
161 }
162
163 fn object_name() -> &'static str {
164 <O as GiteratedObject>::object_name()
165 }
166 }
167
168 impl MaybeDynamicObject for AnyObject {
169 fn from_any(object: &AnyObject) -> Self {
170 object.clone()
171 }
172
173 fn object_name() -> &'static str {
174 "any"
175 }
176 }
177
178 pub trait MaybeDynamicValue {
179 fn from_any(value: &AnyValue) -> Self;
180 fn into_any(self) -> AnyValue;
181 fn meta() -> Option<ValueMeta>;
182
183 fn value_name() -> &'static str;
184 }
185
186 impl<V: GiteratedObjectValue> MaybeDynamicValue for V {
187 fn from_any(_object: &AnyValue) -> Self {
188 todo!()
189 }
190
191 fn value_name() -> &'static str {
192 todo!()
193 }
194
195 fn into_any(self) -> AnyValue {
196 todo!()
197 }
198
199 fn meta() -> Option<ValueMeta> {
200 todo!()
201 }
202 }
203
204 impl MaybeDynamicValue for AnyValue {
205 fn value_name() -> &'static str {
206 "any"
207 }
208
209 fn from_any(value: &AnyValue) -> Self {
210 value.clone()
211 }
212
213 fn into_any(self) -> AnyValue {
214 self
215 }
216
217 fn meta() -> Option<ValueMeta> {
218 todo!()
219 }
220 }
221