]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
Implement instance actor (#1798)
[lemmy.git] / crates / db_schema / src / newtypes.rs
1 use diesel::{
2   backend::Backend,
3   deserialize::FromSql,
4   serialize::{Output, ToSql},
5   sql_types::Text,
6 };
7 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
8 use serde::{Deserialize, Serialize};
9 use std::{
10   fmt,
11   fmt::{Display, Formatter},
12   io::Write,
13   ops::Deref,
14 };
15 use url::Url;
16
17 #[derive(
18   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
19 )]
20 pub struct PostId(pub i32);
21
22 impl fmt::Display for PostId {
23   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24     write!(f, "{}", self.0)
25   }
26 }
27
28 #[derive(
29   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
30 )]
31 pub struct PersonId(pub i32);
32
33 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
34 pub struct CommentId(pub i32);
35
36 impl fmt::Display for CommentId {
37   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38     write!(f, "{}", self.0)
39   }
40 }
41
42 #[derive(
43   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
44 )]
45 pub struct CommunityId(pub i32);
46
47 #[derive(
48   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
49 )]
50 pub struct LocalUserId(pub i32);
51
52 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
53 pub struct PrivateMessageId(i32);
54
55 impl fmt::Display for PrivateMessageId {
56   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57     write!(f, "{}", self.0)
58   }
59 }
60
61 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
62 pub struct PersonMentionId(i32);
63
64 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
65 pub struct PersonBlockId(i32);
66
67 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
68 pub struct CommunityBlockId(i32);
69
70 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
71 pub struct CommentReportId(i32);
72
73 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
74 pub struct PostReportId(i32);
75
76 #[repr(transparent)]
77 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
78 #[sql_type = "Text"]
79 pub struct DbUrl(Url);
80
81 impl<DB: Backend> ToSql<Text, DB> for DbUrl
82 where
83   String: ToSql<Text, DB>,
84 {
85   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
86     self.0.to_string().to_sql(out)
87   }
88 }
89
90 impl<DB: Backend> FromSql<Text, DB> for DbUrl
91 where
92   String: FromSql<Text, DB>,
93 {
94   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
95     let str = String::from_sql(bytes)?;
96     Ok(DbUrl(Url::parse(&str)?))
97   }
98 }
99
100 impl Display for DbUrl {
101   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
102     self.to_owned().0.fmt(f)
103   }
104 }
105
106 // the project doesnt compile with From
107 #[allow(clippy::from_over_into)]
108 impl Into<DbUrl> for Url {
109   fn into(self) -> DbUrl {
110     DbUrl(self)
111   }
112 }
113 #[allow(clippy::from_over_into)]
114 impl Into<Url> for DbUrl {
115   fn into(self) -> Url {
116     self.0
117   }
118 }
119
120 impl<Kind> From<ObjectId<Kind>> for DbUrl
121 where
122   Kind: ApubObject + Send + 'static,
123   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
124 {
125   fn from(id: ObjectId<Kind>) -> Self {
126     DbUrl(id.into())
127   }
128 }
129
130 impl Deref for DbUrl {
131   type Target = Url;
132
133   fn deref(&self) -> &Self::Target {
134     &self.0
135   }
136 }