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

ambee/giterated

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

Fix handling stack

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c53b026

⁨giterated-models/src/object.rs⁩ - ⁨2653⁩ 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<'b, O: GiteratedObject, B: ObjectBackend + 'b + Send + Sync + Clone> {
22 pub(crate) inner: O,
23 pub(crate) backend: B,
24 pub(crate) _marker: PhantomData<&'b ()>,
25 }
26
27 impl<'b, B: ObjectBackend + Send + Sync + Clone, O: GiteratedObject> Object<'b, O, B> {
28 pub fn object(&self) -> &O {
29 &self.inner
30 }
31
32 pub unsafe fn new_unchecked(object: O, backend: B) -> Object<'b, O, B> {
33 Object {
34 inner: object,
35 backend,
36 _marker: PhantomData,
37 }
38 }
39 }
40
41 impl<O: GiteratedObject + Display, B: ObjectBackend + Send + Sync + Clone> Display
42 for Object<'_, O, B>
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 {
50 fn object_name() -> &'static str;
51
52 fn from_object_str(object_str: &str) -> Result<Self, Error>;
53 }
54
55 impl<'b, O: GiteratedObject + Clone + Debug, B: ObjectBackend> Object<'b, O, B> {
56 pub async fn get<V: GiteratedObjectValue<Object = O> + Send + Debug>(
57 &mut self,
58 ) -> Result<V, OperationError<GetValueError>> {
59 let result = self
60 .request(GetValue {
61 value_name: V::value_name().to_string(),
62 _marker: PhantomData,
63 })
64 .await;
65
66 result
67 }
68
69 pub async fn get_setting<S: Setting + Send + Clone + Debug>(
70 &mut self,
71 ) -> Result<S, OperationError<GetSettingError>> {
72 self.request(GetSetting {
73 setting_name: S::name().to_string(),
74 _marker: PhantomData,
75 })
76 .await
77 }
78
79 pub async fn set_setting<S: Setting + Send + Clone + Debug>(
80 &mut self,
81 setting: S,
82 ) -> Result<(), OperationError<SetSettingError>> {
83 self.request(SetSetting {
84 setting_name: S::name().to_string(),
85 value: setting,
86 })
87 .await
88 }
89
90 pub async fn request<R: GiteratedOperation<O> + Debug>(
91 &mut self,
92 request: R,
93 ) -> Result<R::Success, OperationError<R::Failure>> {
94 self.backend
95 .object_operation(self.inner.clone(), R::operation_name(), request)
96 .await
97 }
98 }
99