4 use chrono::NaiveDateTime;
8 serialize::{Output, ToSql},
11 use serde::{Deserialize, Serialize};
13 fmt::{Display, Formatter},
22 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
24 pub struct DbUrl(Url);
26 impl<DB: Backend> ToSql<Text, DB> for DbUrl
28 String: ToSql<Text, DB>,
30 fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> diesel::serialize::Result {
31 self.0.to_string().to_sql(out)
35 impl<DB: Backend> FromSql<Text, DB> for DbUrl
37 String: FromSql<Text, DB>,
39 fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
40 let str = String::from_sql(bytes)?;
41 Ok(DbUrl(Url::parse(&str)?))
46 pub fn into_inner(self) -> Url {
51 impl Display for DbUrl {
52 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53 self.to_owned().into_inner().fmt(f)
57 impl From<DbUrl> for Url {
58 fn from(url: DbUrl) -> Self {
63 impl From<Url> for DbUrl {
64 fn from(url: Url) -> Self {
69 // TODO: can probably move this back to lemmy_db_queries
70 pub fn naive_now() -> NaiveDateTime {
71 chrono::prelude::Utc::now().naive_utc()