5 extern crate diesel_derive_newtype;
7 use chrono::NaiveDateTime;
11 serialize::{Output, ToSql},
14 use serde::{Deserialize, Serialize};
17 fmt::{Display, Formatter},
25 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
26 pub struct PostId(pub i32);
28 impl fmt::Display for PostId {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "{}", self.0)
34 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
35 pub struct PersonId(pub i32);
37 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
38 pub struct CommentId(pub i32);
40 impl fmt::Display for CommentId {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 write!(f, "{}", self.0)
46 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
47 pub struct CommunityId(pub i32);
49 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
50 pub struct LocalUserId(pub i32);
52 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
53 pub struct PrivateMessageId(i32);
55 impl fmt::Display for PrivateMessageId {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}", self.0)
61 #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
62 pub struct PersonMentionId(i32);
65 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
67 pub struct DbUrl(Url);
69 impl<DB: Backend> ToSql<Text, DB> for DbUrl
71 String: ToSql<Text, DB>,
73 fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
74 self.0.to_string().to_sql(out)
78 impl<DB: Backend> FromSql<Text, DB> for DbUrl
80 String: FromSql<Text, DB>,
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)?))
89 pub fn into_inner(self) -> Url {
94 impl Display for DbUrl {
95 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
96 self.to_owned().into_inner().fmt(f)
100 impl From<DbUrl> for Url {
101 fn from(url: DbUrl) -> Self {
106 impl From<Url> for DbUrl {
107 fn from(url: Url) -> Self {
112 // TODO: can probably move this back to lemmy_db_queries
113 pub fn naive_now() -> NaiveDateTime {
114 chrono::prelude::Utc::now().naive_utc()