]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
First pass at adding comment trees. (#2362)
[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 CommentReplyId(i32);
75
76 #[repr(transparent)]
77 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
78 #[cfg_attr(feature = "full", derive(AsExpression, FromSqlRow))]
79 #[cfg_attr(feature = "full", sql_type = "diesel::sql_types::Text")]
80 pub struct DbUrl(pub(crate) Url);
81
82 #[derive(Serialize, Deserialize)]
83 #[serde(remote = "Ltree")]
84 /// Do remote derivation for the Ltree struct
85 pub struct LtreeDef(pub String);
86
87 impl Display for DbUrl {
88   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89     self.to_owned().0.fmt(f)
90   }
91 }
92
93 // the project doesnt compile with From
94 #[allow(clippy::from_over_into)]
95 impl Into<DbUrl> for Url {
96   fn into(self) -> DbUrl {
97     DbUrl(self)
98   }
99 }
100 #[allow(clippy::from_over_into)]
101 impl Into<Url> for DbUrl {
102   fn into(self) -> Url {
103     self.0
104   }
105 }
106
107 impl Deref for DbUrl {
108   type Target = Url;
109
110   fn deref(&self) -> &Self::Target {
111     &self.0
112   }
113 }