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

ambee/giterated

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

Giterated stack changeover, refactor still incomplete

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8d40dfe

⁨giterated-stack/src/lib.rs⁩ - ⁨6406⁩ bytes
Raw
1 pub mod handler;
2 pub mod state;
3
4 use std::{future::Future, pin::Pin, sync::Arc};
5
6 use futures_util::FutureExt;
7 use giterated_models::{
8 error::OperationError,
9 instance::Instance,
10 object::{AnyObject, GiteratedObject, ObjectRequest, ObjectResponse},
11 operation::{AnyOperation, GiteratedOperation},
12 };
13
14 pub struct OperationHandlers<S: Send + Sync + Clone> {
15 operations: Vec<OperationWrapper<S>>,
16 get_object: Vec<OperationWrapper<S>>,
17 }
18
19 impl<S: Send + Sync + Clone> Default for OperationHandlers<S> {
20 fn default() -> Self {
21 Self {
22 operations: Vec::new(),
23 get_object: Vec::new(),
24 }
25 }
26 }
27
28 impl<S: Send + Sync + Clone + 'static> OperationHandlers<S> {
29 pub fn insert<
30 O: GiteratedObject + Send + Sync,
31 D: GiteratedOperation<O> + 'static,
32 H: GiteratedOperationHandler<O, D, S> + Send + Sync + 'static + Clone,
33 >(
34 &mut self,
35 handler: H,
36 ) -> &mut Self {
37 let _operation_name = handler.operation_name().to_string();
38
39 let wrapped = OperationWrapper::new(handler);
40
41 self.operations.push(wrapped);
42
43 self
44 }
45
46 pub fn register_object<O: GiteratedObject + Send + Sync>(&mut self) -> &mut Self {
47 let closure = |_: &Instance, operation: ObjectRequest, _state| {
48 async move {
49 if O::from_str(&operation.0).is_ok() {
50 Ok(ObjectResponse(operation.0))
51 } else {
52 Err(OperationError::Unhandled)
53 }
54 }
55 .boxed()
56 };
57
58 let wrapped = OperationWrapper::new(closure);
59
60 self.get_object.push(wrapped);
61
62 self
63 }
64
65 pub async fn handle<O: GiteratedObject>(
66 &self,
67 object: &O,
68 _operation_name: &str,
69 operation: AnyOperation,
70 state: S,
71 ) -> Result<Vec<u8>, OperationError<Vec<u8>>> {
72 for handler in self.operations.iter() {
73 return match handler
74 .handle(
75 AnyObject(object.to_string()),
76 operation.clone(),
77 state.clone(),
78 )
79 .await
80 {
81 Ok(ok) => Ok(ok),
82 Err(err) => Err(match err {
83 OperationError::Operation(err) => OperationError::Operation(err),
84 OperationError::Internal(err) => OperationError::Internal(err),
85 OperationError::Unhandled => continue,
86 }),
87 };
88 }
89
90 Err(OperationError::Unhandled)
91 }
92
93 pub async fn resolve_object(
94 &self,
95 instance: AnyObject,
96 request: ObjectRequest,
97 state: S,
98 ) -> Result<Vec<u8>, OperationError<Vec<u8>>> {
99 for handler in self.get_object.iter() {
100 if let Ok(response) = handler
101 .handle(
102 instance.clone(),
103 AnyOperation(serde_json::to_value(request.clone()).unwrap()),
104 state.clone(),
105 )
106 .await
107 {
108 return Ok(response);
109 }
110 }
111
112 Err(OperationError::Unhandled)
113 }
114 }
115
116 #[async_trait::async_trait]
117 pub trait GiteratedOperationHandler<
118 O: GiteratedObject,
119 D: GiteratedOperation<O>,
120 S: Send + Sync + Clone,
121 >
122 {
123 fn operation_name(&self) -> &str;
124 fn object_name(&self) -> &str;
125
126 async fn handle(
127 &self,
128 object: &O,
129 operation: D,
130 state: S,
131 ) -> Result<D::Success, OperationError<D::Failure>>;
132 }
133
134 #[async_trait::async_trait]
135 impl<O, D, F, S> GiteratedOperationHandler<O, D, S> for F
136 where
137 F: FnMut(
138 &O,
139 D,
140 S,
141 ) -> Pin<
142 Box<dyn Future<Output = Result<D::Success, OperationError<D::Failure>>> + Send>,
143 > + Send
144 + Sync
145 + Clone,
146 O: GiteratedObject + Send + Sync,
147 D: GiteratedOperation<O> + 'static,
148 <D as GiteratedOperation<O>>::Failure: Send,
149 S: Send + Sync + Clone + 'static,
150 {
151 fn operation_name(&self) -> &str {
152 D::operation_name()
153 }
154
155 fn object_name(&self) -> &str {
156 O::object_name()
157 }
158
159 async fn handle(
160 &self,
161 object: &O,
162 operation: D,
163 state: S,
164 ) -> Result<D::Success, OperationError<D::Failure>> {
165 self.clone()(object, operation, state).await
166 }
167 }
168
169 pub struct OperationWrapper<S: Send + Sync + Clone>(
170 Box<
171 dyn Fn(
172 AnyObject,
173 AnyOperation,
174 S,
175 )
176 -> Pin<Box<dyn Future<Output = Result<Vec<u8>, OperationError<Vec<u8>>>> + Send>>
177 + Send
178 + Sync,
179 >,
180 );
181
182 impl<S: Send + Sync + Clone + 'static> OperationWrapper<S> {
183 pub fn new<
184 O: GiteratedObject + Send + Sync,
185 D: GiteratedOperation<O> + 'static,
186 F: GiteratedOperationHandler<O, D, S> + Send + Sync + 'static + Clone,
187 >(
188 handler: F,
189 ) -> Self {
190 let handler = Arc::new(Box::pin(handler));
191 Self(Box::new(move |any_object, any_operation, state| {
192 let handler = handler.clone();
193 async move {
194 let handler = handler.clone();
195 let object: O =
196 O::from_object_str(&any_object.0).map_err(|_| OperationError::Unhandled)?;
197 let operation: D = serde_json::from_value(any_operation.0.clone())
198 .map_err(|_| OperationError::Unhandled)?;
199
200 let result = handler.handle(&object, operation, state).await;
201 result
202 .map(|success| serde_json::to_vec(&success).unwrap())
203 .map_err(|err| match err {
204 OperationError::Operation(err) => {
205 OperationError::Operation(serde_json::to_vec(&err).unwrap())
206 }
207 OperationError::Internal(internal) => OperationError::Internal(internal),
208 OperationError::Unhandled => OperationError::Unhandled,
209 })
210 }
211 .boxed()
212 }))
213 }
214
215 async fn handle(
216 &self,
217 object: AnyObject,
218 operation: AnyOperation,
219 state: S,
220 ) -> Result<Vec<u8>, OperationError<Vec<u8>>> {
221 self.0(object, operation, state).await
222 }
223 }
224