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

ambee/giterated

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

Fix internal call values

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8e8ab90

⁨giterated-models/src/object.rs⁩ - ⁨3459⁩ 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 + 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 + Sync + Clone {
60 fn object_name() -> &'static str;
61
62 fn from_object_str(object_str: &str) -> Result<Self, Error>;
63 }
64
65 impl<
66 'b,
67 I: Clone + Send + Sync,
68 O: GiteratedObject + Clone + Debug + 'static,
69 B: ObjectBackend<I>,
70 > Object<'b, I, O, B>
71 {
72 pub async fn get<V: GiteratedObjectValue<Object = O> + Send + Debug + 'static>(
73 &mut self,
74 operation_state: &I,
75 ) -> Result<V, OperationError<GetValueError>> {
76 let result = self
77 .request(
78 GetValue {
79 value_name: V::value_name().to_string(),
80 },
81 operation_state,
82 )
83 .await
84 .unwrap();
85
86 Ok(serde_json::from_slice(&result).unwrap())
87 }
88
89 pub async fn get_setting<S: Setting + Send + Clone + Debug>(
90 &mut self,
91 operation_state: &I,
92 ) -> Result<S, OperationError<GetSettingError>> {
93 self.request(
94 GetSetting {
95 setting_name: S::name().to_string(),
96 },
97 operation_state,
98 )
99 .await
100 .map(|success| serde_json::from_value(success).unwrap())
101 }
102
103 pub async fn set_setting<S: Setting + Send + Clone + Debug>(
104 &mut self,
105 setting: S,
106 operation_state: &I,
107 ) -> Result<(), OperationError<SetSettingError>> {
108 self.request(
109 SetSetting {
110 setting_name: S::name().to_string(),
111 value: serde_json::to_value(setting).unwrap(),
112 },
113 operation_state,
114 )
115 .await
116 }
117
118 pub async fn request<R: GiteratedOperation<O> + Debug + 'static>(
119 &mut self,
120 request: R,
121 operation_state: &I,
122 ) -> Result<R::Success, OperationError<R::Failure>>
123 where
124 R::Success: Clone,
125 R::Failure: Clone,
126 {
127 self.backend
128 .object_operation(
129 self.inner.clone(),
130 R::operation_name(),
131 request,
132 operation_state,
133 )
134 .await
135 }
136 }
137