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

ambee/giterated

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

no clue what this is

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨7889bf6

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