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