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

ambee/giterated

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

Fixed imports!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨ef0e853

⁨giterated-models/src/object.rs⁩ - ⁨2355⁩ 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 pub trait GiteratedObject: Send + Display + FromStr {
42 fn object_name() -> &'static str;
43
44 fn from_object_str(object_str: &str) -> Result<Self, Error>;
45 }
46
47 impl<'b, O: GiteratedObject + Clone + Debug, B: ObjectBackend> Object<'b, O, B> {
48 pub async fn get<V: GiteratedObjectValue<Object = O> + Send + Debug>(
49 &mut self,
50 ) -> Result<V, OperationError<GetValueError>> {
51 self.request(GetValue {
52 value_name: V::value_name().to_string(),
53 _marker: PhantomData,
54 })
55 .await
56 }
57
58 pub async fn get_setting<S: Setting + Send + Clone + Debug>(
59 &mut self,
60 ) -> Result<S, OperationError<GetSettingError>> {
61 self.request(GetSetting {
62 setting_name: S::name().to_string(),
63 _marker: PhantomData,
64 })
65 .await
66 }
67
68 pub async fn set_setting<S: Setting + Send + Clone + Debug>(
69 &mut self,
70 setting: S,
71 ) -> Result<(), OperationError<SetSettingError>> {
72 self.request(SetSetting {
73 setting_name: S::name().to_string(),
74 value: setting,
75 })
76 .await
77 }
78
79 pub async fn request<R: GiteratedOperation<O> + Debug>(
80 &mut self,
81 request: R,
82 ) -> Result<R::Success, OperationError<R::Failure>> {
83 self.backend
84 .object_operation(self.inner.clone(), request)
85 .await
86 }
87 }
88