]> Untitled Git - lemmy.git/blob - crates/db_schema/src/lib.rs
Merge pull request #1499 from LemmyNet/strictly_type_db_ids
[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(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
26 pub struct PostId(pub i32);
27
28 impl fmt::Display for PostId {
29   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30     write!(f, "{}", self.0)
31   }
32 }
33
34 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
35 pub struct PersonId(pub i32);
36
37 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
38 pub struct CommentId(pub i32);
39
40 impl fmt::Display for CommentId {
41   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42     write!(f, "{}", self.0)
43   }
44 }
45
46 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
47 pub struct CommunityId(pub i32);
48
49 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
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 #[repr(transparent)]
65 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
66 #[sql_type = "Text"]
67 pub struct DbUrl(Url);
68
69 impl<DB: Backend> ToSql<Text, DB> for DbUrl
70 where
71   String: ToSql<Text, DB>,
72 {
73   fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
74     self.0.to_string().to_sql(out)
75   }
76 }
77
78 impl<DB: Backend> FromSql<Text, DB> for DbUrl
79 where
80   String: FromSql<Text, DB>,
81 {
82   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
83     let str = String::from_sql(bytes)?;
84     Ok(DbUrl(Url::parse(&str)?))
85   }
86 }
87
88 impl DbUrl {
89   pub fn into_inner(self) -> Url {
90     self.0
91   }
92 }
93
94 impl Display for DbUrl {
95   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
96     self.to_owned().into_inner().fmt(f)
97   }
98 }
99
100 impl From<DbUrl> for Url {
101   fn from(url: DbUrl) -> Self {
102     url.0
103   }
104 }
105
106 impl From<Url> for DbUrl {
107   fn from(url: Url) -> Self {
108     DbUrl(url)
109   }
110 }
111
112 // TODO: can probably move this back to lemmy_db_queries
113 pub fn naive_now() -> NaiveDateTime {
114   chrono::prelude::Utc::now().naive_utc()
115 }