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

ambee/giterated

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

The long awaited, exhalted huge networking stack change.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨21b6a72

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