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

ambee/giterated

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

Unified stack refactor clean up

Clean up obsolete code and some warnings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨356f714

⁨giterated-stack/src/update.rs⁩ - ⁨5406⁩ bytes
Raw
1 use futures_util::{future::BoxFuture, FutureExt};
2 use giterated_models::{
3 object::GiteratedObject,
4 settings::{AnySetting, Setting},
5 value::{AnyValue, GiteratedObjectValue},
6 };
7
8 use crate::StackOperationState;
9
10 #[async_trait::async_trait]
11 pub trait HandleValueUpdate<O, V: GiteratedObjectValue<Object = O>> {
12 async fn handle_value_update(
13 &mut self,
14 object: O,
15 value_name: String,
16 value: V,
17 operation_state: &StackOperationState,
18 ) -> Result<(), ()>;
19 }
20
21 #[async_trait::async_trait]
22 impl<F, O, V> HandleValueUpdate<O, V> for F
23 where
24 F: Fn(O, String, V, &StackOperationState) -> BoxFuture<'static, Result<(), ()>> + Send + Sync,
25 O: GiteratedObject + Send + Sync + 'static,
26 V: GiteratedObjectValue<Object = O> + Send + Sync + 'static,
27 {
28 async fn handle_value_update(
29 &mut self,
30 object: O,
31 value_name: String,
32 value: V,
33 operation_state: &StackOperationState,
34 ) -> Result<(), ()> {
35 self(object, value_name, value, operation_state).await
36 }
37 }
38
39 #[async_trait::async_trait]
40 pub trait HandleSettingUpdate<O, S: Setting> {
41 async fn handle_setting_update(
42 &mut self,
43 object: O,
44 setting_name: String,
45 setting: S,
46 operation_state: &StackOperationState,
47 ) -> Result<(), ()>;
48 }
49
50 #[async_trait::async_trait]
51 impl<F, O, S> HandleSettingUpdate<O, S> for F
52 where
53 F: Fn(O, String, S, &StackOperationState) -> BoxFuture<'static, Result<(), ()>> + Send + Sync,
54 O: GiteratedObject + Send + Sync + 'static,
55 S: Setting + Send + Sync + 'static,
56 {
57 async fn handle_setting_update(
58 &mut self,
59 object: O,
60 setting_name: String,
61 setting: S,
62 operation_state: &StackOperationState,
63 ) -> Result<(), ()> {
64 self(object, setting_name, setting, operation_state).await
65 }
66 }
67
68 #[async_trait::async_trait]
69 pub trait ValueUpdatedHandler<O> {
70 async fn value_updated(&mut self, object: &O, value_name: &str, value: AnyValue<()>);
71 }
72
73 #[async_trait::async_trait]
74 pub trait SettingUpdatedHandler<O> {
75 async fn setting_updated(&mut self, object: &O, setting_name: &str, setting: AnySetting);
76 }
77
78 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
79 pub struct ValueUpdateKind {
80 pub object_kind: String,
81 pub value_name: String,
82 }
83
84 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
85 pub struct SettingUpdateKind {
86 pub object_kind: String,
87 pub setting_name: String,
88 }
89
90 pub struct HandleSettingUpdatedFunction {
91 pub target: SettingUpdateKind,
92 pub function: Box<
93 dyn FnOnce(
94 String,
95 String,
96 AnySetting,
97 StackOperationState,
98 ) -> BoxFuture<'static, Result<(), ()>>
99 + Send
100 + Sync,
101 >,
102 }
103
104 impl HandleSettingUpdatedFunction {
105 pub fn new<
106 S: Setting + Send + Sync,
107 T: HandleSettingUpdate<O, S> + 'static + Clone + Send + Sync,
108 O: GiteratedObject + Send + Sync,
109 >(
110 handler: T,
111 setting_name: &str,
112 ) -> Self {
113 Self {
114 target: SettingUpdateKind {
115 object_kind: O::object_name().to_string(),
116 setting_name: setting_name.to_string(),
117 },
118
119 function: Box::new(move |object, setting_name, value, state| {
120 async move {
121 let mut handler = handler;
122
123 let object = match O::from_str(&object) {
124 Ok(object) => object,
125 Err(_) => return Err(()),
126 };
127
128 let setting: S = serde_json::from_value(value.0).unwrap();
129
130 let _ = handler
131 .handle_setting_update(object, setting_name, setting, &state)
132 .await;
133
134 Ok(())
135 }
136 .boxed()
137 }),
138 }
139 }
140 }
141
142 pub struct HandleValueUpdatedFunction {
143 pub target: ValueUpdateKind,
144 pub function: Box<
145 dyn FnOnce(
146 String,
147 String,
148 AnySetting,
149 StackOperationState,
150 ) -> BoxFuture<'static, Result<(), ()>>
151 + Send
152 + Sync,
153 >,
154 }
155
156 impl HandleValueUpdatedFunction {
157 pub fn new<
158 V: GiteratedObjectValue<Object = O> + Send + Sync,
159 T: HandleValueUpdate<O, V> + 'static + Clone + Send + Sync,
160 O: GiteratedObject + Send + Sync,
161 >(
162 handler: T,
163 value_name: &str,
164 ) -> Self {
165 Self {
166 target: ValueUpdateKind {
167 object_kind: O::object_name().to_string(),
168 value_name: value_name.to_string(),
169 },
170
171 function: Box::new(move |object, setting_name, value, state| {
172 async move {
173 let mut handler = handler;
174
175 let object = match O::from_str(&object) {
176 Ok(object) => object,
177 Err(_) => return Err(()),
178 };
179
180 let setting: V = serde_json::from_value(value.0).unwrap();
181
182 let _ = handler
183 .handle_value_update(object, setting_name, setting, &state)
184 .await;
185
186 Ok(())
187 }
188 .boxed()
189 }),
190 }
191 }
192 }
193