2020-12-18 16:17:21 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
|
2021-03-18 20:25:21 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_derive_newtype;
|
|
|
|
|
2020-12-18 17:27:25 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-01-27 16:42:23 +00:00
|
|
|
use diesel::{
|
|
|
|
backend::Backend,
|
|
|
|
deserialize::FromSql,
|
|
|
|
serialize::{Output, ToSql},
|
|
|
|
sql_types::Text,
|
|
|
|
};
|
2021-03-02 12:41:48 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-27 16:42:23 +00:00
|
|
|
use std::{
|
2021-03-18 20:25:21 +00:00
|
|
|
fmt,
|
2021-01-27 16:42:23 +00:00
|
|
|
fmt::{Display, Formatter},
|
|
|
|
io::Write,
|
|
|
|
};
|
2021-03-02 12:41:48 +00:00
|
|
|
use url::Url;
|
2020-12-18 17:27:25 +00:00
|
|
|
|
2020-12-18 16:17:21 +00:00
|
|
|
pub mod schema;
|
2020-12-18 17:27:25 +00:00
|
|
|
pub mod source;
|
|
|
|
|
2021-03-20 20:59:07 +00:00
|
|
|
#[derive(
|
|
|
|
Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
|
|
|
|
)]
|
2021-03-18 20:25:21 +00:00
|
|
|
pub struct PostId(pub i32);
|
|
|
|
|
|
|
|
impl fmt::Display for PostId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:59:07 +00:00
|
|
|
#[derive(
|
|
|
|
Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
|
|
|
|
)]
|
2021-03-18 20:25:21 +00:00
|
|
|
pub struct PersonId(pub i32);
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
|
|
|
|
pub struct CommentId(pub i32);
|
|
|
|
|
|
|
|
impl fmt::Display for CommentId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:59:07 +00:00
|
|
|
#[derive(
|
|
|
|
Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
|
|
|
|
)]
|
2021-03-18 20:25:21 +00:00
|
|
|
pub struct CommunityId(pub i32);
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
|
|
|
|
pub struct LocalUserId(pub i32);
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
|
|
|
|
pub struct PrivateMessageId(i32);
|
|
|
|
|
|
|
|
impl fmt::Display for PrivateMessageId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
|
|
|
|
pub struct PersonMentionId(i32);
|
|
|
|
|
2021-01-27 16:42:23 +00:00
|
|
|
#[repr(transparent)]
|
2021-03-02 12:41:48 +00:00
|
|
|
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
|
2021-01-27 16:42:23 +00:00
|
|
|
#[sql_type = "Text"]
|
2021-03-02 12:41:48 +00:00
|
|
|
pub struct DbUrl(Url);
|
2021-01-27 16:42:23 +00:00
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl<DB: Backend> ToSql<Text, DB> for DbUrl
|
2021-01-27 16:42:23 +00:00
|
|
|
where
|
|
|
|
String: ToSql<Text, DB>,
|
|
|
|
{
|
|
|
|
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
|
|
|
|
self.0.to_string().to_sql(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl<DB: Backend> FromSql<Text, DB> for DbUrl
|
2021-01-27 16:42:23 +00:00
|
|
|
where
|
|
|
|
String: FromSql<Text, DB>,
|
|
|
|
{
|
|
|
|
fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
|
|
|
|
let str = String::from_sql(bytes)?;
|
2021-03-02 12:41:48 +00:00
|
|
|
Ok(DbUrl(Url::parse(&str)?))
|
2021-01-27 16:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl DbUrl {
|
2021-07-31 20:58:11 +00:00
|
|
|
// TODO: remove this method and just use into()
|
2021-03-02 12:41:48 +00:00
|
|
|
pub fn into_inner(self) -> Url {
|
2021-01-27 16:42:23 +00:00
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl Display for DbUrl {
|
2021-01-27 16:42:23 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
2021-07-31 20:58:11 +00:00
|
|
|
self.to_owned().0.fmt(f)
|
2021-01-27 16:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl From<DbUrl> for Url {
|
|
|
|
fn from(url: DbUrl) -> Self {
|
2021-01-27 16:42:23 +00:00
|
|
|
url.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
impl From<Url> for DbUrl {
|
|
|
|
fn from(url: Url) -> Self {
|
|
|
|
DbUrl(url)
|
2021-01-27 16:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:30:34 +00:00
|
|
|
// TODO: can probably move this back to lemmy_db_queries
|
2020-12-18 17:27:25 +00:00
|
|
|
pub fn naive_now() -> NaiveDateTime {
|
|
|
|
chrono::prelude::Utc::now().naive_utc()
|
|
|
|
}
|