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/handler.rs⁩ - ⁨5182⁩ bytes
Raw
1 use giterated_models::{
2 error::OperationError,
3 object::{
4 AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse,
5 },
6 object_backend::ObjectBackend,
7 operation::{AnyOperation, GiteratedOperation},
8 };
9 use std::{fmt::Debug, str::FromStr, sync::Arc};
10 use tracing::warn;
11
12 use crate::{state::HandlerState, OperationHandlers};
13
14 #[derive(Clone)]
15 pub struct GiteratedBackend<S: HandlerState> {
16 state: S,
17 handlers: Arc<OperationHandlers<S>>,
18 }
19
20 impl<S: HandlerState> GiteratedBackend<S> {
21 pub fn new(state: S, handlers: OperationHandlers<S>) -> Self {
22 Self {
23 state,
24 handlers: Arc::new(handlers),
25 }
26 }
27 }
28
29 #[async_trait::async_trait]
30 impl<S: HandlerState> ObjectBackend for GiteratedBackend<S> {
31 async fn object_operation<O, D>(
32 &self,
33 object: O,
34 operation: &str,
35 payload: D,
36 ) -> Result<D::Success, OperationError<D::Failure>>
37 where
38 O: GiteratedObject + Debug,
39 D: GiteratedOperation<O> + Debug,
40 {
41 let serialized =
42 serde_json::to_value(payload).map_err(|e| OperationError::Internal(e.to_string()))?;
43 let object = object.to_string();
44
45 if operation == ObjectRequest::operation_name() {
46 // We're doing an object request
47 let raw_result = self
48 .handlers
49 .resolve_object(
50 AnyObject(object.clone()),
51 serde_json::from_value(serialized).unwrap(),
52 self.state.clone(),
53 )
54 .await;
55
56 return match raw_result {
57 Ok(result) => Ok(serde_json::from_slice(&result)
58 .map_err(|e| OperationError::Internal(e.to_string()))?),
59 Err(err) => match err {
60 OperationError::Internal(internal) => {
61 warn!(
62 "Internal Error: {:?}",
63 OperationError::<()>::Internal(internal.clone())
64 );
65
66 Err(OperationError::Internal(internal))
67 }
68 OperationError::Unhandled => Err(OperationError::Unhandled),
69 OperationError::Operation(err) => Err(OperationError::Operation(
70 serde_json::from_slice(&err)
71 .map_err(|e| OperationError::Internal(e.to_string()))?,
72 )),
73 },
74 };
75 }
76
77 let raw_result = self
78 .handlers
79 .handle(
80 &AnyObject(object),
81 operation,
82 AnyOperation(serialized),
83 self.state.clone(),
84 )
85 .await;
86
87 match raw_result {
88 Ok(result) => Ok(serde_json::from_slice(&result)
89 .map_err(|e| OperationError::Internal(e.to_string()))?),
90 Err(err) => match err {
91 OperationError::Internal(internal) => {
92 warn!(
93 "Internal Error: {:?}",
94 OperationError::<()>::Internal(internal.clone())
95 );
96
97 Err(OperationError::Internal(internal))
98 }
99 OperationError::Unhandled => Err(OperationError::Unhandled),
100 OperationError::Operation(err) => Err(OperationError::Operation(
101 serde_json::from_slice(&err)
102 .map_err(|e| OperationError::Internal(e.to_string()))?,
103 )),
104 },
105 }
106 }
107
108 async fn get_object<O: GiteratedObject + Debug>(
109 &self,
110 object_str: &str,
111 ) -> Result<Object<O, Self>, OperationError<ObjectRequestError>> {
112 let raw_result = self
113 .handlers
114 .resolve_object(
115 AnyObject("giterated.dev".to_string()),
116 ObjectRequest(object_str.to_string()),
117 self.state.clone(),
118 )
119 .await;
120
121 let object: ObjectResponse = match raw_result {
122 Ok(result) => Ok(serde_json::from_slice(&result)
123 .map_err(|e| OperationError::Internal(e.to_string()))?),
124 Err(err) => match err {
125 OperationError::Internal(internal) => {
126 warn!(
127 "Internal Error: {:?}",
128 OperationError::<()>::Internal(internal.clone())
129 );
130
131 Err(OperationError::Internal(internal))
132 }
133 OperationError::Unhandled => Err(OperationError::Unhandled),
134 OperationError::Operation(err) => Err(OperationError::Operation(
135 serde_json::from_slice(&err)
136 .map_err(|e| OperationError::Internal(e.to_string()))?,
137 )),
138 },
139 }?;
140
141 unsafe {
142 Ok(Object::new_unchecked(
143 O::from_str(&object.0)
144 .map_err(|_| OperationError::Internal("deserialize failure".to_string()))?,
145 self.clone(),
146 ))
147 }
148 }
149 }
150