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