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

ambee/giterated

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

More restructuring

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨10b7b7c

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