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

ambee/giterated

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

Major refactor to handler traits

Added `IntoGiteratedHandler`, which is the new trait for handler functions. This allows us to finally get rid of the Object and ObjectOperation bounds that resulted in hacks around the newer features of the unified stack. Squashed commit of the following: commit 62e1ecf76ee31cda0bab4602d9d00fa0dc2f9158 Author: Amber <[email protected]> Date: Wed Oct 11 09:31:11 2023 -0500 Update commit dfd2d1b0b5d81ee3bc48f0321c6aceaa677e3b8b Author: Amber <[email protected]> Date: Wed Oct 11 09:31:07 2023 -0500 Major refactor to handler traits Added `IntoGiteratedHandler`, which is the new trait for handler functions. This allows us to finally get rid of the Object and ObjectOperation bounds that resulted in hacks around the newer features of the unified stack. Removed dead and legacy code. I think... commit 57b4b398eff32e69f2f4b9700e42a1277a4d1055 Author: Amber <[email protected]> Date: Sun Oct 1 23:05:10 2023 -0500 New handler trait for giterated stack Refactor the old handler trait so it is more generic and can be used for specific kinds of handlers

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90c4780

⁨giterated-stack/src/update.rs⁩ - ⁨5431⁩ 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 _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(
129 // let _ = handler
130 // .handle_setting_update(object, setting_name, setting, &state)
131 // .await;value.0).unwrap();
132
133 Ok(())
134 }
135 .boxed()
136 }),
137 }
138 }
139 }
140
141 pub struct HandleValueUpdatedFunction {
142 pub target: ValueUpdateKind,
143 pub function: Box<
144 dyn FnOnce(
145 String,
146 String,
147 AnySetting,
148 StackOperationState,
149 ) -> BoxFuture<'static, Result<(), ()>>
150 + Send
151 + Sync,
152 >,
153 }
154
155 impl HandleValueUpdatedFunction {
156 pub fn new<
157 V: GiteratedObjectValue<Object = O> + Send + Sync,
158 T: HandleValueUpdate<O, V> + 'static + Clone + Send + Sync,
159 O: GiteratedObject + Send + Sync,
160 >(
161 handler: T,
162 value_name: &str,
163 ) -> Self {
164 Self {
165 target: ValueUpdateKind {
166 object_kind: O::object_name().to_string(),
167 value_name: value_name.to_string(),
168 },
169
170 function: Box::new(move |object, _setting_name, _value, _state| {
171 async move {
172 let _handler = handler;
173
174 let _object = match O::from_str(&object) {
175 Ok(object) => object,
176 Err(_) => return Err(()),
177 };
178
179 // let setting: V = serde_json::from_value(value.0).unwrap();
180
181 // let _ = handler
182 // .handle_value_update(object, setting_name, setting, &state)
183 // .await;
184
185 Ok(())
186 }
187 .boxed()
188 }),
189 }
190 }
191 }
192