]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
9219d77f9797fc63874fab9da293fea2007a6836
[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(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
47 pub struct LocalUserId(pub i32);
48
49 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
50 pub struct PrivateMessageId(i32);
51
52 impl fmt::Display for PrivateMessageId {
53   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54     write!(f, "{}", self.0)
55   }
56 }
57
58 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
59 pub struct PersonMentionId(i32);
60
61 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
62 pub struct PersonBlockId(i32);
63
64 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
65 pub struct CommunityBlockId(i32);
66
67 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
68 pub struct CommentReportId(i32);
69
70 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
71 pub struct PostReportId(i32);
72
73 #[repr(transparent)]
74 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
75 #[sql_type = "Text"]
76 pub struct DbUrl(Url);
77
78 impl<DB: Backend> ToSql<Text, DB> for DbUrl
79 where
80   String: ToSql<Text, DB>,
81 {
82   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
83     self.0.to_string().to_sql(out)
84   }
85 }
86
87 impl<DB: Backend> FromSql<Text, DB> for DbUrl
88 where
89   String: FromSql<Text, DB>,
90 {
91   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
92     let str = String::from_sql(bytes)?;
93     Ok(DbUrl(Url::parse(&str)?))
94   }
95 }
96
97 impl Display for DbUrl {
98   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
99     self.to_owned().0.fmt(f)
100   }
101 }
102
103 // the project doesnt compile with From
104 #[allow(clippy::from_over_into)]
105 impl Into<DbUrl> for Url {
106   fn into(self) -> DbUrl {
107     DbUrl(self)
108   }
109 }
110 #[allow(clippy::from_over_into)]
111 impl Into<Url> for DbUrl {
112   fn into(self) -> Url {
113     self.0
114   }
115 }
116
117 impl<Kind> From<ObjectId<Kind>> for DbUrl
118 where
119   Kind: ApubObject + Send + 'static,
120   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
121 {
122   fn from(id: ObjectId<Kind>) -> Self {
123     DbUrl(id.into())
124   }
125 }