]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
Default imprementations for read and delete in Crud trait (#3707)
[lemmy.git] / crates / db_schema / src / newtypes.rs
1 #[cfg(feature = "full")]
2 use activitypub_federation::{
3   fetch::collection_id::CollectionId,
4   fetch::object_id::ObjectId,
5   traits::Collection,
6   traits::Object,
7 };
8 #[cfg(feature = "full")]
9 use diesel_ltree::Ltree;
10 use serde::{Deserialize, Serialize};
11 use std::{
12   fmt,
13   fmt::{Display, Formatter},
14   ops::Deref,
15 };
16 #[cfg(feature = "full")]
17 use ts_rs::TS;
18 use url::Url;
19
20 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
21 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
22 #[cfg_attr(feature = "full", ts(export))]
23 /// The post id.
24 pub struct PostId(pub i32);
25
26 impl fmt::Display for PostId {
27   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28     write!(f, "{}", self.0)
29   }
30 }
31
32 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
33 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
34 #[cfg_attr(feature = "full", ts(export))]
35 /// The person id.
36 pub struct PersonId(pub i32);
37
38 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
39 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
40 #[cfg_attr(feature = "full", ts(export))]
41 /// The comment id.
42 pub struct CommentId(pub i32);
43
44 impl fmt::Display for CommentId {
45   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46     write!(f, "{}", self.0)
47   }
48 }
49
50 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
51 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
52 #[cfg_attr(feature = "full", ts(export))]
53 /// The community id.
54 pub struct CommunityId(pub i32);
55
56 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
57 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
58 #[cfg_attr(feature = "full", ts(export))]
59 /// The local user id.
60 pub struct LocalUserId(pub i32);
61
62 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
63 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
64 #[cfg_attr(feature = "full", ts(export))]
65 /// The private message id.
66 pub struct PrivateMessageId(i32);
67
68 impl fmt::Display for PrivateMessageId {
69   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70     write!(f, "{}", self.0)
71   }
72 }
73
74 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
75 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
76 #[cfg_attr(feature = "full", ts(export))]
77 /// The person mention id.
78 pub struct PersonMentionId(i32);
79
80 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
81 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
82 #[cfg_attr(feature = "full", ts(export))]
83 /// The person block id.
84 pub struct PersonBlockId(i32);
85
86 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
87 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
88 #[cfg_attr(feature = "full", ts(export))]
89 /// The community block id.
90 pub struct CommunityBlockId(i32);
91
92 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
93 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
94 #[cfg_attr(feature = "full", ts(export))]
95 /// The comment report id.
96 pub struct CommentReportId(i32);
97
98 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
99 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
100 #[cfg_attr(feature = "full", ts(export))]
101 /// The post report id.
102 pub struct PostReportId(i32);
103
104 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
105 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
106 #[cfg_attr(feature = "full", ts(export))]
107 /// The private message report id.
108 pub struct PrivateMessageReportId(i32);
109
110 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
111 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
112 #[cfg_attr(feature = "full", ts(export))]
113 /// The site id.
114 pub struct SiteId(i32);
115
116 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
117 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
118 #[cfg_attr(feature = "full", ts(export))]
119 /// The language id.
120 pub struct LanguageId(pub i32);
121
122 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
123 #[cfg_attr(feature = "full", derive(DieselNewType))]
124 pub struct LocalUserLanguageId(pub i32);
125
126 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
127 #[cfg_attr(feature = "full", derive(DieselNewType))]
128 pub struct SiteLanguageId(pub i32);
129
130 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
131 #[cfg_attr(feature = "full", derive(DieselNewType))]
132 pub struct CommunityLanguageId(pub i32);
133
134 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
135 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
136 #[cfg_attr(feature = "full", ts(export))]
137 /// The comment reply id.
138 pub struct CommentReplyId(i32);
139
140 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
141 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
142 #[cfg_attr(feature = "full", ts(export))]
143 /// The instance id.
144 pub struct InstanceId(i32);
145
146 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
147 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
148 #[cfg_attr(feature = "full", ts(export))]
149 /// The local site id.
150 pub struct LocalSiteId(i32);
151
152 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
153 #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
154 #[cfg_attr(feature = "full", ts(export))]
155 /// The custom emoji id.
156 pub struct CustomEmojiId(i32);
157
158 #[cfg(feature = "full")]
159 #[derive(Serialize, Deserialize)]
160 #[serde(remote = "Ltree")]
161 /// Do remote derivation for the Ltree struct
162 pub struct LtreeDef(pub String);
163
164 #[repr(transparent)]
165 #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
166 #[cfg_attr(feature = "full", derive(AsExpression, FromSqlRow))]
167 #[cfg_attr(feature = "full", diesel(sql_type = diesel::sql_types::Text))]
168 pub struct DbUrl(pub(crate) Box<Url>);
169
170 impl DbUrl {
171   pub fn inner(&self) -> &Url {
172     &self.0
173   }
174 }
175
176 impl Display for DbUrl {
177   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
178     self.clone().0.fmt(f)
179   }
180 }
181
182 // the project doesnt compile with From
183 #[allow(clippy::from_over_into)]
184 impl Into<DbUrl> for Url {
185   fn into(self) -> DbUrl {
186     DbUrl(Box::new(self))
187   }
188 }
189 #[allow(clippy::from_over_into)]
190 impl Into<Url> for DbUrl {
191   fn into(self) -> Url {
192     *self.0
193   }
194 }
195
196 #[cfg(feature = "full")]
197 impl<T> From<DbUrl> for ObjectId<T>
198 where
199   T: Object + Send + 'static,
200   for<'de2> <T as Object>::Kind: Deserialize<'de2>,
201 {
202   fn from(value: DbUrl) -> Self {
203     let url: Url = value.into();
204     ObjectId::from(url)
205   }
206 }
207
208 #[cfg(feature = "full")]
209 impl<T> From<DbUrl> for CollectionId<T>
210 where
211   T: Collection + Send + 'static,
212   for<'de2> <T as Collection>::Kind: Deserialize<'de2>,
213 {
214   fn from(value: DbUrl) -> Self {
215     let url: Url = value.into();
216     CollectionId::from(url)
217   }
218 }
219
220 #[cfg(feature = "full")]
221 impl<T> From<CollectionId<T>> for DbUrl
222 where
223   T: Collection,
224   for<'de2> <T as Collection>::Kind: Deserialize<'de2>,
225 {
226   fn from(value: CollectionId<T>) -> Self {
227     let url: Url = value.into();
228     url.into()
229   }
230 }
231
232 impl Deref for DbUrl {
233   type Target = Url;
234
235   fn deref(&self) -> &Self::Target {
236     &self.0
237   }
238 }
239
240 #[cfg(feature = "full")]
241 impl TS for DbUrl {
242   fn name() -> String {
243     "string".to_string()
244   }
245   fn dependencies() -> Vec<ts_rs::Dependency> {
246     Vec::new()
247   }
248   fn transparent() -> bool {
249     true
250   }
251 }