]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/lib.rs
Use URL type in most outstanding struct fields (#1468)
[lemmy.git] / crates / db_schema / src / lib.rs
index b0733884ecd2b9269e3c79e1d7608d649e07274d..f44567b90e3fa55657604e44ec31fcb2659e6c7a 100644 (file)
@@ -8,21 +8,22 @@ use diesel::{
   serialize::{Output, ToSql},
   sql_types::Text,
 };
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
 use std::{
   fmt::{Display, Formatter},
   io::Write,
 };
+use url::Url;
 
 pub mod schema;
 pub mod source;
 
 #[repr(transparent)]
-#[derive(Clone, PartialEq, Serialize, Debug, AsExpression, FromSqlRow)]
+#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
 #[sql_type = "Text"]
-pub struct Url(url::Url);
+pub struct DbUrl(Url);
 
-impl<DB: Backend> ToSql<Text, DB> for Url
+impl<DB: Backend> ToSql<Text, DB> for DbUrl
 where
   String: ToSql<Text, DB>,
 {
@@ -31,37 +32,37 @@ where
   }
 }
 
-impl<DB: Backend> FromSql<Text, DB> for Url
+impl<DB: Backend> FromSql<Text, DB> for DbUrl
 where
   String: FromSql<Text, DB>,
 {
   fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
     let str = String::from_sql(bytes)?;
-    Ok(Url(url::Url::parse(&str)?))
+    Ok(DbUrl(Url::parse(&str)?))
   }
 }
 
-impl Url {
-  pub fn into_inner(self) -> url::Url {
+impl DbUrl {
+  pub fn into_inner(self) -> Url {
     self.0
   }
 }
 
-impl Display for Url {
+impl Display for DbUrl {
   fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
     self.to_owned().into_inner().fmt(f)
   }
 }
 
-impl From<Url> for url::Url {
-  fn from(url: Url) -> Self {
+impl From<DbUrl> for Url {
+  fn from(url: DbUrl) -> Self {
     url.0
   }
 }
 
-impl From<url::Url> for Url {
-  fn from(url: url::Url) -> Self {
-    Url(url)
+impl From<Url> for DbUrl {
+  fn from(url: Url) -> Self {
+    DbUrl(url)
   }
 }