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

ambee/giterated

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

Remove unwrap statements and return OperationError's

erremilia - ⁨2⁩ years ago

parent: tbd commit: ⁨cf491a4

⁨giterated-models/src/object.rs⁩ - ⁨3438⁩ 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
85 Ok(serde_json::from_slice(&result).unwrap())
86 }
87
88 pub async fn get_setting<S: Setting + Send + Clone + Debug>(
89 &mut self,
90 operation_state: &I,
91 ) -> Result<S, OperationError<GetSettingError>> {
92 self.request(
93 GetSetting {
94 setting_name: S::name().to_string(),
95 },
96 operation_state,
97 )
98 .await
99 .map(|success| serde_json::from_value(success).unwrap())
100 }
101
102 pub async fn set_setting<S: Setting + Send + Clone + Debug>(
103 &mut self,
104 setting: S,
105 operation_state: &I,
106 ) -> Result<(), OperationError<SetSettingError>> {
107 self.request(
108 SetSetting {
109 setting_name: S::name().to_string(),
110 value: serde_json::to_value(setting).unwrap(),
111 },
112 operation_state,
113 )
114 .await
115 }
116
117 pub async fn request<R: GiteratedOperation<O> + Debug + 'static>(
118 &mut self,
119 request: R,
120 operation_state: &I,
121 ) -> Result<R::Success, OperationError<R::Failure>>
122 where
123 R::Success: Clone,
124 R::Failure: Clone,
125 {
126 self.backend
127 .object_operation(
128 self.inner.clone(),
129 R::operation_name(),
130 request,
131 operation_state,
132 )
133 .await
134 }
135 }
136