]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / newtypes.rs
1 use diesel_ltree::Ltree;
2 use serde::{Deserialize, Serialize};
3 use std::{
4   fmt,
5   fmt::{Display, Formatter},
6   ops::Deref,
7 };
8 use url::Url;
9
10 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
11 #[cfg_attr(feature = "full", derive(DieselNewType))]
12 pub struct PostId(pub i32);
13
14 impl fmt::Display for PostId {
15   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16     write!(f, "{}", self.0)
17   }
18 }
19
20 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
21 #[cfg_attr(feature = "full", derive(DieselNewType))]
22 pub struct PersonId(pub i32);
23
24 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
25 #[cfg_attr(feature = "full", derive(DieselNewType))]
26 pub struct CommentId(pub i32);
27
28 impl fmt::Display for CommentId {
29   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30     write!(f, "{}", self.0)
31   }
32 }
33
34 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
35 #[cfg_attr(feature = "full", derive(DieselNewType))]
36 pub struct CommunityId(pub i32);
37
38 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
39 #[cfg_attr(feature = "full", derive(DieselNewType))]
40 pub struct LocalUserId(pub i32);
41
42 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
43 #[cfg_attr(feature = "full", derive(DieselNewType))]
44 pub struct PrivateMessageId(i32);
45
46 impl fmt::Display for PrivateMessageId {
47   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48     write!(f, "{}", self.0)
49   }
50 }
51
52 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
53 #[cfg_attr(feature = "full", derive(DieselNewType))]
54 pub struct PersonMentionId(i32);
55
56 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
57 #[cfg_attr(feature = "full", derive(DieselNewType))]
58 pub struct PersonBlockId(i32);
59
60 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
61 #[cfg_attr(feature = "full", derive(DieselNewType))]
62 pub struct CommunityBlockId(i32);
63
64 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
65 #[cfg_attr(feature = "full", derive(DieselNewType))]
66 pub struct CommentReportId(i32);
67
68 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
69 #[cfg_attr(feature = "full", derive(DieselNewType))]
70 pub struct PostReportId(i32);
71
72 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
73 #[cfg_attr(feature = "full", derive(DieselNewType))]
74 pub struct PrivateMessageReportId(i32);
75
76 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
77 #[cfg_attr(feature = "full", derive(DieselNewType))]
78 pub struct LanguageId(pub i32);
79
80 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
81 #[cfg_attr(feature = "full", derive(DieselNewType))]
82 pub struct LocalUserLanguageId(pub i32);
83
84 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
85 #[cfg_attr(feature = "full", derive(DieselNewType))]
86 pub struct CommentReplyId(i32);
87
88 #[repr(transparent)]
89 #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
90 #[cfg_attr(feature = "full", derive(AsExpression, FromSqlRow))]
91 #[cfg_attr(feature = "full", diesel(sql_type = diesel::sql_types::Text))]
92 pub struct DbUrl(pub(crate) Url);
93
94 #[derive(Serialize, Deserialize)]
95 #[serde(remote = "Ltree")]
96 /// Do remote derivation for the Ltree struct
97 pub struct LtreeDef(pub String);
98
99 impl Display for DbUrl {
100   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
101     self.to_owned().0.fmt(f)
102   }
103 }
104
105 // the project doesnt compile with From
106 #[allow(clippy::from_over_into)]
107 impl Into<DbUrl> for Url {
108   fn into(self) -> DbUrl {
109     DbUrl(self)
110   }
111 }
112 #[allow(clippy::from_over_into)]
113 impl Into<Url> for DbUrl {
114   fn into(self) -> Url {
115     self.0
116   }
117 }
118
119 impl Deref for DbUrl {
120   type Target = Url;
121
122   fn deref(&self) -> &Self::Target {
123     &self.0
124   }
125 }