]> Untitled Git - lemmy.git/blob - crates/db_schema/src/newtypes.rs
Merge pull request #1850 from LemmyNet/refactor-apub
[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 serde::{Deserialize, Serialize};
8 use std::{
9   fmt,
10   fmt::{Display, Formatter},
11   io::Write,
12 };
13 use url::Url;
14
15 #[derive(
16   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
17 )]
18 pub struct PostId(pub i32);
19
20 impl fmt::Display for PostId {
21   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22     write!(f, "{}", self.0)
23   }
24 }
25
26 #[derive(
27   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
28 )]
29 pub struct PersonId(pub i32);
30
31 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
32 pub struct CommentId(pub i32);
33
34 impl fmt::Display for CommentId {
35   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36     write!(f, "{}", self.0)
37   }
38 }
39
40 #[derive(
41   Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
42 )]
43 pub struct CommunityId(pub i32);
44
45 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
46 pub struct LocalUserId(pub i32);
47
48 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
49 pub struct PrivateMessageId(i32);
50
51 impl fmt::Display for PrivateMessageId {
52   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53     write!(f, "{}", self.0)
54   }
55 }
56
57 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
58 pub struct PersonMentionId(i32);
59
60 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
61 pub struct PersonBlockId(i32);
62
63 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
64 pub struct CommunityBlockId(i32);
65
66 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
67 pub struct CommentReportId(i32);
68
69 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
70 pub struct PostReportId(i32);
71
72 #[repr(transparent)]
73 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
74 #[sql_type = "Text"]
75 pub struct DbUrl(Url);
76
77 impl<DB: Backend> ToSql<Text, DB> for DbUrl
78 where
79   String: ToSql<Text, DB>,
80 {
81   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
82     self.0.to_string().to_sql(out)
83   }
84 }
85
86 impl<DB: Backend> FromSql<Text, DB> for DbUrl
87 where
88   String: FromSql<Text, DB>,
89 {
90   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
91     let str = String::from_sql(bytes)?;
92     Ok(DbUrl(Url::parse(&str)?))
93   }
94 }
95
96 impl DbUrl {
97   // TODO: remove this method and just use into()
98   pub fn into_inner(self) -> Url {
99     self.0
100   }
101 }
102
103 impl Display for DbUrl {
104   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
105     self.to_owned().0.fmt(f)
106   }
107 }
108
109 impl From<DbUrl> for Url {
110   fn from(url: DbUrl) -> Self {
111     url.0
112   }
113 }
114
115 impl From<Url> for DbUrl {
116   fn from(url: Url) -> Self {
117     DbUrl(url)
118   }
119 }