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

ambee/giterated

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

Begin new protocol refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨26651b1

⁨giterated-models/src/operation/mod.rs⁩ - ⁨4942⁩ bytes
Raw
1 use std::{any::type_name, fmt::Debug, marker::PhantomData};
2
3 use anyhow::Error;
4 use serde::{de::DeserializeOwned, Deserialize, Serialize};
5
6 use crate::{
7 error::{GetValueError, OperationError},
8 model::{instance::Instance, MessageTarget},
9 };
10
11 pub mod instance;
12 pub mod repository;
13 pub mod user;
14
15 pub trait GiteratedObject: Send + Serialize + DeserializeOwned {
16 fn object_name(&self) -> &str;
17
18 fn from_object_str(object_str: &str) -> Result<Self, Error>;
19 }
20
21 pub trait GiteratedOperation<O: GiteratedObject>: Send + Serialize + DeserializeOwned {
22 type Success: Serialize + DeserializeOwned + Send;
23 type Failure: Serialize + DeserializeOwned + Send;
24
25 fn operation_name(&self) -> &'static str {
26 type_name::<Self>()
27 }
28 }
29
30 pub trait GiteratedObjectValue: Serialize + DeserializeOwned {
31 type Object: GiteratedObject;
32
33 fn value_name() -> &'static str;
34 }
35
36 #[derive(Debug, Clone)]
37 pub struct Object<'b, O: GiteratedObject, B: ObjectBackend + 'b + Send + Sync + Clone> {
38 inner: O,
39 backend: B,
40 _marker: PhantomData<&'b ()>,
41 }
42
43 #[async_trait::async_trait]
44 pub trait ObjectBackend: Send + Sync + Sized + Clone {
45 async fn object_operation<O: GiteratedObject + Debug, D: GiteratedOperation<O> + Debug>(
46 &self,
47 object: O,
48 operation: D,
49 ) -> Result<D::Success, OperationError<D::Failure>>;
50
51 async fn get_object<O: GiteratedObject + Debug>(
52 &self,
53 object_str: &str,
54 ) -> Result<Object<O, Self>, OperationError<ObjectRequestError>>;
55 }
56
57 impl<'b, B: ObjectBackend + Send + Sync + Clone, O: GiteratedObject> Object<'b, O, B> {
58 pub unsafe fn new_unchecked(object: O, backend: B) -> Object<'b, O, B> {
59 Object {
60 inner: object,
61 backend,
62 _marker: PhantomData,
63 }
64 }
65 }
66
67 // impl<'b, O: GiteratedObject, B: ObjectBackend> Object<'b, O, B> {
68 // pub unsafe fn new_unchecked(value: O, backend: Arc<B>) -> Object<'b, O, B> {
69 // todo!()
70 // }
71 // }
72
73 impl<'b, O: GiteratedObject + Clone + Debug, B: ObjectBackend + Debug + Send + Sync + Clone>
74 Object<'b, O, B>
75 {
76 pub async fn get<V: GiteratedObjectValue<Object = O> + Send>(
77 &self,
78 ) -> Result<V, OperationError<ObjectRequestError>> {
79 let operation: GetValue<V> = GetValue {
80 value_name: V::value_name().to_string(),
81 _marker: PhantomData,
82 };
83
84 let _message: GiteratedMessage<O, _> = GiteratedMessage {
85 object: self.inner.clone(),
86 operation: operation.operation_name().to_string(),
87 payload: operation,
88 };
89
90 todo!()
91 }
92
93 pub fn request<R: GiteratedOperation<O> + Debug>(
94 &mut self,
95 request: R,
96 ) -> Result<R::Success, R::Failure> {
97 self.backend.object_operation(self.inner.clone(), request);
98
99 todo!()
100 }
101 }
102
103 #[derive(Serialize, Deserialize)]
104 pub struct GetValue<V: GiteratedObjectValue> {
105 value_name: String,
106 _marker: PhantomData<V>,
107 }
108
109 impl<O: GiteratedObject + Send, V: GiteratedObjectValue<Object = O> + Send> GiteratedOperation<O>
110 for GetValue<V>
111 {
112 fn operation_name(&self) -> &'static str {
113 "get_value"
114 }
115 type Success = V;
116 type Failure = GetValueError;
117 }
118
119 #[derive(Serialize)]
120 #[serde(bound(deserialize = "O: GiteratedObject, V: GiteratedOperation<O>"))]
121 pub struct GiteratedMessage<O: GiteratedObject, V: GiteratedOperation<O>> {
122 pub object: O,
123 pub operation: String,
124 pub payload: V,
125 }
126
127 impl<O: GiteratedObject, V: GiteratedOperation<O>> MessageTarget for GiteratedMessage<O, V> {}
128
129 impl<V: GiteratedOperation<O> + Debug, O: GiteratedObject + Debug> Debug
130 for GiteratedMessage<O, V>
131 {
132 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 f.debug_struct("GiteratedMessage")
134 .field("object", &self.object)
135 .field("operation", &self.operation)
136 .field("payload", &self.payload)
137 .finish()
138 }
139 }
140
141 #[derive(Debug, Serialize, Deserialize)]
142 pub struct ObjectRequest(pub String);
143
144 #[derive(Serialize, Deserialize)]
145 pub struct ObjectResponse(pub Vec<u8>);
146
147 impl GiteratedOperation<Instance> for ObjectRequest {
148 type Success = ObjectResponse;
149
150 type Failure = ObjectRequestError;
151 }
152
153 #[derive(Debug, thiserror::Error, Serialize, Deserialize)]
154 pub enum ObjectRequestError {
155 #[error("error decoding the object")]
156 Deserialization(String),
157 }
158
159 #[derive(Clone, Debug, Serialize, Deserialize)]
160 pub struct AnyObject(Vec<u8>);
161
162 impl GiteratedObject for AnyObject {
163 fn object_name(&self) -> &str {
164 "any"
165 }
166
167 fn from_object_str(object_str: &str) -> Result<Self, Error> {
168 Ok(Self(Vec::from(object_str.as_bytes())))
169 }
170 }
171
172 #[derive(Clone, Debug, Serialize, Deserialize)]
173 pub struct AnyOperation(Vec<u8>);
174
175 impl<O: GiteratedObject> GiteratedOperation<O> for AnyOperation {
176 type Success = Vec<u8>;
177
178 type Failure = Vec<u8>;
179 }
180