]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / db_schema / src / newtypes.rs
1 #[cfg(feature = "full")]
2 use activitypub_federation::{core::object_id::ObjectId, traits::ApubObject};
3 #[cfg(feature = "full")]
4 use diesel_ltree::Ltree;
5 use serde::{Deserialize, Serialize};
6 use std::{
7   fmt,
8   fmt::{Display, Formatter},
9   ops::Deref,
10 };
11 use url::Url;
12
13 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
14 #[cfg_attr(feature = "full", derive(DieselNewType))]
15 pub struct PostId(pub i32);
16
17 impl fmt::Display for PostId {
18   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19     write!(f, "{}", self.0)
20   }
21 }
22
23 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
24 #[cfg_attr(feature = "full", derive(DieselNewType))]
25 pub struct PersonId(pub i32);
26
27 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
28 #[cfg_attr(feature = "full", derive(DieselNewType))]
29 pub struct CommentId(pub i32);
30
31 impl fmt::Display for CommentId {
32   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33     write!(f, "{}", self.0)
34   }
35 }
36
37 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
38 #[cfg_attr(feature = "full", derive(DieselNewType))]
39 pub struct CommunityId(pub i32);
40
41 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
42 #[cfg_attr(feature = "full", derive(DieselNewType))]
43 pub struct LocalUserId(pub i32);
44
45 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
46 #[cfg_attr(feature = "full", derive(DieselNewType))]
47 pub struct PrivateMessageId(i32);
48
49 impl fmt::Display for PrivateMessageId {
50   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51     write!(f, "{}", self.0)
52   }
53 }
54
55 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
56 #[cfg_attr(feature = "full", derive(DieselNewType))]
57 pub struct PersonMentionId(i32);
58
59 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
60 #[cfg_attr(feature = "full", derive(DieselNewType))]
61 pub struct PersonBlockId(i32);
62
63 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
64 #[cfg_attr(feature = "full", derive(DieselNewType))]
65 pub struct CommunityBlockId(i32);
66
67 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
68 #[cfg_attr(feature = "full", derive(DieselNewType))]
69 pub struct CommentReportId(i32);
70
71 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
72 #[cfg_attr(feature = "full", derive(DieselNewType))]
73 pub struct PostReportId(i32);
74
75 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
76 #[cfg_attr(feature = "full", derive(DieselNewType))]
77 pub struct PrivateMessageReportId(i32);
78
79 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
80 #[cfg_attr(feature = "full", derive(DieselNewType))]
81 pub struct SiteId(i32);
82
83 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
84 #[cfg_attr(feature = "full", derive(DieselNewType))]
85 pub struct LanguageId(pub i32);
86
87 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
88 #[cfg_attr(feature = "full", derive(DieselNewType))]
89 pub struct LocalUserLanguageId(pub i32);
90
91 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
92 #[cfg_attr(feature = "full", derive(DieselNewType))]
93 pub struct SiteLanguageId(pub i32);
94
95 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
96 #[cfg_attr(feature = "full", derive(DieselNewType))]
97 pub struct CommunityLanguageId(pub i32);
98
99 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
100 #[cfg_attr(feature = "full", derive(DieselNewType))]
101 pub struct CommentReplyId(i32);
102
103 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
104 #[cfg_attr(feature = "full", derive(DieselNewType))]
105 pub struct InstanceId(i32);
106
107 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
108 #[cfg_attr(feature = "full", derive(DieselNewType))]
109 pub struct LocalSiteId(i32);
110
111 #[repr(transparent)]
112 #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
113 #[cfg_attr(feature = "full", derive(AsExpression, FromSqlRow))]
114 #[cfg_attr(feature = "full", diesel(sql_type = diesel::sql_types::Text))]
115 pub struct DbUrl(pub(crate) Box<Url>);
116
117 #[cfg(feature = "full")]
118 #[derive(Serialize, Deserialize)]
119 #[serde(remote = "Ltree")]
120 /// Do remote derivation for the Ltree struct
121 pub struct LtreeDef(pub String);
122
123 impl Display for DbUrl {
124   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
125     self.clone().0.fmt(f)
126   }
127 }
128
129 // the project doesnt compile with From
130 #[allow(clippy::from_over_into)]
131 impl Into<DbUrl> for Url {
132   fn into(self) -> DbUrl {
133     DbUrl(Box::new(self))
134   }
135 }
136 #[allow(clippy::from_over_into)]
137 impl Into<Url> for DbUrl {
138   fn into(self) -> Url {
139     *self.0
140   }
141 }
142 #[cfg(feature = "full")]
143 impl<T> From<DbUrl> for ObjectId<T>
144 where
145   T: ApubObject + Send,
146   for<'de2> <T as ApubObject>::ApubType: Deserialize<'de2>,
147 {
148   fn from(value: DbUrl) -> Self {
149     ObjectId::new(value)
150   }
151 }
152
153 impl Deref for DbUrl {
154   type Target = Url;
155
156   fn deref(&self) -> &Self::Target {
157     &self.0
158   }
159 }