]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
First pass at invite-only migration. (#1949)
[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 };
14 use url::Url;
15
16 #[derive(
17   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
18 )]
19 pub struct PostId(pub i32);
20
21 impl fmt::Display for PostId {
22   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23     write!(f, "{}", self.0)
24   }
25 }
26
27 #[derive(
28   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
29 )]
30 pub struct PersonId(pub i32);
31
32 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
33 pub struct CommentId(pub i32);
34
35 impl fmt::Display for CommentId {
36   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37     write!(f, "{}", self.0)
38   }
39 }
40
41 #[derive(
42   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
43 )]
44 pub struct CommunityId(pub i32);
45
46 #[derive(
47   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
48 )]
49 pub struct LocalUserId(pub i32);
50
51 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
52 pub struct PrivateMessageId(i32);
53
54 impl fmt::Display for PrivateMessageId {
55   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56     write!(f, "{}", self.0)
57   }
58 }
59
60 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
61 pub struct PersonMentionId(i32);
62
63 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
64 pub struct PersonBlockId(i32);
65
66 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
67 pub struct CommunityBlockId(i32);
68
69 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
70 pub struct CommentReportId(i32);
71
72 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
73 pub struct PostReportId(i32);
74
75 #[repr(transparent)]
76 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
77 #[sql_type = "Text"]
78 pub struct DbUrl(Url);
79
80 impl<DB: Backend> ToSql<Text, DB> for DbUrl
81 where
82   String: ToSql<Text, DB>,
83 {
84   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
85     self.0.to_string().to_sql(out)
86   }
87 }
88
89 impl<DB: Backend> FromSql<Text, DB> for DbUrl
90 where
91   String: FromSql<Text, DB>,
92 {
93   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
94     let str = String::from_sql(bytes)?;
95     Ok(DbUrl(Url::parse(&str)?))
96   }
97 }
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<Kind> From<ObjectId<Kind>> for DbUrl
120 where
121   Kind: ApubObject + Send + 'static,
122   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
123 {
124   fn from(id: ObjectId<Kind>) -> Self {
125     DbUrl(id.into())
126   }
127 }