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

ambee/giterated

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

Finish unified stack refactor.

Adds support for operation state, which will be used to pass authentication information around. Added generic backend that uses a channel to communicate with a typed backend.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨d15581c

⁨giterated-models/src/object.rs⁩ - ⁨3242⁩ bytes
Raw
1 use std::{
2 fmt::{Debug, Display},
3 marker::PhantomData,
4 str::FromStr,
5 };
6
7 use anyhow::Error;
8
9 use crate::{
10 error::{GetValueError, OperationError},
11 object_backend::ObjectBackend,
12 operation::GiteratedOperation,
13 settings::{GetSetting, GetSettingError, SetSetting, SetSettingError, Setting},
14 value::{GetValue, GiteratedObjectValue},
15 };
16
17 mod operations;
18 pub use operations::*;
19
20 #[derive(Debug, Clone)]
21 pub struct Object<
22 'b,
23 S: Clone + Send + Sync,
24 O: GiteratedObject,
25 B: ObjectBackend<S> + 'b + Send + Sync + Clone,
26 > {
27 pub(crate) inner: O,
28 pub(crate) backend: B,
29 pub(crate) _marker: PhantomData<&'b S>,
30 }
31
32 impl<'b, S: Clone + Send + Sync, B: ObjectBackend<S> + Send + Sync + Clone, O: GiteratedObject>
33 Object<'b, S, O, B>
34 {
35 pub fn object(&self) -> &O {
36 &self.inner
37 }
38
39 pub unsafe fn new_unchecked(object: O, backend: B) -> Object<'b, S, O, B> {
40 Object {
41 inner: object,
42 backend,
43 _marker: PhantomData,
44 }
45 }
46 }
47
48 impl<
49 S: Clone + Send + Sync,
50 O: GiteratedObject + Display,
51 B: ObjectBackend<S> + Send + Sync + Clone,
52 > Display for Object<'_, S, O, B>
53 {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 self.inner.fmt(f)
56 }
57 }
58
59 pub trait GiteratedObject: Send + Display + FromStr {
60 fn object_name() -> &'static str;
61
62 fn from_object_str(object_str: &str) -> Result<Self, Error>;
63 }
64
65 impl<'b, I: Clone + Send + Sync, O: GiteratedObject + Clone + Debug, B: ObjectBackend<I>>
66 Object<'b, I, O, B>
67 {
68 pub async fn get<V: GiteratedObjectValue<Object = O> + Send + Debug>(
69 &mut self,
70 operation_state: &I,
71 ) -> Result<V, OperationError<GetValueError>> {
72 let result = self
73 .request(
74 GetValue {
75 value_name: V::value_name().to_string(),
76 _marker: PhantomData,
77 },
78 operation_state,
79 )
80 .await;
81
82 result
83 }
84
85 pub async fn get_setting<S: Setting + Send + Clone + Debug>(
86 &mut self,
87 operation_state: &I,
88 ) -> Result<S, OperationError<GetSettingError>> {
89 self.request(
90 GetSetting {
91 setting_name: S::name().to_string(),
92 _marker: PhantomData,
93 },
94 operation_state,
95 )
96 .await
97 }
98
99 pub async fn set_setting<S: Setting + Send + Clone + Debug>(
100 &mut self,
101 setting: S,
102 operation_state: &I,
103 ) -> Result<(), OperationError<SetSettingError>> {
104 self.request(
105 SetSetting {
106 setting_name: S::name().to_string(),
107 value: setting,
108 },
109 operation_state,
110 )
111 .await
112 }
113
114 pub async fn request<R: GiteratedOperation<O> + Debug>(
115 &mut self,
116 request: R,
117 operation_state: &I,
118 ) -> Result<R::Success, OperationError<R::Failure>> {
119 self.backend
120 .object_operation(
121 self.inner.clone(),
122 R::operation_name(),
123 request,
124 operation_state,
125 )
126 .await
127 }
128 }
129