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

ambee/giterated

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

Merge branch dyn_inherent into master

This is a squashed commit of the following: commit 6b7c04bc1aae470d9c95b9c5f4e5698bd8c8fef0 Author: Amber <[email protected]> Date: Tue Sep 26 14:03:52 2023 -0500 Give meta types better ergonomics and location

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f94279e

⁨giterated-stack/src/meta/mod.rs⁩ - ⁨6525⁩ bytes
Raw
1 use std::{any::Any, str::FromStr};
2
3 use giterated_models::{
4 object::GiteratedObject,
5 operation::GiteratedOperation,
6 settings::Setting,
7 value::{GetValueTyped, GiteratedObjectValue},
8 };
9 use serde_json::Value;
10
11 pub struct ValueMeta {
12 pub name: String,
13 pub deserialize: Box<dyn Fn(&[u8]) -> Result<Box<dyn Any>, serde_json::Error> + Send + Sync>,
14 pub serialize:
15 Box<dyn Fn(Box<dyn Any + Send + Sync>) -> Result<Vec<u8>, serde_json::Error> + Send + Sync>,
16 pub typed_get: Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>,
17 pub is_get_value_typed: Box<dyn Fn(&Box<dyn Any + Send + Sync>) -> bool + Send + Sync>,
18 }
19
20 pub trait IntoValueMeta {
21 fn name() -> String;
22 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any>, serde_json::Error>;
23 fn serialize(value: Box<dyn Any + Send + Sync>) -> Result<Vec<u8>, serde_json::Error>;
24 fn typed_get() -> Box<dyn Any + Send + Sync>;
25 fn is_get_value_typed(typed_get_value: &Box<dyn Any + Send + Sync>) -> bool;
26 }
27
28 impl<O: GiteratedObject, V: GiteratedObjectValue<Object = O> + 'static> IntoValueMeta for V {
29 fn name() -> String {
30 V::value_name().to_string()
31 }
32
33 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any>, serde_json::Error> {
34 Ok(Box::new(serde_json::from_slice(&buffer)?))
35 }
36
37 fn serialize(value: Box<dyn Any + Send + Sync>) -> Result<Vec<u8>, serde_json::Error> {
38 let value = value.downcast::<V>().unwrap();
39
40 Ok(serde_json::to_vec(&*value)?)
41 }
42
43 fn typed_get() -> Box<dyn Any + Send + Sync> {
44 Box::new(GetValueTyped::<V> {
45 value_name: V::value_name().to_string(),
46 ty: Default::default(),
47 })
48 }
49
50 fn is_get_value_typed(typed_get_value: &Box<dyn Any + Send + Sync>) -> bool {
51 typed_get_value.is::<GetValueTyped<V>>()
52 }
53 }
54
55 impl ValueMeta {
56 pub fn new<I: IntoValueMeta + 'static>() -> Self {
57 Self {
58 name: I::name(),
59 deserialize: Box::new(I::deserialize) as _,
60 serialize: Box::new(I::serialize) as _,
61 typed_get: Box::new(I::typed_get) as _,
62 is_get_value_typed: Box::new(I::is_get_value_typed) as _,
63 }
64 }
65 }
66
67 pub struct OperationMeta {
68 pub name: String,
69 pub object_kind: String,
70 pub deserialize:
71 Box<dyn Fn(&[u8]) -> Result<Box<dyn Any + Send + Sync>, serde_json::Error> + Send + Sync>,
72 pub any_is_same: Box<dyn Fn(&dyn Any) -> bool + Send + Sync>,
73 pub serialize_success:
74 Box<dyn Fn(Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error> + Send + Sync>,
75 pub serialize_error:
76 Box<dyn Fn(Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error> + Send + Sync>,
77 }
78
79 pub trait IntoOperationMeta<O> {
80 fn name() -> String;
81 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any + Send + Sync>, serde_json::Error>;
82 fn serialize_success(success: Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error>;
83 fn serialize_failure(failure: Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error>;
84 fn any_is_same(other: &dyn Any) -> bool;
85 }
86
87 impl<O, D> IntoOperationMeta<O> for D
88 where
89 D::Failure: 'static,
90 D::Success: 'static,
91 O: GiteratedObject,
92 D: GiteratedOperation<O> + 'static,
93 {
94 fn name() -> String {
95 D::operation_name().to_string()
96 }
97
98 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any + Send + Sync>, serde_json::Error> {
99 Ok(Box::new(serde_json::from_slice::<D>(buffer)?) as Box<dyn Any + Send + Sync>)
100 }
101
102 fn serialize_success(success: Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error> {
103 let to_serialize = success.downcast::<D::Success>().unwrap();
104 serde_json::to_vec(&to_serialize)
105 }
106
107 fn serialize_failure(failure: Box<dyn Any>) -> Result<Vec<u8>, serde_json::Error> {
108 let to_serialize = failure.downcast::<D::Failure>().unwrap();
109 serde_json::to_vec(&to_serialize)
110 }
111
112 fn any_is_same(other: &dyn Any) -> bool {
113 other.is::<D>()
114 }
115 }
116
117 impl OperationMeta {
118 pub fn new<O: GiteratedObject + 'static, I: IntoOperationMeta<O> + 'static>() -> Self {
119 Self {
120 name: I::name(),
121 deserialize: Box::new(I::deserialize) as _,
122 serialize_success: Box::new(I::serialize_success) as _,
123 serialize_error: Box::new(I::serialize_failure) as _,
124 object_kind: O::object_name().to_string(),
125 any_is_same: Box::new(I::any_is_same) as _,
126 }
127 }
128 }
129
130 pub struct ObjectMeta {
131 pub name: String,
132 pub from_str: Box<dyn Fn(&str) -> Result<Box<dyn Any + Send + Sync>, ()> + Send + Sync>,
133 pub any_is_same: Box<dyn Fn(&dyn Any) -> bool + Send + Sync>,
134 }
135
136 pub trait IntoObjectMeta: FromStr {
137 fn name() -> String;
138 fn any_is_same(other: &dyn Any) -> bool;
139 }
140
141 impl<O: GiteratedObject + 'static> IntoObjectMeta for O {
142 fn name() -> String {
143 O::object_name().to_string()
144 }
145
146 fn any_is_same(other: &dyn Any) -> bool {
147 other.is::<O>()
148 }
149 }
150
151 impl ObjectMeta {
152 pub fn new<I: IntoObjectMeta + Send + Sync + 'static>() -> Self {
153 Self {
154 name: I::name(),
155 from_str: Box::new(|source| {
156 let object = I::from_str(source).map_err(|_| ())?;
157
158 Ok(Box::new(object) as Box<dyn Any + Send + Sync>)
159 }),
160 any_is_same: Box::new(I::any_is_same) as _,
161 }
162 }
163 }
164
165 pub struct SettingMeta {
166 pub name: String,
167 pub deserialize: Box<dyn Fn(&[u8]) -> Result<Box<dyn Any>, serde_json::Error> + Send + Sync>,
168 pub serialize:
169 Box<dyn Fn(Box<dyn Any + Send + Sync>) -> Result<Value, serde_json::Error> + Send + Sync>,
170 }
171
172 pub trait IntoSettingMeta {
173 fn name() -> String;
174 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any>, serde_json::Error>;
175 fn serialize(setting: Box<dyn Any + Send + Sync>) -> Result<Value, serde_json::Error>;
176 }
177
178 impl<S: Setting> IntoSettingMeta for S {
179 fn name() -> String {
180 S::name().to_string()
181 }
182
183 fn deserialize(buffer: &[u8]) -> Result<Box<dyn Any>, serde_json::Error> {
184 Ok(Box::new(serde_json::from_slice(buffer)?))
185 }
186
187 fn serialize(setting: Box<dyn Any + Send + Sync>) -> Result<Value, serde_json::Error> {
188 Ok(*setting.downcast::<Value>().unwrap())
189 }
190 }
191
192 impl SettingMeta {
193 pub fn new<I: IntoSettingMeta + 'static>() -> Self {
194 Self {
195 name: I::name(),
196 deserialize: Box::new(I::deserialize) as _,
197 serialize: Box::new(I::serialize) as _,
198 }
199 }
200 }
201