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